1 | /* $Id: clsid.cpp,v 1.11 2000-03-19 22:32:12 davidr Exp $ */
|
---|
2 | /*
|
---|
3 | *
|
---|
4 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
5 | *
|
---|
6 | */
|
---|
7 | /*
|
---|
8 | * ClassID Manipulation.
|
---|
9 | *
|
---|
10 | * 1/7/99
|
---|
11 | *
|
---|
12 | * Copyright 1999 David J. Raison
|
---|
13 | *
|
---|
14 | * Some portions from Wine Implementation
|
---|
15 | * Copyright 1995 Martin von Loewis
|
---|
16 | * Copyright 1998 Justin Bradford
|
---|
17 | * Copyright 1999 Francis Beaudet
|
---|
18 | * Copyright 1999 Sylvain St-Germain
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "ole32.h"
|
---|
22 |
|
---|
23 | #include "oString.h"
|
---|
24 |
|
---|
25 | // ----------------------------------------------------------------------
|
---|
26 | // CLSIDFromProgID16()
|
---|
27 | // ----------------------------------------------------------------------
|
---|
28 | HRESULT WIN32API CLSIDFromProgID16(
|
---|
29 | LPCOLESTR16 lpszProgID, // [in] - UNICODE program id as found in registry
|
---|
30 | LPCLSID pclsid) // [out] - CLSID
|
---|
31 | {
|
---|
32 | dprintf(("OLE32: CLSIDFromProgID16"));
|
---|
33 |
|
---|
34 | LONG lDataLen = 80;
|
---|
35 | oStringA szKey(lpszProgID);
|
---|
36 | oStringA szCLSID(lDataLen, 1);
|
---|
37 | HKEY hKey;
|
---|
38 | HRESULT rc;
|
---|
39 |
|
---|
40 | // Create the registry lookup string...
|
---|
41 | szKey += "\\CLSID";
|
---|
42 |
|
---|
43 | // Try to open the key in the registry...
|
---|
44 | rc = RegOpenKeyA(HKEY_CLASSES_ROOT, szKey, &hKey);
|
---|
45 | if (rc != 0)
|
---|
46 | return OLE_ERROR_GENERIC;
|
---|
47 |
|
---|
48 | // Now get the data from the _default_ entry on this key...
|
---|
49 | rc = RegQueryValueA(hKey, NULL, szCLSID, &lDataLen);
|
---|
50 | RegCloseKey(hKey);
|
---|
51 | if (rc != 0)
|
---|
52 | return OLE_ERROR_GENERIC;
|
---|
53 |
|
---|
54 | // Now convert from a string to a UUID
|
---|
55 | return CLSIDFromString16(szCLSID, pclsid);
|
---|
56 | }
|
---|
57 |
|
---|
58 | // ----------------------------------------------------------------------
|
---|
59 | // CLSIDFromProgID()
|
---|
60 | // ----------------------------------------------------------------------
|
---|
61 | HRESULT WIN32API CLSIDFromProgID(
|
---|
62 | LPCOLESTR lpszProgID, // [in] - UNICODE program id as found in registry
|
---|
63 | LPCLSID pclsid) // [out] - CLSID
|
---|
64 | {
|
---|
65 | dprintf(("OLE32: CLSIDFromProgID"));
|
---|
66 |
|
---|
67 | LONG lDataLen = 80;
|
---|
68 | oStringW szKey(lpszProgID);
|
---|
69 | oStringW szCLSID(lDataLen, 1);
|
---|
70 | HKEY hKey;
|
---|
71 | HRESULT rc;
|
---|
72 |
|
---|
73 | // Create the registry lookup string...
|
---|
74 | szKey += L"\\CLSID";
|
---|
75 |
|
---|
76 | // Try to open the key in the registry...
|
---|
77 | rc = RegOpenKeyW(HKEY_CLASSES_ROOT, szKey, &hKey);
|
---|
78 | if (rc != 0)
|
---|
79 | return OLE_ERROR_GENERIC;
|
---|
80 |
|
---|
81 | // Now get the data from the _default_ entry on this key...
|
---|
82 | rc = RegQueryValueW(hKey, NULL, szCLSID, &lDataLen);
|
---|
83 | RegCloseKey(hKey);
|
---|
84 | if (rc != 0)
|
---|
85 | return OLE_ERROR_GENERIC;
|
---|
86 |
|
---|
87 | // Now convert from a string to a UUID
|
---|
88 | return CLSIDFromString(szCLSID, pclsid);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // ----------------------------------------------------------------------
|
---|
92 | // IIDFromString
|
---|
93 | // ----------------------------------------------------------------------
|
---|
94 | HRESULT WIN32API IIDFromString(LPSTR lpsz, LPIID lpiid)
|
---|
95 | {
|
---|
96 | dprintf(("OLE32: IIDFromString"));
|
---|
97 | return CLSIDFromString((LPCOLESTR)lpsz, (LPCLSID)lpiid);
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | // ----------------------------------------------------------------------
|
---|
102 | // CLSIDFromStringA()
|
---|
103 | // @@@PH: this is not a WINE API, but a replacement for CLSIDFromString16
|
---|
104 | // which used to accept ASCII strings instead of OLE strings
|
---|
105 | // ----------------------------------------------------------------------
|
---|
106 |
|
---|
107 | HRESULT WIN32API CLSIDFromStringA(
|
---|
108 | LPCSTR lpsz, // [in] - ASCII string CLSID
|
---|
109 | LPCLSID pclsid) // [out] - Binary CLSID
|
---|
110 | {
|
---|
111 | return CLSIDFromString16(lpsz, pclsid);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | // ----------------------------------------------------------------------
|
---|
116 | // CLSIDFromString16()
|
---|
117 | // ----------------------------------------------------------------------
|
---|
118 | HRESULT WIN32API CLSIDFromString16(
|
---|
119 | LPCOLESTR16 lpsz, // [in] - Unicode string CLSID
|
---|
120 | LPCLSID pclsid) // [out] - Binary CLSID
|
---|
121 | {
|
---|
122 | dprintf(("OLE32: CLSIDFromString16"));
|
---|
123 |
|
---|
124 | // Convert to binary CLSID
|
---|
125 | char *s = (char *) lpsz;
|
---|
126 | char *p;
|
---|
127 | int i;
|
---|
128 | char table[256];
|
---|
129 |
|
---|
130 | /* quick lookup table */
|
---|
131 | memset(table, 0, 256);
|
---|
132 |
|
---|
133 | for (i = 0; i < 10; i++)
|
---|
134 | {
|
---|
135 | table['0' + i] = i;
|
---|
136 | }
|
---|
137 | for (i = 0; i < 6; i++)
|
---|
138 | {
|
---|
139 | table['A' + i] = i+10;
|
---|
140 | table['a' + i] = i+10;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
|
---|
144 |
|
---|
145 | if (lstrlenA(lpsz) != 38)
|
---|
146 | return OLE_ERROR_OBJECT;
|
---|
147 |
|
---|
148 | p = (char *) pclsid;
|
---|
149 |
|
---|
150 | s++; /* skip leading brace */
|
---|
151 | for (i = 0; i < 4; i++)
|
---|
152 | {
|
---|
153 | p[3 - i] = table[*s]<<4 | table[*(s+1)];
|
---|
154 | s += 2;
|
---|
155 | }
|
---|
156 | p += 4;
|
---|
157 | s++; /* skip - */
|
---|
158 |
|
---|
159 | for (i = 0; i < 2; i++)
|
---|
160 | {
|
---|
161 | p[1-i] = table[*s]<<4 | table[*(s+1)];
|
---|
162 | s += 2;
|
---|
163 | }
|
---|
164 | p += 2;
|
---|
165 | s++; /* skip - */
|
---|
166 |
|
---|
167 | for (i = 0; i < 2; i++)
|
---|
168 | {
|
---|
169 | p[1-i] = table[*s]<<4 | table[*(s+1)];
|
---|
170 | s += 2;
|
---|
171 | }
|
---|
172 | p += 2;
|
---|
173 | s++; /* skip - */
|
---|
174 |
|
---|
175 | /* these are just sequential bytes */
|
---|
176 | for (i = 0; i < 2; i++)
|
---|
177 | {
|
---|
178 | *p++ = table[*s]<<4 | table[*(s+1)];
|
---|
179 | s += 2;
|
---|
180 | }
|
---|
181 | s++; /* skip - */
|
---|
182 |
|
---|
183 | for (i = 0; i < 6; i++)
|
---|
184 | {
|
---|
185 | *p++ = table[*s]<<4 | table[*(s+1)];
|
---|
186 | s += 2;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return S_OK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | // ----------------------------------------------------------------------
|
---|
193 | // CLSIDFromString()
|
---|
194 | // ----------------------------------------------------------------------
|
---|
195 | HRESULT WIN32API CLSIDFromString(
|
---|
196 | LPCOLESTR lpsz, // [in] - Unicode string CLSID
|
---|
197 | LPCLSID pclsid) // [out] - Binary CLSID
|
---|
198 | {
|
---|
199 | dprintf(("OLE32: CLSIDFromString"));
|
---|
200 |
|
---|
201 | oStringA tClsId(lpsz);
|
---|
202 |
|
---|
203 | return CLSIDFromString16(tClsId, pclsid);
|
---|
204 | }
|
---|
205 |
|
---|
206 | // ----------------------------------------------------------------------
|
---|
207 | // CoCreateGuid()
|
---|
208 | // ----------------------------------------------------------------------
|
---|
209 | HRESULT WIN32API CoCreateGuid(GUID *pguid)
|
---|
210 | {
|
---|
211 | dprintf(("OLE32: CoCreateGuid"));
|
---|
212 | memset(pguid, 0, sizeof(GUID)); //TODO: should be random GUID
|
---|
213 | return S_OK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // ----------------------------------------------------------------------
|
---|
217 | // WINE_StringFromCLSID
|
---|
218 | // ----------------------------------------------------------------------
|
---|
219 | HRESULT WIN32API WINE_StringFromCLSID(const CLSID *rclsid, LPSTR idstr)
|
---|
220 | {
|
---|
221 | dprintf(("OLE32: WINE_StringFromCLSID"));
|
---|
222 |
|
---|
223 | if (rclsid == NULL)
|
---|
224 | {
|
---|
225 | dprintf((" clsid: (NULL)"));
|
---|
226 | *idstr = 0;
|
---|
227 | return E_FAIL;
|
---|
228 | }
|
---|
229 |
|
---|
230 | // Setup new string...
|
---|
231 | sprintf(idstr, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
|
---|
232 | rclsid->Data1,
|
---|
233 | rclsid->Data2,
|
---|
234 | rclsid->Data3,
|
---|
235 | rclsid->Data4[0],
|
---|
236 | rclsid->Data4[1],
|
---|
237 | rclsid->Data4[2],
|
---|
238 | rclsid->Data4[3],
|
---|
239 | rclsid->Data4[4],
|
---|
240 | rclsid->Data4[5],
|
---|
241 | rclsid->Data4[6],
|
---|
242 | rclsid->Data4[7]);
|
---|
243 |
|
---|
244 | dprintf((" clsid: %s", idstr));
|
---|
245 |
|
---|
246 | return OLE_OK;
|
---|
247 | }
|
---|
248 |
|
---|
249 | // ----------------------------------------------------------------------
|
---|
250 | // StringFromCLSID
|
---|
251 | // Memory allocated here on behalf of application should be freed using CoTaskMemFree()
|
---|
252 | // ----------------------------------------------------------------------
|
---|
253 | HRESULT WIN32API StringFromCLSID(REFCLSID rclsid, LPOLESTR *ppsz)
|
---|
254 | {
|
---|
255 | char tmp[50];
|
---|
256 | LPWSTR szclsid;
|
---|
257 | size_t strLen;
|
---|
258 |
|
---|
259 | dprintf(("OLE32: StringFromCLSID"));
|
---|
260 |
|
---|
261 | // Setup new string...
|
---|
262 | WINE_StringFromCLSID(rclsid, tmp);
|
---|
263 |
|
---|
264 | strLen = strlen(tmp);
|
---|
265 |
|
---|
266 | // Grab buffer for string...
|
---|
267 | szclsid = (LPWSTR)CoTaskMemAlloc((strLen + 1) * sizeof(WCHAR));
|
---|
268 |
|
---|
269 | AsciiToUnicode(tmp, szclsid);
|
---|
270 |
|
---|
271 | *ppsz = (LPOLESTR)szclsid;
|
---|
272 |
|
---|
273 | return S_OK;
|
---|
274 | }
|
---|
275 |
|
---|
276 | // ----------------------------------------------------------------------
|
---|
277 | // StringFromIID
|
---|
278 | // Memory allocated here on behalf of application should be freed using CoTaskMemFree()
|
---|
279 | // ----------------------------------------------------------------------
|
---|
280 | HRESULT WIN32API StringFromIID(REFIID riid, LPOLESTR *ppsz)
|
---|
281 | {
|
---|
282 | char tmp[50];
|
---|
283 | LPWSTR sziid;
|
---|
284 | size_t strLen;
|
---|
285 |
|
---|
286 | dprintf(("OLE32: StringFromIID"));
|
---|
287 |
|
---|
288 | // Setup new string...
|
---|
289 | WINE_StringFromCLSID(riid, tmp);
|
---|
290 |
|
---|
291 | strLen = strlen(tmp);
|
---|
292 |
|
---|
293 | // Grab buffer for string...
|
---|
294 | sziid = (LPWSTR)CoTaskMemAlloc((strLen + 1) * sizeof(WCHAR));
|
---|
295 |
|
---|
296 | AsciiToUnicode(tmp, sziid);
|
---|
297 |
|
---|
298 | *ppsz = (LPOLESTR)sziid;
|
---|
299 |
|
---|
300 | return S_OK;
|
---|
301 | }
|
---|
302 |
|
---|
303 | // ----------------------------------------------------------------------
|
---|
304 | // StringFromGUID2
|
---|
305 | // ----------------------------------------------------------------------
|
---|
306 | int WIN32API StringFromGUID2(REFGUID rguid, LPOLESTR lpsz, int cbMax)
|
---|
307 | {
|
---|
308 | char tmp[50];
|
---|
309 | size_t strLen;
|
---|
310 |
|
---|
311 | dprintf(("OLE32: StringFromGUID2"));
|
---|
312 |
|
---|
313 | // Setup new string...
|
---|
314 | WINE_StringFromCLSID(rguid, tmp);
|
---|
315 |
|
---|
316 | strLen = strlen(tmp);
|
---|
317 |
|
---|
318 | if ( (strLen / 2) + 1 > cbMax )
|
---|
319 | return 0;
|
---|
320 |
|
---|
321 | AsciiToUnicode(tmp, lpsz);
|
---|
322 |
|
---|
323 | return (strLen / 2) + 1; // including 0 terminator
|
---|
324 | }
|
---|
325 |
|
---|
326 | // ----------------------------------------------------------------------
|
---|
327 | // CONCRETE_IsEqualGUID
|
---|
328 | // ----------------------------------------------------------------------
|
---|
329 | int WIN32API CONCRETE_IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
|
---|
330 | {
|
---|
331 | return IsEqualGUID(rguid1, rguid2);
|
---|
332 | }
|
---|
333 |
|
---|
334 | // ----------------------------------------------------------------------
|
---|
335 | // ReadClassStm
|
---|
336 | // ----------------------------------------------------------------------
|
---|
337 | HRESULT WIN32API ReadClassStm(IStream *pStm, CLSID *rclsid)
|
---|
338 | {
|
---|
339 | ULONG nbByte;
|
---|
340 | HRESULT res;
|
---|
341 |
|
---|
342 | dprintf(("OLE32: ReadClassStm"));
|
---|
343 |
|
---|
344 | if (rclsid == NULL)
|
---|
345 | return E_INVALIDARG;
|
---|
346 |
|
---|
347 | res = IStream_Read(pStm,(void*)rclsid,sizeof(CLSID),&nbByte);
|
---|
348 |
|
---|
349 | if (FAILED(res))
|
---|
350 | return res;
|
---|
351 |
|
---|
352 | if (nbByte != sizeof(CLSID))
|
---|
353 | return S_FALSE;
|
---|
354 |
|
---|
355 | return S_OK;
|
---|
356 | }
|
---|
357 |
|
---|
358 | // ----------------------------------------------------------------------
|
---|
359 | // WriteClassStm
|
---|
360 | // ----------------------------------------------------------------------
|
---|
361 | HRESULT WIN32API WriteClassStm(IStream *pStm, REFCLSID rclsid)
|
---|
362 | {
|
---|
363 | dprintf(("OLE32: WriteClassStm"));
|
---|
364 |
|
---|
365 | if (rclsid == NULL)
|
---|
366 | return E_INVALIDARG;
|
---|
367 |
|
---|
368 | return IStream_Write(pStm, rclsid, sizeof(CLSID), NULL);
|
---|
369 | }
|
---|
370 |
|
---|
371 | // ----------------------------------------------------------------------
|
---|
372 | // ProgIDFromCLSID
|
---|
373 | // ----------------------------------------------------------------------
|
---|
374 | HRESULT WIN32API ProgIDFromCLSID(REFCLSID clsid, LPOLESTR *lplpszProgID)
|
---|
375 | {
|
---|
376 | oStringA tClsId(clsid);
|
---|
377 | oStringA szKey("CLSID\\");
|
---|
378 | LONG lDataLen = 255;
|
---|
379 | oStringA szProgID(lDataLen, 1);
|
---|
380 | HKEY hKey;
|
---|
381 | HRESULT rc;
|
---|
382 | LPOLESTR tmp;
|
---|
383 | LPMALLOC pMllc;
|
---|
384 |
|
---|
385 | dprintf(("OLE32: ProgIDFromCLSID"));
|
---|
386 | dprintf((" clsid = %s", (char *)tClsId));
|
---|
387 |
|
---|
388 | szKey += tClsId + "\\ProgID";
|
---|
389 |
|
---|
390 | // Open key...
|
---|
391 | if (RegOpenKeyA(HKEY_CLASSES_ROOT, szKey, &hKey))
|
---|
392 | return REGDB_E_CLASSNOTREG;
|
---|
393 |
|
---|
394 | // Get default string from the key...
|
---|
395 | rc = RegQueryValueA(hKey, NULL, szProgID, &lDataLen);
|
---|
396 | RegCloseKey(hKey);
|
---|
397 | if (rc != 0)
|
---|
398 | return REGDB_E_CLASSNOTREG;
|
---|
399 |
|
---|
400 | if (CoGetMalloc(0, &pMllc)) // Singleton instance, no need to release
|
---|
401 | return E_OUTOFMEMORY;
|
---|
402 |
|
---|
403 | tmp = (LPOLESTR)IMalloc_Alloc(pMllc, (strlen(szProgID) + 1) * 2);
|
---|
404 | if (tmp == NULL)
|
---|
405 | return E_OUTOFMEMORY;
|
---|
406 |
|
---|
407 | AsciiToUnicode(szProgID, tmp);
|
---|
408 | *lplpszProgID = tmp;
|
---|
409 |
|
---|
410 | return S_OK;
|
---|
411 | }
|
---|