SCODE AFXAPI _AfxOleDoConvert(LPSTORAGE lpStg, REFCLSID rClsidNew) { SCODE sc; CLSID clsidOld; if ((sc = ReadClassStg(lpStg, &clsidOld)) != S_OK) { clsidOld = CLSID_NULL; return sc; } // read old fmt/old user type; sets out params to NULL on error CLIPFORMAT cfOld; LPOLESTR lpszOld = NULL; sc = ReadFmtUserTypeStg(lpStg, &cfOld, &lpszOld); ASSERT(sc == S_OK || (cfOld == 0 && lpszOld == NULL)); // get new user type name; if error, set to NULL string OLECHAR chZero = 0; LPOLESTR lpszNew = NULL; if (OleRegGetUserType(rClsidNew, USERCLASSTYPE_FULL, &lpszNew) != S_OK) lpszNew = &chZero; // write class stg if ((sc = WriteClassStg(lpStg, rClsidNew)) != S_OK) goto ErrorReturn; // write old fmt/new user type; if ((sc = WriteFmtUserTypeStg(lpStg, cfOld, lpszNew)) != S_OK) goto RewriteInfo; // set convert bit if ((sc = SetConvertStg(lpStg, TRUE)) != S_OK) goto RewriteInfo; goto ErrorReturn; RewriteInfo: WriteClassStg(lpStg, clsidOld); WriteFmtUserTypeStg(lpStg, cfOld, lpszOld); ErrorReturn: if (lpszNew != &chZero) CoTaskMemFree(lpszNew); CoTaskMemFree(lpszOld); return sc; }
FARINTERNAL Ut10NativeStmToContentsStm (LPSTORAGE pstg, REFCLSID rclsid, BOOL fDeleteSrcStm) { CLIPFORMAT cfOld; CLIPFORMAT cfNew; LPOLESTR lpszUserType = NULL; HRESULT error; LPSTREAM pstmSrc = NULL; LPSTREAM pstmDst = NULL; if (error = ReadFmtUserTypeStg(pstg, &cfOld, &lpszUserType)) return error; if (rclsid == CLSID_StaticDib) cfNew = CF_DIB; else if (rclsid == CLSID_StaticMetafile) cfNew = CF_METAFILEPICT; else { AssertSz(FALSE, "Internal Error: this routine shouldn't have been called for this class"); return ResultFromScode(E_FAIL); } if (cfOld == g_cfPBrush) { if (cfNew != CF_DIB) { error = ResultFromScode(DV_E_CLIPFORMAT); goto errRtn; } } else if (cfOld == g_cfMSDraw) { if (cfNew != CF_METAFILEPICT) { error = ResultFromScode(DV_E_CLIPFORMAT); goto errRtn; } } else { // Converted to static object from some class other than PBrush or // MSDraw. The data must be in a proper format in the CONTENTS // stream. return NOERROR; } if (error = pstg->OpenStream(OLE10_NATIVE_STREAM, NULL, (STGM_READ|STGM_SHARE_EXCLUSIVE), 0, &pstmSrc)) goto errRtn; if (error = OpenOrCreateStream(pstg, OLE_CONTENTS_STREAM, &pstmDst)) goto errRtn; DWORD dwSize; if (error = pstmSrc->Read(&dwSize, sizeof(DWORD), NULL)) goto errRtn; if (cfOld == g_cfMSDraw) { WORD mfp[3]; // mm, xExt, yExt if (error = pstmSrc->Read(mfp, sizeof(mfp), NULL)) goto errRtn; dwSize -= sizeof(mfp); error = UtMFStmToPlaceableMFStm(pstmSrc, dwSize, (LONG) mfp[1], (LONG) mfp[2], pstmDst); } else { // The PBrush native data format is DIB File format. So all we got to // do is CopyTo. ULARGE_INTEGER ularge_int; ULISet32(ularge_int, dwSize); if ((error = pstmSrc->CopyTo(pstmDst, ularge_int, NULL, NULL)) == NOERROR) StSetSize(pstmDst, 0, TRUE); } errRtn: if (pstmDst) pstmDst->Release(); if (pstmSrc) pstmSrc->Release(); if (error == NOERROR) { error = WriteFmtUserTypeStg(pstg, cfNew, lpszUserType); if (fDeleteSrcStm) pstg->DestroyElement(OLE10_NATIVE_STREAM); } else { pstg->DestroyElement(OLE_CONTENTS_STREAM); } if (lpszUserType) PubMemFree(lpszUserType); return error; }
FARINTERNAL UtContentsStmTo10NativeStm (LPSTORAGE pstg, REFCLSID rclsid, BOOL fDeleteSrcStm, UINT FAR* puiStatus) { CLIPFORMAT cf; LPOLESTR lpszUserType = NULL; HRESULT error; LPSTREAM pstmSrc = NULL; LPSTREAM pstmDst = NULL; *puiStatus = NULL; if (error = ReadFmtUserTypeStg(pstg, &cf, &lpszUserType)) return error; if (! ((cf == CF_DIB && rclsid == CLSID_PBrush) || (cf == CF_METAFILEPICT && rclsid == CLSID_MSDraw))) { error = ResultFromScode(DV_E_CLIPFORMAT); goto errRtn; } if (error = pstg->OpenStream(OLE_CONTENTS_STREAM, NULL, (STGM_READ|STGM_SHARE_EXCLUSIVE), 0, &pstmSrc)) { *puiStatus |= CONVERT_NOSOURCE; // check whether OLE10_NATIVE_STREAM exists if (pstg->OpenStream(OLE10_NATIVE_STREAM, NULL, (STGM_READ|STGM_SHARE_EXCLUSIVE), 0, &pstmDst)) *puiStatus |= CONVERT_NODESTINATION; else { pstmDst->Release(); pstmDst = NULL; } goto errRtn; } if (error = OpenOrCreateStream(pstg, OLE10_NATIVE_STREAM, &pstmDst)) { *puiStatus |= CONVERT_NODESTINATION; goto errRtn; } if (cf == CF_METAFILEPICT) error = UtPlaceableMFStmToMSDrawNativeStm(pstmSrc, pstmDst); else error = UtDIBFileStmToPBrushNativeStm(pstmSrc, pstmDst); errRtn: if (pstmDst) pstmDst->Release(); if (pstmSrc) pstmSrc->Release(); if (error == NOERROR) { LPOLESTR lpszProgId = NULL; ProgIDFromCLSID(rclsid, &lpszProgId); error = WriteFmtUserTypeStg(pstg, RegisterClipboardFormat(lpszProgId), lpszUserType); if (lpszProgId) delete lpszProgId; } if (error == NOERROR) { if (fDeleteSrcStm) pstg->DestroyElement(OLE_CONTENTS_STREAM); } else { pstg->DestroyElement(OLE10_NATIVE_STREAM); } if (lpszUserType) delete lpszUserType; return error; }