Release 980201
[wine] / ole / compobj.c
1 /*
2  *      COMPOBJ library
3  *
4  *      Copyright 1995  Martin von Loewis
5  */
6
7 #define INITGUID
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include "ole.h"
14 #include "ole2.h"
15 #include "stddebug.h"
16 #include "debug.h"
17 #include "file.h"
18 #include "compobj.h"
19 #include "interfaces.h"
20 #include "shlobj.h"
21 #include "ddraw.h"
22 #include "dsound.h"
23 #include "dinput.h"
24 #include "d3d.h"
25
26 DWORD currentMalloc=0;
27
28 /***********************************************************************
29  *           CoBuildVersion [COMPOBJ.1]
30  */
31 DWORD WINAPI CoBuildVersion()
32 {
33         dprintf_ole(stddeb,"CoBuildVersion()\n");
34         return (rmm<<16)+rup;
35 }
36
37 /***********************************************************************
38  *           CoInitialize       [COMPOBJ.2]
39  * lpReserved is an IMalloc pointer in 16bit OLE. We just stored it as-is.
40  */
41 HRESULT WINAPI CoInitialize(DWORD lpReserved)
42 {
43         dprintf_ole(stdnimp,"CoInitialize\n");
44         /* remember the LPMALLOC, maybe somebody wants to read it later on */
45         currentMalloc = lpReserved;
46         return S_OK;
47 }
48
49 /***********************************************************************
50  *           CoUnitialize   [COMPOBJ.3]
51  */
52 void WINAPI CoUnitialize()
53 {
54         dprintf_ole(stdnimp,"CoUnitialize()\n");
55 }
56
57 /***********************************************************************
58  *           CoGetMalloc    [COMPOBJ.4]
59  */
60 HRESULT WINAPI CoGetMalloc(DWORD dwMemContext, DWORD * lpMalloc)
61 {
62         if(currentMalloc)
63         {
64                 *lpMalloc = currentMalloc;
65                 return S_OK;
66         }
67         *lpMalloc = 0;
68         /* 16-bit E_NOTIMPL */
69         return 0x80000001L;
70 }
71
72 /***********************************************************************
73  *           CoDisconnectObject
74  */
75 OLESTATUS WINAPI CoDisconnectObject( LPUNKNOWN lpUnk, DWORD reserved )
76 {
77     dprintf_ole(stdnimp,"CoDisconnectObject:%p %lx\n",lpUnk,reserved);
78     return OLE_OK;
79 }
80
81 /***********************************************************************
82  *           IsEqualGUID [COMPOBJ.18]
83  */
84 BOOL16 WINAPI IsEqualGUID(GUID* g1, GUID* g2)
85 {
86     return !memcmp( g1, g2, sizeof(GUID) );
87 }
88
89 /***********************************************************************
90  *           CLSIDFromString [COMPOBJ.20]
91  */
92
93 /* Class id: DWORD-WORD-WORD-BYTES[2]-BYTES[6] */
94
95 OLESTATUS WINAPI CLSIDFromString(const LPCSTR idstr, CLSID *id)
96 {
97   BYTE *s = (BYTE *) idstr;
98   BYTE *p;
99   int   i;
100   BYTE table[256];
101
102   dprintf_ole(stddeb,"ClsIDFromString() %s -> %p\n", idstr, id);
103
104   /* quick lookup table */
105   memset(table, 0, 256);
106
107   for (i = 0; i < 10; i++) {
108     table['0' + i] = i;
109   }
110   for (i = 0; i < 6; i++) {
111     table['A' + i] = i+10;
112     table['a' + i] = i+10;
113   }
114
115   /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
116
117   if (strlen(idstr) != 38)
118     return OLE_ERROR_OBJECT;
119
120   p = (BYTE *) id;
121
122   s++;  /* skip leading brace  */
123   for (i = 0; i < 4; i++) {
124     p[3 - i] = table[*s]<<4 | table[*(s+1)];
125     s += 2;
126   }
127   p += 4;
128   s++;  /* skip - */
129
130   for (i = 0; i < 2; i++) {
131     p[1-i] = table[*s]<<4 | table[*(s+1)];
132     s += 2;
133   }
134   p += 2;
135   s++;  /* skip - */
136
137   for (i = 0; i < 2; i++) {
138     p[1-i] = table[*s]<<4 | table[*(s+1)];
139     s += 2;
140   }
141   p += 2;
142   s++;  /* skip - */
143
144   /* these are just sequential bytes */
145   for (i = 0; i < 2; i++) {
146     *p++ = table[*s]<<4 | table[*(s+1)];
147     s += 2;
148   }
149   s++;  /* skip - */
150
151   for (i = 0; i < 6; i++) {
152     *p++ = table[*s]<<4 | table[*(s+1)];
153     s += 2;
154   }
155
156   return OLE_OK;
157 }
158
159 /***********************************************************************
160  *           StringFromCLSID [COMPOBJ.19]
161  */
162 OLESTATUS WINAPI StringFromCLSID(const CLSID *id, LPSTR idstr)
163 {
164   static const char *hex = "0123456789ABCDEF";
165   char *s;
166   int   i;
167
168   sprintf(idstr, "{%08lx-%04x-%04x-%02x%02x-",
169           id->Data1, id->Data2, id->Data3,
170           id->Data4[0], id->Data4[1]);
171   s = &idstr[25];
172
173   /* 6 hex bytes */
174   for (i = 2; i < 8; i++) {
175     *s++ = hex[id->Data4[i]>>4];
176     *s++ = hex[id->Data4[i] & 0xf];
177   }
178
179   *s++ = '}';
180   *s++ = '\0';
181
182   for (i = strlen(idstr)-1; i >= 0; i--) {
183     idstr[i] = toupper(idstr[i]);
184   }
185
186   dprintf_ole(stddeb,"StringFromClsID: %p->%s\n", id, idstr);
187
188   return OLE_OK;
189 }
190
191 /***********************************************************************
192  *           CLSIDFromProgID [COMPOBJ.61]
193  */
194
195 OLESTATUS WINAPI CLSIDFromProgID(LPCSTR progid,LPCLSID riid)
196 {
197         char    *buf,buf2[80];
198         DWORD   buf2len;
199         HRESULT err;
200         HKEY    xhkey;
201
202         buf = HeapAlloc(GetProcessHeap(),0,strlen(progid)+8);
203         sprintf(buf,"%s\\CLSID",progid);
204         if ((err=RegOpenKey32A(HKEY_CLASSES_ROOT,buf,&xhkey))) {
205                 HeapFree(GetProcessHeap(),0,buf);
206                 return OLE_ERROR_GENERIC;
207         }
208         HeapFree(GetProcessHeap(),0,buf);
209         buf2len = sizeof(buf2);
210         if ((err=RegQueryValue32A(xhkey,NULL,buf2,&buf2len))) {
211                 RegCloseKey(xhkey);
212                 return OLE_ERROR_GENERIC;
213         }
214         RegCloseKey(xhkey);
215         return CLSIDFromString(buf2,riid);
216 }
217
218 OLESTATUS WINAPI LookupETask(LPVOID p1,LPVOID p2) {
219         fprintf(stderr,"LookupETask(%p,%p),stub!\n",p1,p2);
220         return 0;
221 }
222
223 OLESTATUS WINAPI CallObjectInWOW(LPVOID p1,LPVOID p2) {
224         fprintf(stderr,"CallObjectInWOW(%p,%p),stub!\n",p1,p2);
225         return 0;
226 }
227
228 /***********************************************************************
229  *              CoRegisterClassObject [COMPOBJ.5]
230  */
231 OLESTATUS WINAPI CoRegisterClassObject(
232         REFCLSID rclsid, LPUNKNOWN pUnk,DWORD dwClsContext,DWORD flags,
233         LPDWORD lpdwRegister
234 ) {
235         char    buf[80];
236
237         StringFromCLSID(rclsid,buf);
238
239         fprintf(stderr,"CoRegisterClassObject(%s,%p,0x%08lx,0x%08lx,%p),stub\n",
240                 buf,pUnk,dwClsContext,flags,lpdwRegister
241         );
242         return 0;
243 }
244
245 /***********************************************************************
246  *              CoRegisterClassObject [COMPOBJ.27]
247  */
248 OLESTATUS WINAPI CoRegisterMessageFilter16(
249         LPMESSAGEFILTER lpMessageFilter,LPMESSAGEFILTER *lplpMessageFilter
250 ) {
251         fprintf(stderr,"CoRegisterMessageFilter(%p,%p),stub!\n",
252                 lpMessageFilter,lplpMessageFilter
253         );
254         return 0;
255 }
256
257 /***********************************************************************
258  *           CoCreateInstance [COMPOBJ.13, OLE32.7]
259  */
260 HRESULT WINAPI CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter,
261                                 DWORD dwClsContext, REFIID riid, LPVOID *ppv)
262 {
263         fprintf(stderr, "CoCreateInstance(): stub !\n");
264         *ppv = NULL;
265         return S_OK;
266 }
267
268 /***********************************************************************
269  *           CoFreeUnusedLibraries [COMPOBJ.17]
270  */
271 void WINAPI CoFreeUnusedLibraries()
272 {
273         fprintf(stderr, "CoFreeUnusedLibraries(): stub !\n");
274 }
275
276 /***********************************************************************
277  *           CoFileTimeNow [COMPOBJ.82, OLE32.10]
278  *
279  *      stores the current system time in lpFileTime
280  */
281 HRESULT WINAPI CoFileTimeNow(FILETIME *lpFileTime)
282 {
283         DOSFS_UnixTimeToFileTime(time(NULL), lpFileTime, 0);
284         return S_OK;
285 }