Emulates ins/outs correctly for DOS programs.
[wine/multimedia.git] / ole / ole2disp.c
blob2582b4409a6569699422a64f01cb75356b330bb6
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
5 */
7 #include <string.h>
8 #include "windows.h"
9 #include "ole.h"
10 #include "ole2.h"
11 #include "oleauto.h"
12 #include "interfaces.h"
13 #include "heap.h"
14 #include "ldt.h"
15 #include "debug.h"
17 /* This implementation of the BSTR API is 16-bit only. It
18 represents BSTR as a 16:16 far pointer, and the strings
19 as ISO-8859 */
21 /******************************************************************************
22 * BSTR_AllocBytes [Internal]
24 static BSTR16 BSTR_AllocBytes(int n)
26 void *ptr = SEGPTR_ALLOC(n);
27 return (BSTR16)SEGPTR_GET(ptr);
30 /******************************************************************************
31 * BSTR_Free [INTERNAL]
33 static void BSTR_Free(BSTR16 in)
35 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
38 /******************************************************************************
39 * BSTR_GetAddr [INTERNAL]
41 static void* BSTR_GetAddr(BSTR16 in)
43 return in ? PTR_SEG_TO_LIN(in) : 0;
46 /******************************************************************************
47 * SysAllocString16 [OLE2DISP.2]
49 BSTR16 WINAPI SysAllocString16(LPOLESTR16 in)
51 BSTR16 out=BSTR_AllocBytes(strlen(in)+1);
52 if(!out)return 0;
53 strcpy(BSTR_GetAddr(out),in);
54 return out;
57 /******************************************************************************
58 * SysAllocString32 [OLEAUT32.2]
60 BSTR32 WINAPI SysAllocString32(LPOLESTR32 in)
62 /* Delegate this to the SysAllocStringLen32 method. */
63 return SysAllocStringLen32(in, lstrlen32W(in));
66 /******************************************************************************
67 * SysReAllocString16 [OLE2DISP.3]
69 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPOLESTR16 in)
71 BSTR16 new=SysAllocString16(in);
72 BSTR_Free(*old);
73 *old=new;
74 return 1;
77 /******************************************************************************
78 * SysReAllocString32 [OLEAUT32.3]
80 INT32 WINAPI SysReAllocString32(LPBSTR32 old,LPOLESTR32 in)
83 * Sanity check
85 if (old==NULL)
86 return 0;
89 * Make sure we free the old string.
91 if (*old!=NULL)
92 SysFreeString32(*old);
95 * Allocate the new string
97 *old = SysAllocString32(in);
99 return 1;
102 /******************************************************************************
103 * SysAllocStringLen16 [OLE2DISP.4]
105 BSTR16 WINAPI SysAllocStringLen16(char *in, int len)
107 BSTR16 out=BSTR_AllocBytes(len+1);
108 if(!out)return 0;
109 strcpy(BSTR_GetAddr(out),in);
110 return out;
113 /******************************************************************************
114 * SysAllocStringLen32 [OLEAUT32.4]
116 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
117 * section, he describes the DWORD value placed before the BSTR data type.
118 * he describes it as a "DWORD count of characters". By experimenting with
119 * a windows application, this count seems to be a DWORD count of bytes in
120 * the string. Meaning that the count is double the number of wide
121 * characters in the string.
123 BSTR32 WINAPI SysAllocStringLen32(WCHAR *in, int len)
125 DWORD bufferSize;
126 DWORD* newBuffer;
127 WCHAR* stringBuffer;
130 * Find the lenth of the buffer passed-in in bytes.
132 bufferSize = len * sizeof (WCHAR);
135 * Allocate a new buffer to hold the string.
136 * dont't forget to keep an empty spot at the begining of the
137 * buffer for the character count and an extra character at the
138 * end for the NULL.
140 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
142 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
145 * If the memory allocation failed, return a null pointer.
147 if (newBuffer==0)
148 return 0;
151 * Copy the length of the string in the placeholder.
153 *newBuffer = bufferSize;
156 * Skip the byte count.
158 newBuffer++;
161 * Copy the information in the buffer.
162 * Since it is valid to pass a NULL pointer here, we'll initialize the
163 * buffer to nul if it is the case.
165 if (in != 0)
166 memcpy(newBuffer, in, bufferSize);
167 else
168 memset(newBuffer, 0, bufferSize);
171 * Make sure that there is a nul character at the end of the
172 * string.
174 stringBuffer = (WCHAR*)newBuffer;
175 stringBuffer[len] = L'\0';
177 return (LPWSTR)stringBuffer;
180 /******************************************************************************
181 * SysReAllocStringLen16 [OLE2DISP.5]
183 int WINAPI SysReAllocStringLen16(BSTR16 *old,char *in,int len)
185 BSTR16 new=SysAllocStringLen16(in,len);
186 BSTR_Free(*old);
187 *old=new;
188 return 1;
192 /******************************************************************************
193 * SysReAllocStringLen32 [OLEAUT32.5]
195 int WINAPI SysReAllocStringLen32(BSTR32* old, WCHAR* in, int len)
198 * Sanity check
200 if (old==NULL)
201 return 0;
204 * Make sure we free the old string.
206 if (*old!=NULL)
207 SysFreeString32(*old);
210 * Allocate the new string
212 *old = SysAllocStringLen32(in, len);
214 return 1;
217 /******************************************************************************
218 * SysFreeString16 [OLE2DISP.6]
220 void WINAPI SysFreeString16(BSTR16 in)
222 BSTR_Free(in);
225 /******************************************************************************
226 * SysFreeString32 [OLEAUT32.6]
228 void WINAPI SysFreeString32(BSTR32 in)
230 DWORD* bufferPointer;
233 * We have to be careful when we free a BSTR pointer, it points to
234 * the beginning of the string but it skips the byte count contained
235 * before the string.
237 bufferPointer = (DWORD*)in;
239 bufferPointer--;
242 * Free the memory from it's "real" origin.
244 HeapFree(GetProcessHeap(), 0, bufferPointer);
247 /******************************************************************************
248 * SysStringLen16 [OLE2DISP.7]
250 int WINAPI SysStringLen16(BSTR16 str)
252 return strlen(BSTR_GetAddr(str));
255 /******************************************************************************
256 * SysStringLen32 [OLEAUT32.7]
258 * The Windows documentation states that the length returned by this function
259 * is not necessarely the same as the length returned by the _lstrlenW method.
260 * It is the same number that was passed in as the "len" parameter if the
261 * string was allocated with a SysAllocStringLen method call.
263 int WINAPI SysStringLen32(BSTR32 str)
265 DWORD* bufferPointer;
268 * The length of the string (in bytes) is contained in a DWORD placed
269 * just before the BSTR pointer
271 bufferPointer = (DWORD*)str;
273 bufferPointer--;
275 return (int)(*bufferPointer/sizeof(WCHAR));
278 /******************************************************************************
279 * CreateDispTypeInfo [OLE2DISP.31]
281 OLESTATUS WINAPI CreateDispTypeInfo(
282 INTERFACEDATA *pidata,
283 LCID lcid,
284 LPVOID **pptinfo /*ITypeInfo*/
286 FIXME(ole,"(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
287 return 0;
290 /******************************************************************************
291 * RegisterActiveObject [OLE2DISP.35]
293 OLESTATUS WINAPI RegisterActiveObject(
294 IUnknown * punk,REFCLSID rclsid,DWORD dwFlags, DWORD * pdwRegister
296 char buf[80];
297 WINE_StringFromCLSID(rclsid,buf);
298 FIXME(ole,"(%p,%s,0x%08lx,%p):stub\n",punk,buf,dwFlags,pdwRegister);
299 return 0;