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.

409 lines
9.5 KiB

  1. //
  2. // Microsoft Windows Media Technologies
  3. // Copyright (C) Microsoft Corporation, 1999 - 2001. All rights reserved.
  4. //
  5. // MSHDSP.DLL is a sample WMDM Service Provider(SP) that enumerates fixed drives.
  6. // This sample shows you how to implement an SP according to the WMDM documentation.
  7. // This sample uses fixed drives on your PC to emulate portable media, and
  8. // shows the relationship between different interfaces and objects. Each hard disk
  9. // volume is enumerated as a device and directories and files are enumerated as
  10. // Storage objects under respective devices. You can copy non-SDMI compliant content
  11. // to any device that this SP enumerates. To copy an SDMI compliant content to a
  12. // device, the device must be able to report a hardware embedded serial number.
  13. // Hard disks do not have such serial numbers.
  14. //
  15. // To build this SP, you are recommended to use the MSHDSP.DSP file under Microsoft
  16. // Visual C++ 6.0 and run REGSVR32.EXE to register the resulting MSHDSP.DLL. You can
  17. // then build the sample application from the WMDMAPP directory to see how it gets
  18. // loaded by the application. However, you need to obtain a certificate from
  19. // Microsoft to actually run this SP. This certificate would be in the KEY.C file
  20. // under the INCLUDE directory for one level up.
  21. // MDSPStorageGlobals.cpp : Implementation of CMDSPStorageGlobals
  22. #include "hdspPCH.h"
  23. #include "strsafe.h"
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CMDSPStorageGlobals
  26. CMDSPStorageGlobals::~CMDSPStorageGlobals()
  27. {
  28. if( m_pMDSPDevice != NULL )
  29. {
  30. m_pMDSPDevice->Release();
  31. }
  32. for(int i=0; i<MDSP_MAX_DEVICE_OBJ;i++)
  33. {
  34. if( !wcscmp(g_GlobalDeviceInfo[i].wcsDevName, m_wcsName) )
  35. {
  36. g_GlobalDeviceInfo[i].pIMDSPStorageGlobals = NULL;
  37. }
  38. }
  39. }
  40. STDMETHODIMP CMDSPStorageGlobals::GetCapabilities(DWORD * pdwCapabilities)
  41. {
  42. HRESULT hr = S_OK;
  43. CFRg(g_pAppSCServer);
  44. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  45. {
  46. CORg(WMDM_E_NOTCERTIFIED);
  47. }
  48. CARg(pdwCapabilities);
  49. *pdwCapabilities = 0;
  50. *pdwCapabilities = WMDM_STORAGECAP_FOLDERSINROOT |
  51. WMDM_STORAGECAP_FILESINROOT |
  52. WMDM_STORAGECAP_FOLDERSINFOLDERS |
  53. WMDM_STORAGECAP_FILESINFOLDERS ;
  54. Error:
  55. hrLogDWORD("IMDSPStorageGlobals::GetCapabilities returned 0x%08lx", hr, hr);
  56. return hr;
  57. }
  58. STDMETHODIMP CMDSPStorageGlobals::GetSerialNumber(
  59. PWMDMID pSerialNum,
  60. BYTE abMac[WMDM_MAC_LENGTH])
  61. {
  62. HRESULT hr;
  63. CFRg(g_pAppSCServer);
  64. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  65. {
  66. CORg(WMDM_E_NOTCERTIFIED);
  67. }
  68. CARg(pSerialNum);
  69. IMDSPDevice *pDev; // For PM SP, device is the same as StorageGlobals
  70. CHRg(GetDevice(&pDev));
  71. hr = UtilGetSerialNumber(m_wcsName, pSerialNum, FALSE);
  72. pDev->Release();
  73. if( hr == HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) )
  74. {
  75. hr = WMDM_E_NOTSUPPORTED;
  76. }
  77. if( hr == S_OK )
  78. {
  79. // MAC the parameters
  80. HMAC hMAC;
  81. CORg(g_pAppSCServer->MACInit(&hMAC));
  82. CORg(g_pAppSCServer->MACUpdate(hMAC, (BYTE*)(pSerialNum), sizeof(WMDMID)));
  83. CORg(g_pAppSCServer->MACFinal(hMAC, abMac));
  84. }
  85. Error:
  86. hrLogDWORD("IMDSPStorageGlobals::GetSerialNumber returned 0x%08lx", hr, hr);
  87. return hr;
  88. }
  89. STDMETHODIMP CMDSPStorageGlobals::GetTotalSize(DWORD * pdwTotalSizeLow, DWORD * pdwTotalSizeHigh)
  90. {
  91. HRESULT hr = S_OK;
  92. char pszDrive[32];
  93. DWORD dwSectPerClust;
  94. DWORD dwBytesPerSect;
  95. DWORD dwFreeClusters;
  96. DWORD dwTotalClusters;
  97. CFRg(g_pAppSCServer);
  98. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  99. {
  100. CORg(WMDM_E_NOTCERTIFIED);
  101. }
  102. CARg(pdwTotalSizeLow);
  103. CARg(pdwTotalSizeHigh);
  104. WideCharToMultiByte(CP_ACP, NULL, m_wcsName, -1, pszDrive, 32, NULL, NULL);
  105. if( GetDiskFreeSpace(
  106. pszDrive,
  107. &dwSectPerClust, &dwBytesPerSect,
  108. &dwFreeClusters, &dwTotalClusters))
  109. {
  110. ULARGE_INTEGER i64TotalBytes;
  111. i64TotalBytes.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwTotalClusters);
  112. *pdwTotalSizeLow = i64TotalBytes.LowPart;
  113. *pdwTotalSizeHigh = i64TotalBytes.HighPart;
  114. }
  115. else
  116. {
  117. ULARGE_INTEGER uliFree;
  118. ULARGE_INTEGER uliTotal;
  119. CFRg( GetDiskFreeSpaceEx(
  120. pszDrive,
  121. &uliFree,
  122. &uliTotal,
  123. NULL)
  124. );
  125. *pdwTotalSizeLow = uliTotal.LowPart;
  126. *pdwTotalSizeHigh = uliTotal.HighPart;
  127. }
  128. Error:
  129. hrLogDWORD("IMDSPStorageGlobals::GetTotalFree returned 0x%08lx", hr, hr);
  130. return hr;
  131. }
  132. STDMETHODIMP CMDSPStorageGlobals::GetTotalFree(DWORD * pdwFreeLow, DWORD * pdwFreeHigh)
  133. {
  134. HRESULT hr = S_OK;
  135. char pszDrive[32];
  136. DWORD dwSectPerClust;
  137. DWORD dwBytesPerSect;
  138. DWORD dwFreeClusters;
  139. DWORD dwTotalClusters;
  140. CFRg(g_pAppSCServer);
  141. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  142. {
  143. CORg(WMDM_E_NOTCERTIFIED);
  144. }
  145. CARg(pdwFreeLow);
  146. CARg(pdwFreeHigh);
  147. WideCharToMultiByte(CP_ACP, NULL, m_wcsName, -1, pszDrive, 32, NULL, NULL);
  148. if( GetDiskFreeSpace(
  149. pszDrive,
  150. &dwSectPerClust, &dwBytesPerSect,
  151. &dwFreeClusters, &dwTotalClusters))
  152. {
  153. ULARGE_INTEGER i64FreeBytesToCaller;
  154. i64FreeBytesToCaller.QuadPart = UInt32x32To64(dwBytesPerSect, dwSectPerClust*dwFreeClusters);
  155. *pdwFreeLow = i64FreeBytesToCaller.LowPart;
  156. *pdwFreeHigh = i64FreeBytesToCaller.HighPart;
  157. }
  158. else
  159. {
  160. ULARGE_INTEGER uliFree;
  161. ULARGE_INTEGER uliTotal;
  162. CFRg( GetDiskFreeSpaceEx(
  163. pszDrive,
  164. &uliFree,
  165. &uliTotal,
  166. NULL)
  167. );
  168. *pdwFreeLow = uliFree.LowPart;
  169. *pdwFreeHigh = uliFree.HighPart;
  170. }
  171. Error:
  172. hrLogDWORD("IMDSPStorageGlobals::GetTotalFree returned 0x%08lx", hr, hr);
  173. return hr;
  174. }
  175. STDMETHODIMP CMDSPStorageGlobals::GetTotalBad(DWORD * pdwBadLow, DWORD * pdwBadHigh)
  176. {
  177. HRESULT hr;
  178. CFRg(g_pAppSCServer);
  179. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  180. {
  181. CORg(WMDM_E_NOTCERTIFIED);
  182. }
  183. CORg(WMDM_E_NOTSUPPORTED);
  184. Error:
  185. hrLogDWORD("IMDSPStorageGlobals::GetTotalBad returned 0x%08lx", hr, hr);
  186. return hr;
  187. }
  188. STDMETHODIMP CMDSPStorageGlobals::GetStatus(DWORD * pdwStatus)
  189. {
  190. HRESULT hr;
  191. IMDSPDevice *pDev;
  192. CFRg(g_pAppSCServer);
  193. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  194. {
  195. CORg(WMDM_E_NOTCERTIFIED);
  196. }
  197. CHRg(GetDevice(&pDev));
  198. hr = pDev->GetStatus(pdwStatus);
  199. pDev->Release();
  200. Error:
  201. hrLogDWORD("IMDSPStorageGlobals::GetStatus returned 0x%08lx", hr, hr);
  202. return hr;
  203. }
  204. STDMETHODIMP CMDSPStorageGlobals::Initialize(UINT fuMode, IWMDMProgress * pProgress)
  205. {
  206. HRESULT hr=S_OK;
  207. CFRg(g_pAppSCServer);
  208. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  209. {
  210. CORg(WMDM_E_NOTCERTIFIED);
  211. }
  212. CORg(WMDM_E_NOTSUPPORTED);
  213. Error:
  214. hrLogDWORD("IMDSPStorageGlobals::Initialize returned 0x%08lx", hr, hr);
  215. return hr;
  216. }
  217. STDMETHODIMP CMDSPStorageGlobals::GetDevice(IMDSPDevice * * ppDevice)
  218. {
  219. HRESULT hr;
  220. CComObject<CMDSPDevice> *pObj;
  221. CFRg(g_pAppSCServer);
  222. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  223. {
  224. CORg(WMDM_E_NOTCERTIFIED);
  225. }
  226. CARg(ppDevice);
  227. if( m_pMDSPDevice )
  228. {
  229. *ppDevice = m_pMDSPDevice;
  230. (*ppDevice)->AddRef();
  231. return S_OK;
  232. }
  233. CORg(CComObject<CMDSPDevice>::CreateInstance(&pObj));
  234. hr = pObj->QueryInterface(
  235. IID_IMDSPDevice,
  236. reinterpret_cast<void**>(ppDevice)
  237. );
  238. if( FAILED(hr) )
  239. {
  240. delete pObj;
  241. goto Error;
  242. }
  243. else
  244. {
  245. // wcscpy(pObj->m_wcsName, m_wcsName);
  246. hr = StringCbCopyW(pObj->m_wcsName, sizeof(pObj->m_wcsName), m_wcsName);
  247. if( FAILED(hr) )
  248. {
  249. (*ppDevice)->Release();
  250. *ppDevice = NULL;
  251. goto Error;
  252. }
  253. pObj->InitGlobalDeviceInfo();
  254. m_pMDSPDevice = *ppDevice;
  255. m_pMDSPDevice->AddRef();
  256. }
  257. hr = S_OK;
  258. Error:
  259. hrLogDWORD("IMDSPStorageGlobals::GetDevice returned 0x%08lx", hr, hr);
  260. return hr;
  261. }
  262. STDMETHODIMP CMDSPStorageGlobals::GetRootStorage(IMDSPStorage * * ppRoot)
  263. {
  264. HRESULT hr;
  265. CComObject<CMDSPStorage> *pObj;
  266. CFRg(g_pAppSCServer);
  267. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  268. {
  269. CORg(WMDM_E_NOTCERTIFIED);
  270. }
  271. CARg(ppRoot);
  272. CORg(CComObject<CMDSPStorage>::CreateInstance(&pObj));
  273. hr = pObj->QueryInterface(
  274. IID_IMDSPStorage,
  275. reinterpret_cast<void**>(ppRoot)
  276. );
  277. if( FAILED(hr) )
  278. {
  279. delete pObj;
  280. goto Error;
  281. }
  282. else
  283. {
  284. // wcscpy(pObj->m_wcsName, m_wcsName);
  285. hr = StringCbCopyW(pObj->m_wcsName, sizeof(pObj->m_wcsName), m_wcsName);
  286. if( FAILED(hr) )
  287. {
  288. (*ppRoot)->Release();
  289. *ppRoot = NULL;
  290. goto Error;
  291. }
  292. DWORD dwLen = wcslen(m_wcsName);
  293. if (dwLen == 0)
  294. {
  295. hr = E_FAIL;
  296. (*ppRoot)->Release();
  297. *ppRoot = NULL;
  298. goto Error;
  299. }
  300. if( m_wcsName[wcslen(m_wcsName)-1] != 0x5c )
  301. {
  302. // wcscat(pObj->m_wcsName, g_wcsBackslash);
  303. hr = StringCbCatW(pObj->m_wcsName, sizeof(pObj->m_wcsName),g_wcsBackslash);
  304. if( FAILED(hr) )
  305. {
  306. (*ppRoot)->Release();
  307. *ppRoot = NULL;
  308. goto Error;
  309. }
  310. }
  311. }
  312. hr = S_OK;
  313. Error:
  314. hrLogDWORD("IMDSPStorageGlobals::GetRootStorage returned 0x%08lx", hr, hr);
  315. return hr;
  316. }