| 1 | /* $Id: ole2disp.cpp,v 1.3 2000-04-05 22:28:48 davidr Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | *      OLE2DISP library | 
|---|
| 4 | * | 
|---|
| 5 | * | 
|---|
| 6 | *      Copyright 1995  Martin von Loewis | 
|---|
| 7 | *      Copyright 1999 Sander van Leeuwen  (WINE OS/2 Port 990815) | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | #include "oleaut32.h" | 
|---|
| 14 | #include <string.h> | 
|---|
| 15 | #include "winerror.h" | 
|---|
| 16 | #include "ole2.h" | 
|---|
| 17 | #include "oleauto.h" | 
|---|
| 18 | #include "wine/obj_base.h" | 
|---|
| 19 | #include "heap.h" | 
|---|
| 20 | #include "ldt.h" | 
|---|
| 21 | #include "debugtools.h" | 
|---|
| 22 |  | 
|---|
| 23 | DEFAULT_DEBUG_CHANNEL(ole) | 
|---|
| 24 |  | 
|---|
| 25 | /****************************************************************************** | 
|---|
| 26 | *              SysAllocString32        [OLEAUT32.2] | 
|---|
| 27 | */ | 
|---|
| 28 | BSTR WINAPI SysAllocString(LPCOLESTR in) | 
|---|
| 29 | { | 
|---|
| 30 | /* Delegate this to the SysAllocStringLen32 method. */ | 
|---|
| 31 | return SysAllocStringLen(in, lstrlenW(in)); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | /****************************************************************************** | 
|---|
| 35 | *              SysReAllocString32      [OLEAUT32.3] | 
|---|
| 36 | */ | 
|---|
| 37 | INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in) | 
|---|
| 38 | { | 
|---|
| 39 | /* | 
|---|
| 40 | * Sanity check | 
|---|
| 41 | */ | 
|---|
| 42 | if (old==NULL) | 
|---|
| 43 | return 0; | 
|---|
| 44 |  | 
|---|
| 45 | /* | 
|---|
| 46 | * Make sure we free the old string. | 
|---|
| 47 | */ | 
|---|
| 48 | if (*old!=NULL) | 
|---|
| 49 | SysFreeString(*old); | 
|---|
| 50 |  | 
|---|
| 51 | /* | 
|---|
| 52 | * Allocate the new string | 
|---|
| 53 | */ | 
|---|
| 54 | *old = SysAllocString(in); | 
|---|
| 55 |  | 
|---|
| 56 | return 1; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | /****************************************************************************** | 
|---|
| 61 | *             SysAllocStringLen32     [OLEAUT32.4] | 
|---|
| 62 | * | 
|---|
| 63 | * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation | 
|---|
| 64 | * section, he describes the DWORD value placed before the BSTR data type. | 
|---|
| 65 | * he describes it as a "DWORD count of characters". By experimenting with | 
|---|
| 66 | * a windows application, this count seems to be a DWORD count of bytes in | 
|---|
| 67 | * the string. Meaning that the count is double the number of wide | 
|---|
| 68 | * characters in the string. | 
|---|
| 69 | */ | 
|---|
| 70 | BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len) | 
|---|
| 71 | { | 
|---|
| 72 | DWORD  bufferSize; | 
|---|
| 73 | DWORD* newBuffer; | 
|---|
| 74 | WCHAR* stringBuffer; | 
|---|
| 75 |  | 
|---|
| 76 | /* | 
|---|
| 77 | * Find the lenth of the buffer passed-in in bytes. | 
|---|
| 78 | */ | 
|---|
| 79 | bufferSize = len * sizeof (WCHAR); | 
|---|
| 80 |  | 
|---|
| 81 | /* | 
|---|
| 82 | * Allocate a new buffer to hold the string. | 
|---|
| 83 | * dont't forget to keep an empty spot at the begining of the | 
|---|
| 84 | * buffer for the character count and an extra character at the | 
|---|
| 85 | * end for the NULL. | 
|---|
| 86 | */ | 
|---|
| 87 | newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(), | 
|---|
| 88 | 0, | 
|---|
| 89 | bufferSize + sizeof(WCHAR) + sizeof(DWORD)); | 
|---|
| 90 |  | 
|---|
| 91 | /* | 
|---|
| 92 | * If the memory allocation failed, return a null pointer. | 
|---|
| 93 | */ | 
|---|
| 94 | if (newBuffer==0) | 
|---|
| 95 | return 0; | 
|---|
| 96 |  | 
|---|
| 97 | /* | 
|---|
| 98 | * Copy the length of the string in the placeholder. | 
|---|
| 99 | */ | 
|---|
| 100 | *newBuffer = bufferSize; | 
|---|
| 101 |  | 
|---|
| 102 | /* | 
|---|
| 103 | * Skip the byte count. | 
|---|
| 104 | */ | 
|---|
| 105 | newBuffer++; | 
|---|
| 106 |  | 
|---|
| 107 | /* | 
|---|
| 108 | * Copy the information in the buffer. | 
|---|
| 109 | * Since it is valid to pass a NULL pointer here, we'll initialize the | 
|---|
| 110 | * buffer to nul if it is the case. | 
|---|
| 111 | */ | 
|---|
| 112 | if (in != 0) | 
|---|
| 113 | memcpy(newBuffer, in, bufferSize); | 
|---|
| 114 | else | 
|---|
| 115 | memset(newBuffer, 0, bufferSize); | 
|---|
| 116 |  | 
|---|
| 117 | /* | 
|---|
| 118 | * Make sure that there is a nul character at the end of the | 
|---|
| 119 | * string. | 
|---|
| 120 | */ | 
|---|
| 121 | stringBuffer = (WCHAR*)newBuffer; | 
|---|
| 122 | stringBuffer[len] = L'\0'; | 
|---|
| 123 |  | 
|---|
| 124 | return (LPWSTR)stringBuffer; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | /****************************************************************************** | 
|---|
| 129 | *             SysReAllocStringLen32   [OLEAUT32.5] | 
|---|
| 130 | */ | 
|---|
| 131 | int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len) | 
|---|
| 132 | { | 
|---|
| 133 | /* | 
|---|
| 134 | * Sanity check | 
|---|
| 135 | */ | 
|---|
| 136 | if (old==NULL) | 
|---|
| 137 | return 0; | 
|---|
| 138 |  | 
|---|
| 139 | /* | 
|---|
| 140 | * Make sure we free the old string. | 
|---|
| 141 | */ | 
|---|
| 142 | if (*old!=NULL) | 
|---|
| 143 | SysFreeString(*old); | 
|---|
| 144 |  | 
|---|
| 145 | /* | 
|---|
| 146 | * Allocate the new string | 
|---|
| 147 | */ | 
|---|
| 148 | *old = SysAllocStringLen(in, len); | 
|---|
| 149 |  | 
|---|
| 150 | return 1; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | /****************************************************************************** | 
|---|
| 154 | *              SysFreeString32 [OLEAUT32.6] | 
|---|
| 155 | */ | 
|---|
| 156 | void WINAPI SysFreeString(BSTR in) | 
|---|
| 157 | { | 
|---|
| 158 | DWORD* bufferPointer; | 
|---|
| 159 |  | 
|---|
| 160 | /* NULL is a valid parameter */ | 
|---|
| 161 | if(!in) return; | 
|---|
| 162 |  | 
|---|
| 163 | /* | 
|---|
| 164 | * We have to be careful when we free a BSTR pointer, it points to | 
|---|
| 165 | * the beginning of the string but it skips the byte count contained | 
|---|
| 166 | * before the string. | 
|---|
| 167 | */ | 
|---|
| 168 | bufferPointer = (DWORD*)in; | 
|---|
| 169 |  | 
|---|
| 170 | bufferPointer--; | 
|---|
| 171 |  | 
|---|
| 172 | /* | 
|---|
| 173 | * Free the memory from it's "real" origin. | 
|---|
| 174 | */ | 
|---|
| 175 | HeapFree(GetProcessHeap(), 0, bufferPointer); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | /****************************************************************************** | 
|---|
| 179 | *             SysStringLen32  [OLEAUT32.7] | 
|---|
| 180 | * | 
|---|
| 181 | * The Windows documentation states that the length returned by this function | 
|---|
| 182 | * is not necessarely the same as the length returned by the _lstrlenW method. | 
|---|
| 183 | * It is the same number that was passed in as the "len" parameter if the | 
|---|
| 184 | * string was allocated with a SysAllocStringLen method call. | 
|---|
| 185 | */ | 
|---|
| 186 | int WINAPI SysStringLen(BSTR str) | 
|---|
| 187 | { | 
|---|
| 188 | DWORD* bufferPointer; | 
|---|
| 189 |  | 
|---|
| 190 | /* | 
|---|
| 191 | * The length of the string (in bytes) is contained in a DWORD placed | 
|---|
| 192 | * just before the BSTR pointer | 
|---|
| 193 | */ | 
|---|
| 194 | bufferPointer = (DWORD*)str; | 
|---|
| 195 |  | 
|---|
| 196 | bufferPointer--; | 
|---|
| 197 |  | 
|---|
| 198 | return (int)(*bufferPointer/sizeof(WCHAR)); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /****************************************************************************** | 
|---|
| 202 | *             SysStringByteLen  [OLEAUT32.149] | 
|---|
| 203 | * | 
|---|
| 204 | * The Windows documentation states that the length returned by this function | 
|---|
| 205 | * is not necessarely the same as the length returned by the _lstrlenW method. | 
|---|
| 206 | * It is the same number that was passed in as the "len" parameter if the | 
|---|
| 207 | * string was allocated with a SysAllocStringLen method call. | 
|---|
| 208 | */ | 
|---|
| 209 | int WINAPI SysStringByteLen(BSTR str) | 
|---|
| 210 | { | 
|---|
| 211 | return SysStringLen(str)*sizeof(WCHAR); | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | /****************************************************************************** | 
|---|
| 215 | *              OleTranslateColor       [OLEAUT32.421] | 
|---|
| 216 | * | 
|---|
| 217 | * Converts an OLE_COLOR to a COLORREF. | 
|---|
| 218 | * See the documentation for conversion rules. | 
|---|
| 219 | * pColorRef can be NULL. In that case the user only wants to test the | 
|---|
| 220 | * conversion. | 
|---|
| 221 | */ | 
|---|
| 222 | INT WINAPI OleTranslateColor( | 
|---|
| 223 | LONG clr, | 
|---|
| 224 | HPALETTE  hpal, | 
|---|
| 225 | COLORREF* pColorRef) | 
|---|
| 226 | { | 
|---|
| 227 | COLORREF colorref; | 
|---|
| 228 | BYTE b = HIBYTE(HIWORD(clr)); | 
|---|
| 229 |  | 
|---|
| 230 | TRACE("OleTranslateColor(%08lx, %d, %p)\n", clr, hpal, pColorRef); | 
|---|
| 231 |  | 
|---|
| 232 | /* | 
|---|
| 233 | * In case pColorRef is NULL, provide our own to simplify the code. | 
|---|
| 234 | */ | 
|---|
| 235 | if (pColorRef == NULL) | 
|---|
| 236 | pColorRef = &colorref; | 
|---|
| 237 |  | 
|---|
| 238 | switch (b) | 
|---|
| 239 | { | 
|---|
| 240 | case 0x00: | 
|---|
| 241 | { | 
|---|
| 242 | if (hpal != 0) | 
|---|
| 243 | *pColorRef =  PALETTERGB(GetRValue(clr), | 
|---|
| 244 | GetGValue(clr), | 
|---|
| 245 | GetBValue(clr)); | 
|---|
| 246 | else | 
|---|
| 247 | *pColorRef = clr; | 
|---|
| 248 |  | 
|---|
| 249 | break; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | case 0x01: | 
|---|
| 253 | { | 
|---|
| 254 | if (hpal != 0) | 
|---|
| 255 | { | 
|---|
| 256 | PALETTEENTRY pe; | 
|---|
| 257 | /* | 
|---|
| 258 | * Validate the palette index. | 
|---|
| 259 | */ | 
|---|
| 260 | if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0) | 
|---|
| 261 | return E_INVALIDARG; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | *pColorRef = clr; | 
|---|
| 265 |  | 
|---|
| 266 | break; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | case 0x02: | 
|---|
| 270 | *pColorRef = clr; | 
|---|
| 271 | break; | 
|---|
| 272 |  | 
|---|
| 273 | case 0x80: | 
|---|
| 274 | { | 
|---|
| 275 | int index = LOBYTE(LOWORD(clr)); | 
|---|
| 276 |  | 
|---|
| 277 | /* | 
|---|
| 278 | * Validate GetSysColor index. | 
|---|
| 279 | */ | 
|---|
| 280 | if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION)) | 
|---|
| 281 | return E_INVALIDARG; | 
|---|
| 282 |  | 
|---|
| 283 | *pColorRef =  GetSysColor(index); | 
|---|
| 284 |  | 
|---|
| 285 | break; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | default: | 
|---|
| 289 | return E_INVALIDARG; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | return S_OK; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | /****************************************************************************** | 
|---|
| 296 | *             SysAllocStringByteLen     [OLEAUT32.150] | 
|---|
| 297 | * | 
|---|
| 298 | */ | 
|---|
| 299 | BSTR WINAPI SysAllocStringByteLen(char *in, int len) | 
|---|
| 300 | { | 
|---|
| 301 | DWORD* newBuffer; | 
|---|
| 302 | char* stringBuffer; | 
|---|
| 303 |  | 
|---|
| 304 | /* | 
|---|
| 305 | * Allocate a new buffer to hold the string. | 
|---|
| 306 | * dont't forget to keep an empty spot at the begining of the | 
|---|
| 307 | * buffer for the character count and an extra character at the | 
|---|
| 308 | * end for the NULL. | 
|---|
| 309 | */ | 
|---|
| 310 | newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(), | 
|---|
| 311 | 0, | 
|---|
| 312 | len + sizeof(WCHAR) + sizeof(DWORD)); | 
|---|
| 313 |  | 
|---|
| 314 | /* | 
|---|
| 315 | * If the memory allocation failed, return a null pointer. | 
|---|
| 316 | */ | 
|---|
| 317 | if (newBuffer==0) | 
|---|
| 318 | return 0; | 
|---|
| 319 |  | 
|---|
| 320 | /* | 
|---|
| 321 | * Copy the length of the string in the placeholder. | 
|---|
| 322 | */ | 
|---|
| 323 | *newBuffer = len; | 
|---|
| 324 |  | 
|---|
| 325 | /* | 
|---|
| 326 | * Skip the byte count. | 
|---|
| 327 | */ | 
|---|
| 328 | newBuffer++; | 
|---|
| 329 |  | 
|---|
| 330 | /* | 
|---|
| 331 | * Copy the information in the buffer. | 
|---|
| 332 | * Since it is valid to pass a NULL pointer here, we'll initialize the | 
|---|
| 333 | * buffer to nul if it is the case. | 
|---|
| 334 | */ | 
|---|
| 335 | if (in != 0) | 
|---|
| 336 | memcpy(newBuffer, in, len); | 
|---|
| 337 |  | 
|---|
| 338 | /* | 
|---|
| 339 | * Make sure that there is a nul character at the end of the | 
|---|
| 340 | * string. | 
|---|
| 341 | */ | 
|---|
| 342 | stringBuffer = (char *)newBuffer; | 
|---|
| 343 | stringBuffer[len] = 0; | 
|---|
| 344 | stringBuffer[len+1] = 0; | 
|---|
| 345 |  | 
|---|
| 346 | return (LPWSTR)stringBuffer; | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 |  | 
|---|