Now relying on exception codes to know when debugger is entered for a
[wine.git] / dlls / oleaut32 / ole2disp.c
blob54353f4762df41ac8fa87762f9d15a92464a93aa
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
5 */
6 #include <string.h>
7 #include "windef.h"
8 #include "wingdi.h"
9 #include "winuser.h"
10 #include "winerror.h"
11 #include "olectl.h"
12 #include "oleauto.h"
13 #include "heap.h"
14 #include "ldt.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(ole);
19 /* This implementation of the BSTR API is 16-bit only. It
20 represents BSTR as a 16:16 far pointer, and the strings
21 as ISO-8859 */
23 /******************************************************************************
24 * BSTR_AllocBytes [Internal]
26 static BSTR16 BSTR_AllocBytes(int n)
28 void *ptr = SEGPTR_ALLOC(n);
29 return (BSTR16)SEGPTR_GET(ptr);
32 /******************************************************************************
33 * BSTR_Free [INTERNAL]
35 static void BSTR_Free(BSTR16 in)
37 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
40 /******************************************************************************
41 * BSTR_GetAddr [INTERNAL]
43 static void* BSTR_GetAddr(BSTR16 in)
45 return in ? PTR_SEG_TO_LIN(in) : 0;
48 /******************************************************************************
49 * SysAllocString16 [OLE2DISP.2]
51 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
53 BSTR16 out=BSTR_AllocBytes(strlen(in)+1);
54 if(!out)return 0;
55 strcpy(BSTR_GetAddr(out),in);
56 return out;
59 /******************************************************************************
60 * SysAllocString32 [OLEAUT32.2]
62 BSTR WINAPI SysAllocString(LPCOLESTR in)
64 /* Delegate this to the SysAllocStringLen32 method. */
65 return SysAllocStringLen(in, lstrlenW(in));
68 /******************************************************************************
69 * SysReAllocString16 [OLE2DISP.3]
71 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
73 BSTR16 new=SysAllocString16(in);
74 BSTR_Free(*old);
75 *old=new;
76 return 1;
79 /******************************************************************************
80 * SysReAllocString32 [OLEAUT32.3]
82 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
85 * Sanity check
87 if (old==NULL)
88 return 0;
91 * Make sure we free the old string.
93 if (*old!=NULL)
94 SysFreeString(*old);
97 * Allocate the new string
99 *old = SysAllocString(in);
101 return 1;
104 /******************************************************************************
105 * SysAllocStringLen16 [OLE2DISP.4]
107 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
109 BSTR16 out=BSTR_AllocBytes(len+1);
111 if (!out)
112 return 0;
115 * Copy the information in the buffer.
116 * Since it is valid to pass a NULL pointer here, we'll initialize the
117 * buffer to nul if it is the case.
119 if (in != 0)
120 strcpy(BSTR_GetAddr(out),in);
121 else
122 memset(BSTR_GetAddr(out), 0, len+1);
124 return out;
127 /******************************************************************************
128 * SysAllocStringLen32 [OLEAUT32.4]
130 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
131 * section, he describes the DWORD value placed before the BSTR data type.
132 * he describes it as a "DWORD count of characters". By experimenting with
133 * a windows application, this count seems to be a DWORD count of bytes in
134 * the string. Meaning that the count is double the number of wide
135 * characters in the string.
137 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
139 DWORD bufferSize;
140 DWORD* newBuffer;
141 WCHAR* stringBuffer;
144 * Find the lenth of the buffer passed-in in bytes.
146 bufferSize = len * sizeof (WCHAR);
149 * Allocate a new buffer to hold the string.
150 * dont't forget to keep an empty spot at the begining of the
151 * buffer for the character count and an extra character at the
152 * end for the NULL.
154 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
156 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
159 * If the memory allocation failed, return a null pointer.
161 if (newBuffer==0)
162 return 0;
165 * Copy the length of the string in the placeholder.
167 *newBuffer = bufferSize;
170 * Skip the byte count.
172 newBuffer++;
175 * Copy the information in the buffer.
176 * Since it is valid to pass a NULL pointer here, we'll initialize the
177 * buffer to nul if it is the case.
179 if (in != 0)
180 memcpy(newBuffer, in, bufferSize);
181 else
182 memset(newBuffer, 0, bufferSize);
185 * Make sure that there is a nul character at the end of the
186 * string.
188 stringBuffer = (WCHAR*)newBuffer;
189 stringBuffer[len] = L'\0';
191 return (LPWSTR)stringBuffer;
194 /******************************************************************************
195 * SysReAllocStringLen16 [OLE2DISP.5]
197 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
199 BSTR16 new=SysAllocStringLen16(in,len);
200 BSTR_Free(*old);
201 *old=new;
202 return 1;
206 /******************************************************************************
207 * SysReAllocStringLen32 [OLEAUT32.5]
209 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
212 * Sanity check
214 if (old==NULL)
215 return 0;
218 * Make sure we free the old string.
220 if (*old!=NULL)
221 SysFreeString(*old);
224 * Allocate the new string
226 *old = SysAllocStringLen(in, len);
228 return 1;
231 /******************************************************************************
232 * SysFreeString16 [OLE2DISP.6]
234 void WINAPI SysFreeString16(BSTR16 in)
236 BSTR_Free(in);
239 /******************************************************************************
240 * SysFreeString32 [OLEAUT32.6]
242 void WINAPI SysFreeString(BSTR in)
244 DWORD* bufferPointer;
246 /* NULL is a valid parameter */
247 if(!in) return;
250 * We have to be careful when we free a BSTR pointer, it points to
251 * the beginning of the string but it skips the byte count contained
252 * before the string.
254 bufferPointer = (DWORD*)in;
256 bufferPointer--;
259 * Free the memory from it's "real" origin.
261 HeapFree(GetProcessHeap(), 0, bufferPointer);
264 /******************************************************************************
265 * SysStringLen16 [OLE2DISP.7]
267 int WINAPI SysStringLen16(BSTR16 str)
269 return strlen(BSTR_GetAddr(str));
272 /******************************************************************************
273 * SysStringLen32 [OLEAUT32.7]
275 * The Windows documentation states that the length returned by this function
276 * is not necessarely the same as the length returned by the _lstrlenW method.
277 * It is the same number that was passed in as the "len" parameter if the
278 * string was allocated with a SysAllocStringLen method call.
280 int WINAPI SysStringLen(BSTR str)
282 DWORD* bufferPointer;
284 if (!str) return 0;
286 * The length of the string (in bytes) is contained in a DWORD placed
287 * just before the BSTR pointer
289 bufferPointer = (DWORD*)str;
291 bufferPointer--;
293 return (int)(*bufferPointer/sizeof(WCHAR));
296 /******************************************************************************
297 * SysStringByteLen [OLEAUT32.149]
299 * The Windows documentation states that the length returned by this function
300 * is not necessarely the same as the length returned by the _lstrlenW method.
301 * It is the same number that was passed in as the "len" parameter if the
302 * string was allocated with a SysAllocStringLen method call.
304 int WINAPI SysStringByteLen(BSTR str)
306 return SysStringLen(str)*sizeof(WCHAR);
309 /******************************************************************************
310 * CreateDispTypeInfo [OLE2DISP.31]
312 HRESULT WINAPI CreateDispTypeInfo16(
313 INTERFACEDATA *pidata,
314 LCID lcid,
315 ITypeInfo **pptinfo
317 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
318 return 0;
321 /******************************************************************************
322 * RegisterActiveObject [OLE2DISP.35]
324 HRESULT WINAPI RegisterActiveObject16(
325 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
327 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
328 return 0;
331 /******************************************************************************
332 * OleTranslateColor [OLEAUT32.421]
334 * Converts an OLE_COLOR to a COLORREF.
335 * See the documentation for conversion rules.
336 * pColorRef can be NULL. In that case the user only wants to test the
337 * conversion.
339 HRESULT WINAPI OleTranslateColor(
340 OLE_COLOR clr,
341 HPALETTE hpal,
342 COLORREF* pColorRef)
344 COLORREF colorref;
345 BYTE b = HIBYTE(HIWORD(clr));
347 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
350 * In case pColorRef is NULL, provide our own to simplify the code.
352 if (pColorRef == NULL)
353 pColorRef = &colorref;
355 switch (b)
357 case 0x00:
359 if (hpal != 0)
360 *pColorRef = PALETTERGB(GetRValue(clr),
361 GetGValue(clr),
362 GetBValue(clr));
363 else
364 *pColorRef = clr;
366 break;
369 case 0x01:
371 if (hpal != 0)
373 PALETTEENTRY pe;
375 * Validate the palette index.
377 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
378 return E_INVALIDARG;
381 *pColorRef = clr;
383 break;
386 case 0x02:
387 *pColorRef = clr;
388 break;
390 case 0x80:
392 int index = LOBYTE(LOWORD(clr));
395 * Validate GetSysColor index.
397 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
398 return E_INVALIDARG;
400 *pColorRef = GetSysColor(index);
402 break;
405 default:
406 return E_INVALIDARG;
409 return S_OK;
412 /******************************************************************************
413 * SysAllocStringByteLen [OLEAUT32.150]
416 BSTR WINAPI SysAllocStringByteLen(char *in, int len)
418 DWORD* newBuffer;
419 char* stringBuffer;
422 * Allocate a new buffer to hold the string.
423 * dont't forget to keep an empty spot at the begining of the
424 * buffer for the character count and an extra character at the
425 * end for the NULL.
427 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
429 len + sizeof(WCHAR) + sizeof(DWORD));
432 * If the memory allocation failed, return a null pointer.
434 if (newBuffer==0)
435 return 0;
438 * Copy the length of the string in the placeholder.
440 *newBuffer = len;
443 * Skip the byte count.
445 newBuffer++;
448 * Copy the information in the buffer.
449 * Since it is valid to pass a NULL pointer here, we'll initialize the
450 * buffer to nul if it is the case.
452 if (in != 0)
453 memcpy(newBuffer, in, len);
456 * Make sure that there is a nul character at the end of the
457 * string.
459 stringBuffer = (char *)newBuffer;
460 stringBuffer[len] = 0;
461 stringBuffer[len+1] = 0;
463 return (LPWSTR)stringBuffer;