Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1309 lines
34 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. This module
  7. // Test case scenario
  8. // 1. Open implement some test case scenario for regremap module.
  9. //a ISN node and list content
  10. // 2. Create a ISN node do 1.
  11. // 3. Open a non ISN node and list
  12. // 4. Create a non ISN node and list content
  13. //
  14. Author:
  15. ATM Shafiqul Khalid (askhalid) 10-Nov-1999
  16. Revision History:
  17. --*/
  18. #include <nt.h>
  19. #include <ntrtl.h>
  20. #include <nturtl.h>
  21. #include <windows.h>
  22. #include <windef.h>
  23. #include <stdio.h>
  24. #include "wow64reg.h"
  25. #include "..\wow64reg\reflectr.h"
  26. #define TEST_NODE_NAME L"TEST"
  27. #define GUID_STR L"{00000000-E877-11CE-9F68-00AA00574A40}"
  28. typedef struct _TEST_NODE_TYPE {
  29. HKEY hKey;
  30. WCHAR Name[256];
  31. WCHAR SubKey[256];
  32. }TEST_NODE_TYPE;
  33. TEST_NODE_TYPE TestNodeList [] = {
  34. {HKEY_CLASSES_ROOT, L"test1002", UNICODE_NULL},
  35. {HKEY_CURRENT_USER, L"software\\classes\\test1002", UNICODE_NULL},
  36. {HKEY_LOCAL_MACHINE, L"software\\classes\\test1002", UNICODE_NULL},
  37. //{HKEY_USERS,""""""}
  38. };
  39. #define TEST_NODE_NUM (sizeof (TestNodeList)/sizeof (TEST_NODE_TYPE) )
  40. //should move into right header file
  41. BOOL
  42. NonMergeableValueCLSID (
  43. HKEY SrcKey,
  44. HKEY DestKey
  45. );
  46. BOOL
  47. GetKeyTime (
  48. HKEY SrcKey,
  49. ULONGLONG *Time
  50. );
  51. BOOL
  52. MergeKeySrcDest(
  53. PWCHAR Src,
  54. PWCHAR Dest
  55. );
  56. BOOL
  57. GetWow6432ValueKey (
  58. HKEY hKey,
  59. WOW6432_VALUEKEY *pValue
  60. );
  61. BOOL
  62. MergeK1K2Value (
  63. HKEY SrcKey,
  64. HKEY DestKey,
  65. DWORD dwFlag
  66. );
  67. LONG
  68. Wow64RegCreateKeyEx(
  69. HKEY hKey, // handle to an open key
  70. LPCTSTR lpSubKey, // address of subkey name
  71. DWORD Reserved, // reserved
  72. LPTSTR lpClass, // address of class string
  73. DWORD dwOptions, // special options flag
  74. REGSAM samDesired, // desired security access
  75. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  76. // address of key security structure
  77. PHKEY phkResult, // address of buffer for opened handle
  78. LPDWORD lpdwDisposition // address of disposition value buffer
  79. );
  80. BOOL
  81. AnalyzeNode (
  82. PWCHAR pKeyName,
  83. BOOL bSync
  84. )
  85. {
  86. WCHAR Name[256];
  87. WCHAR Mirror[256];
  88. DWORD AccessFlag;
  89. DWORD Ret;
  90. ULONGLONG Time1, Time2;
  91. WCHAR *GUIDName;
  92. WOW6432_VALUEKEY WowValue1;
  93. WOW6432_VALUEKEY WowValue2;
  94. DWORD dwLen = sizeof (Mirror)/sizeof(Mirror[0]);
  95. HKEY Key1;
  96. HKEY Key2;
  97. if (pKeyName == NULL )
  98. return FALSE;
  99. if (pKeyName[0] == UNICODE_NULL )
  100. return FALSE;
  101. wcscpy (Name, L"\\REGISTRY\\MACHINE\\SOFTWARE\\");
  102. if (_wcsnicmp (pKeyName, L"\\REGISTRY", 9 ) !=0 ) {
  103. wcscat (Name, pKeyName);
  104. } else wcscpy (Name, pKeyName);
  105. printf ("\nAnalyzing key [%S]\n", Name );
  106. //print the time stamp, value on the both side etc
  107. Key1 = OpenNode (Name);
  108. if (Key1 == NULL) {
  109. printf ("\nSorry! couldn't open the Key %S", Name );
  110. return FALSE;
  111. }
  112. //BUGBUG following operation should succeed but don't work for classes\wow6432node\.doc input
  113. //Possible bug in the WOW64RegOpenKeyEx
  114. /*Ret = RegOpenKeyEx(
  115. Key1,
  116. NULL,//NULL,
  117. 0,//OpenOption,
  118. KEY_ALL_ACCESS | ( AccessFlag = Is64bitNode (Name)? KEY_WOW64_32KEY : KEY_WOW64_64KEY ),
  119. &Key2
  120. );
  121. if ( ERROR_SUCCESS != Ret ){
  122. printf ("\nSorry! couldn't open mirror of the Key %S", Name );
  123. return FALSE;
  124. }
  125. HandleToKeyName ( Key2, Mirror, &dwLen );
  126. */
  127. //Need to be removed this section
  128. GetMirrorName (Name, Mirror);
  129. Key2 = OpenNode (Mirror);
  130. if (Key2 == NULL) {
  131. printf ("\nSorry! couldn't open the mirror Key %S", Name );
  132. return FALSE;
  133. }
  134. printf ("\nOpened Mirror Key at %S", Mirror);
  135. //now print all the timing information
  136. printf ("\nExtension Test....");
  137. if ( NonMergeableValueCLSID (Key1, Key2 ))
  138. printf ("\nValue of those keys can't be merged...reasons: 1. not an extension 2. Handler don't exist");
  139. else
  140. printf ("\nValue of those keys will be merged by reflector");
  141. GUIDName = wcsstr (Name, L"\\CLSID\\{");
  142. if ( GUIDName != NULL ) {
  143. HKEY KeyGuid;
  144. DWORD SubkeyNumber=1;
  145. GUIDName +=7;
  146. printf ("\nGUID Test......%S", GUIDName);
  147. *(GUIDName -1) = UNICODE_NULL;
  148. KeyGuid = OpenNode (Name);
  149. *(GUIDName -1) = L'\\';
  150. MarkNonMergeableKey (GUIDName, KeyGuid, &SubkeyNumber);
  151. if ( SubkeyNumber>0)
  152. printf ("\nThe guid will be merged....");
  153. else
  154. printf ("\nthe guid isn't going to be merged");
  155. }
  156. GetKeyTime (Key1, &Time1);
  157. GetKeyTime (Key2, &Time2);
  158. printf ("\nOriginal TimeStamp on Keys Src:%p, Dest:%p", Time1, Time2);
  159. GetWow6432ValueKey ( Key1, &WowValue1);
  160. GetWow6432ValueKey ( Key2, &WowValue2);
  161. printf ("\nWowAttribute associated with Keys Src Value type:%d, Timestamp %p", WowValue1.ValueType, WowValue1.TimeStamp);
  162. printf ("\nWowAttribute associated with Keys Des Value type:%d, Timestamp %p", WowValue2.ValueType, WowValue2.TimeStamp);
  163. printf ("\nValueType 1=>Copy, 2=>Reflected, 3=>NonReflectable..\n(Timestamp,Value)=>0 wow attrib isn't there");
  164. if ( bSync ) {
  165. printf ("\n Merging Value/Keys.....");
  166. //MergeK1K2Value (Key1, Key2, 0);
  167. MergeKeySrcDest(Name, Mirror);
  168. }
  169. /*if ( bSync ) {
  170. printf ("\n Merging Value/Keys.....");
  171. //MergeK1K2Value (Key1, Key2, 0);
  172. MergeKeySrcDest(Name, Mirror);
  173. }
  174. if ( bSync ) {
  175. printf ("\n Merging Value/Keys.....");
  176. //MergeK1K2Value (Key1, Key2, 0);
  177. MergeKeySrcDest(Name, Mirror);
  178. }
  179. Sleep (1000*10);
  180. */
  181. return TRUE;
  182. }
  183. BOOL
  184. DeleteNode (
  185. HKEY hKey,
  186. PWCHAR Name
  187. )
  188. {
  189. return FALSE;
  190. }
  191. LONG
  192. Wow64CreateOpenNode(
  193. HKEY hKey,
  194. PWCHAR Name,
  195. HKEY *phKey,
  196. DWORD dwOption,
  197. WCHAR Mode
  198. )
  199. {
  200. WCHAR Buff [MAX_PATH + 1];
  201. DWORD dwBuffLen = MAX_PATH + 1;
  202. DWORD Ret;
  203. *phKey=NULL;
  204. Ret = Wow64RegCreateKeyEx(
  205. hKey, // handle to an open key
  206. Name, // address of subkey name
  207. 0, // reserved
  208. NULL, // address of class string
  209. REG_OPTION_NON_VOLATILE, // special options flag
  210. KEY_ALL_ACCESS | dwOption, // desired security access
  211. NULL, // address of key security structure
  212. phKey, // address of buffer for opened handle
  213. NULL // address of disposition value buffer
  214. );
  215. Buff[0]=UNICODE_NULL;
  216. if (*phKey == NULL) {
  217. HandleToKeyName ( hKey, Buff, &dwBuffLen );
  218. printf ("\nRegCreateEx failed....error: Flag:%d Ret:%d %S<\\>%S", dwOption, Ret, Buff, Name);
  219. }
  220. else if (Mode==L'V'){
  221. HandleToKeyName ( *phKey, Buff, &dwBuffLen );
  222. printf ("\nWow64RegCreateEx succeeded with ....[%S]", Buff);
  223. }
  224. return Ret;
  225. }
  226. LONG
  227. CreateOpenNode(
  228. HKEY hKey,
  229. PWCHAR Name,
  230. HKEY *phKey,
  231. DWORD dwOption,
  232. WCHAR Mode
  233. )
  234. {
  235. WCHAR Buff [MAX_PATH + 1];
  236. DWORD dwBuffLen = MAX_PATH + 1;
  237. DWORD Ret;
  238. *phKey=NULL;
  239. Ret = RegCreateKeyEx(
  240. hKey, // handle to an open key
  241. Name, // address of subkey name
  242. 0, // reserved
  243. NULL, // address of class string
  244. REG_OPTION_NON_VOLATILE, // special options flag
  245. KEY_ALL_ACCESS | dwOption, // desired security access
  246. NULL, // address of key security structure
  247. phKey, // address of buffer for opened handle
  248. NULL // address of disposition value buffer
  249. );
  250. Buff[0]=UNICODE_NULL;
  251. if (*phKey == NULL) {
  252. HandleToKeyName ( hKey, Buff, &dwBuffLen );
  253. printf ("\nRegCreateEx failed....error: Flag:%d Ret:%d %S<\\>%S", dwOption, Ret, Buff, Name);
  254. }
  255. else if (Mode==L'V'){
  256. HandleToKeyName ( *phKey, Buff, &dwBuffLen );
  257. printf ("\nRegCreateEx succeeded with ....[%S]", Buff);
  258. }
  259. if ( (( dwOption & KEY_WOW64_64KEY )&& ( wcsstr(Buff, L"Wow6432Node") != NULL ) ) ||
  260. ( dwOption & KEY_WOW64_32KEY ) && ( wcsstr(Buff, L"Wow6432Node") == NULL ))
  261. printf ("\nSorry! key was created at wrong location..");
  262. return Ret;
  263. }
  264. OpenKey (
  265. HKEY hKey,
  266. PWCHAR Name,
  267. HKEY *phKey,
  268. DWORD dwOption,
  269. WCHAR Mode
  270. )
  271. {
  272. WCHAR Buff [MAX_PATH + 1];
  273. DWORD dwBuffLen = MAX_PATH + 1;
  274. DWORD Ret;
  275. *phKey=NULL;
  276. Ret = RegOpenKeyEx(
  277. hKey,
  278. Name,
  279. 0,//OpenOption,
  280. KEY_ALL_ACCESS | dwOption ,
  281. phKey
  282. );
  283. Buff[0]=UNICODE_NULL;
  284. if (*phKey == NULL) {
  285. HandleToKeyName ( hKey, Buff, &dwBuffLen );
  286. printf ("\nRegOpenEx failed....error: Flag:%d Ret:%d %S<\\>%S", dwOption, Ret, Buff, Name);
  287. } else if (Mode==L'V'){
  288. HandleToKeyName ( *phKey, Buff, &dwBuffLen );
  289. printf ("\nRegOpenEx succeeded with ....[%S]", Buff);
  290. }
  291. return Ret;
  292. }
  293. BOOL
  294. VerifyNode (
  295. HKEY hKey,
  296. PWCHAR OpenName,
  297. PWCHAR RealName
  298. )
  299. {
  300. return FALSE;
  301. }
  302. HKEY
  303. OpenListNode (
  304. HKEY OpenNode,
  305. WCHAR *NodeName,
  306. DWORD OpenOption
  307. )
  308. {
  309. HKEY Key=NULL;
  310. LONG Ret, lCount =0;
  311. WCHAR Name [MAX_PATH + 1];
  312. DWORD dwBuffLen = MAX_PATH + 1;
  313. if ( NodeName == NULL )
  314. return NULL;
  315. /*
  316. #ifndef _WIN64
  317. //
  318. // just to test the library in the 32bit environment
  319. //
  320. if (OpenOption)
  321. Ret = Wow64RegOpenKeyEx(
  322. OpenNode,
  323. NodeName,
  324. 0,//OpenOption,
  325. KEY_ALL_ACCESS | OpenOption ,
  326. &Key
  327. );
  328. else
  329. #endif
  330. */
  331. Ret = RegOpenKeyEx(
  332. OpenNode,
  333. NodeName,
  334. 0,//OpenOption,
  335. KEY_ALL_ACCESS | OpenOption ,
  336. &Key
  337. );
  338. if ( Key!= NULL )
  339. printf ( "\nOpen Operation successful [%S]", NodeName);
  340. else {
  341. printf ( "\nOpen Operation Failed [%S] %X", NodeName, Ret);
  342. return NULL;
  343. }
  344. //
  345. // Now enumerate some value or key's to see what is there
  346. //
  347. lCount = 0;
  348. for(;;) {
  349. Ret = RegEnumKey( Key, lCount, Name, MAX_PATH);
  350. if ( Ret != ERROR_SUCCESS ) break;
  351. printf ("\nKeyName: [%S]", Name);
  352. lCount++;
  353. }
  354. //
  355. // Print the real name of the Key
  356. //
  357. HandleToKeyName ( Key, Name, &dwBuffLen );
  358. printf ("\nThe Real Name of the Key was [%S]", Name);
  359. return Key;
  360. }
  361. void BasicRegReMapTest()
  362. {
  363. HKEY hSystem;
  364. HKEY Key;
  365. HKEY Key1;
  366. RegOpenKeyEx(
  367. HKEY_LOCAL_MACHINE,
  368. L"SYSTEM",
  369. 0,//OpenOption,
  370. KEY_ALL_ACCESS,
  371. &hSystem
  372. );
  373. printf ("\nHello! Im in the regremap piece this will print different view of a tree\n");
  374. printf ("\nOpening Native tree");
  375. Key = OpenListNode (hSystem, TEST_NODE_NAME, 0);
  376. RegCloseKey(Key);
  377. printf ("\n\nOpening Explicitly 32-bit tree");
  378. Key = OpenListNode (hSystem, TEST_NODE_NAME, KEY_WOW64_32KEY);
  379. {
  380. printf ("\nReopening 64bit key using handle to 32bit Key");
  381. Key1 = OpenListNode (Key, L"64bit Key1", KEY_WOW64_64KEY);
  382. RegCloseKey(Key1);
  383. }
  384. RegCloseKey(Key);
  385. printf ("\n\nOpening Explicitly 64-bit Tree");
  386. Key = OpenListNode (hSystem, TEST_NODE_NAME, KEY_WOW64_64KEY);
  387. {
  388. printf ("\nReopening 32bit key using handle to 64bit Key");
  389. Key1 = OpenListNode (Key, L"32bit Key1", KEY_WOW64_32KEY);
  390. RegCloseKey(Key1);
  391. }
  392. RegCloseKey(Key);
  393. RegCloseKey(hSystem);
  394. printf ("\nDone.");
  395. }
  396. void TestCreateHKCR ()
  397. {
  398. HKEY hCR=NULL;
  399. HKEY Key=NULL;
  400. DWORD Ret;
  401. WCHAR Name [MAX_PATH + 1];
  402. DWORD dwBuffLen = MAX_PATH + 1;
  403. Ret = RegCreateKeyEx(
  404. HKEY_CLASSES_ROOT, // handle to an open key
  405. L".001", // address of subkey name
  406. 0, // reserved
  407. NULL, // address of class string
  408. REG_OPTION_NON_VOLATILE, // special options flag
  409. KEY_ALL_ACCESS, // desired security access
  410. NULL, // address of key security structure
  411. &hCR, // address of buffer for opened handle
  412. NULL // address of disposition value buffer
  413. );
  414. if (Ret == ERROR_SUCCESS ) {
  415. printf ("\nHello! checking key creation at classes root");
  416. HandleToKeyName ( hCR, Name, &dwBuffLen );
  417. printf ("\nThe Real Name of the Key was [%S]", Name);
  418. RegCloseKey(hCR);
  419. }
  420. else printf ("\nCouldn't create key .001 at HKEY_CLASSES_ROOT %d", Ret );
  421. }
  422. void TestOpenHKCR (DWORD x, DWORD y)
  423. {
  424. //
  425. // Need to make it work for true 64bit
  426. //
  427. HKEY hCR=NULL;
  428. HKEY Key=NULL;
  429. DWORD Ret;
  430. WCHAR Name [MAX_PATH + 1];
  431. DWORD dwBuffLen = MAX_PATH + 1;
  432. printf ("\n...Hello....");
  433. Ret = RegOpenKeyEx(
  434. HKEY_CURRENT_USER,
  435. L"Software",//NULL,
  436. 0,//OpenOption,
  437. KEY_ALL_ACCESS | KEY_WOW64_32KEY,
  438. &hCR
  439. );
  440. if (Ret == ERROR_SUCCESS ) {
  441. printf ("\nHello! checking key open at classes root");
  442. HandleToKeyName ( hCR, Name, &dwBuffLen );
  443. printf ("\nThe Real Name of the Key was [%S] %p %p", Name, hCR, HKEY_CLASSES_ROOT);
  444. //RegCloseKey(hCR);
  445. //Name[wcslen(Name)-12]=UNICODE_NULL;
  446. //hCR = OpenNode (Name);
  447. }
  448. else printf ("\nCouldn't open HKEY_CLASSES_ROOT" );
  449. Ret = RegOpenKeyEx(
  450. HKEY_CURRENT_USER,
  451. L"software\\classes\\software\\classes\\abcdef\\xyzw.XYZW.1\\ShellNew",
  452. 0,//OpenOption,
  453. KEY_READ | KEY_WOW64_32KEY,
  454. &Key
  455. );
  456. if (Ret == ERROR_SUCCESS ) {
  457. printf ("\n\nHello! checking key open at subkey under HKEY_CLASSES_ROOT");
  458. HandleToKeyName ( Key, Name, &dwBuffLen );
  459. printf ("\nThe Real Name of the Key was [%S]", Name);
  460. RegCloseKey(Key);
  461. }
  462. else printf ("\nCouldn't open subkey under HKEY_CLASSES_ROOT error%d", Ret);
  463. }
  464. void TestPredefinedHandle ()
  465. {
  466. WCHAR Name [MAX_PATH];
  467. DWORD Ret;
  468. DWORD dwBuffLen = MAX_PATH + 1;
  469. HKEY Key1;
  470. Ret = OpenKey( HKEY_CLASSES_ROOT, NULL, &Key1, KEY_WOW64_64KEY, L'N');
  471. dwBuffLen = MAX_PATH + 1;
  472. HandleToKeyName ( Key1, Name, &dwBuffLen );
  473. RegCloseKey (Key1);
  474. if (!Is64bitNode (Name))
  475. printf ("\nCouldn't get 64bit HKCR using KEY_WOW64_64KEY: %S", Name);
  476. Ret = OpenKey( HKEY_CLASSES_ROOT, NULL, &Key1, KEY_WOW64_32KEY, L'N');
  477. dwBuffLen = MAX_PATH + 1;
  478. HandleToKeyName ( Key1, Name, &dwBuffLen );
  479. RegCloseKey (Key1);
  480. if (Is64bitNode (Name))
  481. printf ("\nCouldn't get 32bit HKCR using KEY_WOW64_32KEY: %S", Name);
  482. Ret = OpenKey( HKEY_CURRENT_USER, L"Software\\Classes", &Key1, KEY_WOW64_64KEY, L'N');
  483. dwBuffLen = MAX_PATH + 1;
  484. HandleToKeyName ( Key1, Name, &dwBuffLen );
  485. RegCloseKey (Key1);
  486. if (!Is64bitNode (Name))
  487. printf ("\nCouldn't get 64bit HKCU Software\\Classes using KEY_WOW64_64KEY: %S", Name);
  488. Ret = OpenKey( HKEY_CURRENT_USER, L"Software\\Classes", &Key1, KEY_WOW64_32KEY, L'N');
  489. dwBuffLen = MAX_PATH + 1;
  490. HandleToKeyName ( Key1, Name, &dwBuffLen );
  491. RegCloseKey (Key1);
  492. if (Is64bitNode (Name))
  493. printf ("\nCouldn't get 32bit HKCU Software\\Classes using KEY_WOW64_32KEY: %S %d", Name, Ret);
  494. }
  495. InProcLocalServerTest ()
  496. {
  497. //
  498. // Create a server on the 64bit side with Inproc, and check on the 32bit if its get reflected.
  499. //
  500. HKEY Key1;
  501. HKEY Key2;
  502. WCHAR KeyName[256];
  503. DWORD Ret=0;
  504. wcscpy (KeyName, L"CLSID\\");
  505. wcscat (KeyName, GUID_STR);
  506. printf ("\nTesting of GUID\\{Inproc Server, Local Server}...");
  507. Ret += CreateOpenNode (HKEY_CLASSES_ROOT, KeyName, &Key1, 0, L'N');
  508. Ret += CreateOpenNode (Key1, L"InprocServer32", &Key2, 0, L'N');
  509. RegCloseKey (Key1);
  510. RegCloseKey (Key2);
  511. //
  512. // Try to open on the 32bit side
  513. //
  514. if (OpenKey (HKEY_CLASSES_ROOT, KeyName, &Key1, KEY_WOW64_32KEY, L'V') == 0) {
  515. RegCloseKey (Key1); //Key Shouldn'e be on the 32bit side
  516. Ret += -1;
  517. }
  518. //
  519. // You Need to add for test of local server...
  520. //
  521. if (Ret ==0)
  522. printf ("\nGUID Test Succeed......");
  523. else printf ("\nGUID test with Inprocserver failed..");
  524. //Delete the Key
  525. }
  526. void
  527. OpenCreateKeyTest ()
  528. {
  529. DWORD Ret=0;
  530. DWORD xx;
  531. HKEY Key1;
  532. HKEY Key2;
  533. InProcLocalServerTest ();
  534. TestPredefinedHandle ();
  535. #define TEST_NODE1 L".0xxxxxx"
  536. printf ("\n//_______________Test 32bit side____________________//");
  537. Ret = 0;
  538. // create 64==>TestNode
  539. Ret += CreateOpenNode( HKEY_CLASSES_ROOT, TEST_NODE1, &Key1, KEY_WOW64_64KEY, L'V');
  540. //create 32==>TestNode\GUIDSTR
  541. Ret += CreateOpenNode(Key1, GUID_STR, &Key2, KEY_WOW64_32KEY, L'V');
  542. RegCloseKey (Key2);
  543. //open 32==>TestNode
  544. Ret += OpenKey( HKEY_CLASSES_ROOT, TEST_NODE1, &Key2, KEY_WOW64_32KEY, L'V' );
  545. //Delete 32\TestNode==>GUID
  546. if ((xx=RegDeleteKey (Key2, GUID_STR ))!= ERROR_SUCCESS )
  547. printf ("\nSorry! couldn't delete key %S Err:%d", GUID_STR, xx);
  548. Ret +=xx;
  549. RegCloseKey(Key2);
  550. RegCloseKey(Key1);
  551. //delete 32==>TestNode
  552. Ret +=OpenKey (HKEY_CLASSES_ROOT, NULL, &Key1, KEY_WOW64_32KEY, L'V');
  553. if ( (xx=RegDeleteKey (Key1, TEST_NODE1))!= ERROR_SUCCESS )
  554. printf ("\nSorry! couldn't delete key from 32bit tree=>%S Err:%d", TEST_NODE1, xx);
  555. Ret +=xx;
  556. RegCloseKey (Key1);
  557. //delete 64==>TestNode
  558. Ret +=OpenKey (HKEY_CLASSES_ROOT, NULL, &Key1, KEY_WOW64_64KEY, L'V');
  559. if ((xx= RegDeleteKey (Key1, TEST_NODE1))!= ERROR_SUCCESS )
  560. printf ("\nSorry! couldn't delete key from 64bit tree=>%S Err:%d", TEST_NODE1, xx);
  561. Ret +=xx;
  562. RegCloseKey (Key1);
  563. if (Ret != ERROR_SUCCESS )
  564. printf ("\nTest failed....");
  565. else
  566. printf ("\nTest succeed...");
  567. printf ("\n//_______________Test 64bit side____________________//");
  568. Ret = 0;
  569. #define TEST_NODE2 L".0000######"
  570. // create 32==>TestNode
  571. Ret += CreateOpenNode( HKEY_CLASSES_ROOT, TEST_NODE2, &Key1, KEY_WOW64_32KEY, L'V');
  572. //create 64==>TestNode\GUIDSTR
  573. Ret += CreateOpenNode(Key1, GUID_STR, &Key2, KEY_WOW64_64KEY, L'V');
  574. RegCloseKey (Key2);
  575. //Open 64==>TestNode
  576. Ret += OpenKey( HKEY_CLASSES_ROOT, TEST_NODE2, &Key2, KEY_WOW64_64KEY, L'V' );
  577. //Delete 64\TestNode==>GUID
  578. if ((xx=RegDeleteKey (Key2, GUID_STR ))!= ERROR_SUCCESS )
  579. printf ("\nSorry! couldn't delete key %S Err:%d", GUID_STR, xx);
  580. Ret +=xx;
  581. RegCloseKey(Key2);
  582. RegCloseKey(Key1);
  583. //Delete 64==>TestNode
  584. Ret +=OpenKey (HKEY_CLASSES_ROOT, NULL, &Key1, KEY_WOW64_64KEY, L'V');
  585. if ((xx= RegDeleteKey (Key1, TEST_NODE2))!= ERROR_SUCCESS )
  586. printf ("\nSorry! couldn't delete key from 64bit tree=>%S Err:%d", TEST_NODE2, xx);
  587. Ret +=xx;
  588. RegCloseKey (Key1);
  589. //Delete 32==>TestNode
  590. Ret +=OpenKey (HKEY_CLASSES_ROOT, NULL, &Key1, KEY_WOW64_32KEY, L'V');
  591. if ( (xx=RegDeleteKey (Key1, TEST_NODE2))!= ERROR_SUCCESS )
  592. printf ("\nSorry! couldn't delete key from 32bit tree=>%S Err:%d", TEST_NODE2, xx);
  593. Ret +=xx;
  594. RegCloseKey (Key1);
  595. if (Ret != ERROR_SUCCESS )
  596. printf ("\nTest failed....");
  597. else
  598. printf ("\nTest succeed...");
  599. }
  600. TestCreateOpenNode ()
  601. {
  602. DWORD Count;
  603. DWORD i;
  604. HKEY hKey=NULL;
  605. DWORD Ret;
  606. Count = TEST_NODE_NUM;
  607. for ( i=0; i<Count;i++) {
  608. Ret = CreateOpenNode (
  609. TestNodeList[i].hKey,
  610. TestNodeList[i].Name,
  611. &hKey,
  612. 0,
  613. L'V'
  614. );
  615. if (hKey == NULL ) {
  616. printf ("\n Couldn't create/open Key %S",TestNodeList[i]);
  617. continue;
  618. }
  619. }
  620. }
  621. VOID
  622. TestSharedResources ()
  623. {
  624. //#define TEST_USER_CLASSES_ROOT L"\\REGISTRY\\MACHINE\\SYSTEM\\TEST345"
  625. #define TEST_USER_CLASSES_ROOT L"\\REGISTRY\\USER\\S-1-5-21-397955417-626881126-188441444-2721867_Classes"
  626. HANDLE hEvent;
  627. WCHAR Name[256];
  628. wcscpy (Name, TEST_USER_CLASSES_ROOT);
  629. if (!CreateNode (Name))
  630. printf ("\nSorry! Couldn't create key.. %S", TEST_USER_CLASSES_ROOT );
  631. Wow64RegNotifyLoadHive ( Name);
  632. //Wow64RegNotifyUnloadHive ( Name );
  633. }
  634. VOID
  635. TestTypeLib ()
  636. {
  637. HKEY SrcKey;
  638. HKEY DestKey;
  639. printf ("\nTesting Typelib copy info...");
  640. SrcKey = OpenNode (L"\\REGISTRY\\MACHINE\\Software\\classes\\TypeLib");
  641. RegCreateKeyEx(
  642. HKEY_LOCAL_MACHINE, // handle to an open key
  643. L"SOFTWARE\\Classes\\Wow6432Node\\TypeLib", // address of subkey name
  644. 0, // reserved
  645. NULL, // address of class string
  646. REG_OPTION_NON_VOLATILE, // special options flag
  647. KEY_ALL_ACCESS, // desired security access
  648. NULL, // address of key security structure
  649. &DestKey, // address of buffer for opened handle
  650. NULL // address of disposition value buffer
  651. );
  652. printf ("\n OpenHandle %p %p", SrcKey, DestKey);
  653. ProcessTypeLib ( SrcKey, DestKey, FALSE );
  654. }
  655. VOID
  656. TestRegReflectKey ()
  657. {
  658. HKEY SrcKey;
  659. printf ("\nTesting RegSyncKey copy info...");
  660. SrcKey = OpenNode (L"\\REGISTRY\\MACHINE\\Software");
  661. printf ("\nRegReflectKey returned with %d", RegReflectKey (SrcKey, L"1\\2\\3", 0) );
  662. RegCloseKey (SrcKey);
  663. }
  664. VOID
  665. Test4()
  666. {
  667. LONG lRes = 0;
  668. HKEY hKey = NULL;
  669. HKEY hKey32 = NULL;
  670. lRes = RegOpenKeyEx(
  671. HKEY_LOCAL_MACHINE,
  672. L"Software",
  673. 0,
  674. KEY_ALL_ACCESS | KEY_WOW64_32KEY,
  675. &hKey
  676. );
  677. if(lRes != ERROR_SUCCESS)
  678. {
  679. return;
  680. }
  681. lRes = RegCreateKeyEx(
  682. hKey,
  683. L"_Key",
  684. 0,
  685. NULL,
  686. REG_OPTION_NON_VOLATILE,
  687. KEY_ALL_ACCESS | KEY_WOW64_32KEY,
  688. NULL,
  689. &hKey32,
  690. NULL
  691. );
  692. if(lRes != 0)
  693. {
  694. printf("key not created\n");
  695. return;
  696. }
  697. RegCloseKey(hKey32);
  698. }
  699. void TestRegistry()
  700. {
  701. HKEY hKey = NULL;
  702. HKEY Key1;
  703. HKEY Key2;
  704. //Test4();
  705. //RegOpenKeyEx ( HKEY_LOCAL_MACHINE, L"Software", 0, KEY_ALL_ACCESS, &hKey );
  706. //hKey = OpenNode (L"\\REGISTRY\\MACHINE");
  707. //CreateOpenNode(hKey, L"Software", &Key1, KEY_WOW64_32KEY, L'V');
  708. //CreateOpenNode(Key1, L"YYYzzz", &Key2, KEY_WOW64_32KEY, L'V');
  709. //TestRegReflectKey ();
  710. //TestSharedResources ();
  711. //TestTypeLib ();
  712. //BasicRegReMapTest();
  713. //TestOpenHKCR(1,2);
  714. printf ("\n\n2nd test.....OpenCreateKeyTest ()");
  715. OpenCreateKeyTest ();
  716. //TestCreateHKCR ();
  717. }
  718. LPTSTR NextParam (
  719. LPTSTR lpStr
  720. )
  721. /*++
  722. Routine Description
  723. Point to the next parameter in the commandline.
  724. Arguments:
  725. lpStr - pointer to the current command line
  726. Return Value:
  727. TRUE if the function succeed, FALSE otherwise.
  728. --*/
  729. {
  730. WCHAR ch = L' ';
  731. if (lpStr == NULL )
  732. return NULL;
  733. if ( *lpStr == 0 )
  734. return lpStr;
  735. while ( ( *lpStr != 0 ) && ( lpStr[0] != ch )) {
  736. if ( ( lpStr [0] == L'\"') || ( lpStr [0] == L'\'') )
  737. ch = lpStr [0];
  738. lpStr++;
  739. }
  740. if ( ch !=L' ' ) lpStr++;
  741. while ( ( *lpStr != 0 ) && (lpStr[0] == L' ') )
  742. lpStr++;
  743. return lpStr;
  744. }
  745. DWORD CopyParam (
  746. LPTSTR lpDestParam,
  747. LPTSTR lpCommandParam
  748. )
  749. /*++
  750. Routine Description
  751. Copy the current parameter to lpDestParam.
  752. Arguments:
  753. lpDestParam - that receive current parameter
  754. lpCommandParam - pointer to the current command line
  755. Return Value:
  756. TRUE if the function succeed, FALSE otherwise.
  757. --*/
  758. {
  759. DWORD dwLen = 0;
  760. WCHAR ch = L' ';
  761. *lpDestParam = 0;
  762. if ( ( lpCommandParam [0] == L'\"') || ( lpCommandParam [0] == L'\'') ) {
  763. ch = lpCommandParam [0];
  764. lpCommandParam++;
  765. };
  766. while ( ( lpCommandParam [0] ) != ch && ( lpCommandParam [0] !=0 ) ) {
  767. *lpDestParam++ = *lpCommandParam++;
  768. dwLen++;
  769. if ( dwLen>255 ) return FALSE;
  770. }
  771. if ( ch != L' ' && ch != lpCommandParam [0] )
  772. return FALSE;
  773. else lpCommandParam++;
  774. *lpDestParam = 0;
  775. return TRUE;
  776. }
  777. ///////////////////////////////////////////////////////////////////////////////
  778. /////////////////////////////////////////////////////////////////////////////////
  779. BOOL
  780. ParseCommand ()
  781. /*++
  782. Routine Description
  783. Parse command line parameters. Get different options from
  784. command line parameters.
  785. Arguments:
  786. None.
  787. Return Value:
  788. TRUE if the function succeed, FALSE otherwise.
  789. --*/
  790. {
  791. BOOL bSync = FALSE;
  792. LPTSTR lptCmdLine1 = GetCommandLine ();
  793. LPTSTR lptCmdLine = NextParam ( lptCmdLine1 );
  794. if ( lptCmdLine[0] == 0 )
  795. return FALSE;
  796. printf ("\nRunning Wow64 registry testing tool\n");
  797. while ( ( lptCmdLine != NULL ) && ( lptCmdLine[0] != 0 ) ) {
  798. if ( lptCmdLine[0] != '-' )
  799. return FALSE;
  800. switch ( lptCmdLine[1] ) {
  801. case L'C': //sync ClsID
  802. case L'c':
  803. Wow64SyncCLSID ();
  804. break;
  805. case L'd':
  806. printf ("\n<TBD>Remove all the Keys from 32bit side that were copied from 64bit side");
  807. //CleanpRegistry ( );
  808. break;
  809. case L'D':
  810. printf ("\n<TBD>Remove all the Keys from 32bit side that were copied from 64bit side");
  811. //CleanpRegistry ();
  812. break;
  813. case L'p':
  814. case L'P': // populate registry
  815. //CleanupWow64NodeKey ();
  816. PopulateReflectorTable ();
  817. break;
  818. case 't':
  819. case 'T':
  820. TestRegistry ();
  821. break;
  822. case L'r':
  823. case L'R':
  824. //
  825. // run the reflector codes;
  826. //
  827. InitReflector ();
  828. if ( !RegisterReflector () ) {
  829. printf ("\nSorry! reflector couldn't be register");
  830. UnRegisterReflector ();
  831. return FALSE;
  832. }
  833. printf ("\nSleeping for 100 min to test reflector codes ...........\n");
  834. Sleep (1000*60*100);
  835. UnRegisterReflector ();
  836. break;
  837. case L's':
  838. case L'S':
  839. bSync = TRUE;
  840. break;
  841. case L'a':
  842. case L'A':
  843. //
  844. // Analyze a key
  845. //
  846. AnalyzeNode (&lptCmdLine[2], bSync);
  847. break;
  848. default:
  849. return FALSE;
  850. break;
  851. }
  852. lptCmdLine = NextParam ( lptCmdLine );
  853. }
  854. return TRUE;
  855. }
  856. BOOL foo ()
  857. {
  858. WCHAR PathName[256];
  859. wcscpy (PathName, L"c:\\Program Files (x86)\\foo.exe");
  860. {
  861. UNICODE_STRING FileNameU;
  862. HANDLE FileHandle;
  863. OBJECT_ATTRIBUTES ObjectAttributes;
  864. NTSTATUS Status;
  865. IO_STATUS_BLOCK statusBlock;
  866. //
  867. //
  868. // Convert the Win32 pathname to an NT pathname
  869. //
  870. printf ("\nne w name %S", PathName);
  871. if (!RtlDosPathNameToNtPathName_U(PathName,
  872. &FileNameU,
  873. NULL,
  874. NULL)) {
  875. // probably out-of-memory
  876. return FALSE;
  877. }
  878. printf ("\nne w name %S", FileNameU.Buffer);
  879. //
  880. // Open the file
  881. //
  882. InitializeObjectAttributes(&ObjectAttributes,
  883. &FileNameU,
  884. OBJ_CASE_INSENSITIVE,
  885. NULL,
  886. NULL);
  887. Status = NtOpenFile(&FileHandle,
  888. FILE_READ_DATA,
  889. NULL,
  890. &statusBlock,
  891. FILE_SHARE_READ,
  892. 0);
  893. RtlFreeHeap(RtlProcessHeap(), 0, FileNameU.Buffer);
  894. if (NT_SUCCESS(Status)) {
  895. printf ("\nfoo");
  896. } else printf ("\nFailed %x",Status);
  897. }
  898. return 0;
  899. }
  900. TestDebugRegistryRedirectionOnClose ()
  901. {
  902. #if DBG
  903. VOID
  904. DebugRegistryRedirectionOnClose (
  905. HANDLE KeyHandle,
  906. PWCHAR Message
  907. );
  908. HKEY hKey = OpenNode (L"\\REGISTRY\\MACHINE\\SOFTWARE");
  909. DebugRegistryRedirectionOnClose (hKey, L"Hello");
  910. NtClose (hKey);
  911. #endif
  912. }
  913. void
  914. InitializeWow64OnBoot1(
  915. DWORD dwFlag
  916. )
  917. /*++
  918. Routine Description:
  919. Called from advapi to get an handle to remapped key that is on reflection list.
  920. Arguments:
  921. dwFlag - define the point where this function were invoked.
  922. 1- means were invoked from csr service
  923. 2- means this were invoked by setup.
  924. Return Value:
  925. None.
  926. --*/
  927. {
  928. DWORD Ret;
  929. HKEY Key;
  930. NTSTATUS st;
  931. OBJECT_ATTRIBUTES Obja;
  932. UNICODE_STRING KeyName;
  933. //
  934. // Delete the link key if that does exist.
  935. //
  936. RtlInitUnicodeString (&KeyName, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Wow6432Node\\Classes");
  937. InitializeObjectAttributes (&Obja, &KeyName, OBJ_CASE_INSENSITIVE, NULL, NULL );
  938. st = NtOpenKey (&Key, KEY_ALL_ACCESS, &Obja);
  939. if (NT_SUCCESS(st)) {
  940. st = NtDeleteKey (Key);
  941. NtClose (Key);
  942. }
  943. RtlInitUnicodeString (&KeyName, L"\\REGISTRY\\MACHINE\\SOFTWARE\\Wow6432Node\\Classes");
  944. InitializeObjectAttributes (&Obja, &KeyName, OBJ_CASE_INSENSITIVE, NULL, NULL );
  945. st = NtCreateKey(
  946. &Key,
  947. KEY_ALL_ACCESS | KEY_CREATE_LINK,
  948. &Obja,
  949. 0,
  950. NULL ,
  951. REG_OPTION_NON_VOLATILE | REG_OPTION_OPEN_LINK | REG_OPTION_CREATE_LINK, // special options flag
  952. NULL
  953. );
  954. if (NT_SUCCESS(st)) {
  955. RtlInitUnicodeString (&KeyName, L"SymbolicLinkValue");
  956. st = NtSetValueKey(
  957. Key,
  958. &KeyName,
  959. 0 ,
  960. REG_LINK,
  961. (PBYTE)WOW64_32BIT_MACHINE_CLASSES_ROOT,
  962. (DWORD ) (wcslen (WOW64_32BIT_MACHINE_CLASSES_ROOT) * sizeof (WCHAR))
  963. );
  964. NtClose(Key);
  965. if ( !NT_SUCCESS(st) ) {
  966. #if DBG
  967. DbgPrint ( "Wow64-InitializeWow64OnBoot: Couldn't create symbolic link%S\n", WOW64_32BIT_MACHINE_CLASSES_ROOT);
  968. #endif
  969. return;
  970. }
  971. }
  972. return;
  973. }
  974. int __cdecl
  975. main()
  976. {
  977. InitializeWow64OnBoot1 (0);
  978. //foo();
  979. //TestDebugRegistryRedirectionOnClose();
  980. /* if (!ParseCommand ()) {
  981. printf ( "\nUsages: w64setup [-c] [-C] [-d] [-D] [-r]\n");
  982. printf ( "\n -c Copy from 32bit to 64bit side of the registry");
  983. printf ( "\n -C Copy from 64bit to 32bit side of the registry");
  984. printf ( "\n");
  985. printf ( "\n -d Remove all the Keys from 32bit side that were copied from 64bit side");
  986. printf ( "\n -D Remove all the Keys from 64bit side that were copied from 32bit side");
  987. printf ( "\n");
  988. printf ( "\n -r Run reflector thread");
  989. printf ( "\n -A Analyze a key if that going to be reflected");
  990. printf ("\n");
  991. return 0;
  992. }
  993. */
  994. printf ("\nDone.");
  995. return 0;
  996. }