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.

379 lines
8.8 KiB

  1. //
  2. // WMITEST.C
  3. //
  4. // Test program for NDIS WMI interface
  5. //
  6. // usage: WMITEST
  7. //
  8. //
  9. #include <windows.h>
  10. #include <winioctl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <memory.h>
  15. #include <ctype.h>
  16. #include <malloc.h>
  17. #include <winerror.h>
  18. #include <winsock.h>
  19. #include <ntddndis.h>
  20. #include <ndisguid.h>
  21. #include <wmium.h>
  22. #ifndef NDIS_STATUS
  23. #define NDIS_STATUS ULONG
  24. #endif
  25. #if DBG
  26. #define DEBUGP(stmt) printf stmt
  27. #else
  28. #define DEBUGP(stmt)
  29. #endif
  30. #define PRINTF(stmt) printf stmt
  31. #define MAX_NDIS_DEVICE_NAME_LEN 256
  32. #define MAX_ADAPTER_NAME_LENGTH 512
  33. #define DEVICE_PREFIX L"\\Device\\"
  34. LPGUID WmiEvent[] = {
  35. (LPGUID) &GUID_NDIS_STATUS_MEDIA_CONNECT,
  36. (LPGUID) &GUID_NDIS_STATUS_MEDIA_DISCONNECT,
  37. (LPGUID) &GUID_NDIS_NOTIFY_ADAPTER_ARRIVAL,
  38. (LPGUID) &GUID_NDIS_NOTIFY_ADAPTER_REMOVAL,
  39. (LPGUID) &GUID_NDIS_NOTIFY_DEVICE_POWER_ON,
  40. (LPGUID) &GUID_NDIS_NOTIFY_DEVICE_POWER_OFF
  41. };
  42. PWCHAR
  43. GetAdapterName(
  44. PWNODE_SINGLE_INSTANCE Instance,
  45. PWCHAR AdapterName,
  46. BOOLEAN HaveLength
  47. );
  48. DWORD
  49. __inline
  50. EnableWmiEvent(
  51. IN LPGUID EventGuid,
  52. IN BOOLEAN Enable
  53. );
  54. VOID
  55. __inline
  56. DeregisterWmiEventNotification(
  57. VOID
  58. );
  59. DWORD
  60. __inline
  61. RegisterWmiEventNotification(
  62. VOID
  63. );
  64. VOID
  65. WINAPI
  66. WmiEventNotification(
  67. IN PWNODE_HEADER Event,
  68. IN UINT_PTR Context
  69. );
  70. VOID __cdecl
  71. main(
  72. INT argc,
  73. CHAR *argv[]
  74. )
  75. {
  76. DWORD ErrorCode;
  77. printf("testing NDIS wmi notifications. press 'q' to abort.\n");
  78. //
  79. // test wmi
  80. //
  81. if ((ErrorCode = RegisterWmiEventNotification()) != NO_ERROR)
  82. {
  83. printf("error %d calling RegisterWmiEventNotification.\n", ErrorCode);
  84. return;
  85. }
  86. while (_fgetchar() != 'q')
  87. {
  88. Sleep(1000);
  89. }
  90. DeregisterWmiEventNotification();
  91. return;
  92. }
  93. DWORD
  94. __inline
  95. EnableWmiEvent(
  96. IN LPGUID EventGuid,
  97. IN BOOLEAN Enable
  98. )
  99. {
  100. return WmiNotificationRegistrationW(
  101. EventGuid, // Event Type.
  102. Enable, // Enable or Disable.
  103. WmiEventNotification, // Callback.
  104. 0, // Context.
  105. NOTIFICATION_CALLBACK_DIRECT); // Notification Flags.
  106. }
  107. VOID
  108. __inline
  109. DeregisterWmiEventNotification(
  110. VOID
  111. )
  112. {
  113. int i;
  114. for (i = 0; i < (sizeof(WmiEvent) / sizeof(LPGUID)); i++) {
  115. (VOID) EnableWmiEvent(WmiEvent[i], FALSE);
  116. }
  117. }
  118. DWORD
  119. __inline
  120. RegisterWmiEventNotification(
  121. VOID
  122. )
  123. {
  124. DWORD Error;
  125. int i;
  126. for (i = 0; i < (sizeof(WmiEvent) / sizeof(LPGUID)); i++) {
  127. Error = EnableWmiEvent(WmiEvent[i], TRUE);
  128. if (Error != NO_ERROR)
  129. {
  130. printf("failed enabling event for %lx\n", i);
  131. }
  132. }
  133. if (Error != NO_ERROR)
  134. {
  135. DeregisterWmiEventNotification();
  136. }
  137. return Error;
  138. }
  139. VOID
  140. WINAPI
  141. WmiEventNotification(
  142. IN PWNODE_HEADER Event,
  143. IN UINT_PTR Context
  144. )
  145. /*++
  146. Routine Description:
  147. Process a WMI event (specifically adapter arrival or removal).
  148. Arguments:
  149. Event - Supplies event specific information.
  150. Context - Supplies the context registered.
  151. Return Value:
  152. None.
  153. --*/
  154. {
  155. PWNODE_SINGLE_INSTANCE Instance = (PWNODE_SINGLE_INSTANCE) Event;
  156. USHORT AdapterNameLength;
  157. WCHAR AdapterName[MAX_ADAPTER_NAME_LENGTH];
  158. PWCHAR AdapterGuid = NULL;
  159. USHORT AdapterInstanceNameLength;
  160. WCHAR AdapterInstanceName[MAX_ADAPTER_NAME_LENGTH];
  161. if (Instance == NULL)
  162. {
  163. return;
  164. }
  165. AdapterInstanceNameLength =
  166. *((PUSHORT) (((PUCHAR) Instance) + Instance->OffsetInstanceName));
  167. if ((AdapterInstanceNameLength / sizeof(WCHAR)) > (MAX_ADAPTER_NAME_LENGTH - 1))
  168. {
  169. return;
  170. }
  171. RtlCopyMemory(
  172. AdapterInstanceName,
  173. ((PUCHAR) Instance) + Instance->OffsetInstanceName + sizeof(USHORT),
  174. AdapterInstanceNameLength);
  175. AdapterInstanceName[AdapterInstanceNameLength / sizeof(WCHAR)] = L'\0';
  176. do
  177. {
  178. if (memcmp(
  179. &(Event->Guid),
  180. &GUID_NDIS_STATUS_MEDIA_CONNECT,
  181. sizeof(GUID)) == 0)
  182. {
  183. //
  184. // Media connect
  185. //
  186. printf("Media connect. %ws\n", AdapterInstanceName);
  187. break;
  188. }
  189. if (memcmp(
  190. &(Event->Guid),
  191. &GUID_NDIS_STATUS_MEDIA_DISCONNECT,
  192. sizeof(GUID)) == 0)
  193. {
  194. //
  195. // Media disconnect
  196. //
  197. printf("Media disconnect. %ws\n", AdapterInstanceName);
  198. break;
  199. }
  200. if (memcmp(
  201. &(Event->Guid),
  202. &GUID_NDIS_NOTIFY_ADAPTER_ARRIVAL,
  203. sizeof(GUID)) == 0)
  204. {
  205. //
  206. // Adapter arrival.
  207. //
  208. AdapterGuid = GetAdapterName(Instance, AdapterName, TRUE);
  209. if (AdapterGuid)
  210. printf("Adapter arrival. %ws\n", AdapterGuid);
  211. break;
  212. }
  213. if (memcmp(
  214. &(Event->Guid),
  215. &GUID_NDIS_NOTIFY_ADAPTER_REMOVAL,
  216. sizeof(GUID)) == 0)
  217. {
  218. //
  219. // Adapter removal.
  220. //
  221. AdapterGuid = GetAdapterName(Instance, AdapterName, TRUE);
  222. if (AdapterGuid)
  223. printf("Adapter removal. %ws\n", AdapterGuid);
  224. break;
  225. }
  226. if (memcmp(
  227. &(Event->Guid),
  228. (PVOID)&GUID_NDIS_NOTIFY_DEVICE_POWER_ON,
  229. sizeof(GUID)) == 0)
  230. {
  231. //
  232. // Adapter powered on
  233. //
  234. printf("Adapter powered on. %ws\n", AdapterInstanceName );
  235. break;
  236. }
  237. if (memcmp(
  238. &(Event->Guid),
  239. (PVOID)&GUID_NDIS_NOTIFY_DEVICE_POWER_OFF,
  240. sizeof(GUID)) == 0)
  241. {
  242. //
  243. // Adapter powered off
  244. //
  245. printf("Adapter powered off. %ws\n", AdapterInstanceName );
  246. break;
  247. }
  248. } while (FALSE);
  249. }
  250. PWCHAR
  251. GetAdapterName(
  252. PWNODE_SINGLE_INSTANCE Instance,
  253. PWCHAR AdapterName,
  254. BOOLEAN HaveLength
  255. )
  256. {
  257. USHORT AdapterNameLength;
  258. PWCHAR AdapterGuid = NULL;
  259. PWCHAR Src;
  260. USHORT i;
  261. do
  262. {
  263. if (HaveLength)
  264. {
  265. //
  266. // WNODE_SINGLE_INSTANCE is organized thus...
  267. // +-----------------------------------------------------------+
  268. // |<--- DataBlockOffset --->| AdapterNameLength | AdapterName |
  269. // +-----------------------------------------------------------+
  270. //
  271. // AdapterName is defined as "\DEVICE\"AdapterGuid
  272. //
  273. AdapterNameLength =
  274. *((PUSHORT) (((PUCHAR) Instance) + Instance->DataBlockOffset));
  275. if (((AdapterNameLength / sizeof(WCHAR)) > (MAX_ADAPTER_NAME_LENGTH - 1)) ||
  276. ((AdapterNameLength / sizeof(WCHAR)) < wcslen(DEVICE_PREFIX)))
  277. {
  278. break;
  279. }
  280. RtlCopyMemory(
  281. AdapterName,
  282. ((PUCHAR) Instance) + Instance->DataBlockOffset + sizeof(USHORT),
  283. AdapterNameLength);
  284. AdapterName[AdapterNameLength / sizeof(WCHAR)] = L'\0';
  285. AdapterGuid = AdapterName + wcslen(DEVICE_PREFIX);
  286. }
  287. else
  288. {
  289. //
  290. // WNODE_SINGLE_INSTANCE is NULL terminated
  291. // +-----------------------------------------------------------+
  292. // |<--- DataBlockOffset --->| AdapterName | L'\0'
  293. // +-----------------------------------------------------------+
  294. //
  295. // AdapterName is defined as "\DEVICE\"AdapterGuid
  296. //
  297. Src = (PWCHAR)((PUCHAR) Instance + Instance->DataBlockOffset);
  298. for (i = 0; i < MAX_ADAPTER_NAME_LENGTH - 1; i++)
  299. {
  300. if (Src[i] == L'\0')
  301. break;
  302. AdapterName[i]= Src[i];
  303. }
  304. AdapterName[i] = L'\0';
  305. AdapterNameLength = i * sizeof(WCHAR);
  306. if (AdapterNameLength > wcslen(DEVICE_PREFIX))
  307. {
  308. AdapterGuid = AdapterName + wcslen(DEVICE_PREFIX);
  309. }
  310. }
  311. } while (FALSE);
  312. return AdapterGuid;
  313. }