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.

373 lines
9.7 KiB

  1. #include "shellprv.h"
  2. #include "ids.h"
  3. #include "hwcmmn.h"
  4. #include "mtptl.h"
  5. HRESULT _GetAutoplayHandler(LPCWSTR pszDeviceID, LPCWSTR pszEventType,
  6. LPCWSTR pszContentTypeHandler, IAutoplayHandler** ppiah)
  7. {
  8. HRESULT hr = CoCreateInstance(CLSID_HWEventSettings, NULL,
  9. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG, IID_PPV_ARG(IAutoplayHandler, ppiah));
  10. if (SUCCEEDED(hr))
  11. {
  12. hr = (*ppiah)->InitWithContent(pszDeviceID, pszEventType,
  13. pszContentTypeHandler);
  14. if (SUCCEEDED(hr))
  15. {
  16. hr = CoSetProxyBlanket((*ppiah),
  17. RPC_C_AUTHN_WINNT,
  18. RPC_C_AUTHZ_NONE,
  19. NULL,
  20. RPC_C_AUTHN_LEVEL_CALL,
  21. RPC_C_IMP_LEVEL_IMPERSONATE,
  22. NULL,
  23. EOAC_NONE
  24. );
  25. }
  26. if (FAILED(hr))
  27. {
  28. (*ppiah)->Release();
  29. *ppiah = NULL;
  30. }
  31. }
  32. return hr;
  33. }
  34. HRESULT _GetAutoplayHandlerNoContent(LPCWSTR pszDeviceID, LPCWSTR pszEventType,
  35. IAutoplayHandler** ppiah)
  36. {
  37. HRESULT hr = CoCreateInstance(CLSID_HWEventSettings, NULL,
  38. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG, IID_PPV_ARG(IAutoplayHandler, ppiah));
  39. if (SUCCEEDED(hr))
  40. {
  41. hr = (*ppiah)->Init(pszDeviceID, pszEventType);
  42. if (SUCCEEDED(hr))
  43. {
  44. hr = CoSetProxyBlanket((*ppiah),
  45. RPC_C_AUTHN_WINNT,
  46. RPC_C_AUTHZ_NONE,
  47. NULL,
  48. RPC_C_AUTHN_LEVEL_CALL,
  49. RPC_C_IMP_LEVEL_IMPERSONATE,
  50. NULL,
  51. EOAC_NONE
  52. );
  53. }
  54. if (FAILED(hr))
  55. {
  56. (*ppiah)->Release();
  57. *ppiah = NULL;
  58. }
  59. }
  60. return hr;
  61. }
  62. HRESULT _GetHWDevice(LPCWSTR pszDeviceID, IHWDevice** ppihwdevice)
  63. {
  64. HRESULT hr = CoCreateInstance(CLSID_HWDevice, NULL,
  65. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG, IID_PPV_ARG(IHWDevice, ppihwdevice));
  66. if (SUCCEEDED(hr))
  67. {
  68. hr = (*ppihwdevice)->Init(pszDeviceID);
  69. if (FAILED(hr))
  70. {
  71. (*ppihwdevice)->Release();
  72. *ppihwdevice = NULL;
  73. }
  74. }
  75. return hr;
  76. }
  77. HICON _GetIconFromIconLocation(LPCWSTR pszIconLocation, BOOL fBigIcon)
  78. {
  79. WCHAR szIconLocation[MAX_PATH + 12];
  80. HIMAGELIST himagelist;
  81. int iImage;
  82. Shell_GetImageLists(fBigIcon ? &himagelist : NULL, fBigIcon ? NULL : &himagelist);
  83. lstrcpyn(szIconLocation, pszIconLocation, ARRAYSIZE(szIconLocation));
  84. iImage = Shell_GetCachedImageIndex(szIconLocation,
  85. PathParseIconLocation(szIconLocation), 0);
  86. return ImageList_GetIcon(himagelist, iImage, ILD_TRANSPARENT);
  87. }
  88. HRESULT _GetHardwareDevices(IHardwareDevices** ppihwdevices)
  89. {
  90. return CoCreateInstance(CLSID_HardwareDevices, NULL,
  91. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG, IID_PPV_ARG(IHardwareDevices, ppihwdevices));
  92. }
  93. BOOL IsShellServiceRunning()
  94. {
  95. BOOL fRunning = FALSE;
  96. SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
  97. if (hSCM)
  98. {
  99. SC_HANDLE hService = OpenService(hSCM, TEXT("ShellHWDetection"),
  100. SERVICE_INTERROGATE);
  101. if (hService)
  102. {
  103. SERVICE_STATUS ss;
  104. if (ControlService(hService, SERVICE_CONTROL_INTERROGATE, &ss))
  105. {
  106. if (SERVICE_RUNNING == ss.dwCurrentState)
  107. {
  108. fRunning = TRUE;
  109. }
  110. }
  111. CloseServiceHandle(hService);
  112. }
  113. CloseServiceHandle(hSCM);
  114. }
  115. return fRunning;
  116. }
  117. STDAPI GetDeviceProperties(LPCWSTR pszDeviceID, IHWDeviceCustomProperties **ppdcp)
  118. {
  119. HRESULT hr = CoCreateInstance(CLSID_HWDeviceCustomProperties, NULL,
  120. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG, IID_PPV_ARG(IHWDeviceCustomProperties, ppdcp));
  121. if (SUCCEEDED(hr))
  122. {
  123. BOOL fVolumeFlag = TRUE;
  124. const TCHAR cszDeviceHeader[] = TEXT("\\\\?\\");
  125. if (!StrCmpN(cszDeviceHeader, pszDeviceID, ARRAYSIZE(cszDeviceHeader) - 1))
  126. {
  127. fVolumeFlag = CMtPtLocal::IsVolume(pszDeviceID);
  128. }
  129. hr = (*ppdcp)->InitFromDeviceID(pszDeviceID, fVolumeFlag ? HWDEVCUSTOMPROP_USEVOLUMEPROCESSING : 0);
  130. if (FAILED(hr))
  131. {
  132. (*ppdcp)->Release();
  133. *ppdcp = NULL;
  134. }
  135. }
  136. return hr;
  137. }
  138. struct CONTENTTYPEINFO
  139. {
  140. DWORD dwContentType;
  141. LPCWSTR pszContentTypeHandler;
  142. int iContentTypeFriendlyName;
  143. LPCWSTR pszContentTypeIconLocation;
  144. };
  145. static const CONTENTTYPEINFO contenttypeinfo[] =
  146. {
  147. { CT_CDAUDIO , TEXT("CDAudioContentHandler"), IDS_AP_CDAUDIOCONTENTHANDLER,
  148. TEXT("%SystemRoot%\\system32\\shell32.dll,-228") },
  149. { CT_DVDMOVIE , TEXT("DVDMovieContentHandler"), IDS_AP_DVDMOVIECONTENTHANDLER,
  150. TEXT("%SystemRoot%\\system32\\shell32.dll,-222") },
  151. { CT_BLANKCDRW , TEXT("BlankCDContentHandler"), IDS_AP_BLANKCDCONTENTHANDLER,
  152. TEXT("%SystemRoot%\\system32\\shell32.dll,-260") },
  153. { CT_BLANKCDR , TEXT("BlankCDContentHandler"), IDS_AP_BLANKCDCONTENTHANDLER,
  154. TEXT("%SystemRoot%\\system32\\shell32.dll,-260") },
  155. { CT_AUTOPLAYMUSIC , TEXT("MusicFilesContentHandler"), IDS_AP_MUSICFILESCONTENTHANDLER,
  156. TEXT("%SystemRoot%\\system32\\shell32.dll,-225") },
  157. { CT_AUTOPLAYPIX , TEXT("PicturesContentHandler"), IDS_AP_PICTURESCONTENTHANDLER,
  158. TEXT("%SystemRoot%\\system32\\shimgvw.dll,3") },
  159. { CT_AUTOPLAYMOVIE , TEXT("VideoFilesContentHandler"), IDS_AP_VIDEOFILESCONTENTHANDLER,
  160. TEXT("%SystemRoot%\\system32\\shell32.dll,-224") },
  161. { CT_AUTOPLAYMIXEDCONTENT, TEXT("MixedContentHandler"), IDS_AP_MIXEDCONTENTCONTENTHANDLER,
  162. TEXT("%SystemRoot%\\system32\\shell32.dll,-227") },
  163. { CT_AUTORUNINF , NULL, 0, NULL },
  164. { CT_UNKNOWNCONTENT , NULL, 0, NULL },
  165. { CT_BLANKDVDR , NULL, 0, NULL },
  166. { CT_BLANKDVDRW , NULL, 0, NULL },
  167. };
  168. HRESULT _GetContentTypeInfo(DWORD dwContentType, LPWSTR pszContentTypeFriendlyName,
  169. DWORD cchContentTypeFriendlyName, LPWSTR pszContentTypeIconLocation,
  170. DWORD cchContentTypeIconLocation)
  171. {
  172. HRESULT hr = E_FAIL;
  173. for (DWORD dw = 0; dw < ARRAYSIZE(contenttypeinfo); ++dw)
  174. {
  175. //
  176. // First matching entry wins.
  177. //
  178. if (contenttypeinfo[dw].dwContentType & dwContentType)
  179. {
  180. ASSERT(contenttypeinfo[dw].pszContentTypeHandler);
  181. if (!LoadString(g_hinst, contenttypeinfo[dw].iContentTypeFriendlyName,
  182. pszContentTypeFriendlyName, cchContentTypeFriendlyName))
  183. {
  184. *pszContentTypeFriendlyName = 0;
  185. }
  186. StrCpyN(pszContentTypeIconLocation, contenttypeinfo[dw].pszContentTypeIconLocation,
  187. cchContentTypeIconLocation);
  188. hr = S_OK;
  189. break;
  190. }
  191. }
  192. return hr;
  193. }
  194. BOOL IsMixedContent(DWORD dwContentType)
  195. {
  196. BOOL fRet;
  197. switch (CT_ANYAUTOPLAYCONTENT & dwContentType)
  198. {
  199. case 0:
  200. case CT_AUTOPLAYMUSIC:
  201. case CT_AUTOPLAYPIX:
  202. case CT_AUTOPLAYMOVIE:
  203. fRet = FALSE;
  204. break;
  205. default:
  206. fRet = TRUE;
  207. break;
  208. }
  209. return fRet;
  210. }
  211. HRESULT _GetContentTypeHandler(DWORD dwContentType, LPWSTR pszContentTypeHandler,
  212. DWORD cchContentTypeHandler)
  213. {
  214. HRESULT hr = E_FAIL;
  215. if (IsMixedContent(dwContentType))
  216. {
  217. StrCpyN(pszContentTypeHandler, TEXT("MixedContentHandler"), cchContentTypeHandler);
  218. hr = S_OK;
  219. }
  220. else
  221. {
  222. for (DWORD dw = 0; dw < ARRAYSIZE(contenttypeinfo); ++dw)
  223. {
  224. //
  225. // First matching entry wins.
  226. //
  227. if (contenttypeinfo[dw].dwContentType & dwContentType)
  228. {
  229. StrCpyN(pszContentTypeHandler, contenttypeinfo[dw].pszContentTypeHandler,
  230. cchContentTypeHandler);
  231. hr = S_OK;
  232. break;
  233. }
  234. }
  235. }
  236. return hr;
  237. }
  238. HRESULT _GetHandlerInvokeProgIDAndVerb(LPCWSTR pszHandler, LPWSTR pszInvokeProgID,
  239. DWORD cchInvokeProgID, LPWSTR pszInvokeVerb, DWORD cchInvokeVerb)
  240. {
  241. IAutoplayHandlerProperties* pahp;
  242. HRESULT hr = CoCreateInstance(CLSID_AutoplayHandlerProperties, NULL,
  243. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG,
  244. IID_PPV_ARG(IAutoplayHandlerProperties, &pahp));
  245. if (SUCCEEDED(hr))
  246. {
  247. hr = pahp->Init(pszHandler);
  248. if (SUCCEEDED(hr))
  249. {
  250. LPWSTR pszInvokeProgIDLocal;
  251. LPWSTR pszInvokeVerbLocal;
  252. hr = pahp->GetInvokeProgIDAndVerb(&pszInvokeProgIDLocal, &pszInvokeVerbLocal);
  253. if (SUCCEEDED(hr))
  254. {
  255. lstrcpyn(pszInvokeProgID, pszInvokeProgIDLocal, cchInvokeProgID);
  256. lstrcpyn(pszInvokeVerb, pszInvokeVerbLocal, cchInvokeVerb);
  257. CoTaskMemFree(pszInvokeProgIDLocal);
  258. CoTaskMemFree(pszInvokeVerbLocal);
  259. }
  260. }
  261. pahp->Release();
  262. }
  263. return hr;
  264. }
  265. //
  266. CCrossThreadFlag::~CCrossThreadFlag()
  267. {
  268. TraceMsg(TF_MOUNTPOINT, "AUTOPLAY: CCrossThreadFlag::~CCrossThreadFlag called");
  269. if (_hEvent)
  270. {
  271. CloseHandle(_hEvent);
  272. }
  273. }
  274. BOOL CCrossThreadFlag::Init()
  275. {
  276. ASSERT(!_fInited);
  277. _hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  278. #ifdef DEBUG
  279. if (_hEvent)
  280. {
  281. _fInited = TRUE;
  282. }
  283. #endif
  284. return !!_hEvent;
  285. }
  286. BOOL CCrossThreadFlag::Signal()
  287. {
  288. ASSERT(_fInited);
  289. TraceMsg(TF_MOUNTPOINT, "AUTOPLAY: CCrossThreadFlag::Signal called");
  290. return SetEvent(_hEvent);
  291. }
  292. BOOL CCrossThreadFlag::IsSignaled()
  293. {
  294. ASSERT(_fInited);
  295. BOOL fRet = (WAIT_OBJECT_0 == WaitForSingleObject(_hEvent, 0));
  296. TraceMsg(TF_MOUNTPOINT, "AUTOPLAY: CCrossThreadFlag::IsSignaled called: %d", fRet);
  297. return fRet;
  298. }