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.

305 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: godvd.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "propp.h"
  11. #include <windows.h>
  12. #include <devioctl.h>
  13. #include <ntdddisk.h>
  14. #include <ntddscsi.h>
  15. #include <ntddcdvd.h>
  16. #include <stdio.h>
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #include <objbase.h>
  20. #include <initguid.h>
  21. #include <mountdev.h>
  22. #include <setupapi.h>
  23. #include <string.h>
  24. INT
  25. DeviceAdvancedPropertiesW(HWND hwndParent, LPCTSTR MachineName, LPCTSTR DeviceId);
  26. BOOL
  27. APIENTRY
  28. DvdLauncher(HWND HWnd, CHAR DriveLetter)
  29. {
  30. BOOL status;
  31. HANDLE fileHandle;
  32. ULONG length,
  33. errorCode,
  34. returned;
  35. WCHAR string[16] = { 0 };
  36. HINSTANCE devmgrInstance;
  37. FARPROC deviceAdvancedPropertiesProc;
  38. ULONG i;
  39. PMOUNTDEV_UNIQUE_ID targetInterfaceName;
  40. ULONG targetInterfaceNameSize;
  41. HDEVINFO devInfoWithInterface;
  42. SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
  43. SP_DEVINFO_DATA devInfoWithInterfaceData;
  44. PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData;
  45. ULONG deviceInterfaceDetailDataSize;
  46. ULONG interfaceIndex;
  47. ULONG deviceInstanceIdSize;
  48. PTSTR deviceInstanceId;
  49. BOOL er;
  50. _snwprintf(string, sizeof(string) / sizeof(string[0]) - 1, L"\\\\.\\%c:", DriveLetter);
  51. fileHandle = CreateFile(string,
  52. GENERIC_READ,
  53. FILE_SHARE_READ | FILE_SHARE_WRITE,
  54. NULL,
  55. OPEN_EXISTING,
  56. 0,
  57. NULL);
  58. devmgrInstance = LoadLibrary (L"devmgr.dll");
  59. if (devmgrInstance == NULL) {
  60. er = 1;
  61. goto GetOut1;
  62. }
  63. deviceAdvancedPropertiesProc = GetProcAddress(
  64. devmgrInstance,
  65. "DeviceAdvancedPropertiesW");
  66. if (deviceAdvancedPropertiesProc == NULL) {
  67. er = 2;
  68. goto GetOut2;
  69. }
  70. er = 3;
  71. if (fileHandle != INVALID_HANDLE_VALUE) {
  72. for (i=0, targetInterfaceName=NULL, targetInterfaceNameSize=sizeof(MOUNTDEV_UNIQUE_ID); i<2; i++) {
  73. targetInterfaceName = LocalAlloc(LPTR, targetInterfaceNameSize);
  74. status = DeviceIoControl(fileHandle,
  75. IOCTL_MOUNTDEV_QUERY_UNIQUE_ID,
  76. NULL,
  77. 0,
  78. targetInterfaceName,
  79. targetInterfaceNameSize,
  80. &returned,
  81. NULL);
  82. if (!status) {
  83. GetLastError();
  84. if (returned >= sizeof(MOUNTDEV_UNIQUE_ID)) {
  85. targetInterfaceNameSize = targetInterfaceName->UniqueIdLength + sizeof(MOUNTDEV_UNIQUE_ID);
  86. }
  87. LocalFree(targetInterfaceName);
  88. targetInterfaceName = NULL;
  89. }
  90. }
  91. devInfoWithInterface = SetupDiGetClassDevs(
  92. (LPGUID) &MOUNTDEV_MOUNTED_DEVICE_GUID,
  93. NULL,
  94. NULL,
  95. DIGCF_DEVICEINTERFACE
  96. );
  97. if (devInfoWithInterface) {
  98. memset(&deviceInterfaceData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
  99. deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  100. interfaceIndex = 0;
  101. deviceInterfaceDetailData = NULL;
  102. while (SetupDiEnumDeviceInterfaces(
  103. devInfoWithInterface,
  104. NULL,
  105. (LPGUID) &MOUNTDEV_MOUNTED_DEVICE_GUID,
  106. interfaceIndex++,
  107. &deviceInterfaceData)) {
  108. for (i=deviceInterfaceDetailDataSize=0; i<2; i++) {
  109. if (deviceInterfaceDetailDataSize) {
  110. deviceInterfaceDetailData = LocalAlloc (LPTR, deviceInterfaceDetailDataSize);
  111. deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  112. } else {
  113. deviceInterfaceDetailData = NULL;
  114. }
  115. memset(&devInfoWithInterfaceData, 0, sizeof(SP_DEVINFO_DATA));
  116. devInfoWithInterfaceData.cbSize = sizeof(SP_DEVINFO_DATA);
  117. if (!SetupDiGetDeviceInterfaceDetail(
  118. devInfoWithInterface,
  119. &deviceInterfaceData,
  120. deviceInterfaceDetailData,
  121. deviceInterfaceDetailDataSize,
  122. &deviceInterfaceDetailDataSize,
  123. &devInfoWithInterfaceData)) {
  124. GetLastError();
  125. if (deviceInterfaceDetailData) {
  126. LocalFree(deviceInterfaceDetailData);
  127. deviceInterfaceDetailData = NULL;
  128. }
  129. }
  130. }
  131. if (deviceInterfaceDetailData) {
  132. PMOUNTDEV_UNIQUE_ID interfaceName;
  133. ULONG interfaceNameSize;
  134. HANDLE fileHandle;
  135. fileHandle = CreateFile(deviceInterfaceDetailData->DevicePath,
  136. GENERIC_READ,
  137. FILE_SHARE_READ | FILE_SHARE_WRITE,
  138. NULL,
  139. OPEN_EXISTING,
  140. 0,
  141. NULL);
  142. if (fileHandle != INVALID_HANDLE_VALUE) {
  143. for (i=0, interfaceName=NULL, interfaceNameSize=sizeof(MOUNTDEV_UNIQUE_ID); i<2; i++) {
  144. interfaceName = LocalAlloc (LPTR, interfaceNameSize);
  145. if (!DeviceIoControl(fileHandle,
  146. IOCTL_MOUNTDEV_QUERY_UNIQUE_ID,
  147. NULL,
  148. 0,
  149. interfaceName,
  150. interfaceNameSize,
  151. &returned,
  152. FALSE)) {
  153. GetLastError();
  154. if (returned >= sizeof(MOUNTDEV_UNIQUE_ID)) {
  155. interfaceNameSize = interfaceName->UniqueIdLength + sizeof(MOUNTDEV_UNIQUE_ID);
  156. }
  157. LocalFree(interfaceName);
  158. interfaceName = NULL;
  159. }
  160. }
  161. if (interfaceName) {
  162. if (!wcscmp((PTSTR)targetInterfaceName->UniqueId,
  163. (PTSTR)interfaceName->UniqueId)) {
  164. LocalFree(interfaceName);
  165. break;
  166. }
  167. LocalFree(interfaceName);
  168. }
  169. }
  170. LocalFree (deviceInterfaceDetailData);
  171. deviceInterfaceDetailData = NULL;
  172. }
  173. memset(&deviceInterfaceData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
  174. deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  175. }
  176. if (deviceInterfaceDetailData) {
  177. for (i=deviceInstanceIdSize=0; i<2; i++) {
  178. if (deviceInstanceIdSize) {
  179. deviceInstanceId = LocalAlloc (LPTR, deviceInstanceIdSize * sizeof(WCHAR));
  180. } else {
  181. deviceInstanceId = NULL;
  182. }
  183. if (!SetupDiGetDeviceInstanceId(
  184. devInfoWithInterface,
  185. &devInfoWithInterfaceData,
  186. deviceInstanceId,
  187. deviceInstanceIdSize,
  188. &deviceInstanceIdSize
  189. )) {
  190. GetLastError();
  191. if (deviceInstanceId) {
  192. LocalFree (deviceInstanceId);
  193. deviceInstanceId = NULL;
  194. }
  195. }
  196. }
  197. if (deviceInstanceId) {
  198. DVD_REGION regionData;
  199. status = (BOOL) deviceAdvancedPropertiesProc(
  200. HWnd,
  201. NULL,
  202. deviceInstanceId
  203. );
  204. memset(&regionData, 0, sizeof(DVD_REGION));
  205. status = DeviceIoControl(fileHandle,
  206. IOCTL_DVD_GET_REGION,
  207. NULL,
  208. 0,
  209. &regionData,
  210. sizeof(DVD_REGION),
  211. &returned,
  212. NULL);
  213. if (status && (returned == sizeof(DVD_REGION))) {
  214. if (~regionData.RegionData & regionData.SystemRegion) {
  215. //
  216. // region codes agree
  217. //
  218. er = 0;
  219. }
  220. }
  221. }
  222. }
  223. SetupDiDestroyDeviceInfoList(
  224. devInfoWithInterface
  225. );
  226. }
  227. }
  228. GetOut2:
  229. FreeLibrary(devmgrInstance);
  230. GetOut1:
  231. return er == 0? TRUE: FALSE;
  232. }