Source code of Windows XP (NT5)
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.

535 lines
13 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. test.c
  5. Abstract:
  6. This source file implements the seven required functions for a
  7. Windows NT 5.0 migration DLL. It is used to perform various tests.
  8. Author:
  9. Jim Schmidt (jimschm) 02-Apr-1998
  10. Revision History:
  11. --*/
  12. #include "pch.h"
  13. #include "resource.h"
  14. HANDLE g_hHeap;
  15. HINSTANCE g_hInst;
  16. typedef struct {
  17. CHAR CompanyName[256];
  18. CHAR SupportNumber[256];
  19. CHAR SupportUrl[256];
  20. CHAR InstructionsToUser[1024];
  21. } VENDORINFO, *PVENDORINFO;
  22. #define SIGNATURE 0x01010102
  23. BOOL
  24. WINAPI
  25. DllMain (
  26. IN HINSTANCE DllInstance,
  27. IN ULONG ReasonForCall,
  28. IN LPVOID Reserved
  29. )
  30. {
  31. switch (ReasonForCall) {
  32. case DLL_PROCESS_ATTACH:
  33. //
  34. // We don't need DLL_THREAD_ATTACH or DLL_THREAD_DETACH messages
  35. //
  36. DisableThreadLibraryCalls (DllInstance);
  37. //
  38. // Global init
  39. //
  40. g_hHeap = GetProcessHeap();
  41. g_hInst = DllInstance;
  42. // Open log; FALSE means do not delete existing log
  43. SetupOpenLog (FALSE);
  44. break;
  45. case DLL_PROCESS_DETACH:
  46. SetupCloseLog();
  47. break;
  48. }
  49. return TRUE;
  50. }
  51. VOID
  52. Barf (
  53. VOID
  54. )
  55. {
  56. PBYTE p;
  57. p = (PBYTE) 2;
  58. *p = 0;
  59. MessageBox (NULL, "Feeling too well to barf", NULL, MB_OK);
  60. }
  61. typedef struct {
  62. DWORD Signature;
  63. CHAR ProductId[256];
  64. UINT DllVersion;
  65. INT CodePageArray[256];
  66. CHAR FileNameMultiSz[4096];
  67. BOOL BarfInQueryVersion;
  68. BOOL BarfInInit9x;
  69. BOOL BarfInUser9x;
  70. BOOL BarfInSystem9x;
  71. BOOL BarfInInitNt;
  72. BOOL BarfInUserNt;
  73. BOOL BarfInSystemNt;
  74. CHAR MigrateInf[16384];
  75. VENDORINFO vi;
  76. } SETTINGS, *PSETTINGS;
  77. SETTINGS g_Settings;
  78. VOID
  79. pSaveSettings (
  80. BOOL Defaults
  81. )
  82. {
  83. HANDLE File;
  84. DWORD DontCare;
  85. if (Defaults) {
  86. File = CreateFile ("c:\\settings.dat", GENERIC_WRITE, 0, NULL,
  87. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  88. } else {
  89. File = CreateFile ("settings.dat", GENERIC_WRITE, 0, NULL,
  90. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  91. }
  92. if (File != INVALID_HANDLE_VALUE) {
  93. WriteFile (File, &g_Settings, sizeof (g_Settings), &DontCare, NULL);
  94. CloseHandle (File);
  95. } else {
  96. MessageBox (NULL, "Unable to save settings to media dir", NULL, MB_OK|MB_TOPMOST);
  97. }
  98. }
  99. VOID
  100. pLoadSettings (
  101. BOOL Defaults
  102. )
  103. {
  104. HANDLE File;
  105. DWORD DontCare;
  106. if (Defaults) {
  107. File = CreateFile ("c:\\settings.dat", GENERIC_READ, 0, NULL,
  108. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  109. } else {
  110. File = CreateFile ("settings.dat", GENERIC_READ, 0, NULL,
  111. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  112. }
  113. if (File != INVALID_HANDLE_VALUE) {
  114. ReadFile (File, &g_Settings, sizeof (g_Settings), &DontCare, NULL);
  115. CloseHandle (File);
  116. if (g_Settings.Signature != SIGNATURE) {
  117. MessageBox (NULL, "settings.dat is not valid", NULL, MB_OK|MB_TOPMOST);
  118. ZeroMemory (&g_Settings, sizeof (g_Settings));
  119. g_Settings.CodePageArray[0] = -1;
  120. g_Settings.Signature = SIGNATURE;
  121. }
  122. }
  123. }
  124. BOOL
  125. CALLBACK
  126. GetArgsProc (
  127. HWND hdlg,
  128. UINT uMsg,
  129. WPARAM wParam,
  130. LPARAM lParam
  131. )
  132. {
  133. CHAR Version[32];
  134. CHAR List[4096];
  135. PSTR p, q;
  136. INT i;
  137. switch (uMsg) {
  138. case WM_INITDIALOG:
  139. SetWindowText (GetDlgItem (hdlg, IDC_PRODUCT_ID), g_Settings.ProductId);
  140. wsprintf (Version, "%u", max (1, g_Settings.DllVersion));
  141. SetWindowText (GetDlgItem (hdlg, IDC_VERSION), Version);
  142. SetWindowText (GetDlgItem (hdlg, IDC_COMPANY), g_Settings.vi.CompanyName);
  143. SetWindowText (GetDlgItem (hdlg, IDC_PHONE), g_Settings.vi.SupportNumber);
  144. SetWindowText (GetDlgItem (hdlg, IDC_URL), g_Settings.vi.SupportUrl);
  145. SetWindowText (GetDlgItem (hdlg, IDC_INSTRUCTIONS), g_Settings.vi.InstructionsToUser);
  146. SetWindowText (GetDlgItem (hdlg, IDC_MIGRATE_INF), g_Settings.MigrateInf);
  147. p = g_Settings.FileNameMultiSz;
  148. q = List;
  149. while (*p) {
  150. if (q != List) {
  151. _mbscpy (q, ",");
  152. q = _mbschr (q, 0);
  153. }
  154. _mbscpy (q, p);
  155. q = _mbschr (q, 0);
  156. p = _mbschr (p, 0) + 1;
  157. }
  158. *q = 0;
  159. SetWindowText (GetDlgItem (hdlg, IDC_FILES), List);
  160. q = List;
  161. if (g_Settings.CodePageArray[0] != -1) {
  162. for (i = 0 ; g_Settings.CodePageArray[i] != -1 ; i++) {
  163. if (i > 0) {
  164. _mbscpy (q, ",");
  165. q = _mbschr (q, 0);
  166. }
  167. wsprintf (q, "%i", g_Settings.CodePageArray[i]);
  168. q = _mbschr (q, 0);
  169. }
  170. _mbscpy (q, ",-1");
  171. q = _mbschr (q, 0);
  172. }
  173. *q = 0;
  174. SetWindowText (GetDlgItem (hdlg, IDC_CODE_PAGES), List);
  175. CheckDlgButton (hdlg, IDC_BARF_QV, g_Settings.BarfInQueryVersion ? BST_CHECKED : BST_UNCHECKED);
  176. CheckDlgButton (hdlg, IDC_BARF_INIT9X, g_Settings.BarfInInit9x ? BST_CHECKED : BST_UNCHECKED);
  177. CheckDlgButton (hdlg, IDC_BARF_USER9X, g_Settings.BarfInUser9x ? BST_CHECKED : BST_UNCHECKED);
  178. CheckDlgButton (hdlg, IDC_BARF_SYSTEM9X, g_Settings.BarfInSystem9x ? BST_CHECKED : BST_UNCHECKED);
  179. CheckDlgButton (hdlg, IDC_BARF_INITNT, g_Settings.BarfInInitNt ? BST_CHECKED : BST_UNCHECKED);
  180. CheckDlgButton (hdlg, IDC_BARF_USERNT, g_Settings.BarfInUserNt ? BST_CHECKED : BST_UNCHECKED);
  181. CheckDlgButton (hdlg, IDC_BARF_SYSTEMNT, g_Settings.BarfInSystemNt ? BST_CHECKED : BST_UNCHECKED);
  182. return FALSE;
  183. case WM_COMMAND:
  184. if (HIWORD (wParam) == BN_CLICKED) {
  185. switch (LOWORD (wParam)) {
  186. case IDOK:
  187. GetDlgItemText (hdlg, IDC_PRODUCT_ID, g_Settings.ProductId, 256);
  188. GetDlgItemText (hdlg, IDC_MIGRATE_INF, g_Settings.MigrateInf, 16384);
  189. GetDlgItemText (hdlg, IDC_VERSION, Version, 32);
  190. g_Settings.DllVersion = strtoul (Version, NULL, 10);
  191. GetDlgItemText (hdlg, IDC_CODE_PAGES, List, 4096);
  192. i = 0;
  193. if (*List) {
  194. q = List;
  195. do {
  196. p = _mbschr (List, TEXT(','));
  197. if (p) {
  198. *p = 0;
  199. p++;
  200. }
  201. g_Settings.CodePageArray[i] = atoi (q);
  202. i++;
  203. q = p;
  204. } while (p);
  205. }
  206. g_Settings.CodePageArray[i] = -1;
  207. GetDlgItemText (hdlg, IDC_FILES, List, 4096);
  208. if (*List) {
  209. _mbscpy (g_Settings.FileNameMultiSz, List);
  210. p = _mbschr (g_Settings.FileNameMultiSz, ',');
  211. while (p) {
  212. *p = 0;
  213. p = _mbschr (p + 1, ',');
  214. }
  215. p++;
  216. *p = 0;
  217. } else {
  218. *g_Settings.FileNameMultiSz = 0;
  219. }
  220. GetDlgItemText (hdlg, IDC_COMPANY, g_Settings.vi.CompanyName, 256);
  221. GetDlgItemText (hdlg, IDC_PHONE, g_Settings.vi.SupportNumber, 256);
  222. GetDlgItemText (hdlg, IDC_URL, g_Settings.vi.SupportUrl, 256);
  223. GetDlgItemText (hdlg, IDC_INSTRUCTIONS, g_Settings.vi.InstructionsToUser, 256);
  224. g_Settings.BarfInQueryVersion = (IsDlgButtonChecked (hdlg, IDC_BARF_QV) == BST_CHECKED);
  225. g_Settings.BarfInInit9x = (IsDlgButtonChecked (hdlg, IDC_BARF_INIT9X) == BST_CHECKED);
  226. g_Settings.BarfInUser9x = (IsDlgButtonChecked (hdlg, IDC_BARF_USER9X) == BST_CHECKED);
  227. g_Settings.BarfInSystem9x = (IsDlgButtonChecked (hdlg, IDC_BARF_SYSTEM9X) == BST_CHECKED);
  228. g_Settings.BarfInInitNt = (IsDlgButtonChecked (hdlg, IDC_BARF_INITNT) == BST_CHECKED);
  229. g_Settings.BarfInUserNt = (IsDlgButtonChecked (hdlg, IDC_BARF_USERNT) == BST_CHECKED);
  230. g_Settings.BarfInSystemNt = (IsDlgButtonChecked (hdlg, IDC_BARF_SYSTEMNT) == BST_CHECKED);
  231. EndDialog (hdlg, IDOK);
  232. break;
  233. case IDCANCEL:
  234. EndDialog (hdlg, IDCANCEL);
  235. break;
  236. }
  237. }
  238. break;
  239. }
  240. return FALSE;
  241. }
  242. LONG
  243. CALLBACK
  244. QueryVersion (
  245. OUT PCSTR *ProductID,
  246. OUT PUINT DllVersion,
  247. OUT PINT *CodePageArray, OPTIONAL
  248. OUT PCSTR *ExeNamesBuf, OPTIONAL
  249. OUT PVENDORINFO *VendorInfo
  250. )
  251. {
  252. ZeroMemory (&g_Settings, sizeof (g_Settings));
  253. g_Settings.CodePageArray[0] = -1;
  254. g_Settings.Signature = SIGNATURE;
  255. pLoadSettings(TRUE);
  256. if (DialogBox (
  257. g_hInst,
  258. MAKEINTRESOURCE(IDD_ARGS_DLG),
  259. NULL,
  260. GetArgsProc
  261. ) != IDOK) {
  262. return ERROR_NOT_INSTALLED;
  263. }
  264. *ProductID = g_Settings.ProductId;
  265. *DllVersion = g_Settings.DllVersion;
  266. if (g_Settings.CodePageArray[0] != -1) {
  267. *CodePageArray = g_Settings.CodePageArray;
  268. }
  269. *ExeNamesBuf = g_Settings.FileNameMultiSz;
  270. *VendorInfo = &g_Settings.vi;
  271. if (g_Settings.BarfInQueryVersion) {
  272. Barf();
  273. }
  274. pSaveSettings(TRUE);
  275. pSaveSettings(FALSE);
  276. return ERROR_SUCCESS;
  277. }
  278. LONG
  279. CALLBACK
  280. Initialize9x (
  281. IN PCSTR WorkingDirectory,
  282. IN PCSTR SourceDirectories,
  283. PVOID Reserved
  284. )
  285. {
  286. HANDLE File;
  287. DWORD DontCare;
  288. pLoadSettings(FALSE);
  289. if (g_Settings.MigrateInf[0]) {
  290. File = CreateFile ("migrate.inf", GENERIC_READ|GENERIC_WRITE, 0, NULL,
  291. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  292. if (File != INVALID_HANDLE_VALUE) {
  293. SetFilePointer (File, 0, NULL, FILE_END);
  294. WriteFile (File, "\r\n", 2, &DontCare, NULL);
  295. WriteFile (File, g_Settings.MigrateInf, lstrlen (g_Settings.MigrateInf), &DontCare, NULL);
  296. WriteFile (File, "\r\n", 2, &DontCare, NULL);
  297. CloseHandle (File);
  298. } else {
  299. return GetLastError();
  300. }
  301. }
  302. if (g_Settings.BarfInInit9x) {
  303. Barf();
  304. }
  305. return ERROR_SUCCESS;
  306. }
  307. LONG
  308. CALLBACK
  309. MigrateUser9x (
  310. IN HWND ParentWnd,
  311. IN PCSTR UnattendFile,
  312. IN HKEY UserRegKey,
  313. IN PCSTR UserName,
  314. PVOID Reserved
  315. )
  316. {
  317. if (g_Settings.BarfInUser9x) {
  318. Barf();
  319. }
  320. return ERROR_SUCCESS;
  321. }
  322. LONG
  323. CALLBACK
  324. MigrateSystem9x (
  325. IN HWND ParentWnd,
  326. IN PCSTR UnattendFile,
  327. PVOID Reserved
  328. )
  329. {
  330. if (g_Settings.BarfInSystem9x) {
  331. Barf();
  332. }
  333. return ERROR_SUCCESS;
  334. }
  335. LONG
  336. CALLBACK
  337. InitializeNT (
  338. IN PCWSTR WorkingDirectory,
  339. IN PCWSTR SourceDirectories,
  340. PVOID Reserved
  341. )
  342. {
  343. pLoadSettings(FALSE);
  344. if (g_Settings.BarfInInitNt) {
  345. Barf();
  346. }
  347. return ERROR_SUCCESS;
  348. }
  349. LONG
  350. CALLBACK
  351. MigrateUserNT (
  352. IN HINF UnattendInfHandle,
  353. IN HKEY UserRegKey,
  354. IN PCWSTR UserName,
  355. PVOID Reserved
  356. )
  357. {
  358. TCHAR Path[MAX_PATH];
  359. TCHAR Msg[2048];
  360. HKEY RegKey;
  361. TCHAR ExpandedPath[MAX_PATH];
  362. DWORD Size;
  363. DWORD rc;
  364. if (g_Settings.BarfInUserNt) {
  365. Barf();
  366. }
  367. wsprintf (Msg, TEXT("User: %ls\r\n"), UserName);
  368. OutputDebugString (Msg);
  369. GetEnvironmentVariable (TEXT("USERPROFILE"), Path, MAX_PATH);
  370. wsprintf (Msg, TEXT("User Profile: %s\r\n"), Path);
  371. OutputDebugString (Msg);
  372. rc = RegOpenKeyEx (
  373. UserRegKey,
  374. TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"),
  375. 0,
  376. KEY_READ,
  377. &RegKey
  378. );
  379. if (rc != ERROR_SUCCESS) {
  380. wsprintf (Msg, TEXT("Can't open reg key. Error: %u\r\n"), rc);
  381. OutputDebugString (Msg);
  382. } else {
  383. Size = sizeof (ExpandedPath);
  384. rc = RegQueryValueEx (RegKey, TEXT("Programs"), NULL, NULL, (PBYTE) ExpandedPath, &Size);
  385. if (rc == ERROR_SUCCESS) {
  386. wsprintf (Msg, TEXT("Programs: %ls\r\n"), ExpandedPath);
  387. OutputDebugString (Msg);
  388. } else {
  389. wsprintf (Msg, TEXT("Can't open reg key. Error: %u\r\n"), rc);
  390. OutputDebugString (Msg);
  391. }
  392. RegCloseKey (RegKey);
  393. }
  394. return ERROR_SUCCESS;
  395. }
  396. LONG
  397. CALLBACK
  398. MigrateSystemNT (
  399. IN HINF UnattendInfHandle,
  400. PVOID Reserved
  401. )
  402. {
  403. if (g_Settings.BarfInSystemNt) {
  404. Barf();
  405. }
  406. return ERROR_SUCCESS;
  407. }