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.

272 lines
8.6 KiB

  1. /** FILE: cyydel.c *******************************************************
  2. *
  3. * This module is used by cyycoins.dll and yinfdelete.exe.
  4. * Please re-generate both files when cyydel.c is changed.
  5. *
  6. * Copyright (C) 2000 Cyclades Corporation
  7. *
  8. *************************************************************************/
  9. //==========================================================================
  10. // Include files
  11. //==========================================================================
  12. // C Runtime
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. //#include <stdio.h> Used with .exe
  17. // Device Class GUID
  18. #include <initguid.h>
  19. #include <devguid.h>
  20. // Application specific
  21. #include <windows.h>
  22. #include <tchar.h> // Make all functions UNICODE safe.
  23. #include <cfgmgr32.h>
  24. #include <setupapi.h> // for SetupDiXxx functions.
  25. #include <regstr.h>
  26. #include "cyydel.h"
  27. //#include "yinfdelete.h" Used with .exe
  28. //==========================================================================
  29. // Macros
  30. //==========================================================================
  31. #define CharSizeOf(x) (sizeof(x) / sizeof(*x))
  32. #define ByteCountOf(x) ((x) * sizeof(TCHAR))
  33. #if DBG
  34. #define DbgOut(Text) OutputDebugString(Text)
  35. #else
  36. #define DbgOut(Text)
  37. #endif
  38. //==========================================================================
  39. // Globals
  40. //==========================================================================
  41. TCHAR y_szCyclomyEnumerator[] = TEXT("Cyclom-Y");
  42. TCHAR y_szParentIdPrefix[] = TEXT("ParentIdPrefix");
  43. //==========================================================================
  44. // Local Function Prototypes
  45. //==========================================================================
  46. BOOL
  47. IsItCyclomy(
  48. PTCHAR ptrChar
  49. );
  50. DWORD
  51. RemoveMyChildren(
  52. PTCHAR ParentIdPrefix
  53. );
  54. //==========================================================================
  55. // Functions
  56. //==========================================================================
  57. void
  58. DeleteNonPresentDevices(
  59. )
  60. {
  61. HDEVINFO MultiportInfoSet, PresentInfoSet;
  62. SP_DEVINFO_DATA MultiportInfoData, PresentInfoData;
  63. DWORD i,j;
  64. DWORD bufType,bufSize;
  65. DWORD present;
  66. TCHAR bufChar[256];
  67. MultiportInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_MULTIPORTSERIAL,
  68. 0,
  69. 0,
  70. 0 ); // All devices, even non present
  71. if (MultiportInfoSet == INVALID_HANDLE_VALUE) {
  72. return;
  73. }
  74. MultiportInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  75. for (i=0;SetupDiEnumDeviceInfo(MultiportInfoSet,i,&MultiportInfoData);i++){
  76. if (SetupDiGetDeviceRegistryProperty(MultiportInfoSet,
  77. &MultiportInfoData,
  78. SPDRP_HARDWAREID, //SPDRP_SERVICE,
  79. &bufType,
  80. (PBYTE) bufChar,
  81. sizeof(bufChar),
  82. NULL)) {
  83. if (bufType != REG_MULTI_SZ) {
  84. continue;
  85. }
  86. if (!IsItCyclomy(bufChar)) {
  87. continue;
  88. }
  89. // Verify if this cyclom-y is present.
  90. PresentInfoSet = SetupDiGetClassDevs(&GUID_DEVCLASS_MULTIPORTSERIAL,
  91. 0,
  92. 0,
  93. DIGCF_PRESENT );
  94. if (PresentInfoSet == INVALID_HANDLE_VALUE) {
  95. continue;
  96. }
  97. present = FALSE;
  98. PresentInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  99. for (j=0;SetupDiEnumDeviceInfo(PresentInfoSet,j,&PresentInfoData);j++) {
  100. if (MultiportInfoData.DevInst == PresentInfoData.DevInst) {
  101. present = TRUE;
  102. break;
  103. }
  104. }
  105. if (GetLastError() == ERROR_NO_MORE_ITEMS) {
  106. if (!present) {
  107. //#if DBG
  108. //TCHAR myDevInstId[200];
  109. //DWORD err;
  110. //err = CM_Get_Device_ID(MultiportInfoData.DevInst,myDevInstId,
  111. // sizeof(myDevInstId),0);
  112. //if (err==CR_SUCCESS) {
  113. // TCHAR buf[500];
  114. // wsprintf(buf, TEXT("Delete %s\n"), myDevInstId);
  115. // DbgOut(buf);
  116. //}
  117. //#endif
  118. GetParentIdAndRemoveChildren(&MultiportInfoData);
  119. SetupDiCallClassInstaller(DIF_REMOVE,MultiportInfoSet,&MultiportInfoData);
  120. }
  121. }
  122. SetupDiDestroyDeviceInfoList(PresentInfoSet);
  123. }
  124. }
  125. SetupDiDestroyDeviceInfoList(MultiportInfoSet);
  126. }
  127. BOOL
  128. IsItCyclomy(
  129. PTCHAR ptrChar
  130. )
  131. {
  132. while (*ptrChar) {
  133. //_tprintf("%s\n", ptrChar);
  134. if (_tcsnicmp(ptrChar,
  135. TEXT("PCI\\VEN_120E&DEV_010"),
  136. _tcslen(TEXT("PCI\\VEN_120E&DEV_010")))
  137. == 0) {
  138. return TRUE;
  139. }
  140. if (_tcsnicmp(ptrChar,
  141. TEXT("YISA"),
  142. _tcslen(TEXT("YISA")))
  143. == 0) {
  144. return TRUE;
  145. }
  146. ptrChar = ptrChar + _tcslen(ptrChar) + 1;
  147. }
  148. return FALSE;
  149. }
  150. DWORD
  151. GetParentIdAndRemoveChildren(
  152. IN PSP_DEVINFO_DATA DeviceInfoData
  153. )
  154. {
  155. DWORD dwSize;
  156. TCHAR instanceId[MAX_DEVICE_ID_LEN];
  157. TCHAR parentIdPrefix[50];
  158. HKEY enumKey,instKey;
  159. BOOL gotParentIdPrefix;
  160. DWORD Status = NO_ERROR;
  161. if (CM_Get_Device_ID(DeviceInfoData->DevInst,instanceId,CharSizeOf(instanceId),0) ==
  162. CR_SUCCESS) {
  163. gotParentIdPrefix = FALSE;
  164. // Open Registry and retrieve ParentIdPrefix value
  165. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGSTR_PATH_SYSTEMENUM,0,KEY_READ,
  166. &enumKey) == ERROR_SUCCESS) {
  167. if (RegOpenKeyEx(enumKey,instanceId,0,KEY_READ,&instKey) == ERROR_SUCCESS) {
  168. dwSize = sizeof(parentIdPrefix);
  169. if (RegQueryValueEx(instKey,y_szParentIdPrefix,NULL,NULL,
  170. (PBYTE)parentIdPrefix,&dwSize) == ERROR_SUCCESS) {
  171. _tcsupr(parentIdPrefix);
  172. gotParentIdPrefix = TRUE;
  173. }
  174. RegCloseKey(instKey);
  175. }
  176. RegCloseKey(enumKey);
  177. }
  178. if (gotParentIdPrefix) {
  179. Status = RemoveMyChildren(parentIdPrefix);
  180. }
  181. }
  182. return Status;
  183. }
  184. DWORD
  185. RemoveMyChildren(
  186. PTCHAR ParentIdPrefix
  187. )
  188. {
  189. HDEVINFO DeviceInfoSet;
  190. SP_DEVINFO_DATA DeviceInfoData;
  191. DWORD i,err;
  192. TCHAR portId[MAX_DEVICE_ID_LEN];
  193. PTCHAR ptrParent;
  194. DeviceInfoSet = SetupDiGetClassDevs( &GUID_DEVCLASS_PORTS,y_szCyclomyEnumerator,0,0 );
  195. if (DeviceInfoSet == INVALID_HANDLE_VALUE)
  196. {
  197. return GetLastError();
  198. }
  199. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  200. for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
  201. {
  202. if (CM_Get_Device_ID(DeviceInfoData.DevInst,portId,CharSizeOf(portId),0)
  203. == CR_SUCCESS) {
  204. // BUG? For ParentIdPrefix "3&2b41c2e&1f" (12 characters), _tcscspn
  205. // always returns 0!! Using _tcsstr instead.
  206. //position = _tcscspn(portId,ParentIdPrefix);
  207. ptrParent = _tcsstr(portId,ParentIdPrefix);
  208. if (ptrParent) {
  209. if (_tcsnicmp (ptrParent,ParentIdPrefix,_tcslen(ParentIdPrefix))
  210. == 0){
  211. //
  212. // Worker function to remove device.
  213. //
  214. //#if DBG
  215. //{
  216. // TCHAR buf[500];
  217. // wsprintf(buf, TEXT("Delete %s\n"), portId);
  218. // DbgOut(buf);
  219. //}
  220. //#endif
  221. SetupDiCallClassInstaller(DIF_REMOVE,DeviceInfoSet,&DeviceInfoData);
  222. }
  223. }
  224. }
  225. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  226. }
  227. SetupDiDestroyDeviceInfoList(DeviceInfoSet);
  228. return NO_ERROR;
  229. }