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