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.

371 lines
10 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. StringCchCopy(szIconLocation, ARRAYSIZE(szIconLocation), pszIconLocation);
  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. hr = StringCchCopy(pszContentTypeIconLocation, cchContentTypeIconLocation,
  187. contenttypeinfo[dw].pszContentTypeIconLocation);
  188. break;
  189. }
  190. }
  191. return hr;
  192. }
  193. BOOL IsMixedContent(DWORD dwContentType)
  194. {
  195. BOOL fRet;
  196. switch (CT_ANYAUTOPLAYCONTENT & dwContentType)
  197. {
  198. case 0:
  199. case CT_AUTOPLAYMUSIC:
  200. case CT_AUTOPLAYPIX:
  201. case CT_AUTOPLAYMOVIE:
  202. fRet = FALSE;
  203. break;
  204. default:
  205. fRet = TRUE;
  206. break;
  207. }
  208. return fRet;
  209. }
  210. HRESULT _GetContentTypeHandler(DWORD dwContentType, LPWSTR pszContentTypeHandler,
  211. DWORD cchContentTypeHandler)
  212. {
  213. HRESULT hr = E_FAIL;
  214. if (IsMixedContent(dwContentType))
  215. {
  216. hr = StringCchCopy(pszContentTypeHandler, cchContentTypeHandler, TEXT("MixedContentHandler"));
  217. }
  218. else
  219. {
  220. for (DWORD dw = 0; dw < ARRAYSIZE(contenttypeinfo); ++dw)
  221. {
  222. //
  223. // First matching entry wins.
  224. //
  225. if (contenttypeinfo[dw].dwContentType & dwContentType)
  226. {
  227. hr = StringCchCopy(pszContentTypeHandler, cchContentTypeHandler,
  228. contenttypeinfo[dw].pszContentTypeHandler);
  229. break;
  230. }
  231. }
  232. }
  233. return hr;
  234. }
  235. HRESULT _GetHandlerInvokeProgIDAndVerb(LPCWSTR pszHandler, LPWSTR pszInvokeProgID,
  236. DWORD cchInvokeProgID, LPWSTR pszInvokeVerb, DWORD cchInvokeVerb)
  237. {
  238. IAutoplayHandlerProperties* pahp;
  239. HRESULT hr = CoCreateInstance(CLSID_AutoplayHandlerProperties, NULL,
  240. CLSCTX_LOCAL_SERVER | CLSCTX_NO_FAILURE_LOG,
  241. IID_PPV_ARG(IAutoplayHandlerProperties, &pahp));
  242. if (SUCCEEDED(hr))
  243. {
  244. hr = pahp->Init(pszHandler);
  245. if (SUCCEEDED(hr))
  246. {
  247. LPWSTR pszInvokeProgIDLocal;
  248. LPWSTR pszInvokeVerbLocal;
  249. hr = pahp->GetInvokeProgIDAndVerb(&pszInvokeProgIDLocal, &pszInvokeVerbLocal);
  250. if (SUCCEEDED(hr))
  251. {
  252. hr = StringCchCopy(pszInvokeProgID, cchInvokeProgID, pszInvokeProgIDLocal);
  253. if (SUCCEEDED(hr))
  254. {
  255. hr = StringCchCopy(pszInvokeVerb, cchInvokeVerb, pszInvokeVerbLocal);
  256. }
  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. }