Source code of Windows XP (NT5)
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.

355 lines
11 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. HWND GetConsoleHwnd(void)
  25. {
  26. #define MY_BUFSIZE 1024 // buffer size for console window titles
  27. HWND hwndFound; // this is what is returned to the caller
  28. WCHAR pszNewWindowTitle[MY_BUFSIZE]; // contains fabricated WindowTitle
  29. WCHAR pszOldWindowTitle[MY_BUFSIZE]; // contains original WindowTitle
  30. // fetch current window title
  31. GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
  32. // format a "unique" NewWindowTitle
  33. wsprintf(pszNewWindowTitle,L"%d/%d",
  34. GetTickCount(),
  35. GetCurrentProcessId());
  36. // change current window title
  37. SetConsoleTitle(pszNewWindowTitle);
  38. // ensure window title has been updated
  39. Sleep(40);
  40. // look for NewWindowTitle
  41. hwndFound=FindWindow(NULL, pszNewWindowTitle);
  42. // restore original window title
  43. SetConsoleTitle(pszOldWindowTitle);
  44. return(hwndFound);
  45. }
  46. int
  47. DeviceAdvancedPropertiesW(
  48. HWND hwndParent,
  49. LPCTSTR MachineName,
  50. LPCTSTR DeviceId
  51. );
  52. BOOL
  53. APIENTRY
  54. DvdLauncher(
  55. HWND HWnd,
  56. CHAR DriveLetter
  57. )
  58. {
  59. BOOL status;
  60. DWORD accessMode,
  61. shareMode;
  62. HANDLE fileHandle;
  63. ULONG length,
  64. errorCode,
  65. returned;
  66. WCHAR string[100];
  67. HINSTANCE devmgrInstance;
  68. FARPROC deviceAdvancedPropertiesProc;
  69. ULONG i;
  70. PMOUNTDEV_UNIQUE_ID targetInterfaceName;
  71. ULONG targetInterfaceNameSize;
  72. HDEVINFO devInfoWithInterface;
  73. SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
  74. SP_DEVINFO_DATA devInfoWithInterfaceData;
  75. PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData;
  76. ULONG deviceInterfaceDetailDataSize;
  77. ULONG interfaceIndex;
  78. ULONG deviceInstanceIdSize;
  79. PTSTR deviceInstanceId;
  80. BOOL er;
  81. wsprintf (string, L"\\\\.\\%c:", DriveLetter);
  82. shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; // default
  83. accessMode = GENERIC_READ; // default
  84. fileHandle = CreateFile(string,
  85. accessMode,
  86. shareMode,
  87. NULL,
  88. OPEN_EXISTING,
  89. 0,
  90. NULL);
  91. devmgrInstance = LoadLibrary (L"devmgr.dll");
  92. if (devmgrInstance == NULL) {
  93. er = 1;
  94. goto GetOut1;
  95. }
  96. deviceAdvancedPropertiesProc = GetProcAddress(
  97. devmgrInstance,
  98. "DeviceAdvancedPropertiesW");
  99. if (deviceAdvancedPropertiesProc == NULL) {
  100. er = 2;
  101. goto GetOut2;
  102. }
  103. er = 3;
  104. if (fileHandle != INVALID_HANDLE_VALUE) {
  105. for (i=0, targetInterfaceName=NULL, targetInterfaceNameSize=sizeof(MOUNTDEV_UNIQUE_ID); i<2; i++) {
  106. targetInterfaceName = LocalAlloc (LPTR, targetInterfaceNameSize);
  107. status = DeviceIoControl(fileHandle,
  108. IOCTL_MOUNTDEV_QUERY_UNIQUE_ID,
  109. NULL,
  110. 0,
  111. targetInterfaceName,
  112. targetInterfaceNameSize,
  113. &returned,
  114. NULL);
  115. if (!status) {
  116. GetLastError();
  117. if (returned >= sizeof(MOUNTDEV_UNIQUE_ID)) {
  118. targetInterfaceNameSize = targetInterfaceName->UniqueIdLength + sizeof(MOUNTDEV_UNIQUE_ID);
  119. }
  120. LocalFree(targetInterfaceName);
  121. targetInterfaceName = NULL;
  122. }
  123. }
  124. devInfoWithInterface = SetupDiGetClassDevs(
  125. (LPGUID) &MOUNTDEV_MOUNTED_DEVICE_GUID,
  126. NULL,
  127. NULL,
  128. DIGCF_DEVICEINTERFACE
  129. );
  130. if (devInfoWithInterface) {
  131. memset(&deviceInterfaceData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
  132. deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  133. interfaceIndex = 0;
  134. deviceInterfaceDetailData = NULL;
  135. while (SetupDiEnumDeviceInterfaces(
  136. devInfoWithInterface,
  137. NULL,
  138. (LPGUID) &MOUNTDEV_MOUNTED_DEVICE_GUID,
  139. interfaceIndex++,
  140. &deviceInterfaceData)) {
  141. for (i=deviceInterfaceDetailDataSize=0; i<2; i++) {
  142. if (deviceInterfaceDetailDataSize) {
  143. deviceInterfaceDetailData = LocalAlloc (LPTR, deviceInterfaceDetailDataSize);
  144. deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  145. } else {
  146. deviceInterfaceDetailData = NULL;
  147. }
  148. memset(&devInfoWithInterfaceData, 0, sizeof(SP_DEVINFO_DATA));
  149. devInfoWithInterfaceData.cbSize = sizeof(SP_DEVINFO_DATA);
  150. if (!SetupDiGetDeviceInterfaceDetail(
  151. devInfoWithInterface,
  152. &deviceInterfaceData,
  153. deviceInterfaceDetailData,
  154. deviceInterfaceDetailDataSize,
  155. &deviceInterfaceDetailDataSize,
  156. &devInfoWithInterfaceData)) {
  157. GetLastError();
  158. if (deviceInterfaceDetailData) {
  159. LocalFree(deviceInterfaceDetailData);
  160. deviceInterfaceDetailData = NULL;
  161. }
  162. }
  163. }
  164. if (deviceInterfaceDetailData) {
  165. PMOUNTDEV_UNIQUE_ID interfaceName;
  166. ULONG interfaceNameSize;
  167. HANDLE fileHandle;
  168. ULONG i;
  169. fileHandle = CreateFile(deviceInterfaceDetailData->DevicePath,
  170. accessMode,
  171. shareMode,
  172. NULL,
  173. OPEN_EXISTING,
  174. 0,
  175. NULL);
  176. if (fileHandle != INVALID_HANDLE_VALUE) {
  177. for (i=0, interfaceName=NULL, interfaceNameSize=sizeof(MOUNTDEV_UNIQUE_ID); i<2; i++) {
  178. interfaceName = LocalAlloc (LPTR, interfaceNameSize);
  179. if (!DeviceIoControl(fileHandle,
  180. IOCTL_MOUNTDEV_QUERY_UNIQUE_ID,
  181. NULL,
  182. 0,
  183. interfaceName,
  184. interfaceNameSize,
  185. &returned,
  186. FALSE)) {
  187. GetLastError();
  188. if (returned >= sizeof(MOUNTDEV_UNIQUE_ID)) {
  189. interfaceNameSize = interfaceName->UniqueIdLength + sizeof(MOUNTDEV_UNIQUE_ID);
  190. }
  191. LocalFree(interfaceName);
  192. interfaceName = NULL;
  193. }
  194. }
  195. if (interfaceName) {
  196. if (!wcscmp((PTSTR)targetInterfaceName->UniqueId,
  197. (PTSTR)interfaceName->UniqueId)) {
  198. LocalFree(interfaceName);
  199. break;
  200. }
  201. LocalFree(interfaceName);
  202. }
  203. }
  204. LocalFree (deviceInterfaceDetailData);
  205. deviceInterfaceDetailData = NULL;
  206. }
  207. memset(&deviceInterfaceData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
  208. deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  209. }
  210. if (deviceInterfaceDetailData) {
  211. for (i=deviceInstanceIdSize=0; i<2; i++) {
  212. if (deviceInstanceIdSize) {
  213. deviceInstanceId = LocalAlloc (LPTR, deviceInstanceIdSize * sizeof(WCHAR));
  214. } else {
  215. deviceInstanceId = NULL;
  216. }
  217. if (!SetupDiGetDeviceInstanceId(
  218. devInfoWithInterface,
  219. &devInfoWithInterfaceData,
  220. deviceInstanceId,
  221. deviceInstanceIdSize,
  222. &deviceInstanceIdSize
  223. )) {
  224. GetLastError();
  225. if (deviceInstanceId) {
  226. LocalFree (deviceInstanceId);
  227. deviceInstanceId = NULL;
  228. }
  229. }
  230. }
  231. if (deviceInstanceId) {
  232. DVD_REGION regionData;
  233. status = (BOOL) deviceAdvancedPropertiesProc(
  234. HWnd,
  235. NULL,
  236. deviceInstanceId
  237. );
  238. memset(&regionData, 0, sizeof(DVD_REGION));
  239. status = DeviceIoControl(fileHandle,
  240. IOCTL_DVD_GET_REGION,
  241. NULL,
  242. 0,
  243. &regionData,
  244. sizeof(DVD_REGION),
  245. &returned,
  246. NULL);
  247. if (status && (returned == sizeof(DVD_REGION))) {
  248. if (~regionData.RegionData & regionData.SystemRegion) {
  249. //
  250. // region codes agree
  251. //
  252. er = 0;
  253. }
  254. }
  255. }
  256. }
  257. SetupDiDestroyDeviceInfoList(
  258. devInfoWithInterface
  259. );
  260. }
  261. }
  262. GetOut2:
  263. FreeLibrary(devmgrInstance);
  264. GetOut1:
  265. return er == 0? TRUE: FALSE;
  266. }