Implemented registry MIME type functions @324-329.
[wine] / dlls / ole32 / ole16.c
1 /*
2  * 16 bit ole functions
3  *
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
9  *
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.
14  *
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.
19  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31
32 #include "windef.h"
33 #include "objbase.h"
34 #include "ole2.h"
35 #include "ole2ver.h"
36 #include "rpc.h"
37 #include "winerror.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "wtypes.h"
41 #include "wine/unicode.h"
42 #include "wine/obj_base.h"
43 #include "wine/obj_clientserver.h"
44 #include "wine/obj_misc.h"
45 #include "wine/obj_marshal.h"
46 #include "wine/obj_storage.h"
47 #include "wine/obj_channel.h"
48 #include "wine/winbase16.h"
49 #include "compobj_private.h"
50 #include "ifs.h"
51 #include "wine/winbase16.h"
52
53 #include "wine/debug.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(ole);
56
57 HINSTANCE16     COMPOBJ_hInstance = 0;
58 static int      COMPOBJ_Attach = 0;
59
60 HTASK16 hETask = 0;
61 WORD Table_ETask[62];
62
63 LPMALLOC16 currentMalloc16=NULL;
64
65 /* --- IMalloc16 implementation */
66
67
68 typedef struct
69 {
70         /* IUnknown fields */
71         ICOM_VFIELD(IMalloc16);
72         DWORD                   ref;
73         /* IMalloc16 fields */
74 } IMalloc16Impl;
75
76 /******************************************************************************
77  *              IMalloc16_QueryInterface        [COMPOBJ.500]
78  */
79 HRESULT WINAPI IMalloc16_fnQueryInterface(IMalloc16* iface,REFIID refiid,LPVOID *obj) {
80         ICOM_THIS(IMalloc16Impl,iface);
81
82         TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
83         if (    !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
84                 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
85         ) {
86                 *obj = This;
87                 return 0;
88         }
89         return OLE_E_ENUM_NOMORE;
90 }
91
92 /******************************************************************************
93  *              IMalloc16_AddRef        [COMPOBJ.501]
94  */
95 ULONG WINAPI IMalloc16_fnAddRef(IMalloc16* iface) {
96         ICOM_THIS(IMalloc16Impl,iface);
97         TRACE("(%p)->AddRef()\n",This);
98         return 1; /* cannot be freed */
99 }
100
101 /******************************************************************************
102  *              IMalloc16_Release       [COMPOBJ.502]
103  */
104 ULONG WINAPI IMalloc16_fnRelease(IMalloc16* iface) {
105         ICOM_THIS(IMalloc16Impl,iface);
106         TRACE("(%p)->Release()\n",This);
107         return 1; /* cannot be freed */
108 }
109
110 /******************************************************************************
111  * IMalloc16_Alloc [COMPOBJ.503]
112  */
113 SEGPTR WINAPI IMalloc16_fnAlloc(IMalloc16* iface,DWORD cb) {
114         ICOM_THIS(IMalloc16Impl,iface);
115         TRACE("(%p)->Alloc(%ld)\n",This,cb);
116         return MapLS( HeapAlloc( GetProcessHeap(), 0, cb ) );
117 }
118
119 /******************************************************************************
120  * IMalloc16_Realloc [COMPOBJ.504]
121  */
122 SEGPTR WINAPI IMalloc16_fnRealloc(IMalloc16* iface,SEGPTR pv,DWORD cb)
123 {
124     SEGPTR ret;
125     ICOM_THIS(IMalloc16Impl,iface);
126     TRACE("(%p)->Realloc(%08lx,%ld)\n",This,pv,cb);
127     ret = MapLS( HeapReAlloc( GetProcessHeap(), 0, MapSL(pv), cb ) );
128     UnMapLS(pv);
129     return ret;
130 }
131
132 /******************************************************************************
133  * IMalloc16_Free [COMPOBJ.505]
134  */
135 VOID WINAPI IMalloc16_fnFree(IMalloc16* iface,SEGPTR pv)
136 {
137     void *ptr = MapSL(pv);
138     ICOM_THIS(IMalloc16Impl,iface);
139     TRACE("(%p)->Free(%08lx)\n",This,pv);
140     UnMapLS(pv);
141     HeapFree( GetProcessHeap(), 0, ptr );
142 }
143
144 /******************************************************************************
145  * IMalloc16_GetSize [COMPOBJ.506]
146  */
147 DWORD WINAPI IMalloc16_fnGetSize(const IMalloc16* iface,SEGPTR pv)
148 {
149         ICOM_CTHIS(IMalloc16Impl,iface);
150         TRACE("(%p)->GetSize(%08lx)\n",This,pv);
151         return HeapSize( GetProcessHeap(), 0, MapSL(pv) );
152 }
153
154 /******************************************************************************
155  * IMalloc16_DidAlloc [COMPOBJ.507]
156  */
157 INT16 WINAPI IMalloc16_fnDidAlloc(const IMalloc16* iface,LPVOID pv) {
158         ICOM_CTHIS(IMalloc16,iface);
159         TRACE("(%p)->DidAlloc(%p)\n",This,pv);
160         return (INT16)-1;
161 }
162
163 /******************************************************************************
164  * IMalloc16_HeapMinimize [COMPOBJ.508]
165  */
166 LPVOID WINAPI IMalloc16_fnHeapMinimize(IMalloc16* iface) {
167         ICOM_THIS(IMalloc16Impl,iface);
168         TRACE("(%p)->HeapMinimize()\n",This);
169         return NULL;
170 }
171
172 /******************************************************************************
173  * IMalloc16_Constructor [VTABLE]
174  */
175 LPMALLOC16
176 IMalloc16_Constructor()
177 {
178     static ICOM_VTABLE(IMalloc16) vt16;
179     static SEGPTR msegvt16;
180     IMalloc16Impl* This;
181     HMODULE16 hcomp = GetModuleHandle16("COMPOBJ");
182
183     This = HeapAlloc( GetProcessHeap(), 0, sizeof(IMalloc16Impl) );
184     if (!msegvt16)
185     {
186 #define VTENT(x) vt16.x = (void*)GetProcAddress16(hcomp,"IMalloc16_"#x);assert(vt16.x)
187         VTENT(QueryInterface);
188         VTENT(AddRef);
189         VTENT(Release);
190         VTENT(Alloc);
191         VTENT(Realloc);
192         VTENT(Free);
193         VTENT(GetSize);
194         VTENT(DidAlloc);
195         VTENT(HeapMinimize);
196 #undef VTENT
197         msegvt16 = MapLS( &vt16 );
198     }
199     ICOM_VTBL(This) = (ICOM_VTABLE(IMalloc16)*)msegvt16;
200     This->ref = 1;
201     return (LPMALLOC16)MapLS( This );
202 }
203
204
205 /***********************************************************************
206  *           CoGetMalloc    [COMPOBJ.4]
207  * RETURNS
208  *      The current win16 IMalloc
209  */
210 HRESULT WINAPI CoGetMalloc16(
211         DWORD dwMemContext,     /* [in] unknown */
212         LPMALLOC16 * lpMalloc   /* [out] current win16 malloc interface */
213 ) {
214     if(!currentMalloc16)
215         currentMalloc16 = IMalloc16_Constructor();
216     *lpMalloc = currentMalloc16;
217     return S_OK;
218 }
219
220 /***********************************************************************
221  *           CoCreateStandardMalloc [COMPOBJ.71]
222  */
223 HRESULT WINAPI CoCreateStandardMalloc16(DWORD dwMemContext,
224                                           LPMALLOC16 *lpMalloc)
225 {
226     /* FIXME: docu says we shouldn't return the same allocator as in
227      * CoGetMalloc16 */
228     *lpMalloc = IMalloc16_Constructor();
229     return S_OK;
230 }
231
232 /******************************************************************************
233  *              CoInitialize    [COMPOBJ.2]
234  * Set the win16 IMalloc used for memory management
235  */
236 HRESULT WINAPI CoInitialize16(
237         LPVOID lpReserved       /* [in] pointer to win16 malloc interface */
238 ) {
239     currentMalloc16 = (LPMALLOC16)lpReserved;
240     return S_OK;
241 }
242
243 /***********************************************************************
244  *           CoUninitialize   [COMPOBJ.3]
245  * Don't know what it does.
246  * 3-Nov-98 -- this was originally misspelled, I changed it to what I
247  *   believe is the correct spelling
248  */
249 void WINAPI CoUninitialize16(void)
250 {
251   TRACE("()\n");
252   CoFreeAllLibraries();
253 }
254
255 /***********************************************************************
256  *           IsEqualGUID [COMPOBJ.18]
257  *
258  * Compares two Unique Identifiers.
259  *
260  * RETURNS
261  *      TRUE if equal
262  */
263 BOOL16 WINAPI IsEqualGUID16(
264         GUID* g1,       /* [in] unique id 1 */
265         GUID* g2)       /* [in] unique id 2 */
266 {
267     return !memcmp( g1, g2, sizeof(GUID) );
268 }
269
270 /******************************************************************************
271  *              CLSIDFromString [COMPOBJ.20]
272  * Converts a unique identifier from its string representation into
273  * the GUID struct.
274  *
275  * Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6]
276  *
277  * RETURNS
278  *      the converted GUID
279  */
280 HRESULT WINAPI CLSIDFromString16(
281         LPCOLESTR16 idstr,      /* [in] string representation of guid */
282         CLSID *id)              /* [out] GUID converted from string */
283 {
284
285   return __CLSIDFromStringA(idstr,id);
286 }
287
288 extern BOOL WINAPI K32WOWCallback16Ex(  DWORD vpfn16, DWORD dwFlags,
289                                         DWORD cbArgs, LPVOID pArgs,
290                                         LPDWORD pdwRetCode );
291
292 /******************************************************************************
293  *              _xmalloc16      [internal]
294  * Allocates size bytes from the standard ole16 allocator.
295  *
296  * RETURNS
297  *      the allocated segmented pointer and a HRESULT
298  */
299 HRESULT
300 _xmalloc16(DWORD size, SEGPTR *ptr) {
301   LPMALLOC16 mllc;
302   DWORD args[2];
303
304   if (CoGetMalloc16(0,&mllc))
305     return E_OUTOFMEMORY;
306
307   args[0] = (DWORD)mllc;
308   args[1] = size;
309   /* No need for a Callback entry, we have WOWCallback16Ex which does
310    * everything we need.
311    */
312   if (!K32WOWCallback16Ex(
313       (DWORD)((ICOM_VTABLE(IMalloc16)*)MapSL(
314           (SEGPTR)ICOM_VTBL(((LPMALLOC16)MapSL((SEGPTR)mllc))))
315       )->Alloc,
316       WCB16_CDECL,
317       2*sizeof(DWORD),
318       (LPVOID)args,
319       (LPDWORD)ptr
320   )) {
321       ERR("CallTo16 IMalloc16 (%ld) failed\n",size);
322       return E_FAIL;
323   }
324   return S_OK;
325 }
326
327 /******************************************************************************
328  *              StringFromCLSID [COMPOBJ.19]
329  * Converts a GUID into the respective string representation.
330  * The target string is allocated using the OLE IMalloc.
331  *
332  * RETURNS
333  *      the string representation and HRESULT
334  */
335
336 HRESULT WINAPI StringFromCLSID16(
337   REFCLSID id,          /* [in] the GUID to be converted */
338   LPOLESTR16 *idstr     /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
339
340 ) {
341   HRESULT ret;
342
343   ret = _xmalloc16(40,(SEGPTR*)idstr);
344   if (ret != S_OK)
345     return ret;
346   return WINE_StringFromCLSID(id,MapSL((SEGPTR)*idstr));
347 }
348
349 /******************************************************************************
350  * ProgIDFromCLSID [COMPOBJ.62]
351  * Converts a class id into the respective Program ID. (By using a registry lookup)
352  * RETURNS S_OK on success
353  * riid associated with the progid
354  */
355 HRESULT WINAPI ProgIDFromCLSID16(
356   REFCLSID clsid, /* [in] class id as found in registry */
357   LPOLESTR16 *lplpszProgID/* [out] associated Prog ID */
358 ) {
359   char     strCLSID[50], *buf, *buf2;
360   DWORD    buf2len;
361   HKEY     xhkey;
362   HRESULT  ret = S_OK;
363
364   WINE_StringFromCLSID(clsid, strCLSID);
365
366   buf = HeapAlloc(GetProcessHeap(), 0, strlen(strCLSID)+14);
367   sprintf(buf,"CLSID\\%s\\ProgID", strCLSID);
368   if (RegOpenKeyA(HKEY_CLASSES_ROOT, buf, &xhkey))
369     ret = REGDB_E_CLASSNOTREG;
370
371   HeapFree(GetProcessHeap(), 0, buf);
372
373   if (ret == S_OK)
374   {
375     buf2 = HeapAlloc(GetProcessHeap(), 0, 255);
376     buf2len = 255;
377     if (RegQueryValueA(xhkey, NULL, buf2, &buf2len))
378       ret = REGDB_E_CLASSNOTREG;
379
380     if (ret == S_OK)
381     {
382       ret = _xmalloc16(buf2len+1, (SEGPTR*)lplpszProgID);
383       if (ret != S_OK)
384         return ret;
385       strcpy(MapSL((SEGPTR)*lplpszProgID),buf2);
386       ret = S_OK;
387     }
388     HeapFree(GetProcessHeap(), 0, buf2);
389   }
390   RegCloseKey(xhkey);
391   return ret;
392 }
393
394 /******************************************************************************
395  *              CLSIDFromProgID [COMPOBJ.61]
396  * Converts a program id into the respective GUID. (By using a registry lookup)
397  * RETURNS
398  *      riid associated with the progid
399  */
400 HRESULT WINAPI CLSIDFromProgID16(
401         LPCOLESTR16 progid,     /* [in] program id as found in registry */
402         LPCLSID riid            /* [out] associated CLSID */
403 ) {
404         char    *buf,buf2[80];
405         DWORD   buf2len;
406         HRESULT err;
407         HKEY    xhkey;
408
409         buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
410         sprintf(buf,"%s\\CLSID",progid);
411         if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
412                 HeapFree(GetProcessHeap(),0,buf);
413                 return CO_E_CLASSSTRING;
414         }
415         HeapFree(GetProcessHeap(),0,buf);
416         buf2len = sizeof(buf2);
417         if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
418                 RegCloseKey(xhkey);
419                 return CO_E_CLASSSTRING;
420         }
421         RegCloseKey(xhkey);
422         return __CLSIDFromStringA(buf2,riid);
423 }
424
425 /***********************************************************************
426  *           LookupETask (COMPOBJ.94)
427  */
428 HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
429         FIXME("(%p,%p),stub!\n",hTask,p);
430         if ((*hTask = GetCurrentTask()) == hETask) {
431                 memcpy(p, Table_ETask, sizeof(Table_ETask));
432         }
433         return 0;
434 }
435
436 /***********************************************************************
437  *           SetETask (COMPOBJ.95)
438  */
439 HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
440         FIXME("(%04x,%p),stub!\n",hTask,p);
441         hETask = hTask;
442         return 0;
443 }
444
445 /***********************************************************************
446  *           CALLOBJECTINWOW (COMPOBJ.201)
447  */
448 HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
449         FIXME("(%p,%p),stub!\n",p1,p2);
450         return 0;
451 }
452
453 /******************************************************************************
454  *              CoRegisterClassObject   [COMPOBJ.5]
455  *
456  * Don't know where it registers it ...
457  */
458 HRESULT WINAPI CoRegisterClassObject16(
459         REFCLSID rclsid,
460         LPUNKNOWN pUnk,
461         DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
462         DWORD flags,        /* [in] REGCLS flags indicating how connections are made */
463         LPDWORD lpdwRegister
464 ) {
465         char    buf[80];
466
467         WINE_StringFromCLSID(rclsid,buf);
468
469         FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
470                 buf,pUnk,dwClsContext,flags,lpdwRegister
471         );
472         return 0;
473 }
474
475 /******************************************************************************
476  *      CoRevokeClassObject [COMPOBJ.6]
477  *
478  */
479 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
480 {
481     FIXME("(0x%08lx),stub!\n", dwRegister);
482     return 0;
483 }
484
485 /******************************************************************************
486  *      CoFileTimeToDosDateTime [COMPOBJ.30]
487  */
488 BOOL16 WINAPI CoFileTimeToDosDateTime16(const FILETIME *ft, LPWORD lpDosDate, LPWORD lpDosTime)
489 {
490     return FileTimeToDosDateTime(ft, lpDosDate, lpDosTime);
491 }
492
493 /******************************************************************************
494  *      CoDosDateTimeToFileTime [COMPOBJ.31]
495  */
496 BOOL16 WINAPI CoDosDateTimeToFileTime16(WORD wDosDate, WORD wDosTime, FILETIME *ft)
497 {
498     return DosDateTimeToFileTime(wDosDate, wDosTime, ft);
499 }
500
501 /******************************************************************************
502  *              CoRegisterMessageFilter [COMPOBJ.27]
503  */
504 HRESULT WINAPI CoRegisterMessageFilter16(
505         LPMESSAGEFILTER lpMessageFilter,
506         LPMESSAGEFILTER *lplpMessageFilter
507 ) {
508         FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
509         return 0;
510 }
511
512 /******************************************************************************
513  *              CoLockObjectExternal    [COMPOBJ.63]
514  */
515 HRESULT WINAPI CoLockObjectExternal16(
516     LPUNKNOWN pUnk,             /* [in] object to be locked */
517     BOOL16 fLock,               /* [in] do lock */
518     BOOL16 fLastUnlockReleases  /* [in] ? */
519 ) {
520     FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
521     return S_OK;
522 }
523
524 /***********************************************************************
525  *           CoGetState [COMPOBJ.115]
526  */
527 HRESULT WINAPI CoGetState16(LPDWORD state)
528 {
529     FIXME("(%p),stub!\n", state);
530
531     *state = 0;
532     return S_OK;
533 }
534
535 /***********************************************************************
536  *      DllEntryPoint                   [COMPOBJ.116]
537  *
538  *    Initialization code for the COMPOBJ DLL
539  *
540  * RETURNS:
541  */
542 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
543 {
544         TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize, res1, res2);
545         switch(Reason)
546         {
547         case DLL_PROCESS_ATTACH:
548                 if (!COMPOBJ_Attach++) COMPOBJ_hInstance = hInst;
549                 break;
550
551         case DLL_PROCESS_DETACH:
552                 if(!--COMPOBJ_Attach)
553                         COMPOBJ_hInstance = 0;
554                 break;
555         }
556         return TRUE;
557 }