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.

245 lines
7.1 KiB

  1. #include <windows.h>
  2. #include <devioctl.h>
  3. #include <spsimioct.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <tchar.h>
  7. typedef struct {
  8. WCHAR *szName;
  9. } MANAGED_DEVICE, *PMANAGED_DEVICE;
  10. PMANAGED_DEVICE g_pManagedDevices;
  11. ULONG g_nDevices;
  12. VOID Usage()
  13. {
  14. _tprintf(TEXT("usage: spctl command args...\n"));
  15. _tprintf(TEXT(" commands are:\n"));
  16. _tprintf(TEXT(" notify devnum code\n"));
  17. _tprintf(TEXT(" setsta devnum value\n"));
  18. _tprintf(TEXT(" insert [devnum | * ]\n"));
  19. _tprintf(TEXT(" eject [devnum | * ]\n"));
  20. _tprintf(TEXT(" <nothing> - prints device status\n"));
  21. }
  22. DWORD
  23. Notify(
  24. HANDLE hSpSim,
  25. DWORD Device,
  26. BYTE NotifyValue
  27. )
  28. {
  29. SPSIM_NOTIFY_DEVICE notify;
  30. BOOL bResult;
  31. DWORD dwReturned;
  32. notify.Device = Device;
  33. notify.NotifyValue = NotifyValue;
  34. bResult = DeviceIoControl(hSpSim,
  35. IOCTL_SPSIM_NOTIFY_DEVICE,
  36. &notify,
  37. sizeof(notify),
  38. NULL,
  39. 0,
  40. &dwReturned,
  41. NULL
  42. );
  43. if (!bResult) {
  44. _tprintf(TEXT("unable to notify device %u: %u\n"), Device, GetLastError());
  45. return GetLastError();
  46. }
  47. return 0;
  48. }
  49. DWORD
  50. SetSta(
  51. HANDLE hSpSim,
  52. DWORD Device,
  53. BYTE StaValue
  54. )
  55. {
  56. SPSIM_ACCESS_STA access;
  57. BOOL bResult;
  58. DWORD dwReturned;
  59. access.Device = Device;
  60. access.StaValue = StaValue;
  61. access.WriteOperation = TRUE;
  62. bResult = DeviceIoControl(hSpSim,
  63. IOCTL_SPSIM_ACCESS_STA,
  64. &access,
  65. sizeof(access),
  66. NULL,
  67. 0,
  68. &dwReturned,
  69. NULL
  70. );
  71. if (!bResult) {
  72. _tprintf(TEXT("unable to set operation region's sta value for device %u: %u\n"), Device, GetLastError());
  73. return GetLastError();
  74. }
  75. return 0;
  76. }
  77. DWORD
  78. GetManagedDevices(
  79. HANDLE hSpSim,
  80. DWORD Count
  81. )
  82. {
  83. PSPSIM_DEVICE_NAME name;
  84. BOOL bResult;
  85. DWORD dwReturned, bufferSize, dwError, i;
  86. g_nDevices = Count;
  87. g_pManagedDevices = LocalAlloc(LPTR,
  88. g_nDevices * sizeof(MANAGED_DEVICE));
  89. if (g_pManagedDevices == NULL) {
  90. return ERROR_NOT_ENOUGH_MEMORY;
  91. }
  92. bufferSize = sizeof(SPSIM_DEVICE_NAME) + sizeof(WCHAR) * 1024;
  93. name = LocalAlloc(LPTR, bufferSize);
  94. if (name == NULL) {
  95. return ERROR_NOT_ENOUGH_MEMORY;
  96. }
  97. for (i = 0; i < g_nDevices; i++) {
  98. name->Device = i;
  99. bResult = DeviceIoControl(hSpSim,
  100. IOCTL_SPSIM_GET_DEVICE_NAME,
  101. name,
  102. bufferSize,
  103. name,
  104. bufferSize,
  105. &dwReturned,
  106. NULL
  107. );
  108. if (!bResult) {
  109. return GetLastError();
  110. }
  111. g_pManagedDevices[i].szName = _wcsdup(name->DeviceName);
  112. }
  113. return 0;
  114. }
  115. void __cdecl
  116. _tmain(INT argx, TCHAR *argv[]) {
  117. HANDLE hSpSim;
  118. PSPSIM_MANAGED_DEVICES managed;
  119. PVOID newmem;
  120. BOOL bResult;
  121. DWORD dwReturned, bufferSize, dwError;
  122. DWORD Device;
  123. BYTE NotifyCode, StaValue;
  124. managed = LocalAlloc(LPTR, sizeof(SPSIM_MANAGED_DEVICES));
  125. if (managed == NULL) {
  126. _tprintf(TEXT("out of memory\n"));
  127. return;
  128. }
  129. bufferSize = sizeof(SPSIM_MANAGED_DEVICES);
  130. //
  131. // Need to get the interface in a more portable way.
  132. //
  133. hSpSim = CreateFile(TEXT("\\\\.\\ACPI#SPSIMUL#2&daba3ff&0#{bdde6934-529d-4183-a952-adffb0dbb3dd}"),
  134. GENERIC_READ | GENERIC_WRITE,
  135. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  136. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
  137. );
  138. if (hSpSim == INVALID_HANDLE_VALUE) {
  139. _tprintf(TEXT("Could not open handle to device.\n GetLastError()==%x\n"),
  140. GetLastError());
  141. return;
  142. }
  143. retry:
  144. bResult = DeviceIoControl(hSpSim,
  145. IOCTL_SPSIM_GET_MANAGED_DEVICES,
  146. NULL,
  147. 0,
  148. managed,
  149. bufferSize,
  150. &dwReturned,
  151. NULL
  152. );
  153. if (!bResult) {
  154. dwError = GetLastError();
  155. if (dwError == ERROR_MORE_DATA) {
  156. bufferSize = sizeof(SPSIM_MANAGED_DEVICES) +
  157. managed->Count * sizeof(UCHAR);
  158. newmem = LocalReAlloc(managed, bufferSize, LMEM_MOVEABLE);
  159. if (newmem == NULL) {
  160. _tprintf(TEXT("unable to realloc memory\n"));
  161. LocalFree(managed);
  162. return;
  163. }
  164. managed = newmem;
  165. goto retry;
  166. }
  167. } else {
  168. dwError = GetManagedDevices(hSpSim, managed->Count);
  169. if (dwError) {
  170. _tprintf("Couldn't get info on managed devices: %u\n", dwError);
  171. LocalFree(managed);
  172. return;
  173. }
  174. }
  175. if (argx == 1) {
  176. for (Device = 0; Device < g_nDevices; Device++) {
  177. _tprintf(TEXT("\tDev %u : %_STA is 0x%x Name %ws\n"), Device, managed->StaValues[Device], g_pManagedDevices[Device].szName);
  178. }
  179. }
  180. else if (argx == 4) {
  181. Device = _ttol(argv[2]);
  182. if (!_tcsicmp(argv[1], "notify")) {
  183. NotifyCode = (BYTE) _ttol(argv[3]);
  184. Notify(hSpSim, Device, NotifyCode);
  185. } else if (!_tcsicmp(argv[1], "setsta")) {
  186. StaValue = (BYTE) _ttol(argv[3]);
  187. SetSta(hSpSim, Device, StaValue);
  188. } else {
  189. Usage();
  190. }
  191. } else if (argx == 3) {
  192. if (!_tcsicmp(argv[1], "insert")) {
  193. if (*argv[2] == '*') {
  194. for (Device = 0; Device < g_nDevices; Device++) {
  195. SetSta(hSpSim, Device, 0xf);
  196. Notify(hSpSim, Device, 1);
  197. }
  198. } else {
  199. Device = _ttol(argv[2]);
  200. SetSta(hSpSim, Device, 0xf);
  201. Notify(hSpSim, Device, 1);
  202. }
  203. } else if (!_tcsicmp(argv[1], "eject")) {
  204. if (*argv[2] == '*') {
  205. for (Device = 0; Device < g_nDevices; Device++) {
  206. Notify(hSpSim, Device, 3);
  207. }
  208. } else {
  209. Device = _ttol(argv[2]);
  210. Notify(hSpSim, Device, 3);
  211. }
  212. } else {
  213. Usage();
  214. }
  215. } else {
  216. Usage();
  217. }
  218. CloseHandle(hSpSim);
  219. LocalFree(managed);
  220. }