LoadModule32/WinExec32 should return 16-bit hInstance/hTask.
[wine] / ole / ifs.c
1 /*
2  *      basic interfaces
3  *
4  *      Copyright 1997  Marcus Meissner
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include "winerror.h"
12 #include "ole.h"
13 #include "ole2.h"
14 #include "ldt.h"
15 #include "heap.h"
16 #include "compobj.h"
17 #include "interfaces.h"
18 #include "shlobj.h"
19 #include "local.h"
20 #include "module.h"
21 #include "debug.h"
22
23 /*
24  * IUnknown
25  */
26
27 /******************************************************************************
28  *              IUnknown_AddRef [VTABLE:IUNKNOWN.1]
29  */
30 static ULONG WINAPI IUnknown_AddRef(LPUNKNOWN this) { 
31         TRACE(relay,"(%p)->AddRef()\n",this);
32         return ++(this->ref);
33 }
34
35 /******************************************************************************
36  * IUnknown_Release [VTABLE:IUNKNOWN.2]
37  */
38 static ULONG WINAPI IUnknown_Release(LPUNKNOWN this) {
39         TRACE(relay,"(%p)->Release()\n",this);
40         if (!--(this->ref)) {
41                 HeapFree(GetProcessHeap(),0,this);
42                 return 0;
43         }
44         return this->ref;
45 }
46
47 /******************************************************************************
48  * IUnknown_QueryInterface [VTABLE:IUNKNOWN.0]
49  */
50 static HRESULT WINAPI IUnknown_QueryInterface(LPUNKNOWN this,REFIID refiid,LPVOID *obj) {
51         char    xrefiid[50];
52
53         WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
54         TRACE(relay,"(%p)->QueryInterface(%s,%p)\n",this,xrefiid,obj);
55
56         if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
57                 *obj = this;
58                 return 0; 
59         }
60         return OLE_E_ENUM_NOMORE; 
61 }
62
63 static IUnknown_VTable uvt = {
64         IUnknown_QueryInterface,
65         IUnknown_AddRef,
66         IUnknown_Release
67 };
68
69 /******************************************************************************
70  * IUnknown_Constructor [INTERNAL]
71  */
72 LPUNKNOWN
73 IUnknown_Constructor() {
74         LPUNKNOWN       unk;
75
76         unk = (LPUNKNOWN)HeapAlloc(GetProcessHeap(),0,sizeof(IUnknown));
77         unk->lpvtbl     = &uvt;
78         unk->ref        = 1;
79         return unk;
80 }
81
82 /*
83  * IMalloc
84  */
85
86 /******************************************************************************
87  *              IMalloc16_AddRef        [COMPOBJ.501]
88  */
89 ULONG WINAPI IMalloc16_AddRef(LPMALLOC16 this) {
90         TRACE(relay,"(%p)->AddRef()\n",this);
91         return 1; /* cannot be freed */
92 }
93
94 /******************************************************************************
95  *              IMalloc16_Release       [COMPOBJ.502]
96  */
97 ULONG WINAPI IMalloc16_Release(LPMALLOC16 this) {
98         TRACE(relay,"(%p)->Release()\n",this);
99         return 1; /* cannot be freed */
100 }
101
102 /******************************************************************************
103  *              IMalloc16_QueryInterface        [COMPOBJ.500]
104  */
105 HRESULT WINAPI IMalloc16_QueryInterface(LPMALLOC16 this,REFIID refiid,LPVOID *obj) {
106         char    xrefiid[50];
107
108         WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
109         TRACE(relay,"(%p)->QueryInterface(%s,%p)\n",this,xrefiid,obj);
110         if (    !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
111                 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
112         ) {
113                 *obj = this;
114                 return 0;
115         }
116         return OLE_E_ENUM_NOMORE; 
117 }
118
119 /******************************************************************************
120  * IMalloc16_Alloc [COMPOBJ.503]
121  */
122 LPVOID WINAPI IMalloc16_Alloc(LPMALLOC16 this,DWORD cb) {
123         TRACE(relay,"(%p)->Alloc(%ld)\n",this,cb);
124         return (LPVOID)PTR_SEG_OFF_TO_SEGPTR(this->heap,LOCAL_Alloc(this->heap,0,cb));
125 }
126
127 /******************************************************************************
128  * IMalloc16_Realloc [COMPOBJ.504]
129  */
130 LPVOID WINAPI IMalloc16_Realloc(LPMALLOC16 this,LPVOID pv,DWORD cb) {
131         TRACE(relay,"(%p)->Realloc(%p,%ld)\n",this,pv,cb);
132         return (LPVOID)PTR_SEG_OFF_TO_SEGPTR(this->heap,LOCAL_ReAlloc(this->heap,0,LOWORD(pv),cb));
133 }
134
135 /******************************************************************************
136  * IMalloc16_Free [COMPOBJ.505]
137  */
138 VOID WINAPI IMalloc16_Free(LPMALLOC16 this,LPVOID pv) {
139         TRACE(relay,"(%p)->Free(%p)\n",this,pv);
140         LOCAL_Free(this->heap,LOWORD(pv));
141 }
142
143 /******************************************************************************
144  * IMalloc16_GetSize [COMPOBJ.506]
145  */
146 DWORD WINAPI IMalloc16_GetSize(LPMALLOC16 this,LPVOID pv) {
147         TRACE(relay,"(%p)->GetSize(%p)\n",this,pv);
148         return LOCAL_Size(this->heap,LOWORD(pv));
149 }
150
151 /******************************************************************************
152  * IMalloc16_DidAlloc [COMPOBJ.507]
153  */
154 INT16 WINAPI IMalloc16_DidAlloc(LPMALLOC16 this,LPVOID pv) {
155         TRACE(relay,"(%p)->DidAlloc(%p)\n",this,pv);
156         return (INT16)-1;
157 }
158
159 /******************************************************************************
160  * IMalloc16_HeapMinimize [COMPOBJ.508]
161  */
162 LPVOID WINAPI IMalloc16_HeapMinimize(LPMALLOC16 this) {
163         TRACE(relay,"(%p)->HeapMinimize()\n",this);
164         return NULL;
165 }
166
167 #ifdef OLD_TABLE
168 /* FIXME: This is unused */
169 static IMalloc16_VTable mvt16 = {
170         IMalloc16_QueryInterface,
171         IMalloc16_AddRef,
172         IMalloc16_Release,
173         IMalloc16_Alloc,
174         IMalloc16_Realloc,
175         IMalloc16_Free,
176         IMalloc16_GetSize,
177         IMalloc16_DidAlloc,
178         IMalloc16_HeapMinimize,
179 };
180 #endif
181 static IMalloc16_VTable *msegvt16 = NULL;
182
183 /******************************************************************************
184  * IMalloc16_Constructor [VTABLE]
185  */
186 LPMALLOC16
187 IMalloc16_Constructor() {
188         LPMALLOC16      this;
189         HMODULE16       hcomp = GetModuleHandle16("COMPOBJ");
190
191         this = (LPMALLOC16)SEGPTR_NEW(IMalloc16);
192         if (!msegvt16) {
193             this->lpvtbl = msegvt16 = SEGPTR_NEW(IMalloc16_VTable);
194
195 #define FN(x) this->lpvtbl->fn##x = (void*)WIN32_GetProcAddress16(hcomp,"IMalloc16_"#x);assert(this->lpvtbl->fn##x)
196             FN(QueryInterface);
197             FN(AddRef);
198             FN(Release);
199             FN(Alloc);
200             FN(Realloc);
201             FN(Free);
202             FN(GetSize);
203             FN(DidAlloc);
204             FN(HeapMinimize);
205             msegvt16 = (LPMALLOC16_VTABLE)SEGPTR_GET(msegvt16);
206 #undef FN
207             this->lpvtbl = msegvt16;
208         }
209         this->ref = 1;
210         /* FIXME: implement multiple heaps */
211         this->heap = GlobalAlloc16(GMEM_MOVEABLE,64000);
212         LocalInit(this->heap,0,64000);
213         return (LPMALLOC16)SEGPTR_GET(this);
214 }
215
216 /*
217  * IMalloc32
218  */
219
220 /******************************************************************************
221  *              IMalloc32_AddRef        [VTABLE]
222  */
223 static ULONG WINAPI IMalloc32_AddRef(LPMALLOC32 this) {
224         TRACE(relay,"(%p)->AddRef()\n",this);
225         return 1; /* cannot be freed */
226 }
227
228 /******************************************************************************
229  *              IMalloc32_Release       [VTABLE]
230  */
231 static ULONG WINAPI IMalloc32_Release(LPMALLOC32 this) {
232         TRACE(relay,"(%p)->Release()\n",this);
233         return 1; /* cannot be freed */
234 }
235
236 /******************************************************************************
237  *              IMalloc32_QueryInterface        [VTABLE]
238  */
239 static HRESULT WINAPI IMalloc32_QueryInterface(LPMALLOC32 this,REFIID refiid,LPVOID *obj) {
240         char    xrefiid[50];
241
242         WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
243         TRACE(relay,"(%p)->QueryInterface(%s,%p)\n",this,xrefiid,obj);
244         if (    !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
245                 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
246         ) {
247                 *obj = this;
248                 return 0;
249         }
250         return OLE_E_ENUM_NOMORE; 
251 }
252
253 /******************************************************************************
254  * IMalloc32_Alloc [VTABLE]
255  */
256 static LPVOID WINAPI IMalloc32_Alloc(LPMALLOC32 this,DWORD cb) {
257         TRACE(relay,"(%p)->Alloc(%ld)\n",this,cb);
258         return HeapAlloc(GetProcessHeap(),0,cb);
259 }
260
261 /******************************************************************************
262  * IMalloc32_Realloc [VTABLE]
263  */
264 static LPVOID WINAPI IMalloc32_Realloc(LPMALLOC32 this,LPVOID pv,DWORD cb) {
265         TRACE(relay,"(%p)->Realloc(%p,%ld)\n",this,pv,cb);
266         return HeapReAlloc(GetProcessHeap(),0,pv,cb);
267 }
268
269 /******************************************************************************
270  * IMalloc32_Free [VTABLE]
271  */
272 static VOID WINAPI IMalloc32_Free(LPMALLOC32 this,LPVOID pv) {
273         TRACE(relay,"(%p)->Free(%p)\n",this,pv);
274         HeapFree(GetProcessHeap(),0,pv);
275 }
276
277 /******************************************************************************
278  * IMalloc32_GetSize [VTABLE]
279  */
280 static DWORD WINAPI IMalloc32_GetSize(LPMALLOC32 this,LPVOID pv) {
281         TRACE(relay,"(%p)->GetSize(%p)\n",this,pv);
282         return HeapSize(GetProcessHeap(),0,pv);
283 }
284
285 /******************************************************************************
286  * IMalloc32_DidAlloc [VTABLE]
287  */
288 static INT32 WINAPI IMalloc32_DidAlloc(LPMALLOC32 this,LPVOID pv) {
289         TRACE(relay,"(%p)->DidAlloc(%p)\n",this,pv);
290         return -1;
291 }
292
293 /******************************************************************************
294  * IMalloc32_HeapMinimize [VTABLE]
295  */
296 static LPVOID WINAPI IMalloc32_HeapMinimize(LPMALLOC32 this) {
297         TRACE(relay,"(%p)->HeapMinimize()\n",this);
298         return NULL;
299 }
300
301 static IMalloc32_VTable VT_IMalloc32 = {
302         IMalloc32_QueryInterface,
303         IMalloc32_AddRef,
304         IMalloc32_Release,
305         IMalloc32_Alloc,
306         IMalloc32_Realloc,
307         IMalloc32_Free,
308         IMalloc32_GetSize,
309         IMalloc32_DidAlloc,
310         IMalloc32_HeapMinimize,
311 };
312
313 /******************************************************************************
314  * IMalloc32_Constructor [VTABLE]
315  */
316 LPMALLOC32
317 IMalloc32_Constructor() {
318         LPMALLOC32      this;
319
320         this = (LPMALLOC32)HeapAlloc(GetProcessHeap(),0,sizeof(IMalloc32));
321         this->lpvtbl = &VT_IMalloc32;
322         this->ref = 1;
323         return this;
324 }
325
326 /****************************************************************************
327  * API Functions
328  */
329
330 /******************************************************************************
331  *              IsValidInterface32      [OLE32.78]
332  *
333  * RETURNS
334  *  True, if the passed pointer is a valid interface
335  */
336 BOOL32 WINAPI IsValidInterface32(
337         LPUNKNOWN punk  /* [in] interface to be tested */
338 ) {
339         return !(
340                 IsBadReadPtr32(punk,4)                                  ||
341                 IsBadReadPtr32(punk->lpvtbl,4)                          ||
342                 IsBadReadPtr32(punk->lpvtbl->fnQueryInterface,9)        ||
343                 IsBadCodePtr32(punk->lpvtbl->fnQueryInterface)
344         );
345 }