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.

437 lines
10 KiB

  1. #include "pch.h"
  2. #define SOFTPCIVIEW L"ViewSettings"
  3. WCHAR g_LastError[MAX_PATH];
  4. BOOL
  5. SoftPCI_WriteDeviceListToRegistry(
  6. IN PHKEY RegKeyHandle,
  7. IN PLIST_ENTRY ListHead,
  8. IN PLIST_ENTRY CurrentEntry
  9. );
  10. HBRUSH
  11. PASCAL
  12. SoftPCI_CreateDitheredBrush(
  13. VOID
  14. )
  15. {
  16. WORD graybits[] = {0x5555,0xAAAA,0x5555,0xAAAA,0x5555,0xAAAA,0x5555,0xAAAA};
  17. HBRUSH hBrush;
  18. HBITMAP hBitmap;
  19. if ((hBitmap = CreateBitmap(8, 8, 1, 1, graybits)) != NULL) {
  20. hBrush = CreatePatternBrush(hBitmap);
  21. DeleteObject(hBitmap);
  22. }else{
  23. hBrush = NULL;
  24. }
  25. return hBrush;
  26. }
  27. VOID
  28. SoftPCI_FormatConfigBuffer(
  29. IN PWCHAR Buffer,
  30. IN PPCI_COMMON_CONFIG Config
  31. )
  32. {
  33. ULONG i = 0, offset;
  34. PULONG p = NULL;
  35. p = (PULONG)Config;
  36. offset = 0;
  37. wsprintf(Buffer + wcslen(Buffer), L"%02X: ", offset);
  38. for (i=0; i < (sizeof(PCI_COMMON_CONFIG) / sizeof(ULONG)); i++) {
  39. wsprintf(Buffer + wcslen(Buffer), L"%08X", *p);
  40. if ((((i+1) % 4) == 0) ||
  41. ((i+1) == (sizeof(PCI_COMMON_CONFIG) / sizeof(ULONG)))) {
  42. wcscat(Buffer, L"\r\n");
  43. if (((offset+=0x10) < sizeof(PCI_COMMON_CONFIG))) {
  44. wsprintf(Buffer + wcslen(Buffer), L"%02X: ", offset);
  45. }
  46. }else{
  47. wsprintf(Buffer + wcslen(Buffer), L",");
  48. }
  49. p++;
  50. }
  51. }
  52. PWCHAR
  53. SoftPCI_GetLastError(
  54. VOID
  55. )
  56. {
  57. DWORD len = 0;
  58. len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  59. NULL,
  60. GetLastError(),
  61. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  62. (LPTSTR) g_LastError,
  63. MAX_PATH,
  64. NULL
  65. );
  66. if (len) {
  67. //
  68. // Drop the extra CR
  69. //
  70. g_LastError[len - 2] = 0;
  71. }else{
  72. wcscpy(g_LastError, L"Failed to retrive LastError() info!");
  73. }
  74. return g_LastError;
  75. }
  76. VOID
  77. SoftPCI_HandleImportDevices(
  78. VOID
  79. )
  80. {
  81. OPENFILENAME openFileName;
  82. WCHAR filePath[MAX_PATH];
  83. WCHAR initialDir[MAX_PATH];
  84. WCHAR importTitle[] = L"Import Devices";
  85. BOOL result;
  86. RtlZeroMemory(&openFileName, sizeof(OPENFILENAME));
  87. if ((GetCurrentDirectory(MAX_PATH, initialDir)) == 0){
  88. //
  89. // error out
  90. //
  91. return;
  92. }
  93. filePath[0] = 0;
  94. openFileName.lStructSize = sizeof(OPENFILENAME);
  95. openFileName.hwndOwner = g_SoftPCIMainWnd;
  96. openFileName.hInstance = g_Instance;
  97. openFileName.lpstrFile = filePath;
  98. openFileName.nMaxFile = MAX_PATH;
  99. openFileName.lpstrTitle = importTitle;
  100. openFileName.lpstrInitialDir = initialDir;
  101. openFileName.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST;
  102. result = GetOpenFileName(&openFileName);
  103. if (result) {
  104. if (SoftPCI_BuildDeviceInstallList(filePath)){
  105. SoftPCI_InstallScriptDevices();
  106. }else{
  107. SoftPCI_MessageBox(L"Error Parsing Script File!",
  108. L"%s\n",
  109. g_ScriptError
  110. );
  111. }
  112. }
  113. }
  114. BOOL
  115. SoftPCI_InitializeRegistry(
  116. VOID
  117. )
  118. {
  119. HKEY key, key2;
  120. LONG cr;
  121. DWORD result;
  122. cr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, CCS_SOFTPCI, 0, KEY_ALL_ACCESS, &key);
  123. if (cr != ERROR_SUCCESS) {
  124. //
  125. // If we failed to open our key then we need to try and create it
  126. //
  127. if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE, CCS, 0, KEY_ALL_ACCESS, &key)) == ERROR_SUCCESS){
  128. if ((RegCreateKeyEx(key,
  129. SOFTPCI_KEY,
  130. 0,
  131. NULL,
  132. REG_OPTION_NON_VOLATILE,
  133. KEY_ALL_ACCESS,
  134. NULL,
  135. &key2,
  136. &result)) == ERROR_SUCCESS) {
  137. #if 0
  138. if (result = REG_CREATED_NEW_KEY) {
  139. PRINTF("Key didnt exist so we created a new one!\n");
  140. }else if (result = REG_OPENED_EXISTING_KEY) {
  141. PRINTF("Key already exists so we just opened it!\n");
  142. }else{
  143. PRINTF("Key Created successfully!\n");
  144. }
  145. #endif
  146. //
  147. // Our key now exists
  148. //
  149. RegCloseKey(key2);
  150. }
  151. }else{
  152. //
  153. // Failed to open CCS! Very bad!
  154. //
  155. return FALSE;
  156. }
  157. }
  158. RegCloseKey(key);
  159. return TRUE;
  160. }
  161. VOID
  162. SoftPCI_MessageBox(
  163. IN PWCHAR MessageTitle,
  164. IN PWCHAR MessageBody,
  165. ...
  166. )
  167. {
  168. va_list ap;
  169. WCHAR buffer[MAX_PATH];
  170. va_start(ap, MessageBody);
  171. _vsnwprintf(buffer, (sizeof(buffer)/sizeof(buffer[0])), MessageBody, ap);
  172. MessageBox(NULL, buffer, MessageTitle, MB_OK);
  173. }
  174. BOOL
  175. SoftPCI_QueryWindowSettings(
  176. OUT PSOFTPCI_WNDVIEW ViewSettings
  177. )
  178. {
  179. BOOL result;
  180. HKEY key;
  181. LONG cr;
  182. ULONG size, regType;
  183. result = FALSE;
  184. cr = RegOpenKeyEx(HKEY_CURRENT_USER, USERMS_SOFTPCI, 0, KEY_ALL_ACCESS, &key);
  185. if (cr == ERROR_SUCCESS) {
  186. //
  187. // Grab our window settings from the registry
  188. //
  189. size = sizeof(SOFTPCI_WNDVIEW);
  190. cr = RegQueryValueEx(
  191. key,
  192. SOFTPCIVIEW,
  193. NULL,
  194. &regType,
  195. (LPBYTE)ViewSettings,
  196. &size
  197. );
  198. if ((cr == ERROR_SUCCESS) &&
  199. (regType == REG_BINARY) &&
  200. (size == sizeof(SOFTPCI_WNDVIEW))) {
  201. result = TRUE;
  202. }
  203. RegCloseKey(key);
  204. }
  205. return result;
  206. }
  207. BOOL
  208. SoftPCI_SaveDeviceToRegisty(
  209. IN PPCI_DN Pdn
  210. )
  211. {
  212. HKEY regKeyHandle;
  213. BOOL result = FALSE;
  214. LIST_ENTRY devicePathList;
  215. InitializeListHead(&devicePathList);
  216. if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE, CCS_SOFTPCI, 0, KEY_ALL_ACCESS, &regKeyHandle)) == ERROR_SUCCESS){
  217. if (SoftPCI_GetDevicePathList(Pdn, &devicePathList)) {
  218. result = SoftPCI_WriteDeviceListToRegistry(&regKeyHandle, &devicePathList, devicePathList.Flink);
  219. }
  220. RegCloseKey(regKeyHandle);
  221. }
  222. return result;
  223. }
  224. VOID
  225. SoftPCI_SaveWindowSettings(
  226. VOID
  227. )
  228. {
  229. HKEY key, key2;
  230. LONG cr;
  231. ULONG status;
  232. SOFTPCI_WNDVIEW wndView;
  233. RtlZeroMemory(&wndView, sizeof(SOFTPCI_WNDVIEW));
  234. if (!GetWindowPlacement(g_SoftPCIMainWnd, &wndView.WindowPlacement)) {
  235. return;
  236. }
  237. wndView.PaneSplit = g_PaneSplit;
  238. cr = RegOpenKeyEx(
  239. HKEY_CURRENT_USER,
  240. USERMS_SOFTPCI,
  241. 0,
  242. KEY_ALL_ACCESS,
  243. &key
  244. );
  245. if (cr == ERROR_SUCCESS) {
  246. cr = RegSetValueEx(
  247. key,
  248. SOFTPCIVIEW,
  249. 0,
  250. REG_BINARY,
  251. (PBYTE)&wndView,
  252. sizeof(SOFTPCI_WNDVIEW)
  253. );
  254. RegCloseKey(key);
  255. }else{
  256. cr = RegOpenKeyEx(
  257. HKEY_CURRENT_USER,
  258. USERMS,
  259. 0,
  260. KEY_ALL_ACCESS,
  261. &key
  262. );
  263. if (cr == ERROR_SUCCESS){
  264. cr = RegCreateKeyEx(
  265. key,
  266. SOFTPCI_KEY,
  267. 0,
  268. NULL,
  269. REG_OPTION_NON_VOLATILE,
  270. KEY_ALL_ACCESS,
  271. NULL,
  272. &key2,
  273. &status
  274. );
  275. if (cr == ERROR_SUCCESS) {
  276. RegSetValueEx(
  277. key2,
  278. SOFTPCIVIEW,
  279. 0,
  280. REG_BINARY,
  281. (PBYTE)&wndView,
  282. sizeof(SOFTPCI_WNDVIEW)
  283. );
  284. }
  285. RegCloseKey(key2);
  286. }
  287. RegCloseKey(key);
  288. }
  289. }
  290. BOOL
  291. SoftPCI_WriteDeviceListToRegistry(
  292. IN PHKEY RegKeyHandle,
  293. IN PLIST_ENTRY ListHead,
  294. IN PLIST_ENTRY CurrentEntry
  295. )
  296. {
  297. BOOL result = FALSE;
  298. LONG cr;
  299. ULONG createStatus;
  300. HKEY newKey;
  301. PPCI_DN pdn;
  302. WCHAR buffer[sizeof(L"XXXX")];
  303. PSOFTPCI_CONFIG softConfig;
  304. if (ListHead == CurrentEntry) {
  305. //
  306. // We are done.
  307. //
  308. return TRUE;
  309. }
  310. pdn = CONTAINING_RECORD(CurrentEntry, PCI_DN, ListEntry);
  311. softConfig = &pdn->SoftDev->Config;
  312. wsprintf(buffer, L"%04x", pdn->Slot.AsUSHORT);
  313. SoftPCI_Debug(SoftPciAlways, L"Saving Slot %04x to registry\n", pdn->Slot.AsUSHORT);
  314. if ((RegCreateKeyEx(*RegKeyHandle,
  315. buffer,
  316. 0,
  317. NULL,
  318. REG_OPTION_NON_VOLATILE,
  319. KEY_ALL_ACCESS,
  320. NULL,
  321. &newKey,
  322. &createStatus)) == ERROR_SUCCESS) {
  323. if (createStatus = REG_CREATED_NEW_KEY) {
  324. SoftPCI_Debug(SoftPciAlways, L"Key didnt exist so we created a new one!\n");
  325. }else if (createStatus = REG_OPENED_EXISTING_KEY) {
  326. SoftPCI_Debug(SoftPciAlways, L"Key already exists so we just opened it!\n");
  327. }else{
  328. SoftPCI_Debug(SoftPciAlways, L"Key Created successfully!\n");
  329. }
  330. cr = RegSetValueEx(newKey, L"Config", 0, REG_BINARY, (PBYTE)softConfig, sizeof(SOFTPCI_CONFIG));
  331. if (cr != ERROR_SUCCESS) {
  332. MessageBox(NULL, L"Failed to update Registry!", L"FAILED", MB_OK);
  333. return FALSE;
  334. }
  335. //
  336. // Write out anything remaining in the list
  337. //
  338. result = SoftPCI_WriteDeviceListToRegistry(&newKey, ListHead, CurrentEntry->Flink);
  339. RegCloseKey(newKey);
  340. return result;
  341. }
  342. return result;
  343. }