Add tests for NORM_IGNORENONSPACE and NORM_IGNORESYMBOLS in the
[wine/wine64.git] / dlls / oleaut32 / oleaut.c
blob5d1802835086ba37220a926fe776c44bd7c5c634
1 /*
2 * OLEAUT32
4 * Copyright 1999, 2000 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <string.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
29 #include "ole2.h"
30 #include "olectl.h"
31 #include "oleauto.h"
32 #include "wine/obj_olefont.h"
34 #include "tmarshal.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 /* The OLE Automation ProxyStub Interface Class (aka Typelib Marshaler) */
41 extern const GUID CLSID_PSOAInterface;
43 /* IDispatch marshaler */
44 extern const GUID CLSID_PSDispatch;
46 /******************************************************************************
47 * SysStringLen [OLEAUT32.7]
49 * The Windows documentation states that the length returned by this function
50 * is not necessarely the same as the length returned by the _lstrlenW method.
51 * It is the same number that was passed in as the "len" parameter if the
52 * string was allocated with a SysAllocStringLen method call.
54 int WINAPI SysStringLen(BSTR str)
56 DWORD* bufferPointer;
58 if (!str) return 0;
60 * The length of the string (in bytes) is contained in a DWORD placed
61 * just before the BSTR pointer
63 bufferPointer = (DWORD*)str;
65 bufferPointer--;
67 return (int)(*bufferPointer/sizeof(WCHAR));
70 /******************************************************************************
71 * SysStringByteLen [OLEAUT32.149]
73 * The Windows documentation states that the length returned by this function
74 * is not necessarely the same as the length returned by the _lstrlenW method.
75 * It is the same number that was passed in as the "len" parameter if the
76 * string was allocated with a SysAllocStringLen method call.
78 int WINAPI SysStringByteLen(BSTR str)
80 DWORD* bufferPointer;
82 if (!str) return 0;
84 * The length of the string (in bytes) is contained in a DWORD placed
85 * just before the BSTR pointer
87 bufferPointer = (DWORD*)str;
89 bufferPointer--;
91 return (int)(*bufferPointer);
94 /******************************************************************************
95 * SysAllocString [OLEAUT32.2]
97 * MSDN (October 2001) states that this returns a NULL value if the argument
98 * is a zero-length string. This does not appear to be true; certainly it
99 * returns a value under Win98 (Oleaut32.dll Ver 2.40.4515.0)
101 BSTR WINAPI SysAllocString(LPCOLESTR in)
103 if (!in) return 0;
105 /* Delegate this to the SysAllocStringLen32 method. */
106 return SysAllocStringLen(in, lstrlenW(in));
109 /******************************************************************************
110 * SysFreeString [OLEAUT32.6]
112 void WINAPI SysFreeString(BSTR in)
114 DWORD* bufferPointer;
116 /* NULL is a valid parameter */
117 if(!in) return;
120 * We have to be careful when we free a BSTR pointer, it points to
121 * the beginning of the string but it skips the byte count contained
122 * before the string.
124 bufferPointer = (DWORD*)in;
126 bufferPointer--;
129 * Free the memory from its "real" origin.
131 HeapFree(GetProcessHeap(), 0, bufferPointer);
134 /******************************************************************************
135 * SysAllocStringLen [OLEAUT32.4]
137 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
138 * section, he describes the DWORD value placed *before* the BSTR data type.
139 * he describes it as a "DWORD count of characters". By experimenting with
140 * a windows application, this count seems to be a DWORD count of bytes in
141 * the string. Meaning that the count is double the number of wide
142 * characters in the string.
144 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
146 DWORD bufferSize;
147 DWORD* newBuffer;
148 WCHAR* stringBuffer;
151 * Find the length of the buffer passed-in in bytes.
153 bufferSize = len * sizeof (WCHAR);
156 * Allocate a new buffer to hold the string.
157 * dont't forget to keep an empty spot at the beginning of the
158 * buffer for the character count and an extra character at the
159 * end for the NULL.
161 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
163 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
166 * If the memory allocation failed, return a null pointer.
168 if (newBuffer==0)
169 return 0;
172 * Copy the length of the string in the placeholder.
174 *newBuffer = bufferSize;
177 * Skip the byte count.
179 newBuffer++;
182 * Copy the information in the buffer.
183 * Since it is valid to pass a NULL pointer here, we'll initialize the
184 * buffer to nul if it is the case.
186 if (in != 0)
187 memcpy(newBuffer, in, bufferSize);
188 else
189 memset(newBuffer, 0, bufferSize);
192 * Make sure that there is a nul character at the end of the
193 * string.
195 stringBuffer = (WCHAR*)newBuffer;
196 stringBuffer[len] = L'\0';
198 return (LPWSTR)stringBuffer;
201 /******************************************************************************
202 * SysReAllocStringLen [OLEAUT32.5]
204 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
207 * Sanity check
209 if (old==NULL)
210 return 0;
212 if (*old!=NULL) {
213 DWORD newbytelen = len*sizeof(WCHAR);
214 DWORD *ptr = HeapReAlloc(GetProcessHeap(),0,((DWORD*)*old)-1,newbytelen+sizeof(WCHAR)+sizeof(DWORD));
215 *old = (BSTR)(ptr+1);
216 *ptr = newbytelen;
217 if (in) {
218 memcpy(*old, in, newbytelen);
219 (*old)[len] = 0;
220 } else {
221 /* Subtle hidden feature: The old string data is still there
222 * when 'in' is NULL!
223 * Some Microsoft program needs it.
226 } else {
228 * Allocate the new string
230 *old = SysAllocStringLen(in, len);
233 return 1;
236 /******************************************************************************
237 * SysAllocStringByteLen [OLEAUT32.150]
240 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
242 DWORD* newBuffer;
243 char* stringBuffer;
246 * Allocate a new buffer to hold the string.
247 * dont't forget to keep an empty spot at the beginning of the
248 * buffer for the character count and an extra character at the
249 * end for the NULL.
251 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
253 len + sizeof(WCHAR) + sizeof(DWORD));
256 * If the memory allocation failed, return a null pointer.
258 if (newBuffer==0)
259 return 0;
262 * Copy the length of the string in the placeholder.
264 *newBuffer = len;
267 * Skip the byte count.
269 newBuffer++;
272 * Copy the information in the buffer.
273 * Since it is valid to pass a NULL pointer here, we'll initialize the
274 * buffer to nul if it is the case.
276 if (in != 0)
277 memcpy(newBuffer, in, len);
280 * Make sure that there is a nul character at the end of the
281 * string.
283 stringBuffer = (char *)newBuffer;
284 stringBuffer[len] = 0;
285 stringBuffer[len+1] = 0;
287 return (LPWSTR)stringBuffer;
290 /******************************************************************************
291 * SysReAllocString [OLEAUT32.3]
293 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
296 * Sanity check
298 if (old==NULL)
299 return 0;
302 * Make sure we free the old string.
304 if (*old!=NULL)
305 SysFreeString(*old);
308 * Allocate the new string
310 *old = SysAllocString(in);
312 return 1;
315 static WCHAR _delimiter[2] = {'!',0}; /* default delimiter apparently */
316 static WCHAR *pdelimiter = &_delimiter[0];
318 /***********************************************************************
319 * RegisterActiveObject (OLEAUT32.33)
321 HRESULT WINAPI RegisterActiveObject(
322 LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
324 WCHAR guidbuf[80];
325 HRESULT ret;
326 LPRUNNINGOBJECTTABLE runobtable;
327 LPMONIKER moniker;
329 StringFromGUID2(rcid,guidbuf,39);
330 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
331 if (FAILED(ret))
332 return ret;
333 ret = GetRunningObjectTable(0,&runobtable);
334 if (FAILED(ret)) {
335 IMoniker_Release(moniker);
336 return ret;
338 ret = IRunningObjectTable_Register(runobtable,dwFlags,punk,moniker,pdwRegister);
339 IRunningObjectTable_Release(runobtable);
340 IMoniker_Release(moniker);
341 return ret;
344 /***********************************************************************
345 * RevokeActiveObject (OLEAUT32.34)
347 HRESULT WINAPI RevokeActiveObject(DWORD xregister,LPVOID reserved)
349 LPRUNNINGOBJECTTABLE runobtable;
350 HRESULT ret;
352 ret = GetRunningObjectTable(0,&runobtable);
353 if (FAILED(ret)) return ret;
354 ret = IRunningObjectTable_Revoke(runobtable,xregister);
355 if (SUCCEEDED(ret)) ret = S_OK;
356 IRunningObjectTable_Release(runobtable);
357 return ret;
360 /***********************************************************************
361 * GetActiveObject (OLEAUT32.35)
363 HRESULT WINAPI GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
365 WCHAR guidbuf[80];
366 HRESULT ret;
367 LPRUNNINGOBJECTTABLE runobtable;
368 LPMONIKER moniker;
370 StringFromGUID2(rcid,guidbuf,39);
371 ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
372 if (FAILED(ret))
373 return ret;
374 ret = GetRunningObjectTable(0,&runobtable);
375 if (FAILED(ret)) {
376 IMoniker_Release(moniker);
377 return ret;
379 ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
380 IRunningObjectTable_Release(runobtable);
381 IMoniker_Release(moniker);
382 return ret;
386 /***********************************************************************
387 * OaBuildVersion [OLEAUT32.170]
389 * known OLEAUT32.DLL versions:
390 * OLE 2.1 NT 1993-95 10 3023
391 * OLE 2.1 10 3027
392 * Win32s 1.1e 20 4049
393 * OLE 2.20 W95/NT 1993-96 20 4112
394 * OLE 2.20 W95/NT 1993-96 20 4118
395 * OLE 2.20 W95/NT 1993-96 20 4122
396 * OLE 2.30 W95/NT 1993-98 30 4265
397 * OLE 2.40 NT?? 1993-98 40 4267
398 * OLE 2.40 W98 SE orig. file 1993-98 40 4275
399 * OLE 2.40 W2K orig. file 1993-XX 40 4514
401 * I just decided to use version 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
402 * and 2.40 for all newer OSs. The build number is maximum, i.e. 0xffff.
404 UINT WINAPI OaBuildVersion()
406 switch(GetVersion() & 0x8000ffff) /* mask off build number */
408 case 0x80000a03: /* WIN31 */
409 return MAKELONG(0xffff, 20);
410 case 0x00003303: /* NT351 */
411 return MAKELONG(0xffff, 30);
412 case 0x80000004: /* WIN95; I'd like to use the "standard" w95 minor
413 version here (30), but as we still use w95
414 as default winver (which is good IMHO), I better
415 play safe and use the latest value for w95 for now.
416 Change this as soon as default winver gets changed
417 to something more recent */
418 case 0x80000a04: /* WIN98 */
419 case 0x00000004: /* NT40 */
420 case 0x00000005: /* W2K */
421 return MAKELONG(0xffff, 40);
422 default:
423 ERR("Version value not known yet. Please investigate it !\n");
424 return 0x0;
428 /******************************************************************************
429 * OleTranslateColor [OLEAUT32.421]
431 * Converts an OLE_COLOR to a COLORREF.
432 * See the documentation for conversion rules.
433 * pColorRef can be NULL. In that case the user only wants to test the
434 * conversion.
436 HRESULT WINAPI OleTranslateColor(
437 OLE_COLOR clr,
438 HPALETTE hpal,
439 COLORREF* pColorRef)
441 COLORREF colorref;
442 BYTE b = HIBYTE(HIWORD(clr));
444 TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
447 * In case pColorRef is NULL, provide our own to simplify the code.
449 if (pColorRef == NULL)
450 pColorRef = &colorref;
452 switch (b)
454 case 0x00:
456 if (hpal != 0)
457 *pColorRef = PALETTERGB(GetRValue(clr),
458 GetGValue(clr),
459 GetBValue(clr));
460 else
461 *pColorRef = clr;
463 break;
466 case 0x01:
468 if (hpal != 0)
470 PALETTEENTRY pe;
472 * Validate the palette index.
474 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
475 return E_INVALIDARG;
478 *pColorRef = clr;
480 break;
483 case 0x02:
484 *pColorRef = clr;
485 break;
487 case 0x80:
489 int index = LOBYTE(LOWORD(clr));
492 * Validate GetSysColor index.
494 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
495 return E_INVALIDARG;
497 *pColorRef = GetSysColor(index);
499 break;
502 default:
503 return E_INVALIDARG;
506 return S_OK;
509 /***********************************************************************
510 * DllRegisterServer (OLEAUT32.320)
512 HRESULT WINAPI OLEAUT32_DllRegisterServer() {
513 FIXME("stub!\n");
514 return S_OK;
517 /***********************************************************************
518 * DllUnregisterServer (OLEAUT32.321)
520 HRESULT WINAPI OLEAUT32_DllUnregisterServer() {
521 FIXME("stub!\n");
522 return S_OK;
525 extern HRESULT OLEAUTPS_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv);
527 extern void _get_STDFONT_CF(LPVOID);
528 extern void _get_STDPIC_CF(LPVOID);
530 /***********************************************************************
531 * DllGetClassObject (OLEAUT32.1)
533 HRESULT WINAPI OLEAUT32_DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
535 *ppv = NULL;
536 if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
537 if (IsEqualGUID(iid,&IID_IClassFactory)) {
538 _get_STDFONT_CF(ppv);
539 IClassFactory_AddRef((IClassFactory*)*ppv);
540 return S_OK;
543 if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
544 if (IsEqualGUID(iid,&IID_IClassFactory)) {
545 _get_STDPIC_CF(ppv);
546 IClassFactory_AddRef((IClassFactory*)*ppv);
547 return S_OK;
550 if (IsEqualGUID(rclsid,&CLSID_PSDispatch)) {
551 return OLEAUTPS_DllGetClassObject(rclsid,iid,ppv);
553 if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
554 if (S_OK==TypeLibFac_DllGetClassObject(rclsid,iid,ppv))
555 return S_OK;
556 /*FALLTHROUGH*/
558 FIXME("\n\tCLSID:\t%s,\n\tIID:\t%s\n",debugstr_guid(rclsid),debugstr_guid(iid));
559 return CLASS_E_CLASSNOTAVAILABLE;
562 /***********************************************************************
563 * DllCanUnloadNow (OLEAUT32.410)
565 HRESULT WINAPI OLEAUT32_DllCanUnloadNow() {
566 FIXME("(), stub!\n");
567 return S_FALSE;