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.

236 lines
6.4 KiB

  1. //========================================================================
  2. //
  3. // GetRegistryInfo.CPP
  4. //
  5. // DirectDraw/Direct3D driver information grabber
  6. // (c) Copyright 1998 Microsoft Corp.
  7. // Written by Michael Lyons ([email protected])
  8. //
  9. // Registry access functions, for querying device stuff from the
  10. // registry
  11. //
  12. // Function names that begin with an underscore are internal only!
  13. //
  14. //========================================================================
  15. #include "ddrawpr.h"
  16. //#include "GetDriverInfo.h"
  17. //#include "GetDriverInfoInt.h"
  18. //========================================================================
  19. // local functions
  20. //========================================================================
  21. BOOL _FindDevice(int iDevice, LPCSTR szDeviceClass, LPCSTR szDeviceClassNot, LPSTR szHardwareKey, BOOL bIgnoreProblems);
  22. static BOOL _GetDeviceValue(LPCSTR szHardwareKey, LPCSTR szKey, LPCSTR szValue, BYTE* buf, DWORD cbbuf);
  23. extern char *_strstr(char *s1, char *s2);
  24. //========================================================================
  25. //
  26. // GetDeviceValue
  27. //
  28. // read a value from the HW or SW of a PnP device
  29. //
  30. // in:
  31. // szHardwareKey the hardware key
  32. // szKey the sub-key
  33. // szValue the value to query
  34. // cbbuf the size of the output buffer
  35. //
  36. // out:
  37. // buf the destination buffer
  38. //
  39. // returns:
  40. // success status
  41. //
  42. //========================================================================
  43. static BOOL _GetDeviceValue(LPCSTR szHardwareKey, LPCSTR szKey, LPCSTR szValue, BYTE* buf, DWORD cbbuf)
  44. {
  45. HKEY hkeyHW;
  46. HKEY hkeySW;
  47. BOOL f = FALSE;
  48. DWORD cb;
  49. char szSoftwareKey[MAX_DDDEVICEID_STRING];
  50. //
  51. // open the HW key
  52. //
  53. if (RegOpenKey(HKEY_LOCAL_MACHINE, szHardwareKey, &hkeyHW) == ERROR_SUCCESS)
  54. {
  55. //
  56. // try to read the value from the HW key
  57. //
  58. *buf = 0;
  59. cb = cbbuf;
  60. if (RegQueryValueEx(hkeyHW, szValue, NULL, NULL, buf, &cb) == ERROR_SUCCESS)
  61. {
  62. f = TRUE;
  63. }
  64. else
  65. {
  66. //
  67. // now try the SW key
  68. //
  69. static char szSW[] = "System\\CurrentControlSet\\Services\\Class\\";
  70. lstrcpy(szSoftwareKey, szSW);
  71. cb = sizeof(szSoftwareKey) - sizeof(szSW);
  72. RegQueryValueEx(hkeyHW, "Driver", NULL, NULL, (BYTE *)&szSoftwareKey[sizeof(szSW) - 1], &cb);
  73. if (szKey)
  74. {
  75. lstrcat(szSoftwareKey, "\\");
  76. lstrcat(szSoftwareKey, szKey);
  77. }
  78. if (RegOpenKey(HKEY_LOCAL_MACHINE, szSoftwareKey, &hkeySW) == ERROR_SUCCESS)
  79. {
  80. *buf = 0;
  81. cb = cbbuf;
  82. if (RegQueryValueEx(hkeySW, szValue, NULL, NULL, buf, &cb) == ERROR_SUCCESS)
  83. {
  84. f = TRUE;
  85. }
  86. RegCloseKey(hkeySW);
  87. }
  88. }
  89. RegCloseKey(hkeyHW);
  90. }
  91. return f;
  92. }
  93. //========================================================================
  94. //
  95. // FindDevice
  96. //
  97. // enum the started PnP devices looking for a device of a particular class
  98. //
  99. // iDevice what device to return (0= first device, 1=second et)
  100. // szDeviceClass what class device (ie "Display") NULL will match all
  101. // szDeviceID buffer to return the hardware ID (MAX_DDDEVICEID_STRING bytes)
  102. //
  103. // return TRUE if a device was found.
  104. //
  105. // example:
  106. //
  107. // for (int i=0; FindDevice(i, "Display", DeviceID); i++)
  108. // {
  109. // }
  110. //
  111. //========================================================================
  112. BOOL _FindDevice(int iDevice, LPCSTR szDeviceClass, LPCSTR szDeviceClassNot, LPSTR szHardwareKey, BOOL bIgnoreProblems)
  113. {
  114. HKEY hkeyPnP;
  115. HKEY hkey;
  116. DWORD n;
  117. DWORD cb;
  118. DWORD dw;
  119. char ach[MAX_DDDEVICEID_STRING];
  120. if (RegOpenKey(HKEY_DYN_DATA, "Config Manager\\Enum", &hkeyPnP) != ERROR_SUCCESS)
  121. return FALSE;
  122. for (n=0; RegEnumKey(hkeyPnP, n, ach, sizeof(ach)) == 0; n++)
  123. {
  124. static char szHW[] = "Enum\\";
  125. if (RegOpenKey(hkeyPnP, ach, &hkey) != ERROR_SUCCESS)
  126. continue;
  127. lstrcpy(szHardwareKey, szHW);
  128. cb = MAX_DDDEVICEID_STRING - sizeof(szHW);
  129. RegQueryValueEx(hkey, "HardwareKey", NULL, NULL, (BYTE*)szHardwareKey + sizeof(szHW) - 1, &cb);
  130. dw = 0;
  131. cb = sizeof(dw);
  132. RegQueryValueEx(hkey, "Problem", NULL, NULL, (BYTE*)&dw, &cb);
  133. RegCloseKey(hkey);
  134. if ((!bIgnoreProblems) && (dw != 0)) // if this device has a problem skip it
  135. continue;
  136. if (szDeviceClass || szDeviceClassNot)
  137. {
  138. _GetDeviceValue(szHardwareKey, NULL, "Class", (BYTE *)ach, sizeof(ach));
  139. if (szDeviceClass && lstrcmpi(szDeviceClass, ach) != 0)
  140. continue;
  141. if (szDeviceClassNot && lstrcmpi(szDeviceClassNot, ach) == 0)
  142. continue;
  143. }
  144. //
  145. // we found a device, make sure it is the one the caller wants
  146. //
  147. if (iDevice-- == 0)
  148. {
  149. RegCloseKey(hkeyPnP);
  150. return TRUE;
  151. }
  152. }
  153. RegCloseKey(hkeyPnP);
  154. return FALSE;
  155. }
  156. //========================================================================
  157. //
  158. // _GetDriverInfoFromRegistry
  159. //
  160. // This function goes through the registry and tries to fill in
  161. // information about a driver given a class and maybe a vendor ID
  162. //
  163. // in:
  164. // szClass the class name (i.e., "Display")
  165. // szVendor the vendor name (i.e., "VEN_121A" for 3Dfx" or NULL
  166. // if any vendor of the class will do
  167. //
  168. // out:
  169. // pDI pointer to a DDDRIVERINFOEX structure to be filled in
  170. //
  171. // returns:
  172. // success status
  173. //
  174. //========================================================================
  175. HRESULT _GetDriverInfoFromRegistry(char *szClass,
  176. char *szClassNot,
  177. char *szVendor,
  178. D3DADAPTER_IDENTIFIER8* pDI,
  179. char *szDeviceID)
  180. {
  181. char szDevice[MAX_DDDEVICEID_STRING];
  182. int i;
  183. szDeviceID[0]=0;
  184. pDI->Description[0]=0;
  185. for (i=0 ; ; i++)
  186. {
  187. if (!_FindDevice(i, szClass, szClassNot, szDevice, FALSE))
  188. break;
  189. if ((szVendor == NULL) || (_strstr(szDevice, szVendor)))
  190. {
  191. //
  192. // skip the first 5 characters "Enum\"
  193. //
  194. strcpy(szDeviceID, &szDevice[5]);
  195. _GetDeviceValue((LPCSTR)szDevice, NULL, "DeviceDesc", (BYTE *)pDI->Description, sizeof(pDI->Description));
  196. //_GetDeviceValue((LPCSTR)szDevice, NULL, "Mfg", (BYTE *)pDI->szManufacturer, sizeof(pDI->szManufacturer));
  197. //_GetDeviceValue((LPCSTR)szDevice, "DEFAULT","drv", (BYTE *)pDI->szGDIDriver, sizeof(pDI->szGDIDriver));
  198. return S_OK;
  199. }
  200. }
  201. return -1;
  202. }