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 */
91 IMalloc16 IMalloc16_iface
;
95 static inline IMalloc16Impl
*impl_from_IMalloc16(IMalloc16
*iface
)
97 return CONTAINING_RECORD(iface
, IMalloc16Impl
, IMalloc16_iface
);
100 /******************************************************************************
101 * IMalloc16_QueryInterface [COMPOBJ.500]
103 HRESULT CDECL
IMalloc16_fnQueryInterface(IMalloc16
* iface
,REFIID refiid
,LPVOID
*obj
) {
104 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
106 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(refiid
),obj
);
107 if ( !memcmp(&IID_IUnknown
,refiid
,sizeof(IID_IUnknown
)) ||
108 !memcmp(&IID_IMalloc
,refiid
,sizeof(IID_IMalloc
))
113 return OLE_E_ENUM_NOMORE
;
116 /******************************************************************************
117 * IMalloc16_AddRef [COMPOBJ.501]
119 ULONG CDECL
IMalloc16_fnAddRef(IMalloc16
* iface
) {
120 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
122 TRACE("(%p)->AddRef()\n",This
);
123 return 1; /* cannot be freed */
126 /******************************************************************************
127 * IMalloc16_Release [COMPOBJ.502]
129 ULONG CDECL
IMalloc16_fnRelease(IMalloc16
* iface
) {
130 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
132 TRACE("(%p)->Release()\n",This
);
133 return 1; /* cannot be freed */
136 /******************************************************************************
137 * IMalloc16_Alloc [COMPOBJ.503]
139 SEGPTR CDECL
IMalloc16_fnAlloc(IMalloc16
* iface
,DWORD cb
) {
140 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
142 TRACE("(%p)->Alloc(%d)\n",This
,cb
);
143 return MapLS( HeapAlloc( GetProcessHeap(), 0, cb
) );
146 /******************************************************************************
147 * IMalloc16_Free [COMPOBJ.505]
149 VOID CDECL
IMalloc16_fnFree(IMalloc16
* iface
,SEGPTR pv
)
151 void *ptr
= MapSL(pv
);
152 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
153 TRACE("(%p)->Free(%08x)\n",This
,pv
);
155 HeapFree( GetProcessHeap(), 0, ptr
);
158 /******************************************************************************
159 * IMalloc16_Realloc [COMPOBJ.504]
161 SEGPTR CDECL
IMalloc16_fnRealloc(IMalloc16
* iface
,SEGPTR pv
,DWORD cb
)
164 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
166 TRACE("(%p)->Realloc(%08x,%d)\n",This
,pv
,cb
);
168 ret
= IMalloc16_fnAlloc(iface
, cb
);
170 ret
= MapLS( HeapReAlloc( GetProcessHeap(), 0, MapSL(pv
), cb
) );
173 IMalloc16_fnFree(iface
, pv
);
179 /******************************************************************************
180 * IMalloc16_GetSize [COMPOBJ.506]
182 DWORD CDECL
IMalloc16_fnGetSize(IMalloc16
* iface
,SEGPTR pv
)
184 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
186 TRACE("(%p)->GetSize(%08x)\n",This
,pv
);
187 return HeapSize( GetProcessHeap(), 0, MapSL(pv
) );
190 /******************************************************************************
191 * IMalloc16_DidAlloc [COMPOBJ.507]
193 INT16 CDECL
IMalloc16_fnDidAlloc(IMalloc16
* iface
,LPVOID pv
) {
194 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
196 TRACE("(%p)->DidAlloc(%p)\n",This
,pv
);
200 /******************************************************************************
201 * IMalloc16_HeapMinimize [COMPOBJ.508]
203 LPVOID CDECL
IMalloc16_fnHeapMinimize(IMalloc16
* iface
) {
204 IMalloc16Impl
*This
= impl_from_IMalloc16(iface
);
206 TRACE("(%p)->HeapMinimize()\n",This
);
210 /******************************************************************************
211 * IMalloc16_Constructor [VTABLE]
214 IMalloc16_Constructor(void)
216 static IMalloc16Vtbl vt16
;
217 static SEGPTR msegvt16
;
219 HMODULE16 hcomp
= GetModuleHandle16("COMPOBJ");
221 This
= HeapAlloc( GetProcessHeap(), 0, sizeof(IMalloc16Impl
) );
224 #define VTENT(x) vt16.x = (void*)GetProcAddress16(hcomp,"IMalloc16_"#x);assert(vt16.x)
225 VTENT(QueryInterface
);
235 msegvt16
= MapLS( &vt16
);
237 This
->IMalloc16_iface
.lpVtbl
= (const IMalloc16Vtbl
*)msegvt16
;
239 return (LPMALLOC16
)MapLS( This
);
243 /******************************************************************************
244 * CoBuildVersion [COMPOBJ.1]
246 DWORD WINAPI
CoBuildVersion16(void)
248 return CoBuildVersion();
251 /***********************************************************************
252 * CoGetMalloc [COMPOBJ.4]
254 * Retrieve the current win16 IMalloc interface.
257 * The current win16 IMalloc
259 HRESULT WINAPI
CoGetMalloc16(
260 DWORD dwMemContext
, /* [in] unknown */
261 LPMALLOC16
* lpMalloc
/* [out] current win16 malloc interface */
264 currentMalloc16
= IMalloc16_Constructor();
265 *lpMalloc
= currentMalloc16
;
269 /***********************************************************************
270 * CoCreateStandardMalloc [COMPOBJ.71]
272 HRESULT WINAPI
CoCreateStandardMalloc16(DWORD dwMemContext
,
273 LPMALLOC16
*lpMalloc
)
275 /* FIXME: docu says we shouldn't return the same allocator as in
277 *lpMalloc
= IMalloc16_Constructor();
281 /******************************************************************************
282 * CoInitialize [COMPOBJ.2]
283 * Set the win16 IMalloc used for memory management
285 HRESULT WINAPI
CoInitialize16(
286 LPVOID lpReserved
/* [in] pointer to win16 malloc interface */
288 currentMalloc16
= (LPMALLOC16
)lpReserved
;
292 /***********************************************************************
293 * CoUninitialize [COMPOBJ.3]
294 * Don't know what it does.
295 * 3-Nov-98 -- this was originally misspelled, I changed it to what I
296 * believe is the correct spelling
298 void WINAPI
CoUninitialize16(void)
301 CoFreeAllLibraries();
304 /***********************************************************************
305 * CoFreeUnusedLibraries [COMPOBJ.17]
307 void WINAPI
CoFreeUnusedLibraries16(void)
309 CoFreeUnusedLibraries();
312 /***********************************************************************
313 * IsEqualGUID [COMPOBJ.18]
315 * Compares two Unique Identifiers.
320 BOOL16 WINAPI
IsEqualGUID16(
321 GUID
* g1
, /* [in] unique id 1 */
322 GUID
* g2
) /* [in] unique id 2 */
324 return !memcmp( g1
, g2
, sizeof(GUID
) );
327 /******************************************************************************
328 * CLSIDFromString [COMPOBJ.20]
329 * Converts a unique identifier from its string representation into
332 * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
337 HRESULT WINAPI
CLSIDFromString16(
338 LPCOLESTR16 idstr
, /* [in] string representation of guid */
339 CLSID
*id
) /* [out] GUID converted from string */
346 memset( id
, 0, sizeof (CLSID
) );
350 /* validate the CLSID string */
351 if (strlen(idstr
) != 38)
352 return CO_E_CLASSSTRING
;
354 s
= (const BYTE
*) idstr
;
355 if ((s
[0]!='{') || (s
[9]!='-') || (s
[14]!='-') || (s
[19]!='-') || (s
[24]!='-') || (s
[37]!='}'))
356 return CO_E_CLASSSTRING
;
358 for (i
=1; i
<37; i
++) {
359 if ((i
== 9)||(i
== 14)||(i
== 19)||(i
== 24)) continue;
360 if (!(((s
[i
] >= '0') && (s
[i
] <= '9')) ||
361 ((s
[i
] >= 'a') && (s
[i
] <= 'f')) ||
362 ((s
[i
] >= 'A') && (s
[i
] <= 'F'))))
363 return CO_E_CLASSSTRING
;
366 TRACE("%s -> %p\n", s
, id
);
368 /* quick lookup table */
369 memset(table
, 0, 256);
371 for (i
= 0; i
< 10; i
++) {
374 for (i
= 0; i
< 6; i
++) {
375 table
['A' + i
] = i
+10;
376 table
['a' + i
] = i
+10;
379 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
381 id
->Data1
= (table
[s
[1]] << 28 | table
[s
[2]] << 24 | table
[s
[3]] << 20 | table
[s
[4]] << 16 |
382 table
[s
[5]] << 12 | table
[s
[6]] << 8 | table
[s
[7]] << 4 | table
[s
[8]]);
383 id
->Data2
= table
[s
[10]] << 12 | table
[s
[11]] << 8 | table
[s
[12]] << 4 | table
[s
[13]];
384 id
->Data3
= table
[s
[15]] << 12 | table
[s
[16]] << 8 | table
[s
[17]] << 4 | table
[s
[18]];
386 /* these are just sequential bytes */
387 id
->Data4
[0] = table
[s
[20]] << 4 | table
[s
[21]];
388 id
->Data4
[1] = table
[s
[22]] << 4 | table
[s
[23]];
389 id
->Data4
[2] = table
[s
[25]] << 4 | table
[s
[26]];
390 id
->Data4
[3] = table
[s
[27]] << 4 | table
[s
[28]];
391 id
->Data4
[4] = table
[s
[29]] << 4 | table
[s
[30]];
392 id
->Data4
[5] = table
[s
[31]] << 4 | table
[s
[32]];
393 id
->Data4
[6] = table
[s
[33]] << 4 | table
[s
[34]];
394 id
->Data4
[7] = table
[s
[35]] << 4 | table
[s
[36]];
399 /******************************************************************************
400 * _xmalloc16 [internal]
401 * Allocates size bytes from the standard ole16 allocator.
404 * the allocated segmented pointer and a HRESULT
407 _xmalloc16(DWORD size
, SEGPTR
*ptr
) {
411 if (CoGetMalloc16(0,&mllc
))
412 return E_OUTOFMEMORY
;
414 args
[0] = (DWORD
)mllc
;
416 /* No need for a Callback entry, we have WOWCallback16Ex which does
417 * everything we need.
419 if (!WOWCallback16Ex(
420 (DWORD
)((const IMalloc16Vtbl
*)MapSL(
421 (SEGPTR
)((LPMALLOC16
)MapSL((SEGPTR
)mllc
))->lpVtbl
)
428 ERR("CallTo16 IMalloc16 (%d) failed\n",size
);
434 /******************************************************************************
435 * StringFromCLSID [COMPOBJ.19]
436 * Converts a GUID into the respective string representation.
437 * The target string is allocated using the OLE IMalloc.
440 * the string representation and HRESULT
443 HRESULT WINAPI
StringFromCLSID16(
444 REFCLSID id
, /* [in] the GUID to be converted */
445 LPOLESTR16
*idstr
) /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
450 ret
= _xmalloc16(40,(SEGPTR
*)idstr
);
453 StringFromGUID2( id
, buffer
, 40 );
454 WideCharToMultiByte( CP_ACP
, 0, buffer
, -1, MapSL((SEGPTR
)*idstr
), 40, NULL
, NULL
);
458 /******************************************************************************
459 * ProgIDFromCLSID [COMPOBJ.62]
461 * Converts a class id into the respective Program ID. (By using a registry lookup)
465 * riid associated with the progid
467 HRESULT WINAPI
ProgIDFromCLSID16(
468 REFCLSID clsid
, /* [in] class id as found in registry */
469 LPOLESTR16
*lplpszProgID
)/* [out] associated Program ID */
474 ret
= ProgIDFromCLSID( clsid
, &progid
);
477 INT len
= WideCharToMultiByte( CP_ACP
, 0, progid
, -1, NULL
, 0, NULL
, NULL
);
478 ret
= _xmalloc16(len
, (SEGPTR
*)lplpszProgID
);
480 WideCharToMultiByte( CP_ACP
, 0, progid
, -1, MapSL((SEGPTR
)*lplpszProgID
), len
, NULL
, NULL
);
481 CoTaskMemFree( progid
);
486 /***********************************************************************
487 * LookupETask (COMPOBJ.94)
489 HRESULT WINAPI
LookupETask16(HTASK16
*hTask
,LPVOID p
) {
490 FIXME("(%p,%p),stub!\n",hTask
,p
);
491 if ((*hTask
= GetCurrentTask()) == hETask
) {
492 memcpy(p
, Table_ETask
, sizeof(Table_ETask
));
497 /***********************************************************************
498 * SetETask (COMPOBJ.95)
500 HRESULT WINAPI
SetETask16(HTASK16 hTask
, LPVOID p
) {
501 FIXME("(%04x,%p),stub!\n",hTask
,p
);
506 /***********************************************************************
507 * CALLOBJECTINWOW (COMPOBJ.201)
509 HRESULT WINAPI
CallObjectInWOW(LPVOID p1
,LPVOID p2
) {
510 FIXME("(%p,%p),stub!\n",p1
,p2
);
514 /******************************************************************************
515 * CoRegisterClassObject [COMPOBJ.5]
517 * Don't know where it registers it ...
519 HRESULT WINAPI
CoRegisterClassObject16(
522 DWORD dwClsContext
, /* [in] CLSCTX flags indicating the context in which to run the executable */
523 DWORD flags
, /* [in] REGCLS flags indicating how connections are made */
526 FIXME("(%s,%p,0x%08x,0x%08x,%p),stub\n",
527 debugstr_guid(rclsid
),pUnk
,dwClsContext
,flags
,lpdwRegister
532 /******************************************************************************
533 * CoRevokeClassObject [COMPOBJ.6]
536 HRESULT WINAPI
CoRevokeClassObject16(DWORD dwRegister
) /* [in] token on class obj */
538 FIXME("(0x%08x),stub!\n", dwRegister
);
542 /******************************************************************************
543 * IsValidInterface [COMPOBJ.23]
545 * Determines whether a pointer is a valid interface.
548 * punk [I] Interface to be tested.
551 * TRUE, if the passed pointer is a valid interface, or FALSE otherwise.
553 BOOL WINAPI
IsValidInterface16(SEGPTR punk
)
557 if (IsBadReadPtr16(punk
,4))
560 if (IsBadReadPtr16((SEGPTR
)ptr
[0],4)) /* check vtable ptr */
562 ptr
= MapSL((SEGPTR
)ptr
[0]); /* ptr to first method */
563 if (IsBadReadPtr16((SEGPTR
)ptr
[0],2))
568 /******************************************************************************
569 * CoFileTimeToDosDateTime [COMPOBJ.30]
571 BOOL16 WINAPI
CoFileTimeToDosDateTime16(const FILETIME
*ft
, LPWORD lpDosDate
, LPWORD lpDosTime
)
573 return FileTimeToDosDateTime(ft
, lpDosDate
, lpDosTime
);
576 /******************************************************************************
577 * CoDosDateTimeToFileTime [COMPOBJ.31]
579 BOOL16 WINAPI
CoDosDateTimeToFileTime16(WORD wDosDate
, WORD wDosTime
, FILETIME
*ft
)
581 return DosDateTimeToFileTime(wDosDate
, wDosTime
, ft
);
584 /******************************************************************************
585 * CoGetCurrentProcess [COMPOBJ.34]
587 DWORD WINAPI
CoGetCurrentProcess16(void)
589 return CoGetCurrentProcess();
592 /******************************************************************************
593 * CoRegisterMessageFilter [COMPOBJ.27]
595 HRESULT WINAPI
CoRegisterMessageFilter16(
596 LPMESSAGEFILTER lpMessageFilter
,
597 LPMESSAGEFILTER
*lplpMessageFilter
599 FIXME("(%p,%p),stub!\n",lpMessageFilter
,lplpMessageFilter
);
603 /******************************************************************************
604 * CoLockObjectExternal [COMPOBJ.63]
606 HRESULT WINAPI
CoLockObjectExternal16(
607 LPUNKNOWN pUnk
, /* [in] object to be locked */
608 BOOL16 fLock
, /* [in] do lock */
609 BOOL16 fLastUnlockReleases
/* [in] ? */
611 FIXME("(%p,%d,%d),stub!\n",pUnk
,fLock
,fLastUnlockReleases
);
615 /***********************************************************************
616 * CoGetState [COMPOBJ.115]
618 HRESULT WINAPI
CoGetState16(LPDWORD state
)
620 FIXME("(%p),stub!\n", state
);
626 /***********************************************************************
627 * DllEntryPoint [COMPOBJ.116]
629 * Initialization code for the COMPOBJ DLL
633 BOOL WINAPI
COMPOBJ_DllEntryPoint(DWORD Reason
, HINSTANCE16 hInst
, WORD ds
, WORD HeapSize
, DWORD res1
, WORD res2
)
635 TRACE("(%08x, %04x, %04x, %04x, %08x, %04x)\n", Reason
, hInst
, ds
, HeapSize
, res1
, res2
);
639 /***********************************************************************
640 * CoMemAlloc [COMPOBJ.151]
642 SEGPTR WINAPI
CoMemAlloc(DWORD size
, DWORD dwMemContext
, DWORD x
) {
646 /* FIXME: check context handling */
647 TRACE("(%d, 0x%08x, 0x%08x)\n", size
, dwMemContext
, x
);
648 hres
= _xmalloc16(size
, &segptr
);
654 /******************************************************************************
655 * CLSIDFromProgID [COMPOBJ.61]
657 * Converts a program ID into the respective GUID.
660 * progid [I] program id as found in registry
661 * riid [O] associated CLSID
665 * Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
667 HRESULT WINAPI
CLSIDFromProgID16(LPCOLESTR16 progid
, LPCLSID riid
)
673 buf
= HeapAlloc(GetProcessHeap(),0,strlen(progid
)+8);
674 sprintf(buf
,"%s\\CLSID",progid
);
675 if (RegOpenKeyA(HKEY_CLASSES_ROOT
,buf
,&xhkey
)) {
676 HeapFree(GetProcessHeap(),0,buf
);
677 return CO_E_CLASSSTRING
;
679 HeapFree(GetProcessHeap(),0,buf
);
680 buf2len
= sizeof(buf2
);
681 if (RegQueryValueA(xhkey
,NULL
,buf2
,&buf2len
)) {
683 return CO_E_CLASSSTRING
;
686 return CLSIDFromString16(buf2
,riid
);
689 /******************************************************************************
690 * StringFromGUID2 [COMPOBJ.76]
692 INT WINAPI
StringFromGUID216(REFGUID id
, LPOLESTR str
, INT cmax
)
694 return StringFromGUID2( id
, str
, cmax
);
698 /***********************************************************************
699 * CoFileTimeNow [COMPOBJ.82]
701 HRESULT WINAPI
CoFileTimeNow16( FILETIME
*lpFileTime
)
703 return CoFileTimeNow( lpFileTime
);
706 /***********************************************************************
707 * CoGetClassObject [COMPOBJ.7]
710 HRESULT WINAPI
CoGetClassObject16(
711 REFCLSID rclsid
, DWORD dwClsContext
, COSERVERINFO
*pServerInfo
,
712 REFIID iid
, LPVOID
*ppv
)
714 FIXME(", stub!\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid
), debugstr_guid(iid
));
717 FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo
->pwszName
));
718 FIXME("\t\tpAuthInfo=%p\n",pServerInfo
->pAuthInfo
);
723 /******************************************************************************
724 * CoCreateGuid [COMPOBJ.73]
726 HRESULT WINAPI
CoCreateGuid16(GUID
*pguid
)
728 return CoCreateGuid( pguid
);
731 /***********************************************************************
732 * CoCreateInstance [COMPOBJ.13]
734 HRESULT WINAPI
CoCreateInstance16(
741 FIXME("(%s, %p, %x, %s, %p), stub!\n",
742 debugstr_guid(rclsid
), pUnkOuter
, dwClsContext
, debugstr_guid(iid
),
748 /***********************************************************************
749 * CoDisconnectObject [COMPOBJ.15]
751 HRESULT WINAPI
CoDisconnectObject16( LPUNKNOWN lpUnk
, DWORD reserved
)
753 FIXME("(%p, 0x%08x): stub!\n", lpUnk
, reserved
);