Fix OemToAnsiBuff for real Win31 behavior.
[wine.git] / ole / ole2disp.c
blob13b69642ee04fce0e98b08ea7d43ec58f1aad725
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
5 */
7 #include <string.h>
8 #include "winerror.h"
9 #include "ole2.h"
10 #include "oleauto.h"
11 #include "wine/obj_base.h"
12 #include "heap.h"
13 #include "ldt.h"
14 #include "debug.h"
16 /* This implementation of the BSTR API is 16-bit only. It
17 represents BSTR as a 16:16 far pointer, and the strings
18 as ISO-8859 */
20 /******************************************************************************
21 * BSTR_AllocBytes [Internal]
23 static BSTR16 BSTR_AllocBytes(int n)
25 void *ptr = SEGPTR_ALLOC(n);
26 return (BSTR16)SEGPTR_GET(ptr);
29 /******************************************************************************
30 * BSTR_Free [INTERNAL]
32 static void BSTR_Free(BSTR16 in)
34 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
37 /******************************************************************************
38 * BSTR_GetAddr [INTERNAL]
40 static void* BSTR_GetAddr(BSTR16 in)
42 return in ? PTR_SEG_TO_LIN(in) : 0;
45 /******************************************************************************
46 * SysAllocString16 [OLE2DISP.2]
48 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
50 BSTR16 out=BSTR_AllocBytes(strlen(in)+1);
51 if(!out)return 0;
52 strcpy(BSTR_GetAddr(out),in);
53 return out;
56 /******************************************************************************
57 * SysAllocString32 [OLEAUT32.2]
59 BSTR WINAPI SysAllocString(LPCOLESTR in)
61 /* Delegate this to the SysAllocStringLen32 method. */
62 return SysAllocStringLen(in, lstrlenW(in));
65 /******************************************************************************
66 * SysReAllocString16 [OLE2DISP.3]
68 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
70 BSTR16 new=SysAllocString16(in);
71 BSTR_Free(*old);
72 *old=new;
73 return 1;
76 /******************************************************************************
77 * SysReAllocString32 [OLEAUT32.3]
79 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
82 * Sanity check
84 if (old==NULL)
85 return 0;
88 * Make sure we free the old string.
90 if (*old!=NULL)
91 SysFreeString(*old);
94 * Allocate the new string
96 *old = SysAllocString(in);
98 return 1;
101 /******************************************************************************
102 * SysAllocStringLen16 [OLE2DISP.4]
104 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
106 BSTR16 out=BSTR_AllocBytes(len+1);
107 if(!out)return 0;
108 strcpy(BSTR_GetAddr(out),in);
109 return out;
112 /******************************************************************************
113 * SysAllocStringLen32 [OLEAUT32.4]
115 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
116 * section, he describes the DWORD value placed before the BSTR data type.
117 * he describes it as a "DWORD count of characters". By experimenting with
118 * a windows application, this count seems to be a DWORD count of bytes in
119 * the string. Meaning that the count is double the number of wide
120 * characters in the string.
122 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
124 DWORD bufferSize;
125 DWORD* newBuffer;
126 WCHAR* stringBuffer;
129 * Find the lenth of the buffer passed-in in bytes.
131 bufferSize = len * sizeof (WCHAR);
134 * Allocate a new buffer to hold the string.
135 * dont't forget to keep an empty spot at the begining of the
136 * buffer for the character count and an extra character at the
137 * end for the NULL.
139 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
141 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
144 * If the memory allocation failed, return a null pointer.
146 if (newBuffer==0)
147 return 0;
150 * Copy the length of the string in the placeholder.
152 *newBuffer = bufferSize;
155 * Skip the byte count.
157 newBuffer++;
160 * Copy the information in the buffer.
161 * Since it is valid to pass a NULL pointer here, we'll initialize the
162 * buffer to nul if it is the case.
164 if (in != 0)
165 memcpy(newBuffer, in, bufferSize);
166 else
167 memset(newBuffer, 0, bufferSize);
170 * Make sure that there is a nul character at the end of the
171 * string.
173 stringBuffer = (WCHAR*)newBuffer;
174 stringBuffer[len] = L'\0';
176 return (LPWSTR)stringBuffer;
179 /******************************************************************************
180 * SysReAllocStringLen16 [OLE2DISP.5]
182 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
184 BSTR16 new=SysAllocStringLen16(in,len);
185 BSTR_Free(*old);
186 *old=new;
187 return 1;
191 /******************************************************************************
192 * SysReAllocStringLen32 [OLEAUT32.5]
194 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
197 * Sanity check
199 if (old==NULL)
200 return 0;
203 * Make sure we free the old string.
205 if (*old!=NULL)
206 SysFreeString(*old);
209 * Allocate the new string
211 *old = SysAllocStringLen(in, len);
213 return 1;
216 /******************************************************************************
217 * SysFreeString16 [OLE2DISP.6]
219 void WINAPI SysFreeString16(BSTR16 in)
221 BSTR_Free(in);
224 /******************************************************************************
225 * SysFreeString32 [OLEAUT32.6]
227 void WINAPI SysFreeString(BSTR in)
229 DWORD* bufferPointer;
232 * We have to be careful when we free a BSTR pointer, it points to
233 * the beginning of the string but it skips the byte count contained
234 * before the string.
236 bufferPointer = (DWORD*)in;
238 bufferPointer--;
241 * Free the memory from it's "real" origin.
243 HeapFree(GetProcessHeap(), 0, bufferPointer);
246 /******************************************************************************
247 * SysStringLen16 [OLE2DISP.7]
249 int WINAPI SysStringLen16(BSTR16 str)
251 return strlen(BSTR_GetAddr(str));
254 /******************************************************************************
255 * SysStringLen32 [OLEAUT32.7]
257 * The Windows documentation states that the length returned by this function
258 * is not necessarely the same as the length returned by the _lstrlenW method.
259 * It is the same number that was passed in as the "len" parameter if the
260 * string was allocated with a SysAllocStringLen method call.
262 int WINAPI SysStringLen(BSTR str)
264 DWORD* bufferPointer;
267 * The length of the string (in bytes) is contained in a DWORD placed
268 * just before the BSTR pointer
270 bufferPointer = (DWORD*)str;
272 bufferPointer--;
274 return (int)(*bufferPointer/sizeof(WCHAR));
277 /******************************************************************************
278 * SysStringByteLen [OLEAUT32.149]
280 * The Windows documentation states that the length returned by this function
281 * is not necessarely the same as the length returned by the _lstrlenW method.
282 * It is the same number that was passed in as the "len" parameter if the
283 * string was allocated with a SysAllocStringLen method call.
285 int WINAPI SysStringByteLen(BSTR str)
287 return SysStringLen(str)*sizeof(WCHAR);
290 /******************************************************************************
291 * CreateDispTypeInfo [OLE2DISP.31]
293 HRESULT CreateDispTypeInfo16(
294 INTERFACEDATA *pidata,
295 LCID lcid,
296 ITypeInfo **pptinfo
298 FIXME(ole,"(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
299 return 0;
302 /******************************************************************************
303 * RegisterActiveObject [OLE2DISP.35]
305 HRESULT RegisterActiveObject16(
306 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
308 char buf[80];
309 WINE_StringFromCLSID(rclsid,buf);
310 FIXME(ole,"(%p,%s,0x%08lx,%p):stub\n",punk,buf,dwFlags,pdwRegister);
311 return 0;
314 /******************************************************************************
315 * OleTranslateColor [OLEAUT32.421]
317 INT WINAPI OleTranslateColor(
318 LONG clr,
319 HPALETTE hpal,
320 COLORREF* pColorRef)
322 FIXME(ole,"():stub\n");
324 *pColorRef = clr;
326 return S_OK;
329 /******************************************************************************
330 * SysAllocStringByteLen [OLEAUT32.150]
333 BSTR WINAPI SysAllocStringByteLen(char *in, int len)
335 DWORD* newBuffer;
336 char* stringBuffer;
339 * Allocate a new buffer to hold the string.
340 * dont't forget to keep an empty spot at the begining of the
341 * buffer for the character count and an extra character at the
342 * end for the NULL.
344 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
346 len + sizeof(WCHAR) + sizeof(DWORD));
349 * If the memory allocation failed, return a null pointer.
351 if (newBuffer==0)
352 return 0;
355 * Copy the length of the string in the placeholder.
357 *newBuffer = len;
360 * Skip the byte count.
362 newBuffer++;
365 * Copy the information in the buffer.
366 * Since it is valid to pass a NULL pointer here, we'll initialize the
367 * buffer to nul if it is the case.
369 if (in != 0)
370 memcpy(newBuffer, in, len);
373 * Make sure that there is a nul character at the end of the
374 * string.
376 stringBuffer = (char *)newBuffer;
377 stringBuffer[len] = 0;
378 stringBuffer[len+1] = 0;
380 return (LPWSTR)stringBuffer;