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
43 #include "wine/unicode.h"
44 #include "wine/winbase16.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 typedef LPSTR LPOLESTR16;
51 typedef LPCSTR LPCOLESTR16;
53 #define STDMETHOD16CALLTYPE __cdecl
54 #define STDMETHOD16(m) HRESULT (STDMETHOD16CALLTYPE *m)
55 #define STDMETHOD16_(t,m) t (STDMETHOD16CALLTYPE *m)
58 /***********************************************************************
62 typedef struct IMalloc16 *LPMALLOC16;
64 #define INTERFACE IMalloc16
65 DECLARE_INTERFACE_(IMalloc16,IUnknown)
67 /*** IUnknown methods ***/
68 STDMETHOD16_(HRESULT,QueryInterface)(THIS_ REFIID riid, void** ppvObject) PURE;
69 STDMETHOD16_(ULONG,AddRef)(THIS) PURE;
70 STDMETHOD16_(ULONG,Release)(THIS) PURE;
71 /*** IMalloc16 methods ***/
72 STDMETHOD16_(LPVOID,Alloc)(THIS_ DWORD cb) PURE;
73 STDMETHOD16_(LPVOID,Realloc)(THIS_ LPVOID pv, DWORD cb) PURE;
74 STDMETHOD16_(void,Free)(THIS_ LPVOID pv) PURE;
75 STDMETHOD16_(DWORD,GetSize)(THIS_ LPVOID pv) PURE;
76 STDMETHOD16_(INT16,DidAlloc)(THIS_ LPVOID pv) PURE;
77 STDMETHOD16_(LPVOID,HeapMinimize)(THIS) PURE;
81 static HTASK16 hETask = 0;
82 static WORD Table_ETask[62];
84 static LPMALLOC16 currentMalloc16=NULL;
86 /* --- IMalloc16 implementation */
92 const IMalloc16Vtbl *lpVtbl;
94 /* IMalloc16 fields */
97 /******************************************************************************
98 * IMalloc16_QueryInterface [COMPOBJ.500]
100 HRESULT CDECL IMalloc16_fnQueryInterface(IMalloc16* iface,REFIID refiid,LPVOID *obj) {
101 IMalloc16Impl *This = (IMalloc16Impl *)iface;
103 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
104 if ( !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
105 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
110 return OLE_E_ENUM_NOMORE;
113 /******************************************************************************
114 * IMalloc16_AddRef [COMPOBJ.501]
116 ULONG CDECL IMalloc16_fnAddRef(IMalloc16* iface) {
117 IMalloc16Impl *This = (IMalloc16Impl *)iface;
118 TRACE("(%p)->AddRef()\n",This);
119 return 1; /* cannot be freed */
122 /******************************************************************************
123 * IMalloc16_Release [COMPOBJ.502]
125 ULONG CDECL IMalloc16_fnRelease(IMalloc16* iface) {
126 IMalloc16Impl *This = (IMalloc16Impl *)iface;
127 TRACE("(%p)->Release()\n",This);
128 return 1; /* cannot be freed */
131 /******************************************************************************
132 * IMalloc16_Alloc [COMPOBJ.503]
134 SEGPTR CDECL IMalloc16_fnAlloc(IMalloc16* iface,DWORD cb) {
135 IMalloc16Impl *This = (IMalloc16Impl *)iface;
136 TRACE("(%p)->Alloc(%d)\n",This,cb);
137 return MapLS( HeapAlloc( GetProcessHeap(), 0, cb ) );
140 /******************************************************************************
141 * IMalloc16_Free [COMPOBJ.505]
143 VOID CDECL IMalloc16_fnFree(IMalloc16* iface,SEGPTR pv)
145 void *ptr = MapSL(pv);
146 IMalloc16Impl *This = (IMalloc16Impl *)iface;
147 TRACE("(%p)->Free(%08x)\n",This,pv);
149 HeapFree( GetProcessHeap(), 0, ptr );
152 /******************************************************************************
153 * IMalloc16_Realloc [COMPOBJ.504]
155 SEGPTR CDECL IMalloc16_fnRealloc(IMalloc16* iface,SEGPTR pv,DWORD cb)
158 IMalloc16Impl *This = (IMalloc16Impl *)iface;
159 TRACE("(%p)->Realloc(%08x,%d)\n",This,pv,cb);
161 ret = IMalloc16_fnAlloc(iface, cb);
163 ret = MapLS( HeapReAlloc( GetProcessHeap(), 0, MapSL(pv), cb ) );
166 IMalloc16_fnFree(iface, pv);
172 /******************************************************************************
173 * IMalloc16_GetSize [COMPOBJ.506]
175 DWORD CDECL IMalloc16_fnGetSize(IMalloc16* iface,SEGPTR pv)
177 IMalloc16Impl *This = (IMalloc16Impl *)iface;
178 TRACE("(%p)->GetSize(%08x)\n",This,pv);
179 return HeapSize( GetProcessHeap(), 0, MapSL(pv) );
182 /******************************************************************************
183 * IMalloc16_DidAlloc [COMPOBJ.507]
185 INT16 CDECL IMalloc16_fnDidAlloc(IMalloc16* iface,LPVOID pv) {
186 IMalloc16 *This = iface;
187 TRACE("(%p)->DidAlloc(%p)\n",This,pv);
191 /******************************************************************************
192 * IMalloc16_HeapMinimize [COMPOBJ.508]
194 LPVOID CDECL IMalloc16_fnHeapMinimize(IMalloc16* iface) {
195 IMalloc16Impl *This = (IMalloc16Impl *)iface;
196 TRACE("(%p)->HeapMinimize()\n",This);
200 /******************************************************************************
201 * IMalloc16_Constructor [VTABLE]
204 IMalloc16_Constructor(void)
206 static IMalloc16Vtbl vt16;
207 static SEGPTR msegvt16;
209 HMODULE16 hcomp = GetModuleHandle16("COMPOBJ");
211 This = HeapAlloc( GetProcessHeap(), 0, sizeof(IMalloc16Impl) );
214 #define VTENT(x) vt16.x = (void*)GetProcAddress16(hcomp,"IMalloc16_"#x);assert(vt16.x)
215 VTENT(QueryInterface);
225 msegvt16 = MapLS( &vt16 );
227 This->lpVtbl = (const IMalloc16Vtbl*)msegvt16;
229 return (LPMALLOC16)MapLS( This );
233 /******************************************************************************
234 * CoBuildVersion [COMPOBJ.1]
236 DWORD WINAPI CoBuildVersion16(void)
238 return CoBuildVersion();
241 /***********************************************************************
242 * CoGetMalloc [COMPOBJ.4]
244 * Retrieve the current win16 IMalloc interface.
247 * The current win16 IMalloc
249 HRESULT WINAPI CoGetMalloc16(
250 DWORD dwMemContext, /* [in] unknown */
251 LPMALLOC16 * lpMalloc /* [out] current win16 malloc interface */
254 currentMalloc16 = IMalloc16_Constructor();
255 *lpMalloc = currentMalloc16;
259 /***********************************************************************
260 * CoCreateStandardMalloc [COMPOBJ.71]
262 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
263 LPMALLOC16 *lpMalloc)
265 /* FIXME: docu says we shouldn't return the same allocator as in
267 *lpMalloc = IMalloc16_Constructor();
271 /******************************************************************************
272 * CoInitialize [COMPOBJ.2]
273 * Set the win16 IMalloc used for memory management
275 HRESULT WINAPI CoInitialize16(
276 LPVOID lpReserved /* [in] pointer to win16 malloc interface */
278 currentMalloc16 = (LPMALLOC16)lpReserved;
282 /***********************************************************************
283 * CoUninitialize [COMPOBJ.3]
284 * Don't know what it does.
285 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
286 * believe is the correct spelling
288 void WINAPI CoUninitialize16(void)
291 CoFreeAllLibraries();
294 /***********************************************************************
295 * CoFreeUnusedLibraries [COMPOBJ.17]
297 void WINAPI CoFreeUnusedLibraries16(void)
299 return CoFreeUnusedLibraries();
302 /***********************************************************************
303 * IsEqualGUID [COMPOBJ.18]
305 * Compares two Unique Identifiers.
310 BOOL16 WINAPI IsEqualGUID16(
311 GUID* g1, /* [in] unique id 1 */
312 GUID* g2) /* [in] unique id 2 */
314 return !memcmp( g1, g2, sizeof(GUID) );
317 /******************************************************************************
318 * CLSIDFromString [COMPOBJ.20]
319 * Converts a unique identifier from its string representation into
322 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
327 HRESULT WINAPI CLSIDFromString16(
328 LPCOLESTR16 idstr, /* [in] string representation of guid */
329 CLSID *id) /* [out] GUID converted from string */
336 memset( id, 0, sizeof (CLSID) );
340 /* validate the CLSID string */
341 if (strlen(idstr) != 38)
342 return CO_E_CLASSSTRING;
344 s = (const BYTE *) idstr;
345 if ((s[0]!='{') || (s[9]!='-') || (s[14]!='-') || (s[19]!='-') || (s[24]!='-') || (s[37]!='}'))
346 return CO_E_CLASSSTRING;
348 for (i=1; i<37; i++) {
349 if ((i == 9)||(i == 14)||(i == 19)||(i == 24)) continue;
350 if (!(((s[i] >= '0') && (s[i] <= '9')) ||
351 ((s[i] >= 'a') && (s[i] <= 'f')) ||
352 ((s[i] >= 'A') && (s[i] <= 'F'))))
353 return CO_E_CLASSSTRING;
356 TRACE("%s -> %p\n", s, id);
358 /* quick lookup table */
359 memset(table, 0, 256);
361 for (i = 0; i < 10; i++) {
364 for (i = 0; i < 6; i++) {
365 table['A' + i] = i+10;
366 table['a' + i] = i+10;
369 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
371 id->Data1 = (table[s[1]] << 28 | table[s[2]] << 24 | table[s[3]] << 20 | table[s[4]] << 16 |
372 table[s[5]] << 12 | table[s[6]] << 8 | table[s[7]] << 4 | table[s[8]]);
373 id->Data2 = table[s[10]] << 12 | table[s[11]] << 8 | table[s[12]] << 4 | table[s[13]];
374 id->Data3 = table[s[15]] << 12 | table[s[16]] << 8 | table[s[17]] << 4 | table[s[18]];
376 /* these are just sequential bytes */
377 id->Data4[0] = table[s[20]] << 4 | table[s[21]];
378 id->Data4[1] = table[s[22]] << 4 | table[s[23]];
379 id->Data4[2] = table[s[25]] << 4 | table[s[26]];
380 id->Data4[3] = table[s[27]] << 4 | table[s[28]];
381 id->Data4[4] = table[s[29]] << 4 | table[s[30]];
382 id->Data4[5] = table[s[31]] << 4 | table[s[32]];
383 id->Data4[6] = table[s[33]] << 4 | table[s[34]];
384 id->Data4[7] = table[s[35]] << 4 | table[s[36]];
389 /******************************************************************************
390 * _xmalloc16 [internal]
391 * Allocates size bytes from the standard ole16 allocator.
394 * the allocated segmented pointer and a HRESULT
397 _xmalloc16(DWORD size, SEGPTR *ptr) {
401 if (CoGetMalloc16(0,&mllc))
402 return E_OUTOFMEMORY;
404 args[0] = (DWORD)mllc;
406 /* No need for a Callback entry, we have WOWCallback16Ex which does
407 * everything we need.
409 if (!WOWCallback16Ex(
410 (DWORD)((const IMalloc16Vtbl*)MapSL(
411 (SEGPTR)((LPMALLOC16)MapSL((SEGPTR)mllc))->lpVtbl )
418 ERR("CallTo16 IMalloc16 (%d) failed\n",size);
424 /******************************************************************************
425 * StringFromCLSID [COMPOBJ.19]
426 * Converts a GUID into the respective string representation.
427 * The target string is allocated using the OLE IMalloc.
430 * the string representation and HRESULT
433 HRESULT WINAPI StringFromCLSID16(
434 REFCLSID id, /* [in] the GUID to be converted */
435 LPOLESTR16 *idstr ) /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
440 ret = _xmalloc16(40,(SEGPTR*)idstr);
443 StringFromGUID2( id, buffer, 40 );
444 WideCharToMultiByte( CP_ACP, 0, buffer, -1, MapSL((SEGPTR)*idstr), 40, NULL, NULL );
448 /******************************************************************************
449 * ProgIDFromCLSID [COMPOBJ.62]
451 * Converts a class id into the respective Program ID. (By using a registry lookup)
455 * riid associated with the progid
457 HRESULT WINAPI ProgIDFromCLSID16(
458 REFCLSID clsid, /* [in] class id as found in registry */
459 LPOLESTR16 *lplpszProgID )/* [out] associated Program ID */
464 ret = ProgIDFromCLSID( clsid, &progid );
467 INT len = WideCharToMultiByte( CP_ACP, 0, progid, -1, NULL, 0, NULL, NULL );
468 ret = _xmalloc16(len, (SEGPTR*)lplpszProgID);
470 WideCharToMultiByte( CP_ACP, 0, progid, -1, MapSL((SEGPTR)*lplpszProgID), len, NULL, NULL );
471 CoTaskMemFree( progid );
476 /***********************************************************************
477 * LookupETask (COMPOBJ.94)
479 HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
480 FIXME("(%p,%p),stub!\n",hTask,p);
481 if ((*hTask = GetCurrentTask()) == hETask) {
482 memcpy(p, Table_ETask, sizeof(Table_ETask));
487 /***********************************************************************
488 * SetETask (COMPOBJ.95)
490 HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
491 FIXME("(%04x,%p),stub!\n",hTask,p);
496 /***********************************************************************
497 * CALLOBJECTINWOW (COMPOBJ.201)
499 HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
500 FIXME("(%p,%p),stub!\n",p1,p2);
504 /******************************************************************************
505 * CoRegisterClassObject [COMPOBJ.5]
507 * Don't know where it registers it ...
509 HRESULT WINAPI CoRegisterClassObject16(
512 DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
513 DWORD flags, /* [in] REGCLS flags indicating how connections are made */
516 FIXME("(%s,%p,0x%08x,0x%08x,%p),stub\n",
517 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister
522 /******************************************************************************
523 * CoRevokeClassObject [COMPOBJ.6]
526 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
528 FIXME("(0x%08x),stub!\n", dwRegister);
532 /******************************************************************************
533 * IsValidInterface [COMPOBJ.23]
535 * Determines whether a pointer is a valid interface.
538 * punk [I] Interface to be tested.
541 * TRUE, if the passed pointer is a valid interface, or FALSE otherwise.
543 BOOL WINAPI IsValidInterface16(SEGPTR punk)
547 if (IsBadReadPtr16(punk,4))
550 if (IsBadReadPtr16((SEGPTR)ptr[0],4)) /* check vtable ptr */
552 ptr = MapSL((SEGPTR)ptr[0]); /* ptr to first method */
553 if (IsBadReadPtr16((SEGPTR)ptr[0],2))
558 /******************************************************************************
559 * CoFileTimeToDosDateTime [COMPOBJ.30]
561 BOOL16 WINAPI CoFileTimeToDosDateTime16(const FILETIME *ft, LPWORD lpDosDate, LPWORD lpDosTime)
563 return FileTimeToDosDateTime(ft, lpDosDate, lpDosTime);
566 /******************************************************************************
567 * CoDosDateTimeToFileTime [COMPOBJ.31]
569 BOOL16 WINAPI CoDosDateTimeToFileTime16(WORD wDosDate, WORD wDosTime, FILETIME *ft)
571 return DosDateTimeToFileTime(wDosDate, wDosTime, ft);
574 /******************************************************************************
575 * CoGetCurrentProcess [COMPOBJ.34]
577 DWORD WINAPI CoGetCurrentProcess16(void)
579 return CoGetCurrentProcess();
582 /******************************************************************************
583 * CoRegisterMessageFilter [COMPOBJ.27]
585 HRESULT WINAPI CoRegisterMessageFilter16(
586 LPMESSAGEFILTER lpMessageFilter,
587 LPMESSAGEFILTER *lplpMessageFilter
589 FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
593 /******************************************************************************
594 * CoLockObjectExternal [COMPOBJ.63]
596 HRESULT WINAPI CoLockObjectExternal16(
597 LPUNKNOWN pUnk, /* [in] object to be locked */
598 BOOL16 fLock, /* [in] do lock */
599 BOOL16 fLastUnlockReleases /* [in] ? */
601 FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
605 /***********************************************************************
606 * CoGetState [COMPOBJ.115]
608 HRESULT WINAPI CoGetState16(LPDWORD state)
610 FIXME("(%p),stub!\n", state);
616 /***********************************************************************
617 * DllEntryPoint [COMPOBJ.116]
619 * Initialization code for the COMPOBJ DLL
623 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
625 TRACE("(%08x, %04x, %04x, %04x, %08x, %04x)\n", Reason, hInst, ds, HeapSize, res1, res2);
629 /***********************************************************************
630 * CoMemAlloc [COMPOBJ.151]
632 SEGPTR WINAPI CoMemAlloc(DWORD size, DWORD dwMemContext, DWORD x) {
636 /* FIXME: check context handling */
637 TRACE("(%d, 0x%08x, 0x%08x)\n", size, dwMemContext, x);
638 hres = _xmalloc16(size, &segptr);
644 /******************************************************************************
645 * CLSIDFromProgID [COMPOBJ.61]
647 * Converts a program ID into the respective GUID.
650 * progid [I] program id as found in registry
651 * riid [O] associated CLSID
655 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
657 HRESULT WINAPI CLSIDFromProgID16(LPCOLESTR16 progid, LPCLSID riid)
664 buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
665 sprintf(buf,"%s\\CLSID",progid);
666 if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
667 HeapFree(GetProcessHeap(),0,buf);
668 return CO_E_CLASSSTRING;
670 HeapFree(GetProcessHeap(),0,buf);
671 buf2len = sizeof(buf2);
672 if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
674 return CO_E_CLASSSTRING;
677 return CLSIDFromString16(buf2,riid);
680 /******************************************************************************
681 * StringFromGUID2 [COMPOBJ.76]
683 INT WINAPI StringFromGUID216(REFGUID id, LPOLESTR str, INT cmax)
685 return StringFromGUID2( id, str, cmax );
689 /***********************************************************************
690 * CoFileTimeNow [COMPOBJ.82]
692 HRESULT WINAPI CoFileTimeNow16( FILETIME *lpFileTime )
694 return CoFileTimeNow( lpFileTime );
697 /***********************************************************************
698 * CoGetClassObject [COMPOBJ.7]
701 HRESULT WINAPI CoGetClassObject16(
702 REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
703 REFIID iid, LPVOID *ppv)
705 FIXME(", stub!\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
708 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
709 FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
714 /******************************************************************************
715 * CoCreateGuid [COMPOBJ.73]
717 HRESULT WINAPI CoCreateGuid16(GUID *pguid)
719 return CoCreateGuid( pguid );
722 /***********************************************************************
723 * CoCreateInstance [COMPOBJ.13]
725 HRESULT WINAPI CoCreateInstance16(
732 FIXME("(%s, %p, %x, %s, %p), stub!\n",
733 debugstr_guid(rclsid), pUnkOuter, dwClsContext, debugstr_guid(iid),
739 /***********************************************************************
740 * CoDisconnectObject [COMPOBJ.15]
742 HRESULT WINAPI CoDisconnectObject16( LPUNKNOWN lpUnk, DWORD reserved )
744 FIXME("(%p, 0x%08x): stub!\n", lpUnk, reserved);