push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / ole32 / ole16.c
bloba8fa59bceb7b2ea86ec07f3c05dade9a554c695d
1 /*
2 * 16 bit ole functions
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 Justin Bradford
6 * Copyright 1999 Francis Beaudet
7 * Copyright 1999 Sylvain St-Germain
8 * Copyright 2002 Marcus Meissner
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "rpc.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "wownt32.h"
42 #include "wtypes.h"
43 #include "wine/unicode.h"
44 #include "wine/winbase16.h"
45 #include "compobj_private.h"
46 #include "ifs.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 static HTASK16 hETask = 0;
53 static WORD Table_ETask[62];
55 static LPMALLOC16 currentMalloc16=NULL;
57 /* --- IMalloc16 implementation */
60 typedef struct
62 /* IUnknown fields */
63 const IMalloc16Vtbl *lpVtbl;
64 DWORD ref;
65 /* IMalloc16 fields */
66 } IMalloc16Impl;
68 /******************************************************************************
69 * IMalloc16_QueryInterface [COMPOBJ.500]
71 HRESULT CDECL IMalloc16_fnQueryInterface(IMalloc16* iface,REFIID refiid,LPVOID *obj) {
72 IMalloc16Impl *This = (IMalloc16Impl *)iface;
74 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
75 if ( !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
76 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
77 ) {
78 *obj = This;
79 return 0;
81 return OLE_E_ENUM_NOMORE;
84 /******************************************************************************
85 * IMalloc16_AddRef [COMPOBJ.501]
87 ULONG CDECL IMalloc16_fnAddRef(IMalloc16* iface) {
88 IMalloc16Impl *This = (IMalloc16Impl *)iface;
89 TRACE("(%p)->AddRef()\n",This);
90 return 1; /* cannot be freed */
93 /******************************************************************************
94 * IMalloc16_Release [COMPOBJ.502]
96 ULONG CDECL IMalloc16_fnRelease(IMalloc16* iface) {
97 IMalloc16Impl *This = (IMalloc16Impl *)iface;
98 TRACE("(%p)->Release()\n",This);
99 return 1; /* cannot be freed */
102 /******************************************************************************
103 * IMalloc16_Alloc [COMPOBJ.503]
105 SEGPTR CDECL IMalloc16_fnAlloc(IMalloc16* iface,DWORD cb) {
106 IMalloc16Impl *This = (IMalloc16Impl *)iface;
107 TRACE("(%p)->Alloc(%d)\n",This,cb);
108 return MapLS( HeapAlloc( GetProcessHeap(), 0, cb ) );
111 /******************************************************************************
112 * IMalloc16_Free [COMPOBJ.505]
114 VOID CDECL IMalloc16_fnFree(IMalloc16* iface,SEGPTR pv)
116 void *ptr = MapSL(pv);
117 IMalloc16Impl *This = (IMalloc16Impl *)iface;
118 TRACE("(%p)->Free(%08x)\n",This,pv);
119 UnMapLS(pv);
120 HeapFree( GetProcessHeap(), 0, ptr );
123 /******************************************************************************
124 * IMalloc16_Realloc [COMPOBJ.504]
126 SEGPTR CDECL IMalloc16_fnRealloc(IMalloc16* iface,SEGPTR pv,DWORD cb)
128 SEGPTR ret;
129 IMalloc16Impl *This = (IMalloc16Impl *)iface;
130 TRACE("(%p)->Realloc(%08x,%d)\n",This,pv,cb);
131 if (!pv)
132 ret = IMalloc16_fnAlloc(iface, cb);
133 else if (cb) {
134 ret = MapLS( HeapReAlloc( GetProcessHeap(), 0, MapSL(pv), cb ) );
135 UnMapLS(pv);
136 } else {
137 IMalloc16_fnFree(iface, pv);
138 ret = 0;
140 return ret;
143 /******************************************************************************
144 * IMalloc16_GetSize [COMPOBJ.506]
146 DWORD CDECL IMalloc16_fnGetSize(IMalloc16* iface,SEGPTR pv)
148 IMalloc16Impl *This = (IMalloc16Impl *)iface;
149 TRACE("(%p)->GetSize(%08x)\n",This,pv);
150 return HeapSize( GetProcessHeap(), 0, MapSL(pv) );
153 /******************************************************************************
154 * IMalloc16_DidAlloc [COMPOBJ.507]
156 INT16 CDECL IMalloc16_fnDidAlloc(IMalloc16* iface,LPVOID pv) {
157 IMalloc16 *This = iface;
158 TRACE("(%p)->DidAlloc(%p)\n",This,pv);
159 return (INT16)-1;
162 /******************************************************************************
163 * IMalloc16_HeapMinimize [COMPOBJ.508]
165 LPVOID CDECL IMalloc16_fnHeapMinimize(IMalloc16* iface) {
166 IMalloc16Impl *This = (IMalloc16Impl *)iface;
167 TRACE("(%p)->HeapMinimize()\n",This);
168 return NULL;
171 /******************************************************************************
172 * IMalloc16_Constructor [VTABLE]
174 LPMALLOC16
175 IMalloc16_Constructor(void)
177 static IMalloc16Vtbl vt16;
178 static SEGPTR msegvt16;
179 IMalloc16Impl* This;
180 HMODULE16 hcomp = GetModuleHandle16("COMPOBJ");
182 This = HeapAlloc( GetProcessHeap(), 0, sizeof(IMalloc16Impl) );
183 if (!msegvt16)
185 #define VTENT(x) vt16.x = (void*)GetProcAddress16(hcomp,"IMalloc16_"#x);assert(vt16.x)
186 VTENT(QueryInterface);
187 VTENT(AddRef);
188 VTENT(Release);
189 VTENT(Alloc);
190 VTENT(Realloc);
191 VTENT(Free);
192 VTENT(GetSize);
193 VTENT(DidAlloc);
194 VTENT(HeapMinimize);
195 #undef VTENT
196 msegvt16 = MapLS( &vt16 );
198 This->lpVtbl = (const IMalloc16Vtbl*)msegvt16;
199 This->ref = 1;
200 return (LPMALLOC16)MapLS( This );
204 /***********************************************************************
205 * CoGetMalloc [COMPOBJ.4]
207 * Retrieve the current win16 IMalloc interface.
209 * RETURNS
210 * The current win16 IMalloc
212 HRESULT WINAPI CoGetMalloc16(
213 DWORD dwMemContext, /* [in] unknown */
214 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
216 if(!currentMalloc16)
217 currentMalloc16 = IMalloc16_Constructor();
218 *lpMalloc = currentMalloc16;
219 return S_OK;
222 /***********************************************************************
223 * CoCreateStandardMalloc [COMPOBJ.71]
225 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
226 LPMALLOC16 *lpMalloc)
228 /* FIXME: docu says we shouldn't return the same allocator as in
229 * CoGetMalloc16 */
230 *lpMalloc = IMalloc16_Constructor();
231 return S_OK;
234 /******************************************************************************
235 * CoInitialize [COMPOBJ.2]
236 * Set the win16 IMalloc used for memory management
238 HRESULT WINAPI CoInitialize16(
239 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
241 currentMalloc16 = (LPMALLOC16)lpReserved;
242 return S_OK;
245 /***********************************************************************
246 * CoUninitialize [COMPOBJ.3]
247 * Don't know what it does.
248 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
249 * believe is the correct spelling
251 void WINAPI CoUninitialize16(void)
253 TRACE("()\n");
254 CoFreeAllLibraries();
257 /***********************************************************************
258 * IsEqualGUID [COMPOBJ.18]
260 * Compares two Unique Identifiers.
262 * RETURNS
263 * TRUE if equal
265 BOOL16 WINAPI IsEqualGUID16(
266 GUID* g1, /* [in] unique id 1 */
267 GUID* g2) /* [in] unique id 2 */
269 return !memcmp( g1, g2, sizeof(GUID) );
272 /******************************************************************************
273 * CLSIDFromString [COMPOBJ.20]
274 * Converts a unique identifier from its string representation into
275 * the GUID struct.
277 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
279 * RETURNS
280 * the converted GUID
282 HRESULT WINAPI CLSIDFromString16(
283 LPCOLESTR16 idstr, /* [in] string representation of guid */
284 CLSID *id) /* [out] GUID converted from string */
286 const BYTE *s;
287 int i;
288 BYTE table[256];
290 if (!idstr) {
291 memset( id, 0, sizeof (CLSID) );
292 return S_OK;
295 /* validate the CLSID string */
296 if (strlen(idstr) != 38)
297 return CO_E_CLASSSTRING;
299 s = (const BYTE *) idstr;
300 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
301 return CO_E_CLASSSTRING;
303 for (i=1; i<37; i++) {
304 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
305 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
306 ((s[i] >= 'a') && (s[i] <= 'f')) ||
307 ((s[i] >= 'A') && (s[i] <= 'F'))))
308 return CO_E_CLASSSTRING;
311 TRACE("%s -> %p\n", s, id);
313 /* quick lookup table */
314 memset(table, 0, 256);
316 for (i = 0; i < 10; i++) {
317 table['0' + i] = i;
319 for (i = 0; i < 6; i++) {
320 table['A' + i] = i+10;
321 table['a' + i] = i+10;
324 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
326 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
327 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
328 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
329 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
331 /* these are just sequential bytes */
332 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
333 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
334 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
335 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
336 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
337 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
338 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
339 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
341 return S_OK;
344 /******************************************************************************
345 * _xmalloc16 [internal]
346 * Allocates size bytes from the standard ole16 allocator.
348 * RETURNS
349 * the allocated segmented pointer and a HRESULT
351 static HRESULT
352 _xmalloc16(DWORD size, SEGPTR *ptr) {
353 LPMALLOC16 mllc;
354 DWORD args[2];
356 if (CoGetMalloc16(0,&mllc))
357 return E_OUTOFMEMORY;
359 args[0] = (DWORD)mllc;
360 args[1] = size;
361 /* No need for a Callback entry, we have WOWCallback16Ex which does
362 * everything we need.
364 if (!WOWCallback16Ex(
365 (DWORD)((const IMalloc16Vtbl*)MapSL(
366 (SEGPTR)((LPMALLOC16)MapSL((SEGPTR)mllc))->lpVtbl )
367 )->Alloc,
368 WCB16_CDECL,
369 2*sizeof(DWORD),
370 (LPVOID)args,
371 (LPDWORD)ptr
372 )) {
373 ERR("CallTo16 IMalloc16 (%d) failed\n",size);
374 return E_FAIL;
376 return S_OK;
379 /******************************************************************************
380 * StringFromCLSID [COMPOBJ.19]
381 * Converts a GUID into the respective string representation.
382 * The target string is allocated using the OLE IMalloc.
384 * RETURNS
385 * the string representation and HRESULT
388 HRESULT WINAPI StringFromCLSID16(
389 REFCLSID id, /* [in] the GUID to be converted */
390 LPOLESTR16 *idstr /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
393 HRESULT ret;
395 ret = _xmalloc16(40,(SEGPTR*)idstr);
396 if (ret != S_OK)
397 return ret;
398 return WINE_StringFromCLSID(id,MapSL((SEGPTR)*idstr));
401 /******************************************************************************
402 * ProgIDFromCLSID [COMPOBJ.62]
404 * Converts a class id into the respective Program ID. (By using a registry lookup)
406 * RETURNS
407 * S_OK on success
408 * riid associated with the progid
410 HRESULT WINAPI ProgIDFromCLSID16(
411 REFCLSID clsid, /* [in] class id as found in registry */
412 LPOLESTR16 *lplpszProgID/* [out] associated Program ID */
414 static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
415 HKEY hkey;
416 HRESULT ret;
417 LONG len;
418 char *buffer;
420 ret = COM_OpenKeyForCLSID(clsid, wszProgID, KEY_READ, &hkey);
421 if (FAILED(ret))
422 return ret;
424 if (RegQueryValueA(hkey, NULL, NULL, &len))
425 ret = REGDB_E_READREGDB;
427 if (ret == S_OK)
429 buffer = HeapAlloc(GetProcessHeap(), 0, len);
430 if (RegQueryValueA(hkey, NULL, buffer, &len))
431 ret = REGDB_E_READREGDB;
433 if (ret == S_OK)
435 ret = _xmalloc16(len, (SEGPTR*)lplpszProgID);
436 if (ret == S_OK)
437 strcpy(MapSL((SEGPTR)*lplpszProgID),buffer);
439 HeapFree(GetProcessHeap(), 0, buffer);
441 RegCloseKey(hkey);
442 return ret;
445 /***********************************************************************
446 * LookupETask (COMPOBJ.94)
448 HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
449 FIXME("(%p,%p),stub!\n",hTask,p);
450 if ((*hTask = GetCurrentTask()) == hETask) {
451 memcpy(p, Table_ETask, sizeof(Table_ETask));
453 return 0;
456 /***********************************************************************
457 * SetETask (COMPOBJ.95)
459 HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
460 FIXME("(%04x,%p),stub!\n",hTask,p);
461 hETask = hTask;
462 return 0;
465 /***********************************************************************
466 * CALLOBJECTINWOW (COMPOBJ.201)
468 HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
469 FIXME("(%p,%p),stub!\n",p1,p2);
470 return 0;
473 /******************************************************************************
474 * CoRegisterClassObject [COMPOBJ.5]
476 * Don't know where it registers it ...
478 HRESULT WINAPI CoRegisterClassObject16(
479 REFCLSID rclsid,
480 LPUNKNOWN pUnk,
481 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
482 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
483 LPDWORD lpdwRegister
485 FIXME("(%s,%p,0x%08x,0x%08x,%p),stub\n",
486 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister
488 return 0;
491 /******************************************************************************
492 * CoRevokeClassObject [COMPOBJ.6]
495 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
497 FIXME("(0x%08x),stub!\n", dwRegister);
498 return 0;
501 /******************************************************************************
502 * CoFileTimeToDosDateTime [COMPOBJ.30]
504 BOOL16 WINAPI CoFileTimeToDosDateTime16(const FILETIME *ft, LPWORD lpDosDate, LPWORD lpDosTime)
506 return FileTimeToDosDateTime(ft, lpDosDate, lpDosTime);
509 /******************************************************************************
510 * CoDosDateTimeToFileTime [COMPOBJ.31]
512 BOOL16 WINAPI CoDosDateTimeToFileTime16(WORD wDosDate, WORD wDosTime, FILETIME *ft)
514 return DosDateTimeToFileTime(wDosDate, wDosTime, ft);
517 /******************************************************************************
518 * CoRegisterMessageFilter [COMPOBJ.27]
520 HRESULT WINAPI CoRegisterMessageFilter16(
521 LPMESSAGEFILTER lpMessageFilter,
522 LPMESSAGEFILTER *lplpMessageFilter
524 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
525 return 0;
528 /******************************************************************************
529 * CoLockObjectExternal [COMPOBJ.63]
531 HRESULT WINAPI CoLockObjectExternal16(
532 LPUNKNOWN pUnk, /* [in] object to be locked */
533 BOOL16 fLock, /* [in] do lock */
534 BOOL16 fLastUnlockReleases /* [in] ? */
536 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
537 return S_OK;
540 /***********************************************************************
541 * CoGetState [COMPOBJ.115]
543 HRESULT WINAPI CoGetState16(LPDWORD state)
545 FIXME("(%p),stub!\n", state);
547 *state = 0;
548 return S_OK;
551 /***********************************************************************
552 * DllEntryPoint [COMPOBJ.116]
554 * Initialization code for the COMPOBJ DLL
556 * RETURNS:
558 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
560 TRACE("(%08x, %04x, %04x, %04x, %08x, %04x)\n", Reason, hInst, ds, HeapSize, res1, res2);
561 return TRUE;
564 /***********************************************************************
565 * CoMemAlloc [COMPOBJ.151]
567 SEGPTR WINAPI CoMemAlloc(DWORD size, DWORD dwMemContext, DWORD x) {
568 HRESULT hres;
569 SEGPTR segptr;
571 /* FIXME: check context handling */
572 TRACE("(%d, 0x%08x, 0x%08x)\n", size, dwMemContext, x);
573 hres = _xmalloc16(size, &segptr);
574 if (hres != S_OK)
575 return 0;
576 return segptr;
579 /******************************************************************************
580 * CLSIDFromProgID [COMPOBJ.61]
582 * Converts a program ID into the respective GUID.
584 * PARAMS
585 * progid [I] program id as found in registry
586 * riid [O] associated CLSID
588 * RETURNS
589 * Success: S_OK
590 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
592 HRESULT WINAPI CLSIDFromProgID16(LPCOLESTR16 progid, LPCLSID riid)
594 char *buf,buf2[80];
595 LONG buf2len;
596 HRESULT err;
597 HKEY xhkey;
599 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
600 sprintf(buf,"%s\\CLSID",progid);
601 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
602 HeapFree(GetProcessHeap(),0,buf);
603 return CO_E_CLASSSTRING;
605 HeapFree(GetProcessHeap(),0,buf);
606 buf2len = sizeof(buf2);
607 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
608 RegCloseKey(xhkey);
609 return CO_E_CLASSSTRING;
611 RegCloseKey(xhkey);
612 return CLSIDFromString16(buf2,riid);
615 /***********************************************************************
616 * CoGetClassObject [COMPOBJ.7]
619 HRESULT WINAPI CoGetClassObject16(
620 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
621 REFIID iid, LPVOID *ppv)
623 FIXME(", stub!\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
625 if (pServerInfo) {
626 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
627 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
629 return E_NOTIMPL;
632 /***********************************************************************
633 * CoCreateInstance [COMPOBJ.13]
635 HRESULT WINAPI CoCreateInstance16(
636 REFCLSID rclsid,
637 LPUNKNOWN pUnkOuter,
638 DWORD dwClsContext,
639 REFIID iid,
640 LPVOID *ppv)
642 FIXME("(%s, %p, %x, %s, %p), stub!\n",
643 debugstr_guid(rclsid), pUnkOuter, dwClsContext, debugstr_guid(iid),
646 return E_NOTIMPL;
649 /***********************************************************************
650 * CoDisconnectObject [COMPOBJ.15]
652 HRESULT WINAPI CoDisconnectObject16( LPUNKNOWN lpUnk, DWORD reserved )
654 FIXME("(%p, 0x%08x): stub!\n", lpUnk, reserved);
655 return E_NOTIMPL;
658 /***********************************************************************
659 * DllGetClassObject [OLE2.4]
661 HRESULT WINAPI DllGetClassObject16(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
663 FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
664 return E_NOTIMPL;
667 /******************************************************************************
668 * GetRunningObjectTable (OLE2.30)
670 HRESULT WINAPI
671 GetRunningObjectTable16(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
673 FIXME("(%d,%p),stub!\n",reserved,pprot);
674 return E_NOTIMPL;