Release 980329
[wine/multimedia.git] / ole / ifs.c
blob6f24584ecc3831b37055020f3c764ded4f0dfdb0
1 /*
2 * basic interfaces
4 * Copyright 1997 Marcus Meissner
5 */
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"
23 /****************************************************************************
24 * IUnknown
26 static ULONG WINAPI IUnknown_AddRef(LPUNKNOWN this) {
27 TRACE(relay,"(%p)->AddRef()\n",this);
28 return ++(this->ref);
30 static ULONG WINAPI IUnknown_Release(LPUNKNOWN this) {
31 TRACE(relay,"(%p)->Release()\n",this);
32 if (!--(this->ref)) {
33 HeapFree(GetProcessHeap(),0,this);
34 return 0;
36 return this->ref;
39 static HRESULT WINAPI IUnknown_QueryInterface(LPUNKNOWN this,REFIID refiid,LPVOID *obj) {
40 char xrefiid[50];
42 WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
43 TRACE(relay,"(%p)->QueryInterface(%s,%p)\n",this,xrefiid,obj);
45 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
46 *obj = this;
47 return 0;
49 return OLE_E_ENUM_NOMORE;
52 static IUnknown_VTable uvt = {
53 IUnknown_QueryInterface,
54 IUnknown_AddRef,
55 IUnknown_Release
59 LPUNKNOWN
60 IUnknown_Constructor() {
61 LPUNKNOWN unk;
63 unk = (LPUNKNOWN)HeapAlloc(GetProcessHeap(),0,sizeof(IUnknown));
64 unk->lpvtbl = &uvt;
65 unk->ref = 1;
66 return unk;
69 /****************************************************************************
70 * IMalloc16
72 ULONG WINAPI IMalloc16_AddRef(LPMALLOC16 this) {
73 TRACE(relay,"(%p)->AddRef()\n",this);
74 return 1; /* cannot be freed */
77 ULONG WINAPI IMalloc16_Release(LPMALLOC16 this) {
78 TRACE(relay,"(%p)->Release()\n",this);
79 return 1; /* cannot be freed */
82 HRESULT WINAPI IMalloc16_QueryInterface(LPMALLOC16 this,REFIID refiid,LPVOID *obj) {
83 char xrefiid[50];
85 WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
86 TRACE(relay,"(%p)->QueryInterface(%s,%p)\n",this,xrefiid,obj);
87 if ( !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
88 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
89 ) {
90 *obj = this;
91 return 0;
93 return OLE_E_ENUM_NOMORE;
96 LPVOID WINAPI IMalloc16_Alloc(LPMALLOC16 this,DWORD cb) {
97 TRACE(relay,"(%p)->Alloc(%ld)\n",this,cb);
98 return (LPVOID)PTR_SEG_OFF_TO_SEGPTR(this->heap,LOCAL_Alloc(this->heap,0,cb));
101 LPVOID WINAPI IMalloc16_Realloc(LPMALLOC16 this,LPVOID pv,DWORD cb) {
102 TRACE(relay,"(%p)->Realloc(%p,%ld)\n",this,pv,cb);
103 return (LPVOID)PTR_SEG_OFF_TO_SEGPTR(this->heap,LOCAL_ReAlloc(this->heap,0,LOWORD(pv),cb));
105 VOID WINAPI IMalloc16_Free(LPMALLOC16 this,LPVOID pv) {
106 TRACE(relay,"(%p)->Free(%p)\n",this,pv);
107 LOCAL_Free(this->heap,LOWORD(pv));
110 DWORD WINAPI IMalloc16_GetSize(LPMALLOC16 this,LPVOID pv) {
111 TRACE(relay,"(%p)->GetSize(%p)\n",this,pv);
112 return LOCAL_Size(this->heap,LOWORD(pv));
115 INT16 WINAPI IMalloc16_DidAlloc(LPMALLOC16 this,LPVOID pv) {
116 TRACE(relay,"(%p)->DidAlloc(%p)\n",this,pv);
117 return (INT16)-1;
119 LPVOID WINAPI IMalloc16_HeapMinimize(LPMALLOC16 this) {
120 TRACE(relay,"(%p)->HeapMinimize()\n",this);
121 return NULL;
124 static IMalloc16_VTable mvt16 = {
125 IMalloc16_QueryInterface,
126 IMalloc16_AddRef,
127 IMalloc16_Release,
128 IMalloc16_Alloc,
129 IMalloc16_Realloc,
130 IMalloc16_Free,
131 IMalloc16_GetSize,
132 IMalloc16_DidAlloc,
133 IMalloc16_HeapMinimize,
135 static IMalloc16_VTable *msegvt16 = NULL;
137 LPMALLOC16
138 IMalloc16_Constructor() {
139 LPMALLOC16 this;
141 this = (LPMALLOC16)SEGPTR_NEW(IMalloc16);
142 if (__winelib) {
143 this->lpvtbl = &mvt16;
144 } else {
145 HMODULE16 hcomp = GetModuleHandle16("COMPOBJ");
146 if (!msegvt16) {
147 this->lpvtbl = msegvt16 = SEGPTR_NEW(IMalloc16_VTable);
149 #define FN(x) this->lpvtbl->fn##x = (void*)WIN32_GetProcAddress16(hcomp,"IMalloc16_"#x);assert(this->lpvtbl->fn##x);
150 FN(QueryInterface)
151 FN(AddRef)
152 FN(Release)
153 FN(Alloc)
154 FN(Realloc)
155 FN(Free)
156 FN(GetSize)
157 FN(DidAlloc)
158 FN(HeapMinimize)
159 msegvt16 = (LPMALLOC16_VTABLE)SEGPTR_GET(msegvt16);
160 #undef FN
162 this->lpvtbl = msegvt16;
164 this->ref = 1;
165 /* FIXME: implement multiple heaps */
166 this->heap = GlobalAlloc16(GMEM_MOVEABLE,64000);
167 LocalInit(this->heap,0,64000);
168 return (LPMALLOC16)SEGPTR_GET(this);
171 /****************************************************************************
172 * IMalloc32
174 static ULONG WINAPI IMalloc32_AddRef(LPMALLOC32 this) {
175 TRACE(relay,"(%p)->AddRef()\n",this);
176 return 1; /* cannot be freed */
179 static ULONG WINAPI IMalloc32_Release(LPMALLOC32 this) {
180 TRACE(relay,"(%p)->Release()\n",this);
181 return 1; /* cannot be freed */
184 static HRESULT WINAPI IMalloc32_QueryInterface(LPMALLOC32 this,REFIID refiid,LPVOID *obj) {
185 char xrefiid[50];
187 WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
188 TRACE(relay,"(%p)->QueryInterface(%s,%p)\n",this,xrefiid,obj);
189 if ( !memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown)) ||
190 !memcmp(&IID_IMalloc,refiid,sizeof(IID_IMalloc))
192 *obj = this;
193 return 0;
195 return OLE_E_ENUM_NOMORE;
198 static LPVOID WINAPI IMalloc32_Alloc(LPMALLOC32 this,DWORD cb) {
199 TRACE(relay,"(%p)->Alloc(%ld)\n",this,cb);
200 return HeapAlloc(GetProcessHeap(),0,cb);
203 static LPVOID WINAPI IMalloc32_Realloc(LPMALLOC32 this,LPVOID pv,DWORD cb) {
204 TRACE(relay,"(%p)->Realloc(%p,%ld)\n",this,pv,cb);
205 return HeapReAlloc(GetProcessHeap(),0,pv,cb);
207 static VOID WINAPI IMalloc32_Free(LPMALLOC32 this,LPVOID pv) {
208 TRACE(relay,"(%p)->Free(%p)\n",this,pv);
209 HeapFree(GetProcessHeap(),0,pv);
212 static DWORD WINAPI IMalloc32_GetSize(LPMALLOC32 this,LPVOID pv) {
213 TRACE(relay,"(%p)->GetSize(%p)\n",this,pv);
214 return HeapSize(GetProcessHeap(),0,pv);
217 static INT32 WINAPI IMalloc32_DidAlloc(LPMALLOC32 this,LPVOID pv) {
218 TRACE(relay,"(%p)->DidAlloc(%p)\n",this,pv);
219 return -1;
221 static LPVOID WINAPI IMalloc32_HeapMinimize(LPMALLOC32 this) {
222 TRACE(relay,"(%p)->HeapMinimize()\n",this);
223 return NULL;
226 static IMalloc32_VTable VT_IMalloc32 = {
227 IMalloc32_QueryInterface,
228 IMalloc32_AddRef,
229 IMalloc32_Release,
230 IMalloc32_Alloc,
231 IMalloc32_Realloc,
232 IMalloc32_Free,
233 IMalloc32_GetSize,
234 IMalloc32_DidAlloc,
235 IMalloc32_HeapMinimize,
238 LPMALLOC32
239 IMalloc32_Constructor() {
240 LPMALLOC32 this;
242 this = (LPMALLOC32)HeapAlloc(GetProcessHeap(),0,sizeof(IMalloc32));
243 this->lpvtbl = &VT_IMalloc32;
244 this->ref = 1;
245 return this;
247 /****************************************************************************
248 * API Functions
250 /****************************************************************************
251 * IsValidInterface (OLE32.78)
252 * RETURNS
253 * True, if the passed pointer is a valid interface
255 BOOL32 WINAPI IsValidInterface32(
256 LPUNKNOWN punk /* [in] interface to be tested */
258 return !(
259 IsBadReadPtr32(punk,4) ||
260 IsBadReadPtr32(punk->lpvtbl,4) ||
261 IsBadReadPtr32(punk->lpvtbl->fnQueryInterface,9) ||
262 IsBadCodePtr32(punk->lpvtbl->fnQueryInterface)