source: trunk/src/ole32/clsid.cpp@ 869

Last change on this file since 869 was 869, checked in by phaller, 26 years ago

Fix: exported CLSIDFromStringA as replacement for CLSIDFromString16

File size: 7.0 KB
Line 
1/*
2 *
3 * Project Odin Software License can be found in LICENSE.TXT
4 *
5 */
6/*
7 * ClassID Manipulation.
8 *
9 * 1/7/99
10 *
11 * Copyright 1999 David J. Raison
12 *
13 * Some portions from Wine Implementation
14 * Copyright 1995 Martin von Loewis
15 * Copyright 1998 Justin Bradford
16 * Copyright 1999 Francis Beaudet
17 * Copyright 1999 Sylvain St-Germain
18 */
19
20#include "ole32.h"
21
22#include "oString.h"
23
24// ----------------------------------------------------------------------
25// CLSIDFromProgID()
26// ----------------------------------------------------------------------
27HRESULT WIN32API CLSIDFromProgID(
28 LPCOLESTR lpszProgID, // [in] - UNICODE program id as found in registry
29 LPCLSID pclsid) // [out] - CLSID
30{
31 dprintf(("OLE32: CLSIDFromProgID"));
32
33 LONG lDataLen = 80;
34 oStringW szKey(lpszProgID);
35 oStringW szCLSID(lDataLen, 1);
36 HKEY hKey;
37 HRESULT rc;
38
39 // Create the registry lookup string...
40 szKey += L"\\CLSID";
41
42 // Try to open the key in the registry...
43 rc = RegOpenKeyW(HKEY_CLASSES_ROOT, szKey, &hKey);
44 if (rc != 0)
45 return OLE_ERROR_GENERIC;
46
47 // Now get the data from the _default_ entry on this key...
48 rc = RegQueryValueW(hKey, NULL, szCLSID, &lDataLen);
49 RegCloseKey(hKey);
50 if (rc != 0)
51 return OLE_ERROR_GENERIC;
52
53 // Now convert from a string to a UUID
54 return CLSIDFromString(szCLSID, pclsid);
55}
56
57// ----------------------------------------------------------------------
58// IIDFromString
59// ----------------------------------------------------------------------
60HRESULT WIN32API IIDFromString(LPSTR lpsz, LPIID lpiid)
61{
62 dprintf(("OLE32: IIDFromString"));
63 return CLSIDFromString((LPCOLESTR)lpsz, (LPCLSID)lpiid);
64}
65
66
67// ----------------------------------------------------------------------
68// CLSIDFromStringA()
69// @@@PH: this is not a WINE API, but a replacement for CLSIDFromString16
70// which used to accept ASCII strings instead of OLE strings
71// ----------------------------------------------------------------------
72
73// missing prototype
74LPWSTR WIN32API HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str );
75
76HRESULT WIN32API CLSIDFromStringA(
77 LPSTR lpsz, // [in] - ASCII string CLSID
78 LPCLSID pclsid) // [out] - Binary CLSID
79{
80 LPWSTR lpszOle = HEAP_strdupAtoW(GetProcessHeap(),
81 0,
82 lpsz);
83 HRESULT hRes;
84
85 dprintf(("OLE32: CLSIDFromStringA"));
86
87 hRes = CLSIDFromString(lpszOle, pclsid);
88 HeapFree(GetProcessHeap(), 0, lpszOle);
89 return hRes;
90}
91
92
93// ----------------------------------------------------------------------
94// CLSIDFromString()
95// ----------------------------------------------------------------------
96HRESULT WIN32API CLSIDFromString(
97 LPCOLESTR lpsz, // [in] - Unicode string CLSID
98 LPCLSID pclsid) // [out] - Binary CLSID
99{
100 dprintf(("OLE32: CLSIDFromString"));
101
102 oStringA tClsId(lpsz);
103
104 HRESULT ret = OLE_ERROR_GENERIC;
105
106 // Convert to binary CLSID
107 char *s = (char *) tClsId;
108 char *p;
109 int i;
110 char table[256];
111
112 /* quick lookup table */
113 memset(table, 0, 256);
114
115 for (i = 0; i < 10; i++)
116 {
117 table['0' + i] = i;
118 }
119 for (i = 0; i < 6; i++)
120 {
121 table['A' + i] = i+10;
122 table['a' + i] = i+10;
123 }
124
125 /* in form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} */
126
127 if (lstrlenW(lpsz) != 38)
128 return OLE_ERROR_OBJECT;
129
130 p = (char *) pclsid;
131
132 s++; /* skip leading brace */
133 for (i = 0; i < 4; i++)
134 {
135 p[3 - i] = table[*s]<<4 | table[*(s+1)];
136 s += 2;
137 }
138 p += 4;
139 s++; /* skip - */
140
141 for (i = 0; i < 2; i++)
142 {
143 p[1-i] = table[*s]<<4 | table[*(s+1)];
144 s += 2;
145 }
146 p += 2;
147 s++; /* skip - */
148
149 for (i = 0; i < 2; i++)
150 {
151 p[1-i] = table[*s]<<4 | table[*(s+1)];
152 s += 2;
153 }
154 p += 2;
155 s++; /* skip - */
156
157 /* these are just sequential bytes */
158 for (i = 0; i < 2; i++)
159 {
160 *p++ = table[*s]<<4 | table[*(s+1)];
161 s += 2;
162 }
163 s++; /* skip - */
164
165 for (i = 0; i < 6; i++)
166 {
167 *p++ = table[*s]<<4 | table[*(s+1)];
168 s += 2;
169 }
170
171 return S_OK;
172}
173
174// ----------------------------------------------------------------------
175// CoCreateGuid()
176// ----------------------------------------------------------------------
177HRESULT WIN32API CoCreateGuid(GUID *pguid)
178{
179 dprintf(("OLE32: CoCreateGuid"));
180 memset(pguid, 0, sizeof(GUID)); //TODO: should be random GUID
181 return S_OK;
182}
183
184// ----------------------------------------------------------------------
185// WINE_StringFromCLSID
186// ----------------------------------------------------------------------
187HRESULT WIN32API WINE_StringFromCLSID(const CLSID *rclsid, LPSTR idstr)
188{
189 dprintf(("OLE32: WINE_StringFromCLSID"));
190
191 if (rclsid == NULL)
192 {
193 dprintf((" clsid: (NULL)"));
194 *idstr = 0;
195 return E_FAIL;
196 }
197
198 // Setup new string...
199 sprintf(idstr, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
200 rclsid->Data1,
201 rclsid->Data2,
202 rclsid->Data3,
203 rclsid->Data4[0],
204 rclsid->Data4[1],
205 rclsid->Data4[2],
206 rclsid->Data4[3],
207 rclsid->Data4[4],
208 rclsid->Data4[5],
209 rclsid->Data4[6],
210 rclsid->Data4[7]);
211
212 dprintf((" clsid: %s", idstr));
213
214 return OLE_OK;
215}
216
217// ----------------------------------------------------------------------
218// StringFromCLSID
219// Memory allocated here on behalf of application should be freed using CoTaskMemFree()
220// ----------------------------------------------------------------------
221HRESULT WIN32API StringFromCLSID(REFCLSID rclsid, LPOLESTR *ppsz)
222{
223 char tmp[50];
224 LPWSTR szclsid;
225 size_t strLen;
226
227 dprintf(("OLE32: StringFromCLSID"));
228
229 // Setup new string...
230 WINE_StringFromCLSID(rclsid, tmp);
231
232 strLen = strlen(tmp);
233
234 // Grab buffer for string...
235 szclsid = (LPWSTR)CoTaskMemAlloc((strLen + 1) * sizeof(WCHAR));
236
237 AsciiToUnicode(tmp, szclsid);
238
239 *ppsz = (LPOLESTR)szclsid;
240
241 return S_OK;
242}
243
244// ----------------------------------------------------------------------
245// StringFromIID
246// Memory allocated here on behalf of application should be freed using CoTaskMemFree()
247// ----------------------------------------------------------------------
248HRESULT WIN32API StringFromIID(REFIID riid, LPOLESTR *ppsz)
249{
250 char tmp[50];
251 LPWSTR sziid;
252 size_t strLen;
253
254 dprintf(("OLE32: StringFromIID"));
255
256 // Setup new string...
257 WINE_StringFromCLSID(riid, tmp);
258
259 strLen = strlen(tmp);
260
261 // Grab buffer for string...
262 sziid = (LPWSTR)CoTaskMemAlloc((strLen + 1) * sizeof(WCHAR));
263
264 AsciiToUnicode(tmp, sziid);
265
266 *ppsz = (LPOLESTR)sziid;
267
268 return S_OK;
269}
270
271// ----------------------------------------------------------------------
272// StringFromGUID2
273// ----------------------------------------------------------------------
274int WIN32API StringFromGUID2(REFGUID rguid, LPOLESTR lpsz, int cbMax)
275{
276 char tmp[50];
277 size_t strLen;
278
279 dprintf(("OLE32: StringFromGUID2"));
280
281 // Setup new string...
282 WINE_StringFromCLSID(rguid, tmp);
283
284 strLen = strlen(tmp);
285
286 if(cbMax < (strLen * 2 + 1))
287 return 0;
288
289 AsciiToUnicode(tmp, lpsz);
290
291 return(strLen * 2 + 1); // including 0 terminator
292}
293
Note: See TracBrowser for help on using the repository browser.