Only test $(LIB_TARGET) = libwine.so.1.0 if $(LIB_TARGET) is
[wine/wine-kai.git] / ole / ole2disp.c
blobdee4935731fd3540df9dae1a7ab6644b466aa92c
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;
233 /* NULL is a valid parameter */
234 if(!in) return;
237 * We have to be careful when we free a BSTR pointer, it points to
238 * the beginning of the string but it skips the byte count contained
239 * before the string.
241 bufferPointer = (DWORD*)in;
243 bufferPointer--;
246 * Free the memory from it's "real" origin.
248 HeapFree(GetProcessHeap(), 0, bufferPointer);
251 /******************************************************************************
252 * SysStringLen16 [OLE2DISP.7]
254 int WINAPI SysStringLen16(BSTR16 str)
256 return strlen(BSTR_GetAddr(str));
259 /******************************************************************************
260 * SysStringLen32 [OLEAUT32.7]
262 * The Windows documentation states that the length returned by this function
263 * is not necessarely the same as the length returned by the _lstrlenW method.
264 * It is the same number that was passed in as the "len" parameter if the
265 * string was allocated with a SysAllocStringLen method call.
267 int WINAPI SysStringLen(BSTR str)
269 DWORD* bufferPointer;
272 * The length of the string (in bytes) is contained in a DWORD placed
273 * just before the BSTR pointer
275 bufferPointer = (DWORD*)str;
277 bufferPointer--;
279 return (int)(*bufferPointer/sizeof(WCHAR));
282 /******************************************************************************
283 * SysStringByteLen [OLEAUT32.149]
285 * The Windows documentation states that the length returned by this function
286 * is not necessarely the same as the length returned by the _lstrlenW method.
287 * It is the same number that was passed in as the "len" parameter if the
288 * string was allocated with a SysAllocStringLen method call.
290 int WINAPI SysStringByteLen(BSTR str)
292 return SysStringLen(str)*sizeof(WCHAR);
295 /******************************************************************************
296 * CreateDispTypeInfo [OLE2DISP.31]
298 HRESULT WINAPI CreateDispTypeInfo16(
299 INTERFACEDATA *pidata,
300 LCID lcid,
301 ITypeInfo **pptinfo
303 FIXME(ole,"(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
304 return 0;
307 /******************************************************************************
308 * RegisterActiveObject [OLE2DISP.35]
310 HRESULT WINAPI RegisterActiveObject16(
311 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
313 char buf[80];
314 WINE_StringFromCLSID(rclsid,buf);
315 FIXME(ole,"(%p,%s,0x%08lx,%p):stub\n",punk,buf,dwFlags,pdwRegister);
316 return 0;
319 /******************************************************************************
320 * OleTranslateColor [OLEAUT32.421]
322 * Converts an OLE_COLOR to a COLORREF.
323 * See the documentation for conversion rules.
324 * pColorRef can be NULL. In that case the user only wants to test the
325 * conversion.
327 INT WINAPI OleTranslateColor(
328 LONG clr,
329 HPALETTE hpal,
330 COLORREF* pColorRef)
332 COLORREF colorref;
333 BYTE b = HIBYTE(HIWORD(clr));
335 TRACE(ole,"(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
338 * In case pColorRef is NULL, provide our own to simplify the code.
340 if (pColorRef == NULL)
341 pColorRef = &colorref;
343 switch (b)
345 case 0x00:
347 if (hpal != 0)
348 *pColorRef = PALETTERGB(GetRValue(clr),
349 GetGValue(clr),
350 GetBValue(clr));
351 else
352 *pColorRef = clr;
354 break;
357 case 0x01:
359 if (hpal != 0)
361 PALETTEENTRY pe;
363 * Validate the palette index.
365 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
366 return E_INVALIDARG;
369 *pColorRef = clr;
371 break;
374 case 0x02:
375 *pColorRef = clr;
376 break;
378 case 0x80:
380 int index = LOBYTE(LOWORD(clr));
383 * Validate GetSysColor index.
385 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
386 return E_INVALIDARG;
388 *pColorRef = GetSysColor(index);
390 break;
393 default:
394 return E_INVALIDARG;
397 return S_OK;
400 /******************************************************************************
401 * SysAllocStringByteLen [OLEAUT32.150]
404 BSTR WINAPI SysAllocStringByteLen(char *in, int len)
406 DWORD* newBuffer;
407 char* stringBuffer;
410 * Allocate a new buffer to hold the string.
411 * dont't forget to keep an empty spot at the begining of the
412 * buffer for the character count and an extra character at the
413 * end for the NULL.
415 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
417 len + sizeof(WCHAR) + sizeof(DWORD));
420 * If the memory allocation failed, return a null pointer.
422 if (newBuffer==0)
423 return 0;
426 * Copy the length of the string in the placeholder.
428 *newBuffer = len;
431 * Skip the byte count.
433 newBuffer++;
436 * Copy the information in the buffer.
437 * Since it is valid to pass a NULL pointer here, we'll initialize the
438 * buffer to nul if it is the case.
440 if (in != 0)
441 memcpy(newBuffer, in, len);
444 * Make sure that there is a nul character at the end of the
445 * string.
447 stringBuffer = (char *)newBuffer;
448 stringBuffer[len] = 0;
449 stringBuffer[len+1] = 0;
451 return (LPWSTR)stringBuffer;