2
0

WinDevice.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. #include "types.h"
  2. #if defined(DSI_TYPES_WINDOWS)
  3. #include "WinDevice.h"
  4. #include "macros.h"
  5. #include <tchar.h>
  6. #include <ctype.h>
  7. #include <setupapi.h>
  8. #include <cfgmgr32.h>
  9. #include <stdlib.h>
  10. // Prototypes for functions found in the SetupAPI dll.
  11. typedef BOOL (WINAPI* SetupAPI_CallClassInstaller_t)(DI_FUNCTION,HDEVINFO,PSP_DEVINFO_DATA);
  12. typedef BOOL (WINAPI* SetupAPI_EnumDeviceInfo_t)(HDEVINFO,DWORD,PSP_DEVINFO_DATA);
  13. typedef BOOL (WINAPI* SetupAPI_DestroyDeviceInfoList_t)(HDEVINFO);
  14. typedef BOOL (WINAPI* SetupAPI_GetDeviceRegistryProperty_t)(HDEVINFO,PSP_DEVINFO_DATA,DWORD,PDWORD,PBYTE,DWORD,PDWORD);
  15. typedef BOOL (WINAPI* SetupAPI_SetClassInstallParams_t)(HDEVINFO,PSP_DEVINFO_DATA,PSP_CLASSINSTALL_HEADER,DWORD);
  16. #ifdef UNICODE
  17. typedef BOOL (WINAPI* SetupAPI_GetDeviceInstallParams_t)(HDEVINFO,PSP_DEVINFO_DATA,PSP_DEVINSTALL_PARAMS_W);
  18. typedef BOOL (WINAPI* SetupAPI_ClassGuidsFromNameEx_t)(PCWSTR,LPGUID,DWORD,PDWORD,PCWSTR,PVOID);
  19. typedef HDEVINFO (WINAPI* SetupAPI_GetClassDevsEx_t)(CONST GUID*,PCWSTR,HWND,DWORD,HDEVINFO,PCWSTR,PVOID);
  20. typedef HDEVINFO (WINAPI* SetupAPI_CreateDeviceInfoListEx_t)(CONST GUID*,HWND,PCWSTR,PVOID);
  21. typedef BOOL (WINAPI* SetupAPI_OpenDeviceInfo_t)(HDEVINFO,PCWSTR,HWND,DWORD,PSP_DEVINFO_DATA);
  22. typedef BOOL (WINAPI* SetupAPI_GetDeviceInfoListDetail_t)(HDEVINFO,PSP_DEVINFO_LIST_DETAIL_DATA_W);
  23. #else
  24. typedef BOOL (WINAPI* SetupAPI_GetDeviceInstallParams_t)(HDEVINFO,PSP_DEVINFO_DATA,PSP_DEVINSTALL_PARAMS_A);
  25. typedef BOOL (WINAPI* SetupAPI_ClassGuidsFromNameEx_t)(PCSTR,LPGUID,DWORD,PDWORD,PCSTR,PVOID);
  26. typedef HDEVINFO (WINAPI* SetupAPI_GetClassDevsEx_t)(CONST GUID*,PCSTR,HWND,DWORD,HDEVINFO,PCSTR,PVOID);
  27. typedef HDEVINFO (WINAPI* SetupAPI_CreateDeviceInfoListEx_t)(CONST GUID*,HWND,PCSTR,PVOID);
  28. typedef BOOL (WINAPI* SetupAPI_OpenDeviceInfo_t)(HDEVINFO,PCSTR,HWND,DWORD,PSP_DEVINFO_DATA);
  29. typedef BOOL (WINAPI* SetupAPI_GetDeviceInfoListDetail_t)(HDEVINFO,PSP_DEVINFO_LIST_DETAIL_DATA_A);
  30. #endif
  31. // Function pointers for functions found in the SetupAPI dll.
  32. SetupAPI_SetClassInstallParams_t SetupAPI_SetClassInstallParams;
  33. SetupAPI_CallClassInstaller_t SetupAPI_CallClassInstaller;
  34. SetupAPI_GetDeviceInstallParams_t SetupAPI_GetDeviceInstallParams;
  35. SetupAPI_GetDeviceRegistryProperty_t SetupAPI_GetDeviceRegistryProperty;
  36. SetupAPI_ClassGuidsFromNameEx_t SetupAPI_ClassGuidsFromNameEx;
  37. SetupAPI_GetClassDevsEx_t SetupAPI_GetClassDevsEx;
  38. SetupAPI_CreateDeviceInfoListEx_t SetupAPI_CreateDeviceInfoListEx;
  39. SetupAPI_OpenDeviceInfo_t SetupAPI_OpenDeviceInfo;
  40. SetupAPI_GetDeviceInfoListDetail_t SetupAPI_GetDeviceInfoListDetail;
  41. SetupAPI_EnumDeviceInfo_t SetupAPI_EnumDeviceInfo;
  42. SetupAPI_DestroyDeviceInfoList_t SetupAPI_DestroyDeviceInfoList;
  43. // Prototypes for functions found in the cfgmgr32 dll.
  44. #ifdef UNICODE
  45. typedef CONFIGRET (WINAPI* CfgMgr32_Get_Device_ID_Ex_t)(DEVINST dnDevInst, PWCHAR Buffer, ULONG BufferLen, ULONG ulFlags, HMACHINE hMachine);
  46. #else
  47. typedef CONFIGRET (WINAPI* CfgMgr32_Get_Device_ID_Ex_t)(DEVINST dnDevInst, PCHAR Buffer, ULONG BufferLen, ULONG ulFlags, HMACHINE hMachine);
  48. #endif
  49. // Function pointers for functions found in the cfgmgr32 dll.
  50. CfgMgr32_Get_Device_ID_Ex_t CfgMgr32_Get_Device_ID_Ex;
  51. typedef int (*CallbackFunc)(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,LPVOID Context);
  52. int ControlCallback(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,LPVOID Context);
  53. //DLL Definitions
  54. BOOL LoadSetupDLLFunctions();
  55. void FreeSetupDLLFunctions();
  56. HMODULE hSetupDll;
  57. HMODULE hCfgMgrDll;
  58. int EnumerateDevices(LPCTSTR Machine,DWORD Flags,int argc,LPTSTR argv[],CallbackFunc Callback,LPVOID Context);
  59. LPTSTR GetDeviceStringProperty(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,DWORD Prop);
  60. LPTSTR GetDeviceDescription(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo);
  61. LPTSTR * GetDevMultiSz(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,DWORD Prop);
  62. LPTSTR * GetRegMultiSz(HKEY hKey,LPCTSTR Val);
  63. LPTSTR * GetMultiSzIndexArray(LPTSTR MultiSz);
  64. void DelMultiSz(LPTSTR * Array);
  65. LPTSTR * CopyMultiSz(LPTSTR * Array);
  66. typedef struct
  67. {
  68. LPCTSTR String; // string looking for
  69. LPCTSTR Wild; // first wild character if any
  70. BOOL InstanceId;
  71. } IdEntry;
  72. typedef struct
  73. {
  74. DWORD count;
  75. DWORD control;
  76. BOOL reboot;
  77. } GenericContext;
  78. typedef struct
  79. {
  80. int argc_right;
  81. LPTSTR * argv_right;
  82. DWORD prop;
  83. int skipped;
  84. int modified;
  85. } SetHwidContext;
  86. int WinDevice_Enable(int argc,TCHAR* argv[])
  87. /*++
  88. Routine Description:
  89. ENABLE <id> ...
  90. use EnumerateDevices to do hardwareID matching
  91. for each match, attempt to enable global, and if needed, config specific
  92. Arguments:
  93. argc/argv - remaining parameters - passed into EnumerateDevices
  94. Return Value:
  95. EXIT_xxxx (EXIT_REBOOT if reboot is required)
  96. --*/
  97. {
  98. GenericContext context;
  99. int failcode = EXIT_FAIL;
  100. if(!LoadSetupDLLFunctions())
  101. return EXIT_FAIL;
  102. if(!argc)
  103. {
  104. //
  105. // arguments required
  106. //
  107. FreeSetupDLLFunctions();
  108. return EXIT_USAGE;
  109. }
  110. context.control = DICS_ENABLE; // DICS_PROPCHANGE DICS_ENABLE DICS_DISABLE
  111. context.reboot = FALSE;
  112. context.count = 0;
  113. failcode = EnumerateDevices(NULL,DIGCF_PRESENT,argc,argv,ControlCallback,&context);
  114. if(failcode == EXIT_OK)
  115. {
  116. if((context.count) && (context.reboot))
  117. failcode = EXIT_REBOOT;
  118. }
  119. FreeSetupDLLFunctions();
  120. return failcode;
  121. }
  122. int WinDevice_Disable(int argc,TCHAR* argv[])
  123. /*++
  124. Routine Description:
  125. DISABLE <id> ...
  126. use EnumerateDevices to do hardwareID matching
  127. for each match, attempt to disable global
  128. Arguments:
  129. argc/argv - remaining parameters - passed into EnumerateDevices
  130. Return Value:
  131. EXIT_xxxx (EXIT_REBOOT if reboot is required)
  132. --*/
  133. {
  134. GenericContext context;
  135. int failcode = EXIT_FAIL;
  136. if(!LoadSetupDLLFunctions())
  137. return EXIT_FAIL;
  138. if(!argc)
  139. {
  140. //
  141. // arguments required
  142. //
  143. FreeSetupDLLFunctions();
  144. return EXIT_USAGE;
  145. }
  146. context.control = DICS_DISABLE; // DICS_PROPCHANGE DICS_ENABLE DICS_DISABLE
  147. context.reboot = FALSE;
  148. context.count = 0;
  149. failcode = EnumerateDevices(NULL,DIGCF_PRESENT,argc,argv,ControlCallback,&context);
  150. if(failcode == EXIT_OK)
  151. {
  152. if((context.count) && (context.reboot))
  153. failcode = EXIT_REBOOT;
  154. }
  155. FreeSetupDLLFunctions();
  156. return failcode;
  157. }
  158. int WinDevice_Restart(int argc,TCHAR* argv[])
  159. /*++
  160. Routine Description:
  161. RESTART <id> ...
  162. use EnumerateDevices to do hardwareID matching
  163. for each match, attempt to restart by issueing a PROPCHANGE
  164. Arguments:
  165. argc/argv - remaining parameters - passed into EnumerateDevices
  166. Return Value:
  167. EXIT_xxxx (EXIT_REBOOT if reboot is required)
  168. --*/
  169. {
  170. GenericContext context;
  171. int failcode = EXIT_FAIL;
  172. if(!LoadSetupDLLFunctions())
  173. return EXIT_FAIL;
  174. if(!argc)
  175. {
  176. //
  177. // arguments required
  178. //
  179. FreeSetupDLLFunctions();
  180. return EXIT_USAGE;
  181. }
  182. context.control = DICS_PROPCHANGE;
  183. context.reboot = FALSE;
  184. context.count = 0;
  185. failcode = EnumerateDevices(NULL,DIGCF_PRESENT,argc,argv,ControlCallback,&context);
  186. if(failcode == EXIT_OK)
  187. {
  188. if((context.count) && (context.reboot))
  189. failcode = EXIT_REBOOT;
  190. }
  191. FreeSetupDLLFunctions();
  192. return failcode;
  193. }
  194. int ControlCallback(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,LPVOID Context)
  195. /*++
  196. Routine Description:
  197. Callback for use by Enable/Disable/Restart
  198. Invokes DIF_PROPERTYCHANGE with correct parameters
  199. uses SetupDiCallClassInstaller so cannot be done for remote devices
  200. Don't use CM_xxx API's, they bypass class/co-installers and this is bad.
  201. In Enable case, we try global first, and if still disabled, enable local
  202. Arguments:
  203. Devs )_ uniquely identify the device
  204. DevInfo )
  205. Context - GenericContext
  206. Return Value:
  207. EXIT_xxxx
  208. --*/
  209. {
  210. SP_PROPCHANGE_PARAMS pcp;
  211. GenericContext *pControlContext = (GenericContext*)Context;
  212. SP_DEVINSTALL_PARAMS devParams;
  213. switch(pControlContext->control)
  214. {
  215. case DICS_ENABLE:
  216. //
  217. // enable both on global and config-specific profile
  218. // do global first and see if that succeeded in enabling the device
  219. // (global enable doesn't mark reboot required if device is still
  220. // disabled on current config whereas vice-versa isn't true)
  221. //
  222. pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  223. pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  224. pcp.StateChange = pControlContext->control;
  225. pcp.Scope = DICS_FLAG_GLOBAL;
  226. pcp.HwProfile = 0;
  227. //
  228. // don't worry if this fails, we'll get an error when we try config-
  229. // specific.
  230. if(SetupAPI_SetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp)))
  231. {
  232. SetupAPI_CallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo);
  233. }
  234. //
  235. // now enable on config-specific
  236. //
  237. pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  238. pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  239. pcp.StateChange = pControlContext->control;
  240. pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
  241. pcp.HwProfile = 0;
  242. break;
  243. default:
  244. //
  245. // operate on config-specific profile
  246. //
  247. pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  248. pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  249. pcp.StateChange = pControlContext->control;
  250. pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
  251. pcp.HwProfile = 0;
  252. break;
  253. }
  254. if(!SetupAPI_SetClassInstallParams(Devs,DevInfo,&pcp.ClassInstallHeader,sizeof(pcp)) ||
  255. !SetupAPI_CallClassInstaller(DIF_PROPERTYCHANGE,Devs,DevInfo))
  256. {
  257. //
  258. // failed to invoke DIF_PROPERTYCHANGE
  259. //
  260. }
  261. else
  262. {
  263. //
  264. // see if device needs reboot
  265. //
  266. devParams.cbSize = sizeof(devParams);
  267. if(SetupAPI_GetDeviceInstallParams(Devs,DevInfo,&devParams) && (devParams.Flags & (DI_NEEDRESTART|DI_NEEDREBOOT)))
  268. {
  269. pControlContext->reboot = TRUE;
  270. }
  271. else
  272. {
  273. //
  274. // appears to have succeeded
  275. //
  276. }
  277. pControlContext->count++;
  278. }
  279. return EXIT_OK;
  280. }
  281. ///////////////////////////////////////////////////////////////////////
  282. // Loads Setup API functions from the DLL.
  283. ///////////////////////////////////////////////////////////////////////
  284. BOOL LoadSetupDLLFunctions(void)
  285. {
  286. BOOL bStatus = TRUE;
  287. FreeSetupDLLFunctions(); // Guarantee the library functions are freed.
  288. char acSystemDirectory[MAX_PATH];
  289. char asSetupAPI[255];
  290. char asConfig[255];
  291. if(GetSystemDirectory(acSystemDirectory, MAX_PATH) == 0)
  292. return FALSE;
  293. if(acSystemDirectory == NULL)
  294. return FALSE;
  295. SNPRINTF(asSetupAPI, 255, "%s/setupapi.dll", acSystemDirectory);
  296. SNPRINTF(asConfig, 255, "%s/cfgmgr32.dll", acSystemDirectory);
  297. hSetupDll = LoadLibrary(asSetupAPI);
  298. hCfgMgrDll = LoadLibrary(asConfig);
  299. if(hSetupDll == NULL || hCfgMgrDll == NULL)
  300. {
  301. bStatus = FALSE;
  302. }
  303. else
  304. {
  305. //Functions from SetupAPI DLL.
  306. SetupAPI_CallClassInstaller = (SetupAPI_CallClassInstaller_t) GetProcAddress(hSetupDll, "SetupDiCallClassInstaller");
  307. if(SetupAPI_CallClassInstaller == NULL)
  308. bStatus = FALSE;
  309. SetupAPI_EnumDeviceInfo = (SetupAPI_EnumDeviceInfo_t) GetProcAddress(hSetupDll, "SetupDiEnumDeviceInfo");
  310. if(SetupAPI_EnumDeviceInfo == NULL)
  311. bStatus = FALSE;
  312. SetupAPI_DestroyDeviceInfoList = (SetupAPI_DestroyDeviceInfoList_t) GetProcAddress(hSetupDll, "SetupDiDestroyDeviceInfoList");
  313. if(SetupAPI_DestroyDeviceInfoList == NULL)
  314. bStatus = FALSE;
  315. #ifdef UNICODE
  316. SetupAPI_SetClassInstallParams = (SetupAPI_SetClassInstallParams_t) GetProcAddress(hSetupDll, "SetupDiSetClassInstallParamsW");
  317. if(SetupAPI_SetClassInstallParams == NULL)
  318. bStatus = FALSE;
  319. SetupAPI_GetDeviceInstallParams = (SetupAPI_GetDeviceInstallParams_t) GetProcAddress(hSetupDll, "SetupDiGetDeviceInstallParamsW");
  320. if(SetupAPI_GetDeviceInstallParams == NULL)
  321. bStatus = FALSE;
  322. SetupAPI_GetDeviceRegistryProperty = (SetupAPI_GetDeviceRegistryProperty_t) GetProcAddress(hSetupDll, "SetupDiGetDeviceRegistryPropertyW");
  323. if(SetupAPI_GetDeviceRegistryProperty == NULL)
  324. bStatus = FALSE;
  325. SetupAPI_ClassGuidsFromNameEx = (SetupAPI_ClassGuidsFromNameEx_t) GetProcAddress(hSetupDll, "SetupDiClassGuidsFromNameExW");
  326. if(SetupAPI_ClassGuidsFromNameEx == NULL)
  327. bStatus = FALSE;
  328. SetupAPI_GetClassDevsEx = (SetupAPI_GetClassDevsEx_t) GetProcAddress(hSetupDll, "SetupDiGetClassDevsExW");
  329. if(SetupAPI_GetClassDevsEx == NULL)
  330. bStatus = FALSE;
  331. SetupAPI_CreateDeviceInfoListEx = (SetupAPI_CreateDeviceInfoListEx_t) GetProcAddress(hSetupDll, "SetupDiCreateDeviceInfoListExW");
  332. if(SetupAPI_CreateDeviceInfoListEx == NULL)
  333. bStatus = FALSE;
  334. SetupAPI_OpenDeviceInfo = (SetupAPI_OpenDeviceInfo_t) GetProcAddress(hSetupDll, "SetupDiOpenDeviceInfoW");
  335. if(SetupAPI_OpenDeviceInfo == NULL)
  336. bStatus = FALSE;
  337. SetupAPI_GetDeviceInfoListDetail = (SetupAPI_GetDeviceInfoListDetail_t) GetProcAddress(hSetupDll, "SetupDiGetDeviceInfoListDetailW");
  338. if(SetupAPI_GetDeviceInfoListDetail == NULL)
  339. bStatus = FALSE;
  340. #else
  341. SetupAPI_SetClassInstallParams = (SetupAPI_SetClassInstallParams_t) GetProcAddress(hSetupDll, "SetupDiSetClassInstallParamsA");
  342. if(SetupAPI_SetClassInstallParams == NULL)
  343. bStatus = FALSE;
  344. SetupAPI_GetDeviceInstallParams = (SetupAPI_GetDeviceInstallParams_t) GetProcAddress(hSetupDll, "SetupDiGetDeviceInstallParamsA");
  345. if(SetupAPI_GetDeviceInstallParams == NULL)
  346. bStatus = FALSE;
  347. SetupAPI_GetDeviceRegistryProperty = (SetupAPI_GetDeviceRegistryProperty_t) GetProcAddress(hSetupDll, "SetupDiGetDeviceRegistryPropertyA");
  348. if(SetupAPI_GetDeviceRegistryProperty == NULL)
  349. bStatus = FALSE;
  350. SetupAPI_ClassGuidsFromNameEx = (SetupAPI_ClassGuidsFromNameEx_t) GetProcAddress(hSetupDll, "SetupDiClassGuidsFromNameExA");
  351. if(SetupAPI_ClassGuidsFromNameEx == NULL)
  352. bStatus = FALSE;
  353. SetupAPI_GetClassDevsEx = (SetupAPI_GetClassDevsEx_t) GetProcAddress(hSetupDll, "SetupDiGetClassDevsExA");
  354. if(SetupAPI_GetClassDevsEx == NULL)
  355. bStatus = FALSE;
  356. SetupAPI_CreateDeviceInfoListEx = (SetupAPI_CreateDeviceInfoListEx_t) GetProcAddress(hSetupDll, "SetupDiCreateDeviceInfoListExA");
  357. if(SetupAPI_CreateDeviceInfoListEx == NULL)
  358. bStatus = FALSE;
  359. SetupAPI_OpenDeviceInfo = (SetupAPI_OpenDeviceInfo_t) GetProcAddress(hSetupDll, "SetupDiOpenDeviceInfoA");
  360. if(SetupAPI_OpenDeviceInfo == NULL)
  361. bStatus = FALSE;
  362. SetupAPI_GetDeviceInfoListDetail = (SetupAPI_GetDeviceInfoListDetail_t) GetProcAddress(hSetupDll, "SetupDiGetDeviceInfoListDetailA");
  363. if(SetupAPI_GetDeviceInfoListDetail == NULL)
  364. bStatus = FALSE;
  365. #endif
  366. //Functions from CfgMgr32 DLL.
  367. #ifdef UNICODE
  368. SetupAPI_Get_Device_ID_Ex = (SetupAPI_Get_Device_ID_Ex_t) GetProcAddress(hSetupDll, "CM_Get_Device_ID_ExW");
  369. if(SetupAPI_Get_Device_ID_Ex == NULL)
  370. bStatus = FALSE;
  371. #else
  372. CfgMgr32_Get_Device_ID_Ex = (CfgMgr32_Get_Device_ID_Ex_t) GetProcAddress(hCfgMgrDll, "CM_Get_Device_ID_ExA");
  373. if(CfgMgr32_Get_Device_ID_Ex == NULL)
  374. bStatus = FALSE;
  375. #endif
  376. }
  377. if (!bStatus)
  378. FreeSetupDLLFunctions();
  379. return bStatus;
  380. }
  381. ///////////////////////////////////////////////////////////////////////
  382. // Unloads Setup API DLL.
  383. ///////////////////////////////////////////////////////////////////////
  384. void FreeSetupDLLFunctions(void)
  385. {
  386. if (hSetupDll != NULL)
  387. {
  388. FreeLibrary(hSetupDll);
  389. hSetupDll = NULL;
  390. }
  391. if(hCfgMgrDll != NULL)
  392. {
  393. FreeLibrary(hCfgMgrDll);
  394. hCfgMgrDll = NULL;
  395. }
  396. }
  397. LPTSTR GetDeviceStringProperty(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,DWORD Prop)
  398. /*++
  399. Routine Description:
  400. Return a string property for a device, otherwise NULL
  401. Arguments:
  402. Devs )_ uniquely identify device
  403. DevInfo )
  404. Prop - string property to obtain
  405. Return Value:
  406. string containing description
  407. --*/
  408. {
  409. LPTSTR buffer;
  410. DWORD size;
  411. DWORD reqSize;
  412. DWORD dataType;
  413. DWORD szChars;
  414. size = 1024; // initial guess
  415. buffer = new TCHAR[(size/sizeof(TCHAR))+1];
  416. if(!buffer)
  417. {
  418. return NULL;
  419. }
  420. while(!SetupAPI_GetDeviceRegistryProperty(Devs,DevInfo,Prop,&dataType,(LPBYTE)buffer,size,&reqSize))
  421. {
  422. if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  423. {
  424. goto failed;
  425. }
  426. if(dataType != REG_SZ)
  427. {
  428. goto failed;
  429. }
  430. size = reqSize;
  431. delete [] buffer;
  432. buffer = new TCHAR[(size/sizeof(TCHAR))+1];
  433. if(!buffer)
  434. {
  435. goto failed;
  436. }
  437. }
  438. szChars = reqSize/sizeof(TCHAR);
  439. buffer[szChars] = TEXT('\0');
  440. return buffer;
  441. failed:
  442. if(buffer)
  443. {
  444. delete [] buffer;
  445. }
  446. return NULL;
  447. }
  448. LPTSTR GetDeviceDescription(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo)
  449. /*++
  450. Routine Description:
  451. Return a string containing a description of the device, otherwise NULL
  452. Always try friendly name first
  453. Arguments:
  454. Devs )_ uniquely identify device
  455. DevInfo )
  456. Return Value:
  457. string containing description
  458. --*/
  459. {
  460. LPTSTR desc;
  461. desc = GetDeviceStringProperty(Devs,DevInfo,SPDRP_FRIENDLYNAME);
  462. if(!desc)
  463. {
  464. desc = GetDeviceStringProperty(Devs,DevInfo,SPDRP_DEVICEDESC);
  465. }
  466. return desc;
  467. }
  468. IdEntry GetIdType(LPCTSTR Id)
  469. /*++
  470. Routine Description:
  471. Determine if this is instance id or hardware id and if there's any wildcards
  472. instance ID is prefixed by '@'
  473. wildcards are '*'
  474. Arguments:
  475. Id - ptr to string to check
  476. Return Value:
  477. IdEntry
  478. --*/
  479. {
  480. IdEntry Entry;
  481. Entry.InstanceId = FALSE;
  482. Entry.Wild = NULL;
  483. Entry.String = Id;
  484. if(Entry.String[0] == INSTANCEID_PREFIX_CHAR)
  485. {
  486. Entry.InstanceId = TRUE;
  487. Entry.String = CharNext(Entry.String);
  488. }
  489. if(Entry.String[0] == QUOTE_PREFIX_CHAR)
  490. {
  491. //
  492. // prefix to treat rest of string literally
  493. //
  494. Entry.String = CharNext(Entry.String);
  495. }
  496. else
  497. {
  498. //
  499. // see if any wild characters exist
  500. //
  501. Entry.Wild = _tcschr(Entry.String,WILD_CHAR);
  502. }
  503. return Entry;
  504. }
  505. LPTSTR * GetMultiSzIndexArray(LPTSTR MultiSz)
  506. /*++
  507. Routine Description:
  508. Get an index array pointing to the MultiSz passed in
  509. Arguments:
  510. MultiSz - well formed multi-sz string
  511. Return Value:
  512. array of strings. last entry+1 of array contains NULL
  513. returns NULL on failure
  514. --*/
  515. {
  516. LPTSTR scan;
  517. LPTSTR * array;
  518. int elements;
  519. for(scan = MultiSz, elements = 0; scan[0] ;elements++)
  520. {
  521. scan += lstrlen(scan)+1;
  522. }
  523. array = new LPTSTR[elements+2];
  524. if(!array) {
  525. return NULL;
  526. }
  527. array[0] = MultiSz;
  528. array++;
  529. if(elements)
  530. {
  531. for(scan = MultiSz, elements = 0; scan[0]; elements++)
  532. {
  533. array[elements] = scan;
  534. scan += lstrlen(scan)+1;
  535. }
  536. }
  537. array[elements] = NULL;
  538. return array;
  539. }
  540. LPTSTR * CopyMultiSz(LPTSTR * Array)
  541. /*++
  542. Routine Description:
  543. Creates a new array from old
  544. old array need not have been allocated by GetMultiSzIndexArray
  545. Arguments:
  546. Array - array of strings, last entry is NULL
  547. Return Value:
  548. MultiSz array allocated by GetMultiSzIndexArray
  549. --*/
  550. {
  551. LPTSTR multiSz = NULL;
  552. int len = 0;
  553. int c;
  554. if(Array)
  555. {
  556. for(c=0;Array[c];c++)
  557. {
  558. len+=lstrlen(Array[c])+1;
  559. }
  560. }
  561. len+=1; // final Null
  562. multiSz = new TCHAR[len];
  563. if(!multiSz)
  564. {
  565. return NULL;
  566. }
  567. len = 0;
  568. if(Array)
  569. {
  570. for(c=0;Array[c];c++)
  571. {
  572. lstrcpy(multiSz+len,Array[c]);
  573. len+=lstrlen(multiSz+len)+1;
  574. }
  575. }
  576. multiSz[len] = TEXT('\0');
  577. LPTSTR * pRes = GetMultiSzIndexArray(multiSz);
  578. if(pRes)
  579. {
  580. return pRes;
  581. }
  582. delete [] multiSz;
  583. return NULL;
  584. }
  585. void DelMultiSz(LPTSTR * Array)
  586. /*++
  587. Routine Description:
  588. Deletes the string array allocated by GetDevMultiSz/GetRegMultiSz/GetMultiSzIndexArray
  589. Arguments:
  590. Array - pointer returned by GetMultiSzIndexArray
  591. Return Value:
  592. None
  593. --*/
  594. {
  595. if(Array)
  596. {
  597. Array--;
  598. if(Array[0])
  599. {
  600. delete [] Array[0];
  601. }
  602. delete [] Array;
  603. }
  604. }
  605. LPTSTR * GetDevMultiSz(HDEVINFO Devs,PSP_DEVINFO_DATA DevInfo,DWORD Prop)
  606. /*++
  607. Routine Description:
  608. Get a multi-sz device property
  609. and return as an array of strings
  610. Arguments:
  611. Devs - HDEVINFO containing DevInfo
  612. DevInfo - Specific device
  613. Prop - SPDRP_HARDWAREID or SPDRP_COMPATIBLEIDS
  614. Return Value:
  615. array of strings. last entry+1 of array contains NULL
  616. returns NULL on failure
  617. --*/
  618. {
  619. LPTSTR buffer;
  620. DWORD size;
  621. DWORD reqSize;
  622. DWORD dataType;
  623. LPTSTR * array;
  624. DWORD szChars;
  625. size = 8192; // initial guess, nothing magic about this
  626. buffer = new TCHAR[(size/sizeof(TCHAR))+2];
  627. if(!buffer)
  628. {
  629. return NULL;
  630. }
  631. while(!SetupAPI_GetDeviceRegistryProperty(Devs,DevInfo,Prop,&dataType,(LPBYTE)buffer,size,&reqSize))
  632. {
  633. if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  634. {
  635. goto failed;
  636. }
  637. if(dataType != REG_MULTI_SZ)
  638. {
  639. goto failed;
  640. }
  641. size = reqSize;
  642. delete [] buffer;
  643. buffer = new TCHAR[(size/sizeof(TCHAR))+2];
  644. if(!buffer)
  645. {
  646. goto failed;
  647. }
  648. }
  649. szChars = reqSize/sizeof(TCHAR);
  650. buffer[szChars] = TEXT('\0');
  651. buffer[szChars+1] = TEXT('\0');
  652. array = GetMultiSzIndexArray(buffer);
  653. if(array)
  654. {
  655. return array;
  656. }
  657. failed:
  658. if(buffer)
  659. {
  660. delete [] buffer;
  661. }
  662. return NULL;
  663. }
  664. LPTSTR * GetRegMultiSz(HKEY hKey,LPCTSTR Val)
  665. /*++
  666. Routine Description:
  667. Get a multi-sz from registry
  668. and return as an array of strings
  669. Arguments:
  670. hKey - Registry Key
  671. Val - Value to query
  672. Return Value:
  673. array of strings. last entry+1 of array contains NULL
  674. returns NULL on failure
  675. --*/
  676. {
  677. LPTSTR buffer;
  678. DWORD size;
  679. DWORD reqSize;
  680. DWORD dataType;
  681. LPTSTR * array;
  682. DWORD szChars;
  683. size = 8192; // initial guess, nothing magic about this
  684. buffer = new TCHAR[(size/sizeof(TCHAR))+2];
  685. if(!buffer)
  686. {
  687. return NULL;
  688. }
  689. reqSize = size;
  690. while(RegQueryValueEx(hKey,Val,NULL,&dataType,(PBYTE)buffer,&reqSize) != NO_ERROR)
  691. {
  692. if(GetLastError() != ERROR_MORE_DATA)
  693. {
  694. goto failed;
  695. }
  696. if(dataType != REG_MULTI_SZ)
  697. {
  698. goto failed;
  699. }
  700. size = reqSize;
  701. delete [] buffer;
  702. buffer = new TCHAR[(size/sizeof(TCHAR))+2];
  703. if(!buffer)
  704. {
  705. goto failed;
  706. }
  707. }
  708. szChars = reqSize/sizeof(TCHAR);
  709. buffer[szChars] = TEXT('\0');
  710. buffer[szChars+1] = TEXT('\0');
  711. array = GetMultiSzIndexArray(buffer);
  712. if(array)
  713. {
  714. return array;
  715. }
  716. failed:
  717. if(buffer)
  718. {
  719. delete [] buffer;
  720. }
  721. return NULL;
  722. }
  723. BOOL WildCardMatch(LPCTSTR Item,const IdEntry & MatchEntry)
  724. /*++
  725. Routine Description:
  726. Compare a single item against wildcard
  727. I'm sure there's better ways of implementing this
  728. Other than a command-line management tools
  729. it's a bad idea to use wildcards as it implies
  730. assumptions about the hardware/instance ID
  731. eg, it might be tempting to enumerate root\* to
  732. find all root devices, however there is a CfgMgr
  733. API to query status and determine if a device is
  734. root enumerated, which doesn't rely on implementation
  735. details.
  736. Arguments:
  737. Item - item to find match for eg a\abcd\c
  738. MatchEntry - eg *\*bc*\*
  739. Return Value:
  740. TRUE if any match, otherwise FALSE
  741. --*/
  742. {
  743. LPCTSTR scanItem;
  744. LPCTSTR wildMark;
  745. LPCTSTR nextWild;
  746. size_t matchlen;
  747. //
  748. // before attempting anything else
  749. // try and compare everything up to first wild
  750. //
  751. if(!MatchEntry.Wild)
  752. {
  753. return _tcsicmp(Item,MatchEntry.String) ? FALSE : TRUE;
  754. }
  755. if(_tcsnicmp(Item,MatchEntry.String,MatchEntry.Wild-MatchEntry.String) != 0)
  756. {
  757. return FALSE;
  758. }
  759. wildMark = MatchEntry.Wild;
  760. scanItem = Item + (MatchEntry.Wild-MatchEntry.String);
  761. for(;wildMark[0];)
  762. {
  763. //
  764. // if we get here, we're either at or past a wildcard
  765. //
  766. if(wildMark[0] == WILD_CHAR)
  767. {
  768. //
  769. // so skip wild chars
  770. //
  771. wildMark = CharNext(wildMark);
  772. continue;
  773. }
  774. //
  775. // find next wild-card
  776. //
  777. nextWild = _tcschr(wildMark,WILD_CHAR);
  778. if(nextWild)
  779. {
  780. //
  781. // substring
  782. //
  783. matchlen = nextWild-wildMark;
  784. }
  785. else
  786. {
  787. //
  788. // last portion of match
  789. //
  790. size_t scanlen = lstrlen(scanItem);
  791. matchlen = lstrlen(wildMark);
  792. if(scanlen < matchlen)
  793. {
  794. return FALSE;
  795. }
  796. return _tcsicmp(scanItem+scanlen-matchlen,wildMark) ? FALSE : TRUE;
  797. }
  798. if(_istalpha(wildMark[0]))
  799. {
  800. //
  801. // scan for either lower or uppercase version of first character
  802. //
  803. TCHAR u = (TCHAR)_totupper(wildMark[0]);
  804. TCHAR l = (TCHAR)_totlower(wildMark[0]);
  805. while(scanItem[0] && scanItem[0]!=u && scanItem[0]!=l)
  806. {
  807. scanItem = CharNext(scanItem);
  808. }
  809. if(!scanItem[0])
  810. {
  811. //
  812. // ran out of string
  813. //
  814. return FALSE;
  815. }
  816. }
  817. else
  818. {
  819. //
  820. // scan for first character (no case)
  821. //
  822. scanItem = _tcschr(scanItem,wildMark[0]);
  823. if(!scanItem)
  824. {
  825. //
  826. // ran out of string
  827. //
  828. return FALSE;
  829. }
  830. }
  831. //
  832. // try and match the sub-string at wildMark against scanItem
  833. //
  834. if(_tcsnicmp(scanItem,wildMark,matchlen)!=0)
  835. {
  836. //
  837. // nope, try again
  838. //
  839. scanItem = CharNext(scanItem);
  840. continue;
  841. }
  842. //
  843. // substring matched
  844. //
  845. scanItem += matchlen;
  846. wildMark += matchlen;
  847. }
  848. return (wildMark[0] ? FALSE : TRUE);
  849. }
  850. BOOL WildCompareHwIds(LPTSTR * Array,const IdEntry & MatchEntry)
  851. /*++
  852. Routine Description:
  853. Compares all strings in Array against Id
  854. Use WildCardMatch to do real compare
  855. Arguments:
  856. Array - pointer returned by GetDevMultiSz
  857. MatchEntry - string to compare against
  858. Return Value:
  859. TRUE if any match, otherwise FALSE
  860. --*/
  861. {
  862. if(Array)
  863. {
  864. while(Array[0])
  865. {
  866. if(WildCardMatch(Array[0],MatchEntry))
  867. {
  868. return TRUE;
  869. }
  870. Array++;
  871. }
  872. }
  873. return FALSE;
  874. }
  875. int EnumerateDevices(LPCTSTR Machine,DWORD Flags,int argc,LPTSTR argv[],CallbackFunc Callback,LPVOID Context)
  876. /*++
  877. Routine Description:
  878. Generic enumerator for devices that will be passed the following arguments:
  879. <id> [<id>...]
  880. =<class> [<id>...]
  881. where <id> can either be @instance-id, or hardware-id and may contain wildcards
  882. <class> is a class name
  883. Arguments:
  884. Machine - name of machine to enumerate
  885. Flags - extra enumeration flags (eg DIGCF_PRESENT)
  886. argc/argv - remaining arguments on command line
  887. Callback - function to call for each hit
  888. Context - data to pass function for each hit
  889. Return Value:
  890. EXIT_xxxx
  891. --*/
  892. {
  893. HDEVINFO devs = INVALID_HANDLE_VALUE;
  894. IdEntry * templ = NULL;
  895. int failcode = EXIT_FAIL;
  896. int retcode;
  897. int argIndex;
  898. DWORD devIndex;
  899. SP_DEVINFO_DATA devInfo;
  900. SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
  901. BOOL doSearch = FALSE;
  902. BOOL match;
  903. BOOL all = FALSE;
  904. GUID cls;
  905. DWORD numClass = 0;
  906. int skip = 0;
  907. if(!argc)
  908. {
  909. return EXIT_USAGE;
  910. }
  911. templ = new IdEntry[argc];
  912. if(!templ)
  913. {
  914. goto final;
  915. }
  916. //
  917. // determine if a class is specified
  918. //
  919. if(argc>skip && argv[skip][0]==CLASS_PREFIX_CHAR && argv[skip][1])
  920. {
  921. if(!SetupAPI_ClassGuidsFromNameEx(argv[skip]+1,&cls,1,&numClass,Machine,NULL) &&
  922. GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  923. {
  924. goto final;
  925. }
  926. if(!numClass)
  927. {
  928. failcode = EXIT_OK;
  929. goto final;
  930. }
  931. skip++;
  932. }
  933. if(argc>skip && argv[skip][0]==WILD_CHAR && !argv[skip][1])
  934. {
  935. //
  936. // catch convinient case of specifying a single argument '*'
  937. //
  938. all = TRUE;
  939. skip++;
  940. }
  941. else if(argc<=skip)
  942. {
  943. //
  944. // at least one parameter, but no <id>'s
  945. //
  946. all = TRUE;
  947. }
  948. //
  949. // determine if any instance id's were specified
  950. //
  951. // note, if =<class> was specified with no id's
  952. // we'll mark it as not doSearch
  953. // but will go ahead and add them all
  954. //
  955. for(argIndex=skip;argIndex<argc;argIndex++)
  956. {
  957. templ[argIndex] = GetIdType(argv[argIndex]);
  958. if(templ[argIndex].Wild || !templ[argIndex].InstanceId)
  959. {
  960. //
  961. // anything other than simple InstanceId's require a search
  962. //
  963. doSearch = TRUE;
  964. }
  965. }
  966. if(doSearch || all)
  967. {
  968. //
  969. // add all id's to list
  970. // if there's a class, filter on specified class
  971. //
  972. devs = SetupAPI_GetClassDevsEx(numClass ? &cls : NULL,
  973. NULL,
  974. NULL,
  975. (numClass ? 0 : DIGCF_ALLCLASSES) | Flags,
  976. NULL,
  977. Machine,
  978. NULL);
  979. }
  980. else
  981. {
  982. //
  983. // blank list, we'll add instance id's by hand
  984. //
  985. devs = SetupAPI_CreateDeviceInfoListEx(numClass ? &cls : NULL,
  986. NULL,
  987. Machine,
  988. NULL);
  989. }
  990. if(devs == INVALID_HANDLE_VALUE)
  991. {
  992. goto final;
  993. }
  994. for(argIndex=skip;argIndex<argc;argIndex++)
  995. {
  996. //
  997. // add explicit instances to list (even if enumerated all,
  998. // this gets around DIGCF_PRESENT)
  999. // do this even if wildcards appear to be detected since they
  1000. // might actually be part of the instance ID of a non-present device
  1001. //
  1002. if(templ[argIndex].InstanceId)
  1003. {
  1004. SetupAPI_OpenDeviceInfo(devs,templ[argIndex].String,NULL,0,NULL);
  1005. }
  1006. }
  1007. devInfoListDetail.cbSize = sizeof(devInfoListDetail);
  1008. if(!SetupAPI_GetDeviceInfoListDetail(devs,&devInfoListDetail))
  1009. {
  1010. goto final;
  1011. }
  1012. //
  1013. // now enumerate them
  1014. //
  1015. if(all)
  1016. {
  1017. doSearch = FALSE;
  1018. }
  1019. devInfo.cbSize = sizeof(devInfo);
  1020. for(devIndex=0;SetupAPI_EnumDeviceInfo(devs,devIndex,&devInfo);devIndex++)
  1021. {
  1022. if(doSearch)
  1023. {
  1024. for(argIndex=skip,match=FALSE;(argIndex<argc) && !match;argIndex++)
  1025. {
  1026. TCHAR devID[MAX_DEVICE_ID_LEN];
  1027. LPTSTR *hwIds = NULL;
  1028. LPTSTR *compatIds = NULL;
  1029. //
  1030. // determine instance ID
  1031. //
  1032. if(CfgMgr32_Get_Device_ID_Ex(devInfo.DevInst,devID,MAX_DEVICE_ID_LEN,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)
  1033. {
  1034. devID[0] = TEXT('\0');
  1035. }
  1036. if(templ[argIndex].InstanceId)
  1037. {
  1038. //
  1039. // match on the instance ID
  1040. //
  1041. if(WildCardMatch(devID,templ[argIndex]))
  1042. {
  1043. match = TRUE;
  1044. }
  1045. }
  1046. else
  1047. {
  1048. //
  1049. // determine hardware ID's
  1050. // and search for matches
  1051. //
  1052. hwIds = GetDevMultiSz(devs,&devInfo,SPDRP_HARDWAREID);
  1053. compatIds = GetDevMultiSz(devs,&devInfo,SPDRP_COMPATIBLEIDS);
  1054. if(WildCompareHwIds(hwIds,templ[argIndex]) ||
  1055. WildCompareHwIds(compatIds,templ[argIndex]))
  1056. {
  1057. match = TRUE;
  1058. }
  1059. }
  1060. DelMultiSz(hwIds);
  1061. DelMultiSz(compatIds);
  1062. }
  1063. }
  1064. else
  1065. {
  1066. match = TRUE;
  1067. }
  1068. if(match)
  1069. {
  1070. retcode = Callback(devs,&devInfo,Context);
  1071. if(retcode)
  1072. {
  1073. failcode = retcode;
  1074. goto final;
  1075. }
  1076. }
  1077. }
  1078. failcode = EXIT_OK;
  1079. final:
  1080. if(templ)
  1081. {
  1082. delete [] templ;
  1083. }
  1084. if(devs != INVALID_HANDLE_VALUE)
  1085. {
  1086. SetupAPI_DestroyDeviceInfoList(devs);
  1087. }
  1088. return failcode;
  1089. }
  1090. #endif //defined(DSI_TYPES_WINDOWS)