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.

444 lines
12 KiB

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