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.

471 lines
13 KiB

  1. #include "precomp.h"
  2. #ifdef _DEBUG
  3. #undef THIS_FILE
  4. static char THIS_FILE[]=__FILE__;
  5. #define new DEBUG_NEW
  6. #endif
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CWiahelper::CWiahelper()
  11. {
  12. m_pIWiaItem = NULL;
  13. m_pIWiaPropStg = NULL;
  14. }
  15. CWiahelper::~CWiahelper()
  16. {
  17. // release property storage
  18. if(m_pIWiaPropStg){
  19. m_pIWiaPropStg->Release();
  20. m_pIWiaPropStg = NULL;
  21. }
  22. // release item
  23. if(m_pIWiaItem){
  24. m_pIWiaItem->Release();
  25. m_pIWiaItem = NULL;
  26. }
  27. }
  28. HRESULT CWiahelper::SetIWiaItem(IWiaItem *pIWiaItem)
  29. {
  30. HRESULT hr = S_OK;
  31. // release old property storage
  32. if(m_pIWiaPropStg){
  33. m_pIWiaPropStg->Release();
  34. m_pIWiaPropStg = NULL;
  35. }
  36. // release old item pointer
  37. if(m_pIWiaItem){
  38. m_pIWiaItem->Release();
  39. m_pIWiaItem = NULL;
  40. }
  41. // add ref item pointer (because we are storing it in this object)
  42. if(pIWiaItem){
  43. // get property storage interface
  44. hr = pIWiaItem->QueryInterface(IID_IWiaPropertyStorage,(VOID**)&m_pIWiaPropStg);
  45. if(SUCCEEDED(hr)){
  46. pIWiaItem->AddRef();
  47. m_pIWiaItem = pIWiaItem;
  48. }
  49. } else {
  50. hr = E_POINTER;
  51. }
  52. return hr;
  53. }
  54. HRESULT CWiahelper::ReadPropertyString(PROPID PropertyID, LPTSTR szPropertyValue, INT iBufferSize)
  55. {
  56. HRESULT hr = S_OK;
  57. if (m_pIWiaPropStg) {
  58. // initialize propspecs
  59. PROPSPEC PropSpec[1];
  60. PROPVARIANT PropVar[1];
  61. memset(PropVar, 0, sizeof(PropVar));
  62. PropSpec[0].ulKind = PRSPEC_PROPID;
  63. PropSpec[0].propid = PropertyID;
  64. hr = m_pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  65. if (SUCCEEDED(hr)) {
  66. #ifndef UNICODE
  67. WideCharToMultiByte(CP_ACP, 0,PropVar[0].bstrVal,-1,szPropertyValue,iBufferSize,NULL,NULL);
  68. #else
  69. lstrcpyn(szPropertyValue,PropVar[0].bstrVal, (iBufferSize/sizeof(TCHAR)));
  70. #endif
  71. PropVariantClear(PropVar);
  72. }
  73. } else {
  74. hr = E_POINTER;
  75. }
  76. return hr;
  77. }
  78. HRESULT CWiahelper::ReadPropertyLong(PROPID PropertyID, LONG *plPropertyValue)
  79. {
  80. HRESULT hr = S_OK;
  81. if (m_pIWiaPropStg) {
  82. // initialize propspecs
  83. PROPSPEC PropSpec[1];
  84. PROPVARIANT PropVar[1];
  85. memset(PropVar, 0, sizeof(PropVar));
  86. PropSpec[0].ulKind = PRSPEC_PROPID;
  87. PropSpec[0].propid = PropertyID;
  88. hr = m_pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  89. if (SUCCEEDED(hr)) {
  90. *plPropertyValue = PropVar[0].lVal;
  91. PropVariantClear(PropVar);
  92. }
  93. } else {
  94. hr = E_POINTER;
  95. }
  96. return hr;
  97. }
  98. HRESULT CWiahelper::ReadPropertyFloat(PROPID PropertyID, FLOAT *pfPropertyValue)
  99. {
  100. HRESULT hr = S_OK;
  101. if (m_pIWiaPropStg) {
  102. // initialize propspecs
  103. PROPSPEC PropSpec[1];
  104. PROPVARIANT PropVar[1];
  105. memset(PropVar, 0, sizeof(PropVar));
  106. PropSpec[0].ulKind = PRSPEC_PROPID;
  107. PropSpec[0].propid = PropertyID;
  108. hr = m_pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  109. if (SUCCEEDED(hr)) {
  110. *pfPropertyValue = PropVar[0].fltVal;
  111. PropVariantClear(PropVar);
  112. }
  113. } else {
  114. hr = E_POINTER;
  115. }
  116. return hr;
  117. }
  118. HRESULT CWiahelper::ReadPropertyGUID(PROPID PropertyID, GUID *pguidPropertyValue)
  119. {
  120. HRESULT hr = S_OK;
  121. if (m_pIWiaPropStg) {
  122. // initialize propspecs
  123. PROPSPEC PropSpec[1];
  124. PROPVARIANT PropVar[1];
  125. memset(PropVar, 0, sizeof(PropVar));
  126. PropSpec[0].ulKind = PRSPEC_PROPID;
  127. PropSpec[0].propid = PropertyID;
  128. hr = m_pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  129. if (SUCCEEDED(hr)) {
  130. memcpy(pguidPropertyValue,PropVar[0].puuid,sizeof(GUID));
  131. PropVariantClear(PropVar);
  132. }
  133. } else {
  134. hr = E_POINTER;
  135. }
  136. return hr;
  137. }
  138. HRESULT CWiahelper::ReadPropertyData(PROPID PropertyID, BYTE **ppData, LONG *pDataSize)
  139. {
  140. HRESULT hr = E_POINTER;
  141. if (m_pIWiaPropStg) {
  142. if (NULL != pDataSize) {
  143. if (NULL != ppData) {
  144. // initialize propspecs
  145. PROPSPEC PropSpec[1];
  146. PROPVARIANT PropVar[1];
  147. memset(PropVar, 0, sizeof(PropVar));
  148. PropSpec[0].ulKind = PRSPEC_PROPID;
  149. PropSpec[0].propid = PropertyID;
  150. hr = m_pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  151. if (SUCCEEDED(hr)) {
  152. *pDataSize = PropVar[0].caub.cElems;
  153. *ppData = NULL;
  154. *ppData = (BYTE*)GlobalAlloc(GPTR,PropVar[0].caub.cElems);
  155. if (NULL != *ppData) {
  156. memcpy(*ppData,PropVar[0].caub.pElems,PropVar[0].caub.cElems);
  157. } else {
  158. hr = E_OUTOFMEMORY;
  159. }
  160. PropVariantClear(PropVar);
  161. }
  162. }
  163. }
  164. }
  165. return hr;
  166. }
  167. HRESULT CWiahelper::ReadPropertyBSTR(PROPID PropertyID, BSTR *pbstrPropertyValue)
  168. {
  169. HRESULT hr = S_OK;
  170. if (m_pIWiaPropStg) {
  171. // initialize propspecs
  172. PROPSPEC PropSpec[1];
  173. PROPVARIANT PropVar[1];
  174. memset(PropVar, 0, sizeof(PropVar));
  175. PropSpec[0].ulKind = PRSPEC_PROPID;
  176. PropSpec[0].propid = PropertyID;
  177. hr = m_pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  178. if (SUCCEEDED(hr)) {
  179. *pbstrPropertyValue = SysAllocString(PropVar[0].bstrVal);
  180. PropVariantClear(PropVar);
  181. }
  182. } else {
  183. hr = E_POINTER;
  184. }
  185. return hr;
  186. }
  187. HRESULT CWiahelper::ReadPropertyStreamFile(TCHAR *szPropertyStreamFile)
  188. {
  189. #ifdef NO_STREAMSUPPORT
  190. HRESULT hr = E_NOTIMPL;
  191. #else
  192. HRESULT hr = S_OK;
  193. if (m_pIWiaPropStg) {
  194. HGLOBAL hMem = NULL;
  195. LPSTREAM pstmProp = NULL;
  196. LPBYTE pStreamData = NULL;
  197. CFile StreamFile;
  198. CFileException Exception;
  199. if (StreamFile.Open(szPropertyStreamFile,CFile::modeRead,&Exception)) {
  200. DWORD dwSize = 0;
  201. StreamFile.Read(&dwSize,sizeof(DWORD));
  202. if (dwSize) {
  203. hMem = GlobalAlloc(GMEM_MOVEABLE, dwSize);
  204. if (hMem) {
  205. pStreamData = (LPBYTE)GlobalLock(hMem);
  206. if (pStreamData != NULL) {
  207. DWORD dwReadSize = 0;
  208. dwReadSize = StreamFile.Read(pStreamData,dwSize);
  209. GlobalUnlock(hMem);
  210. if(dwSize == dwReadSize){
  211. hr = CreateStreamOnHGlobal(hMem, TRUE, &pstmProp);
  212. if (SUCCEEDED(hr)) {
  213. hr = m_pIWiaPropStg->SetPropertyStream((GUID*) &GUID_NULL, pstmProp);
  214. pstmProp->Release();
  215. }
  216. } else {
  217. hr = E_INVALIDARG;
  218. }
  219. } else {
  220. hr = E_OUTOFMEMORY;
  221. }
  222. GlobalFree(hMem);
  223. } else {
  224. hr = E_OUTOFMEMORY;
  225. }
  226. }
  227. StreamFile.Close();
  228. } else {
  229. AfxThrowFileException(Exception.m_cause);
  230. }
  231. } else {
  232. hr = E_POINTER;
  233. }
  234. #endif
  235. return hr;
  236. }
  237. HRESULT CWiahelper::ReadPropertyAttributes(PROPID PropertyID, LONG *plAccessFlags, PROPVARIANT *pPropertyVariant)
  238. {
  239. HRESULT hr = S_OK;
  240. if (m_pIWiaPropStg) {
  241. if(pPropertyVariant){
  242. // initialize propspecs
  243. PROPSPEC PropSpec[1];
  244. memset(pPropertyVariant, 0, sizeof(PROPVARIANT));
  245. PropSpec[0].ulKind = PRSPEC_PROPID;
  246. PropSpec[0].propid = PropertyID;
  247. hr = m_pIWiaPropStg->GetPropertyAttributes(1, PropSpec,(ULONG*)plAccessFlags,pPropertyVariant);
  248. } else {
  249. hr = E_INVALIDARG;
  250. }
  251. } else {
  252. hr = E_POINTER;
  253. }
  254. return hr;
  255. }
  256. HRESULT CWiahelper::WritePropertyString(PROPID PropertyID, LPTSTR szPropertyValue)
  257. {
  258. HRESULT hr = S_OK;
  259. if (m_pIWiaPropStg) {
  260. PROPSPEC propspec[1];
  261. PROPVARIANT propvar[1];
  262. memset(propvar, 0, sizeof(propvar));
  263. propspec[0].ulKind = PRSPEC_PROPID;
  264. propspec[0].propid = PropertyID;
  265. propvar[0].vt = VT_BSTR;
  266. #ifndef UNICODE
  267. WCHAR wszPropertyValue[MAX_PATH];
  268. memset(wszPropertyValue,0,sizeof(wszPropertyValue));
  269. MultiByteToWideChar(CP_ACP, 0,szPropertyValue,-1,wszPropertyValue,(sizeof(wszPropertyValue)/sizeof(wszPropertyValue[0])));
  270. // allocate BSTR
  271. propvar[0].bstrVal = SysAllocString(wszPropertyValue);
  272. #else
  273. // allocate BSTR
  274. propvar[0].bstrVal = SysAllocString(szPropertyValue);
  275. #endif
  276. hr = m_pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  277. // free allocated BSTR
  278. SysFreeString(propvar[0].bstrVal);
  279. } else {
  280. hr = E_POINTER;
  281. }
  282. return hr;
  283. }
  284. HRESULT CWiahelper::WritePropertyLong(PROPID PropertyID, LONG lPropertyValue)
  285. {
  286. HRESULT hr = S_OK;
  287. if (m_pIWiaPropStg) {
  288. PROPSPEC propspec[1];
  289. PROPVARIANT propvar[1];
  290. memset(propvar, 0, sizeof(propvar));
  291. propspec[0].ulKind = PRSPEC_PROPID;
  292. propspec[0].propid = PropertyID;
  293. propvar[0].vt = VT_I4;
  294. propvar[0].lVal = lPropertyValue;
  295. hr = m_pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  296. } else {
  297. hr = E_POINTER;
  298. }
  299. return hr;
  300. }
  301. HRESULT CWiahelper::WritePropertyFloat(PROPID PropertyID, FLOAT fPropertyValue)
  302. {
  303. HRESULT hr = S_OK;
  304. if (m_pIWiaPropStg) {
  305. PROPSPEC propspec[1];
  306. PROPVARIANT propvar[1];
  307. memset(propvar, 0, sizeof(propvar));
  308. propspec[0].ulKind = PRSPEC_PROPID;
  309. propspec[0].propid = PropertyID;
  310. propvar[0].vt = VT_R4;
  311. propvar[0].fltVal = fPropertyValue;
  312. hr = m_pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  313. } else {
  314. hr = E_POINTER;
  315. }
  316. return hr;
  317. }
  318. HRESULT CWiahelper::WritePropertyGUID(PROPID PropertyID, GUID guidPropertyValue)
  319. {
  320. HRESULT hr = S_OK;
  321. if (m_pIWiaPropStg) {
  322. PROPSPEC propspec[1];
  323. PROPVARIANT propvar[1];
  324. memset(propvar, 0, sizeof(propvar));
  325. propspec[0].ulKind = PRSPEC_PROPID;
  326. propspec[0].propid = PropertyID;
  327. propvar[0].vt = VT_CLSID;
  328. propvar[0].puuid = &guidPropertyValue;
  329. hr = m_pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  330. } else {
  331. hr = E_POINTER;
  332. }
  333. return hr;
  334. }
  335. HRESULT CWiahelper::WritePropertyBSTR(PROPID PropertyID, BSTR bstrPropertyValue)
  336. {
  337. HRESULT hr = S_OK;
  338. if (m_pIWiaPropStg) {
  339. PROPSPEC propspec[1];
  340. PROPVARIANT propvar[1];
  341. memset(propvar, 0, sizeof(propvar));
  342. propspec[0].ulKind = PRSPEC_PROPID;
  343. propspec[0].propid = PropertyID;
  344. propvar[0].vt = VT_BSTR;
  345. // allocate BSTR
  346. propvar[0].bstrVal = SysAllocString(bstrPropertyValue);
  347. hr = m_pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  348. // free allocated BSTR
  349. SysFreeString(propvar[0].bstrVal);
  350. } else {
  351. hr = E_POINTER;
  352. }
  353. return hr;
  354. }
  355. HRESULT CWiahelper::WritePropertyStreamFile(TCHAR *szPropertyStreamFile)
  356. {
  357. #ifdef NO_STREAMSUPPORT
  358. HRESULT hr = E_NOTIMPL;
  359. #else
  360. HRESULT hr = S_OK;
  361. if (m_pIWiaPropStg) {
  362. IStream *pIStrm = NULL;
  363. CFile StreamFile;
  364. CFileException Exception;
  365. GUID guidCompatId = GUID_NULL;
  366. hr = m_pIWiaPropStg->GetPropertyStream(&guidCompatId, &pIStrm);
  367. if (S_OK == hr) {
  368. if (StreamFile.Open(szPropertyStreamFile,CFile::modeCreate|CFile::modeWrite,&Exception)) {
  369. ULARGE_INTEGER uliSize = {0,0};
  370. LARGE_INTEGER liOrigin = {0,0};
  371. pIStrm->Seek(liOrigin, STREAM_SEEK_END, &uliSize);
  372. DWORD dwSize = uliSize.u.LowPart;
  373. if (dwSize) {
  374. StreamFile.Write(&dwSize, sizeof(DWORD));
  375. PBYTE pBuf = (PBYTE) LocalAlloc(LPTR, dwSize);
  376. if (pBuf) {
  377. pIStrm->Seek(liOrigin, STREAM_SEEK_SET, NULL);
  378. ULONG ulRead = 0;
  379. pIStrm->Read(pBuf, dwSize, &ulRead);
  380. StreamFile.Write(pBuf, ulRead);
  381. LocalFree(pBuf);
  382. }
  383. }
  384. StreamFile.Close();
  385. } else {
  386. AfxThrowFileException(Exception.m_cause);
  387. }
  388. pIStrm->Release();
  389. }
  390. } else {
  391. hr = E_POINTER;
  392. }
  393. #endif
  394. return hr;
  395. }