enhmetafile added
[wine/multimedia.git] / ole / ole2disp.c
blob86329808e07a612b7998bd2d7429ccca1fa4a241
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 DEFAULT_DEBUG_CHANNEL(ole)
18 /* This implementation of the BSTR API is 16-bit only. It
19 represents BSTR as a 16:16 far pointer, and the strings
20 as ISO-8859 */
22 /******************************************************************************
23 * BSTR_AllocBytes [Internal]
25 static BSTR16 BSTR_AllocBytes(int n)
27 void *ptr = SEGPTR_ALLOC(n);
28 return (BSTR16)SEGPTR_GET(ptr);
31 /******************************************************************************
32 * BSTR_Free [INTERNAL]
34 static void BSTR_Free(BSTR16 in)
36 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
39 /******************************************************************************
40 * BSTR_GetAddr [INTERNAL]
42 static void* BSTR_GetAddr(BSTR16 in)
44 return in ? PTR_SEG_TO_LIN(in) : 0;
47 /******************************************************************************
48 * SysAllocString16 [OLE2DISP.2]
50 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
52 BSTR16 out=BSTR_AllocBytes(strlen(in)+1);
53 if(!out)return 0;
54 strcpy(BSTR_GetAddr(out),in);
55 return out;
58 /******************************************************************************
59 * SysAllocString32 [OLEAUT32.2]
61 BSTR WINAPI SysAllocString(LPCOLESTR in)
63 /* Delegate this to the SysAllocStringLen32 method. */
64 return SysAllocStringLen(in, lstrlenW(in));
67 /******************************************************************************
68 * SysReAllocString16 [OLE2DISP.3]
70 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
72 BSTR16 new=SysAllocString16(in);
73 BSTR_Free(*old);
74 *old=new;
75 return 1;
78 /******************************************************************************
79 * SysReAllocString32 [OLEAUT32.3]
81 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
84 * Sanity check
86 if (old==NULL)
87 return 0;
90 * Make sure we free the old string.
92 if (*old!=NULL)
93 SysFreeString(*old);
96 * Allocate the new string
98 *old = SysAllocString(in);
100 return 1;
103 /******************************************************************************
104 * SysAllocStringLen16 [OLE2DISP.4]
106 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
108 BSTR16 out=BSTR_AllocBytes(len+1);
109 if(!out)return 0;
110 strcpy(BSTR_GetAddr(out),in);
111 return out;
114 /******************************************************************************
115 * SysAllocStringLen32 [OLEAUT32.4]
117 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
118 * section, he describes the DWORD value placed before the BSTR data type.
119 * he describes it as a "DWORD count of characters". By experimenting with
120 * a windows application, this count seems to be a DWORD count of bytes in
121 * the string. Meaning that the count is double the number of wide
122 * characters in the string.
124 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
126 DWORD bufferSize;
127 DWORD* newBuffer;
128 WCHAR* stringBuffer;
131 * Find the lenth of the buffer passed-in in bytes.
133 bufferSize = len * sizeof (WCHAR);
136 * Allocate a new buffer to hold the string.
137 * dont't forget to keep an empty spot at the begining of the
138 * buffer for the character count and an extra character at the
139 * end for the NULL.
141 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
143 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
146 * If the memory allocation failed, return a null pointer.
148 if (newBuffer==0)
149 return 0;
152 * Copy the length of the string in the placeholder.
154 *newBuffer = bufferSize;
157 * Skip the byte count.
159 newBuffer++;
162 * Copy the information in the buffer.
163 * Since it is valid to pass a NULL pointer here, we'll initialize the
164 * buffer to nul if it is the case.
166 if (in != 0)
167 memcpy(newBuffer, in, bufferSize);
168 else
169 memset(newBuffer, 0, bufferSize);
172 * Make sure that there is a nul character at the end of the
173 * string.
175 stringBuffer = (WCHAR*)newBuffer;
176 stringBuffer[len] = L'\0';
178 return (LPWSTR)stringBuffer;
181 /******************************************************************************
182 * SysReAllocStringLen16 [OLE2DISP.5]
184 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
186 BSTR16 new=SysAllocStringLen16(in,len);
187 BSTR_Free(*old);
188 *old=new;
189 return 1;
193 /******************************************************************************
194 * SysReAllocStringLen32 [OLEAUT32.5]
196 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
199 * Sanity check
201 if (old==NULL)
202 return 0;
205 * Make sure we free the old string.
207 if (*old!=NULL)
208 SysFreeString(*old);
211 * Allocate the new string
213 *old = SysAllocStringLen(in, len);
215 return 1;
218 /******************************************************************************
219 * SysFreeString16 [OLE2DISP.6]
221 void WINAPI SysFreeString16(BSTR16 in)
223 BSTR_Free(in);
226 /******************************************************************************
227 * SysFreeString32 [OLEAUT32.6]
229 void WINAPI SysFreeString(BSTR in)
231 DWORD* bufferPointer;
234 * We have to be careful when we free a BSTR pointer, it points to
235 * the beginning of the string but it skips the byte count contained
236 * before the string.
238 bufferPointer = (DWORD*)in;
240 bufferPointer--;
243 * Free the memory from it's "real" origin.
245 HeapFree(GetProcessHeap(), 0, bufferPointer);
248 /******************************************************************************
249 * SysStringLen16 [OLE2DISP.7]
251 int WINAPI SysStringLen16(BSTR16 str)
253 return strlen(BSTR_GetAddr(str));
256 /******************************************************************************
257 * SysStringLen32 [OLEAUT32.7]
259 * The Windows documentation states that the length returned by this function
260 * is not necessarely the same as the length returned by the _lstrlenW method.
261 * It is the same number that was passed in as the "len" parameter if the
262 * string was allocated with a SysAllocStringLen method call.
264 int WINAPI SysStringLen(BSTR str)
266 DWORD* bufferPointer;
269 * The length of the string (in bytes) is contained in a DWORD placed
270 * just before the BSTR pointer
272 bufferPointer = (DWORD*)str;
274 bufferPointer--;
276 return (int)(*bufferPointer/sizeof(WCHAR));
279 /******************************************************************************
280 * SysStringByteLen [OLEAUT32.149]
282 * The Windows documentation states that the length returned by this function
283 * is not necessarely the same as the length returned by the _lstrlenW method.
284 * It is the same number that was passed in as the "len" parameter if the
285 * string was allocated with a SysAllocStringLen method call.
287 int WINAPI SysStringByteLen(BSTR str)
289 return SysStringLen(str)*sizeof(WCHAR);
292 /******************************************************************************
293 * CreateDispTypeInfo [OLE2DISP.31]
295 HRESULT WINAPI CreateDispTypeInfo16(
296 INTERFACEDATA *pidata,
297 LCID lcid,
298 ITypeInfo **pptinfo
300 FIXME(ole,"(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
301 return 0;
304 /******************************************************************************
305 * RegisterActiveObject [OLE2DISP.35]
307 HRESULT WINAPI RegisterActiveObject16(
308 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
310 char buf[80];
311 WINE_StringFromCLSID(rclsid,buf);
312 FIXME(ole,"(%p,%s,0x%08lx,%p):stub\n",punk,buf,dwFlags,pdwRegister);
313 return 0;
316 /******************************************************************************
317 * OleTranslateColor [OLEAUT32.421]
319 INT WINAPI OleTranslateColor(
320 LONG clr,
321 HPALETTE hpal,
322 COLORREF* pColorRef)
324 FIXME(ole,"():stub\n");
326 *pColorRef = clr;
328 return S_OK;
331 /******************************************************************************
332 * SysAllocStringByteLen [OLEAUT32.150]
335 BSTR WINAPI SysAllocStringByteLen(char *in, int len)
337 DWORD* newBuffer;
338 char* stringBuffer;
341 * Allocate a new buffer to hold the string.
342 * dont't forget to keep an empty spot at the begining of the
343 * buffer for the character count and an extra character at the
344 * end for the NULL.
346 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
348 len + sizeof(WCHAR) + sizeof(DWORD));
351 * If the memory allocation failed, return a null pointer.
353 if (newBuffer==0)
354 return 0;
357 * Copy the length of the string in the placeholder.
359 *newBuffer = len;
362 * Skip the byte count.
364 newBuffer++;
367 * Copy the information in the buffer.
368 * Since it is valid to pass a NULL pointer here, we'll initialize the
369 * buffer to nul if it is the case.
371 if (in != 0)
372 memcpy(newBuffer, in, len);
375 * Make sure that there is a nul character at the end of the
376 * string.
378 stringBuffer = (char *)newBuffer;
379 stringBuffer[len] = 0;
380 stringBuffer[len+1] = 0;
382 return (LPWSTR)stringBuffer;