Don't define COBJMACROS in objbase.h.
[wine/multimedia.git] / dlls / ole32 / ole2impl.c
blob9607a220f5181a0fc33b9a79d10d0117690363c5
1 /*
2 * Ole 2 Create functions implementation
4 * Copyright (C) 1999-2000 Abey George
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <string.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "wine/debug.h"
33 #include "ole2.h"
34 #include "olestd.h"
35 #include "winreg.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39 #define MAX_CLIPFORMAT_NAME 80
41 /******************************************************************************
42 * OleQueryCreateFromData [OLE32.@]
44 * Author : Abey George
45 * Checks whether an object can become an embedded object.
46 * the clipboard or OLE drag and drop.
47 * Returns : S_OK - Format that supports Embedded object creation are present.
48 * OLE_E_STATIC - Format that supports static object creation are present.
49 * S_FALSE - No acceptable format is available.
52 HRESULT WINAPI OleQueryCreateFromData(LPDATAOBJECT pSrcDataObject)
54 IEnumFORMATETC *pfmt;
55 FORMATETC fmt;
56 CHAR szFmtName[MAX_CLIPFORMAT_NAME];
57 BOOL bFoundStatic = FALSE;
59 HRESULT hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt);
61 if (hr == S_OK)
62 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
64 while (hr == S_OK)
66 GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1);
68 /* first, Check for Embedded Object, Embed Source or Filename */
70 if (!strcmp(szFmtName, CF_EMBEDDEDOBJECT) || !strcmp(szFmtName, CF_EMBEDSOURCE) || !strcmp(szFmtName, CF_FILENAME))
71 return S_OK;
73 /* Check for Metafile, Bitmap or DIB */
75 if (fmt.cfFormat == CF_METAFILEPICT || fmt.cfFormat == CF_BITMAP || fmt.cfFormat == CF_DIB)
76 bFoundStatic = TRUE;
78 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
81 /* Found a static format, but no embed format */
83 if (bFoundStatic)
84 return OLE_S_STATIC;
86 return S_FALSE;
89 /******************************************************************************
90 * OleCreateFromData [OLE32.@]
92 * Author : Abey George
93 * Creates an embedded object from data transfer object retrieved from
94 * the clipboard or OLE drag and drop.
95 * Returns : S_OK - Embedded object was created successfully.
96 * OLE_E_STATIC - OLE can create only a static object
97 * DV_E_FORMATETC - No acceptable format is available (only error return code)
98 * TODO : CF_FILENAME, CF_EMBEDEDOBJECT formats. Parameter renderopt is currently ignored.
101 HRESULT WINAPI OleCreateFromData(LPDATAOBJECT pSrcDataObject, REFIID riid,
102 DWORD renderopt, LPFORMATETC pFormatEtc,
103 LPOLECLIENTSITE pClientSite, LPSTORAGE pStg,
104 LPVOID* ppvObj)
106 IEnumFORMATETC *pfmt;
107 FORMATETC fmt;
108 CHAR szFmtName[MAX_CLIPFORMAT_NAME];
109 STGMEDIUM std;
110 HRESULT hr;
111 HRESULT hr1;
113 hr = IDataObject_EnumFormatEtc(pSrcDataObject, DATADIR_GET, &pfmt);
115 if (hr == S_OK)
117 memset(&std, 0, sizeof(STGMEDIUM));
119 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
120 while (hr == S_OK)
122 GetClipboardFormatNameA(fmt.cfFormat, szFmtName, MAX_CLIPFORMAT_NAME-1);
124 /* first, Check for Embedded Object, Embed Source or Filename */
125 /* TODO: Currently checks only for Embed Source. */
127 if (!strcmp(szFmtName, CF_EMBEDSOURCE))
129 std.tymed = TYMED_HGLOBAL;
131 if ((hr1 = IDataObject_GetData(pSrcDataObject, &fmt, &std)) == S_OK)
133 ILockBytes *ptrILockBytes = 0;
134 IStorage *pStorage = 0;
135 IOleObject *pOleObject = 0;
136 IPersistStorage *pPersistStorage = 0;
137 CLSID clsID;
139 /* Create ILock bytes */
141 hr1 = CreateILockBytesOnHGlobal(std.u.hGlobal, FALSE, &ptrILockBytes);
143 /* Open storage on the ILock bytes */
145 if (hr1 == S_OK)
146 hr1 = StgOpenStorageOnILockBytes(ptrILockBytes, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage);
148 /* Get Class ID from the opened storage */
150 if (hr1 == S_OK)
151 hr1 = ReadClassStg(pStorage, &clsID);
153 /* Create default handler for Persist storage */
155 if (hr1 == S_OK)
156 hr1 = OleCreateDefaultHandler(&clsID, NULL, &IID_IPersistStorage, (LPVOID*)&pPersistStorage);
158 /* Load the storage to Persist storage */
160 if (hr1 == S_OK)
161 hr1 = IPersistStorage_Load(pPersistStorage, pStorage);
163 /* Query for IOleObject */
165 if (hr1 == S_OK)
166 hr1 = IPersistStorage_QueryInterface(pPersistStorage, &IID_IOleObject, (LPVOID*)&pOleObject);
168 /* Set client site with the IOleObject */
170 if (hr1 == S_OK)
171 hr1 = IOleObject_SetClientSite(pOleObject, pClientSite);
173 IPersistStorage_Release(pPersistStorage);
174 /* Query for the requested interface */
176 if (hr1 == S_OK)
177 hr1 = IPersistStorage_QueryInterface(pPersistStorage, riid, ppvObj);
179 IPersistStorage_Release(pPersistStorage);
181 IStorage_Release(pStorage);
183 if (hr1 == S_OK)
184 return S_OK;
187 /* Return error */
189 return DV_E_FORMATETC;
192 hr = IEnumFORMATETC_Next(pfmt, 1, &fmt, NULL);
196 return DV_E_FORMATETC;