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.

372 lines
8.4 KiB

  1. /*
  2. * o l e u t i l . c p p
  3. *
  4. * Purpose:
  5. * OLE utilities
  6. *
  7. * History
  8. * Feb '97: brettm - created
  9. *
  10. * Copyright (C) Microsoft Corp. 1995, 1996.
  11. */
  12. #include "pch.hxx"
  13. #include "msoert.h"
  14. #include "oleutil.h"
  15. #include "demand.h"
  16. ASSERTDATA
  17. HRESULT HrGetDataStream(LPUNKNOWN pUnk, CLIPFORMAT cf, LPSTREAM *ppstm)
  18. {
  19. LPDATAOBJECT pDataObj=0;
  20. HRESULT hr;
  21. FORMATETC fetc={cf, 0, DVASPECT_CONTENT, -1, TYMED_ISTREAM};
  22. STGMEDIUM stgmed;
  23. ZeroMemory(&stgmed, sizeof(STGMEDIUM));
  24. if (!pUnk || !ppstm)
  25. return E_INVALIDARG;
  26. hr=pUnk->QueryInterface(IID_IDataObject, (LPVOID *)&pDataObj);
  27. if(FAILED(hr))
  28. goto error;
  29. hr=pDataObj->GetData(&fetc, &stgmed);
  30. if(FAILED(hr))
  31. goto error;
  32. Assert(stgmed.pstm);
  33. *ppstm = stgmed.pstm;
  34. (*ppstm)->AddRef();
  35. // addref the pUnk as it will be release in releasestgmed
  36. if(stgmed.pUnkForRelease)
  37. stgmed.pUnkForRelease->AddRef();
  38. ReleaseStgMedium(&stgmed);
  39. error:
  40. ReleaseObj(pDataObj);
  41. return hr;
  42. }
  43. HRESULT CmdSelectAllCopy(LPOLECOMMANDTARGET pCmdTarget)
  44. {
  45. HRESULT hr;
  46. if (!pCmdTarget)
  47. return E_FAIL;
  48. hr=pCmdTarget->Exec(NULL, OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
  49. if (FAILED(hr))
  50. goto error;
  51. hr=pCmdTarget->Exec(NULL, OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
  52. if (FAILED(hr))
  53. goto error;
  54. error:
  55. return hr;
  56. }
  57. HRESULT HrInitNew(LPUNKNOWN pUnk)
  58. {
  59. LPPERSISTSTREAMINIT ppsi=0;
  60. HRESULT hr;
  61. if (!pUnk)
  62. return E_INVALIDARG;
  63. hr=pUnk->QueryInterface(IID_IPersistStreamInit, (LPVOID *)&ppsi);
  64. if (FAILED(hr))
  65. goto error;
  66. hr = ppsi->InitNew();
  67. error:
  68. ReleaseObj(ppsi);
  69. return hr;
  70. }
  71. HRESULT HrIPersistStreamInitLoad(LPUNKNOWN pUnk, LPSTREAM pstm)
  72. {
  73. LPPERSISTSTREAMINIT ppsi=0;
  74. HRESULT hr;
  75. if (!pUnk)
  76. return E_INVALIDARG;
  77. hr = HrRewindStream(pstm);
  78. if (FAILED(hr))
  79. goto error;
  80. hr=pUnk->QueryInterface(IID_IPersistStreamInit, (LPVOID *)&ppsi);
  81. if (FAILED(hr))
  82. goto error;
  83. hr = ppsi->Load(pstm);
  84. error:
  85. ReleaseObj(ppsi);
  86. return hr;
  87. }
  88. HRESULT HrIPersistFileSave(LPUNKNOWN pUnk, LPSTR lpszFile)
  89. {
  90. HRESULT hr;
  91. LPPERSISTFILE ppf=0;
  92. WCHAR szFileW[MAX_PATH];
  93. Assert(lstrlen(lpszFile) <= MAX_PATH);
  94. if (lpszFile == NULL || *lpszFile == NULL)
  95. return E_INVALIDARG;
  96. hr=pUnk->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
  97. if (FAILED(hr))
  98. goto error;
  99. if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszFile, -1, szFileW, MAX_PATH))
  100. {
  101. hr=E_FAIL;
  102. goto error;
  103. }
  104. hr=ppf->Save(szFileW, FALSE);
  105. if (FAILED(hr))
  106. goto error;
  107. error:
  108. ReleaseObj(ppf);
  109. return hr;
  110. }
  111. HRESULT HrIPersistFileLoad(LPUNKNOWN pUnk, LPSTR lpszFile)
  112. {
  113. HRESULT hr;
  114. LPPERSISTFILE ppf=0;
  115. WCHAR szFileW[MAX_PATH];
  116. Assert(lstrlen(lpszFile) <= MAX_PATH);
  117. if (lpszFile == NULL || *lpszFile == NULL)
  118. return E_INVALIDARG;
  119. hr=pUnk->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
  120. if (FAILED(hr))
  121. goto error;
  122. if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszFile, -1, szFileW, MAX_PATH))
  123. {
  124. hr=E_FAIL;
  125. goto error;
  126. }
  127. hr=ppf->Load(szFileW, STGM_READ|STGM_SHARE_DENY_NONE);
  128. if (FAILED(hr))
  129. goto error;
  130. error:
  131. ReleaseObj(ppf);
  132. return hr;
  133. }
  134. HRESULT GetDispProp(IDispatch * pDisp, DISPID dispid, LCID lcid, VARIANT *pvar, EXCEPINFO * pexcepinfo)
  135. {
  136. HRESULT hr;
  137. DISPPARAMS dp; // Params for IDispatch::Invoke.
  138. UINT uiErr; // Argument error.
  139. Assert(pDisp);
  140. Assert(pvar);
  141. dp.rgvarg = NULL;
  142. dp.rgdispidNamedArgs = NULL;
  143. dp.cArgs = 0;
  144. dp.cNamedArgs = 0;
  145. return (pDisp->Invoke(
  146. dispid,
  147. IID_NULL,
  148. lcid,
  149. DISPATCH_PROPERTYGET,
  150. &dp,
  151. pvar,
  152. pexcepinfo,
  153. &uiErr));
  154. }
  155. HRESULT SetDispProp(IDispatch *pDisp, DISPID dispid, LCID lcid, VARIANTARG *pvarg, EXCEPINFO *pexcepinfo, DWORD dwFlags)
  156. {
  157. HRESULT hr;
  158. DISPID dispidPut = DISPID_PROPERTYPUT; // Dispid of prop arg.
  159. DISPPARAMS dp; // Params for Invoke
  160. UINT uiErr; // Invoke error param.
  161. Assert(pDisp);
  162. Assert(pvarg);
  163. dp.rgvarg = pvarg;
  164. dp.cArgs = 1;
  165. if (dwFlags & DISPATCH_METHOD)
  166. {
  167. dp.cNamedArgs = 0;
  168. dp.rgdispidNamedArgs = NULL;
  169. }
  170. else
  171. {
  172. dp.cNamedArgs = 1;
  173. dp.rgdispidNamedArgs = &dispidPut;
  174. }
  175. return pDisp->Invoke(
  176. dispid,
  177. IID_NULL,
  178. lcid,
  179. (UINT)dwFlags,
  180. &dp,
  181. NULL,
  182. pexcepinfo,
  183. &uiErr);
  184. }
  185. HRESULT HrLoadSync(LPUNKNOWN pUnk, LPSTR lpszFile)
  186. {
  187. LPSTREAM pstm=0;
  188. HRESULT hr;
  189. hr = OpenFileStream(lpszFile, OPEN_EXISTING , GENERIC_READ, &pstm);
  190. if (SUCCEEDED(hr))
  191. {
  192. hr = HrIPersistStreamInitLoad(pUnk, pstm);
  193. pstm->Release();
  194. }
  195. return hr;
  196. }
  197. HRESULT HrCoTaskStringDupeToW(LPCSTR lpsz, LPOLESTR *ppszW)
  198. {
  199. HRESULT hr = NOERROR;
  200. ULONG cch, cchRet;
  201. LPCSTR lpszPath;
  202. if (lpsz == NULL || ppszW == NULL)
  203. return E_INVALIDARG;
  204. *ppszW = 0;
  205. cch = lstrlen(lpsz)+1;
  206. // allocate a wide-string with enough character to hold string
  207. *ppszW=(LPOLESTR)CoTaskMemAlloc(cch * sizeof(OLECHAR));
  208. if(!*ppszW)
  209. {
  210. hr=E_OUTOFMEMORY;
  211. goto error;
  212. }
  213. cchRet=MultiByteToWideChar(CP_ACP, 0, lpsz, cch, *ppszW, cch);
  214. if(!cchRet)
  215. {
  216. hr=E_FAIL;
  217. goto error;
  218. }
  219. error:
  220. if(FAILED(hr))
  221. CoTaskMemFree(*ppszW);
  222. return hr;
  223. }
  224. #ifdef DEBUG
  225. /////////////////////////////////////////////////////////////////////////////
  226. //
  227. // BEGIN Debug Code
  228. //
  229. void DebugPrintInterface(REFIID riid, char *szPrefix)
  230. {
  231. LPOLESTR pszW=0;
  232. char szGuid[MAX_PATH];
  233. char szTmp[MAX_PATH];
  234. char szOut[MAX_PATH];
  235. LONG cb=MAX_PATH;
  236. HKEY hk=0;
  237. TraceCall("DebugPrintInterface");
  238. AssertSz(szPrefix, "Hey! Atleast pass an empty string!");
  239. StringFromIID(riid, &pszW);
  240. WideCharToMultiByte(CP_ACP, 0, pszW, -1, szGuid, MAX_PATH, NULL, NULL);
  241. StrCpyN(szTmp, "SOFTWARE\\Classes\\Interface\\", ARRAYSIZE(szTmp));
  242. StrCatBuff(szTmp, szGuid, ARRAYSIZE(szTmp));
  243. if((RegOpenKeyEx(HKEY_LOCAL_MACHINE, szTmp, 0, KEY_QUERY_VALUE, &hk)==ERROR_SUCCESS) &&
  244. RegQueryValue(hk, NULL, szTmp, &cb)==ERROR_SUCCESS)
  245. wnsprintf(szOut, ARRAYSIZE(szOut), "%s: {%s}", szPrefix, szTmp);
  246. else
  247. wnsprintf(szOut, ARRAYSIZE(szOut), "%s: [notfound] %s", szPrefix, szGuid);
  248. TraceInfo(szOut);
  249. if(hk)
  250. RegCloseKey(hk);
  251. if(pszW)
  252. CoTaskMemFree(pszW);
  253. }
  254. void DebugPrintCmdIdBlock(ULONG cCmds, OLECMD *rgCmds)
  255. {
  256. char sz[255],
  257. szT[255];
  258. TraceCall("DebugPrintCmdIdBlock");
  259. wnsprintf(sz, ARRAYSIZE(sz), "CmdId:: {%d,{", cCmds);
  260. for (ULONG ul=0;ul<cCmds; ul++)
  261. {
  262. switch (rgCmds[ul].cmdID)
  263. {
  264. case OLECMDID_COPY:
  265. StrCatBuff(sz, "copy", ARRAYSIZE(sz));
  266. break;
  267. case OLECMDID_CUT:
  268. StrCatBuff(sz, "cut", ARRAYSIZE(sz));
  269. break;
  270. case OLECMDID_PASTE:
  271. StrCatBuff(sz, "paste", ARRAYSIZE(sz));
  272. break;
  273. default:
  274. wnsprintf(szT, ARRAYSIZE(szT), "%d", rgCmds[ul].cmdID);
  275. StrCatBuff(sz, szT, ARRAYSIZE(sz));
  276. }
  277. if(ul+1!=cCmds)
  278. StrCatBuff(sz, ", ", ARRAYSIZE(sz));
  279. }
  280. StrCatBuff(sz, "}}", ARRAYSIZE(sz));
  281. // TraceInfo(sz); // Turning down the noise
  282. }
  283. //
  284. // END Debug Code
  285. //
  286. /////////////////////////////////////////////////////////////////////////////
  287. #endif // DEBUG