ole: Fix mis-handling of return value in StgStreamImpl_Read.
[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 static HTASK16 hETask = 0;
54 static WORD Table_ETask[62];
55
56 static LPMALLOC16 currentMalloc16=NULL;
57
58 /* --- IMalloc16 implementation */
59
60
61 typedef struct
62 {
63         /* IUnknown fields */
64         const IMalloc16Vtbl    *lpVtbl;
65         DWORD                   ref;
66         /* IMalloc16 fields */
67 } IMalloc16Impl;
68
69 /******************************************************************************
70  *              IMalloc16_QueryInterface        [COMPOBJ.500]
71  */
72 HRESULT IMalloc16_fnQueryInterface(IMalloc16* iface,REFIID refiid,LPVOID *obj) {
73         IMalloc16Impl *This = (IMalloc16Impl *)iface;
74
75         TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(refiid),obj);
76         if (    !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
77                 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
78         ) {
79                 *obj = This;
80                 return 0;
81         }
82         return OLE_E_ENUM_NOMORE;
83 }
84
85 /******************************************************************************
86  *              IMalloc16_AddRef        [COMPOBJ.501]
87  */
88 ULONG IMalloc16_fnAddRef(IMalloc16* iface) {
89         IMalloc16Impl *This = (IMalloc16Impl *)iface;
90         TRACE("(%p)->AddRef()\n",This);
91         return 1; /* cannot be freed */
92 }
93
94 /******************************************************************************
95  *              IMalloc16_Release       [COMPOBJ.502]
96  */
97 ULONG IMalloc16_fnRelease(IMalloc16* iface) {
98         IMalloc16Impl *This = (IMalloc16Impl *)iface;
99         TRACE("(%p)->Release()\n",This);
100         return 1; /* cannot be freed */
101 }
102
103 /******************************************************************************
104  * IMalloc16_Alloc [COMPOBJ.503]
105  */
106 SEGPTR IMalloc16_fnAlloc(IMalloc16* iface,DWORD cb) {
107         IMalloc16Impl *This = (IMalloc16Impl *)iface;
108         TRACE("(%p)->Alloc(%ld)\n",This,cb);
109         return MapLS( HeapAlloc( GetProcessHeap(), 0, cb ) );
110 }
111
112 /******************************************************************************
113  * IMalloc16_Free [COMPOBJ.505]
114  */
115 VOID IMalloc16_fnFree(IMalloc16* iface,SEGPTR pv)
116 {
117     void *ptr = MapSL(pv);
118     IMalloc16Impl *This = (IMalloc16Impl *)iface;
119     TRACE("(%p)->Free(%08lx)\n",This,pv);
120     UnMapLS(pv);
121     HeapFree( GetProcessHeap(), 0, ptr );
122 }
123
124 /******************************************************************************
125  * IMalloc16_Realloc [COMPOBJ.504]
126  */
127 SEGPTR IMalloc16_fnRealloc(IMalloc16* iface,SEGPTR pv,DWORD cb)
128 {
129     SEGPTR ret;
130     IMalloc16Impl *This = (IMalloc16Impl *)iface;
131     TRACE("(%p)->Realloc(%08lx,%ld)\n",This,pv,cb);
132     if (!pv) 
133         ret = IMalloc16_fnAlloc(iface, cb);
134     else if (cb) {
135         ret = MapLS( HeapReAlloc( GetProcessHeap(), 0, MapSL(pv), cb ) );
136         UnMapLS(pv);
137     } else {
138         IMalloc16_fnFree(iface, pv);
139         ret = 0;
140     }
141     return ret;
142 }
143
144 /******************************************************************************
145  * IMalloc16_GetSize [COMPOBJ.506]
146  */
147 DWORD IMalloc16_fnGetSize(IMalloc16* iface,SEGPTR pv)
148 {
149         IMalloc16Impl *This = (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 IMalloc16_fnDidAlloc(IMalloc16* iface,LPVOID pv) {
158         IMalloc16 *This = (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 IMalloc16_fnHeapMinimize(IMalloc16* iface) {
167         IMalloc16Impl *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(void)
177 {
178     static IMalloc16Vtbl 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     This->lpVtbl = (const IMalloc16Vtbl*)msegvt16;
200     This->ref = 1;
201     return (LPMALLOC16)MapLS( This );
202 }
203
204
205 /***********************************************************************
206  *           CoGetMalloc    [COMPOBJ.4]
207  *
208  * Retrieve the current win16 IMalloc interface.
209  *
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 /******************************************************************************
292  *              _xmalloc16      [internal]
293  * Allocates size bytes from the standard ole16 allocator.
294  *
295  * RETURNS
296  *      the allocated segmented pointer and a HRESULT
297  */
298 static HRESULT
299 _xmalloc16(DWORD size, SEGPTR *ptr) {
300   LPMALLOC16 mllc;
301   DWORD args[2];
302
303   if (CoGetMalloc16(0,&mllc))
304     return E_OUTOFMEMORY;
305
306   args[0] = (DWORD)mllc;
307   args[1] = size;
308   /* No need for a Callback entry, we have WOWCallback16Ex which does
309    * everything we need.
310    */
311   if (!WOWCallback16Ex(
312       (DWORD)((const IMalloc16Vtbl*)MapSL(
313           (SEGPTR)((LPMALLOC16)MapSL((SEGPTR)mllc))->lpVtbl  )
314       )->Alloc,
315       WCB16_CDECL,
316       2*sizeof(DWORD),
317       (LPVOID)args,
318       (LPDWORD)ptr
319   )) {
320       ERR("CallTo16 IMalloc16 (%ld) failed\n",size);
321       return E_FAIL;
322   }
323   return S_OK;
324 }
325
326 /******************************************************************************
327  *              StringFromCLSID [COMPOBJ.19]
328  * Converts a GUID into the respective string representation.
329  * The target string is allocated using the OLE IMalloc.
330  *
331  * RETURNS
332  *      the string representation and HRESULT
333  */
334
335 HRESULT WINAPI StringFromCLSID16(
336   REFCLSID id,          /* [in] the GUID to be converted */
337   LPOLESTR16 *idstr     /* [out] a pointer to a to-be-allocated segmented pointer pointing to the resulting string */
338
339 ) {
340   HRESULT ret;
341
342   ret = _xmalloc16(40,(SEGPTR*)idstr);
343   if (ret != S_OK)
344     return ret;
345   return WINE_StringFromCLSID(id,MapSL((SEGPTR)*idstr));
346 }
347
348 /******************************************************************************
349  * ProgIDFromCLSID [COMPOBJ.62]
350  *
351  * Converts a class id into the respective Program ID. (By using a registry lookup)
352  *
353  * RETURNS
354  *  S_OK on success
355  *  riid associated with the progid
356  */
357 HRESULT WINAPI ProgIDFromCLSID16(
358   REFCLSID clsid, /* [in] class id as found in registry */
359   LPOLESTR16 *lplpszProgID/* [out] associated Prog ID */
360 ) {
361   static const WCHAR wszProgID[] = {'P','r','o','g','I','D',0};
362   HKEY     hkey;
363   HRESULT  ret;
364   LONG     len;
365   char    *buffer;
366
367   ret = COM_OpenKeyForCLSID(clsid, wszProgID, KEY_READ, &hkey);
368   if (FAILED(ret))
369     return ret;
370   
371   if (RegQueryValueA(hkey, NULL, NULL, &len))
372     ret = REGDB_E_READREGDB;
373
374   if (ret == S_OK)
375   {
376     buffer = HeapAlloc(GetProcessHeap(), 0, len);
377     if (RegQueryValueA(hkey, NULL, buffer, &len))
378       ret = REGDB_E_READREGDB;
379
380     if (ret == S_OK)
381     {
382       ret = _xmalloc16(len, (SEGPTR*)lplpszProgID);
383       if (ret == S_OK)
384         strcpy(MapSL((SEGPTR)*lplpszProgID),buffer);
385     }
386     HeapFree(GetProcessHeap(), 0, buffer);
387   }
388   RegCloseKey(hkey);
389   return ret;
390 }
391
392 /***********************************************************************
393  *           LookupETask (COMPOBJ.94)
394  */
395 HRESULT WINAPI LookupETask16(HTASK16 *hTask,LPVOID p) {
396         FIXME("(%p,%p),stub!\n",hTask,p);
397         if ((*hTask = GetCurrentTask()) == hETask) {
398                 memcpy(p, Table_ETask, sizeof(Table_ETask));
399         }
400         return 0;
401 }
402
403 /***********************************************************************
404  *           SetETask (COMPOBJ.95)
405  */
406 HRESULT WINAPI SetETask16(HTASK16 hTask, LPVOID p) {
407         FIXME("(%04x,%p),stub!\n",hTask,p);
408         hETask = hTask;
409         return 0;
410 }
411
412 /***********************************************************************
413  *           CALLOBJECTINWOW (COMPOBJ.201)
414  */
415 HRESULT WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
416         FIXME("(%p,%p),stub!\n",p1,p2);
417         return 0;
418 }
419
420 /******************************************************************************
421  *              CoRegisterClassObject   [COMPOBJ.5]
422  *
423  * Don't know where it registers it ...
424  */
425 HRESULT WINAPI CoRegisterClassObject16(
426         REFCLSID rclsid,
427         LPUNKNOWN pUnk,
428         DWORD dwClsContext, /* [in] CLSCTX flags indicating the context in which to run the executable */
429         DWORD flags,        /* [in] REGCLS flags indicating how connections are made */
430         LPDWORD lpdwRegister
431 ) {
432         FIXME("(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
433                 debugstr_guid(rclsid),pUnk,dwClsContext,flags,lpdwRegister
434         );
435         return 0;
436 }
437
438 /******************************************************************************
439  *      CoRevokeClassObject [COMPOBJ.6]
440  *
441  */
442 HRESULT WINAPI CoRevokeClassObject16(DWORD dwRegister) /* [in] token on class obj */
443 {
444     FIXME("(0x%08lx),stub!\n", dwRegister);
445     return 0;
446 }
447
448 /******************************************************************************
449  *      CoFileTimeToDosDateTime [COMPOBJ.30]
450  */
451 BOOL16 WINAPI CoFileTimeToDosDateTime16(const FILETIME *ft, LPWORD lpDosDate, LPWORD lpDosTime)
452 {
453     return FileTimeToDosDateTime(ft, lpDosDate, lpDosTime);
454 }
455
456 /******************************************************************************
457  *      CoDosDateTimeToFileTime [COMPOBJ.31]
458  */
459 BOOL16 WINAPI CoDosDateTimeToFileTime16(WORD wDosDate, WORD wDosTime, FILETIME *ft)
460 {
461     return DosDateTimeToFileTime(wDosDate, wDosTime, ft);
462 }
463
464 /******************************************************************************
465  *              CoRegisterMessageFilter [COMPOBJ.27]
466  */
467 HRESULT WINAPI CoRegisterMessageFilter16(
468         LPMESSAGEFILTER lpMessageFilter,
469         LPMESSAGEFILTER *lplpMessageFilter
470 ) {
471         FIXME("(%p,%p),stub!\n",lpMessageFilter,lplpMessageFilter);
472         return 0;
473 }
474
475 /******************************************************************************
476  *              CoLockObjectExternal    [COMPOBJ.63]
477  */
478 HRESULT WINAPI CoLockObjectExternal16(
479     LPUNKNOWN pUnk,             /* [in] object to be locked */
480     BOOL16 fLock,               /* [in] do lock */
481     BOOL16 fLastUnlockReleases  /* [in] ? */
482 ) {
483     FIXME("(%p,%d,%d),stub!\n",pUnk,fLock,fLastUnlockReleases);
484     return S_OK;
485 }
486
487 /***********************************************************************
488  *           CoGetState [COMPOBJ.115]
489  */
490 HRESULT WINAPI CoGetState16(LPDWORD state)
491 {
492     FIXME("(%p),stub!\n", state);
493
494     *state = 0;
495     return S_OK;
496 }
497
498 /***********************************************************************
499  *      DllEntryPoint                   [COMPOBJ.116]
500  *
501  *    Initialization code for the COMPOBJ DLL
502  *
503  * RETURNS:
504  */
505 BOOL WINAPI COMPOBJ_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst, WORD ds, WORD HeapSize, DWORD res1, WORD res2)
506 {
507         TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n", Reason, hInst, ds, HeapSize, res1, res2);
508         return TRUE;
509 }
510
511 /***********************************************************************
512  *           CoMemAlloc [COMPOBJ.151]
513  */
514 SEGPTR WINAPI CoMemAlloc(DWORD size, DWORD dwMemContext, DWORD x) {
515         HRESULT         hres;
516         SEGPTR          segptr;
517
518         /* FIXME: check context handling */
519         TRACE("(%ld, 0x%08lx, 0x%08lx)\n", size, dwMemContext, x);
520         hres = _xmalloc16(size, &segptr);
521         if (hres != S_OK)
522                 return (SEGPTR)0;
523         return segptr;
524 }
525
526 /******************************************************************************
527  *              CLSIDFromProgID [COMPOBJ.61]
528  *
529  * Converts a program ID into the respective GUID.
530  *
531  * PARAMS
532  *  progid       [I] program id as found in registry
533  *  riid         [O] associated CLSID
534  *
535  * RETURNS
536  *      Success: S_OK
537  *  Failure: CO_E_CLASSSTRING - the given ProgID cannot be found.
538  */
539 HRESULT WINAPI CLSIDFromProgID16(LPCOLESTR16 progid, LPCLSID riid)
540 {
541         char    *buf,buf2[80];
542         LONG    buf2len;
543         HRESULT err;
544         HKEY    xhkey;
545
546         buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
547         sprintf(buf,"%s\\CLSID",progid);
548         if ((err=RegOpenKeyA(HKEY_CLASSES_ROOT,buf,&xhkey))) {
549                 HeapFree(GetProcessHeap(),0,buf);
550                 return CO_E_CLASSSTRING;
551         }
552         HeapFree(GetProcessHeap(),0,buf);
553         buf2len = sizeof(buf2);
554         if ((err=RegQueryValueA(xhkey,NULL,buf2,&buf2len))) {
555                 RegCloseKey(xhkey);
556                 return CO_E_CLASSSTRING;
557         }
558         RegCloseKey(xhkey);
559         return __CLSIDFromStringA(buf2,riid);
560 }
561
562 /***********************************************************************
563  *           CoGetClassObject [COMPOBJ.7]
564  *
565  */
566 HRESULT WINAPI CoGetClassObject16(
567     REFCLSID rclsid, DWORD dwClsContext, COSERVERINFO *pServerInfo,
568     REFIID iid, LPVOID *ppv)
569 {
570     FIXME(", stub!\n\tCLSID:\t%s,\n\tIID:\t%s\n", debugstr_guid(rclsid), debugstr_guid(iid));
571
572     if (pServerInfo) {
573         FIXME("\tpServerInfo: name=%s\n",debugstr_w(pServerInfo->pwszName));
574         FIXME("\t\tpAuthInfo=%p\n",pServerInfo->pAuthInfo);
575     }
576     return E_NOTIMPL;
577 }
578
579 /***********************************************************************
580  *           CoCreateInstance [COMPOBJ.13]
581  */
582 HRESULT WINAPI CoCreateInstance16(
583         REFCLSID rclsid,
584         LPUNKNOWN pUnkOuter,
585         DWORD dwClsContext,
586         REFIID iid,
587         LPVOID *ppv)
588 {
589   FIXME("(%s, %p, %lx, %s, %p), stub!\n", 
590         debugstr_guid(rclsid), pUnkOuter, dwClsContext, debugstr_guid(iid),
591         ppv
592   );
593   return E_NOTIMPL;
594 }
595
596 /***********************************************************************
597  *           DllGetClassObject                          [OLE2.4]
598  */
599 HRESULT WINAPI DllGetClassObject16(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
600 {
601   FIXME("(%s, %s, %p): stub\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
602   return E_NOTIMPL;
603 }
604
605 /******************************************************************************
606  *              GetRunningObjectTable (OLE2.30)
607  */
608 HRESULT WINAPI
609 GetRunningObjectTable16(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
610 {
611     FIXME("(%ld,%p),stub!\n",reserved,pprot);
612     return E_NOTIMPL;
613 }