Synchronize with trunk r58606.
[reactos.git] / dll / win32 / oleaut32 / typelib2.c
1 /*
2 * TYPELIB2
3 *
4 * Copyright 2004 Alastair Bridgewater
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * --------------------------------------------------------------------------------------
21 * Known problems:
22 *
23 * Badly incomplete.
24 *
25 * Only works on little-endian systems.
26 *
27 */
28
29 #define WIN32_NO_STATUS
30 #define _INC_WINDOWS
31
32 #include <config.h>
33 //#include "wine/port.h"
34
35 //#include <stdlib.h>
36 //#include <string.h>
37 //#include <stdarg.h>
38 //#include <stdio.h>
39 //#include <ctype.h>
40
41 #define COBJMACROS
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44
45 //#include "winerror.h"
46 //#include "windef.h"
47 //#include "winbase.h"
48 //#include "winnls.h"
49 //#include "winreg.h"
50 //#include "winuser.h"
51
52 #include <wine/unicode.h>
53 #include <objbase.h>
54 #include "typelib.h"
55 #include <wine/debug.h>
56
57 WINE_DEFAULT_DEBUG_CHANNEL(typelib2);
58 /* WINE_DEFAULT_DEBUG_CHANNEL(ole); */
59
60
61 /******************************************************************************
62 * ICreateTypeLib2 {OLEAUT32}
63 *
64 * NOTES
65 * The ICreateTypeLib2 interface provides an interface whereby one may create
66 * new type library (.tlb) files.
67 *
68 * This interface inherits from ICreateTypeLib, and can be freely cast back
69 * and forth between an ICreateTypeLib and an ICreateTypeLib2 on local clients.
70 * This dispensation applies only to ICreateTypeLib objects obtained on MSFT
71 * format type libraries (those made through CreateTypeLib2).
72 *
73 * METHODS
74 */
75
76 /******************************************************************************
77 * ICreateTypeInfo2 {OLEAUT32}
78 *
79 * NOTES
80 * The ICreateTypeInfo2 interface provides an interface whereby one may add
81 * type information to type library (.tlb) files.
82 *
83 * This interface inherits from ICreateTypeInfo, and can be freely cast back
84 * and forth between an ICreateTypeInfo and an ICreateTypeInfo2 on local clients.
85 * This dispensation applies only to ICreateTypeInfo objects obtained on MSFT
86 * format type libraries (those made through CreateTypeLib2).
87 *
88 * METHODS
89 */
90
91 /******************************************************************************
92 * ITypeLib2 {OLEAUT32}
93 *
94 * NOTES
95 * The ITypeLib2 interface provides an interface whereby one may query MSFT
96 * format type library (.tlb) files.
97 *
98 * This interface inherits from ITypeLib, and can be freely cast back and
99 * forth between an ITypeLib and an ITypeLib2 on local clients. This
100 * dispensation applies only to ITypeLib objects obtained on MSFT format type
101 * libraries (those made through CreateTypeLib2).
102 *
103 * METHODS
104 */
105
106 /******************************************************************************
107 * ITypeInfo2 {OLEAUT32}
108 *
109 * NOTES
110 * The ITypeInfo2 interface provides an interface whereby one may query type
111 * information stored in MSFT format type library (.tlb) files.
112 *
113 * This interface inherits from ITypeInfo, and can be freely cast back and
114 * forth between an ITypeInfo and an ITypeInfo2 on local clients. This
115 * dispensation applies only to ITypeInfo objects obtained on MSFT format type
116 * libraries (those made through CreateTypeLib2).
117 *
118 * METHODS
119 */
120
121 /*================== Implementation Structures ===================================*/
122
123 /* Used for storing cyclic list. Tail address is kept */
124 typedef enum tagCyclicListElementType {
125 CyclicListSentinel,
126 CyclicListFunc,
127 CyclicListVar
128 } CyclicListElementType;
129 typedef struct tagCyclicList {
130 struct tagCyclicList *next;
131 int indice;
132 int name;
133 CyclicListElementType type;
134
135 union {
136 int val;
137 int *data;
138 }u;
139 } CyclicList;
140
141 enum MSFT_segment_index {
142 MSFT_SEG_TYPEINFO = 0, /* type information */
143 MSFT_SEG_IMPORTINFO, /* import information */
144 MSFT_SEG_IMPORTFILES, /* import filenames */
145 MSFT_SEG_REFERENCES, /* references (?) */
146 MSFT_SEG_GUIDHASH, /* hash table for guids? */
147 MSFT_SEG_GUID, /* guid storage */
148 MSFT_SEG_NAMEHASH, /* hash table for names */
149 MSFT_SEG_NAME, /* name storage */
150 MSFT_SEG_STRING, /* string storage */
151 MSFT_SEG_TYPEDESC, /* type descriptions */
152 MSFT_SEG_ARRAYDESC, /* array descriptions */
153 MSFT_SEG_CUSTDATA, /* custom data */
154 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
155 MSFT_SEG_UNKNOWN, /* ??? */
156 MSFT_SEG_UNKNOWN2, /* ??? */
157 MSFT_SEG_MAX /* total number of segments */
158 };
159
160 typedef struct tagMSFT_ImpFile {
161 int guid;
162 LCID lcid;
163 int version;
164 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
165 } MSFT_ImpFile;
166
167 typedef struct tagICreateTypeLib2Impl
168 {
169 ICreateTypeLib2 ICreateTypeLib2_iface;
170 ITypeLib2 ITypeLib2_iface;
171 LONG ref;
172
173 BSTR filename;
174
175 MSFT_Header typelib_header;
176 INT helpStringDll;
177 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
178 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
179 int typelib_segment_block_length[MSFT_SEG_MAX];
180
181 int typelib_guids; /* Number of defined typelib guids */
182 int typeinfo_guids; /* Number of defined typeinfo guids */
183
184 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
185
186 INT *typelib_namehash_segment;
187 INT *typelib_guidhash_segment;
188
189 struct tagICreateTypeInfo2Impl *typeinfos;
190 struct tagICreateTypeInfo2Impl *last_typeinfo;
191 } ICreateTypeLib2Impl;
192
193 static inline ICreateTypeLib2Impl *impl_from_ICreateTypeLib2(ICreateTypeLib2 *iface)
194 {
195 return CONTAINING_RECORD(iface, ICreateTypeLib2Impl, ICreateTypeLib2_iface);
196 }
197
198 static inline ICreateTypeLib2Impl *impl_from_ITypeLib2(ITypeLib2 *iface)
199 {
200 return CONTAINING_RECORD(iface, ICreateTypeLib2Impl, ITypeLib2_iface);
201 }
202
203 typedef struct tagICreateTypeInfo2Impl
204 {
205 ICreateTypeInfo2 ICreateTypeInfo2_iface;
206 ITypeInfo2 ITypeInfo2_iface;
207
208 LONG ref;
209
210 ICreateTypeLib2Impl *typelib;
211 MSFT_TypeInfoBase *typeinfo;
212
213 struct tagCyclicList *typedata; /* tail of cyclic list */
214
215 TYPEKIND typekind;
216 int datawidth;
217
218 struct tagICreateTypeInfo2Impl *next_typeinfo;
219 struct tagICreateTypeInfo2Impl *dual;
220 } ICreateTypeInfo2Impl;
221
222 static inline ICreateTypeInfo2Impl *impl_from_ICreateTypeInfo2(ICreateTypeInfo2 *iface)
223 {
224 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ICreateTypeInfo2_iface);
225 }
226
227 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
228 {
229 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ITypeInfo2_iface);
230 }
231
232 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
233
234 static CyclicList *alloc_cyclic_list_item(CyclicListElementType type)
235 {
236 CyclicList *ret = heap_alloc_zero(sizeof(CyclicList));
237 if (!ret)
238 return NULL;
239 ret->type = type;
240 return ret;
241 }
242
243 /*================== Internal functions ===================================*/
244
245 static inline UINT cti2_get_var_count(const MSFT_TypeInfoBase *typeinfo)
246 {
247 return typeinfo->cElement >> 16;
248 }
249
250 static inline UINT cti2_get_func_count(const MSFT_TypeInfoBase *typeinfo)
251 {
252 return typeinfo->cElement & 0xFFFF;
253 }
254
255 static inline INT ctl2_get_record_size(const CyclicList *iter)
256 {
257 return iter->u.data[0] & 0xFFFF;
258 }
259
260 static void ctl2_update_var_size(const ICreateTypeInfo2Impl *This, CyclicList *var, int size)
261 {
262 int old = ctl2_get_record_size(var), i;
263
264 if (old >= size) return;
265
266 /* initialize fields included in size but currently unused */
267 for (i = old/sizeof(int); i < (size/sizeof(int) - 1); i++)
268 {
269 /* HelpContext/HelpStringContext being 0 means it's not set */
270 var->u.data[i] = (i == 5 || i == 9) ? 0 : -1;
271 }
272
273 var->u.data[0] += size - old;
274 This->typedata->next->u.val += size - old;
275 }
276
277 /* NOTE: entry always assumed to be a function */
278 static inline INVOKEKIND ctl2_get_invokekind(const CyclicList *func)
279 {
280 /* INVOKEKIND uses bit flags up to 8 */
281 return (func->u.data[4] >> 3) & 0xF;
282 }
283
284 static inline SYSKIND ctl2_get_syskind(const ICreateTypeLib2Impl *This)
285 {
286 return This->typelib_header.varflags & 0xF;
287 }
288
289 /****************************************************************************
290 * ctl2_init_header
291 *
292 * Initializes the type library header of a new typelib.
293 */
294 static void ctl2_init_header(
295 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
296 {
297 This->typelib_header.magic1 = MSFT_SIGNATURE;
298 This->typelib_header.magic2 = 0x00010002;
299 This->typelib_header.posguid = -1;
300 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
301 This->typelib_header.varflags = 0x40;
302 This->typelib_header.version = 0;
303 This->typelib_header.flags = 0;
304 This->typelib_header.nrtypeinfos = 0;
305 This->typelib_header.helpstring = -1;
306 This->typelib_header.helpstringcontext = 0;
307 This->typelib_header.helpcontext = 0;
308 This->typelib_header.nametablecount = 0;
309 This->typelib_header.nametablechars = 0;
310 This->typelib_header.NameOffset = -1;
311 This->typelib_header.helpfile = -1;
312 This->typelib_header.CustomDataOffset = -1;
313 This->typelib_header.res44 = 0x20;
314 This->typelib_header.res48 = 0x80;
315 This->typelib_header.dispatchpos = -1;
316 This->typelib_header.nimpinfos = 0;
317 This->helpStringDll = -1;
318 }
319
320 /****************************************************************************
321 * ctl2_init_segdir
322 *
323 * Initializes the segment directory of a new typelib.
324 */
325 static void ctl2_init_segdir(
326 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
327 {
328 int i;
329 MSFT_pSeg *segdir;
330
331 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
332
333 for (i = 0; i < 15; i++) {
334 segdir[i].offset = -1;
335 segdir[i].length = 0;
336 segdir[i].res08 = -1;
337 segdir[i].res0c = 0x0f;
338 }
339 }
340
341 /****************************************************************************
342 * ctl2_hash_guid
343 *
344 * Generates a hash key from a GUID.
345 *
346 * RETURNS
347 *
348 * The hash key for the GUID.
349 */
350 static int ctl2_hash_guid(
351 REFGUID guid) /* [I] The guid to find. */
352 {
353 int hash;
354 int i;
355
356 hash = 0;
357 for (i = 0; i < 8; i ++) {
358 hash ^= ((const short *)guid)[i];
359 }
360
361 return hash & 0x1f;
362 }
363
364 /****************************************************************************
365 * ctl2_find_guid
366 *
367 * Locates a guid in a type library.
368 *
369 * RETURNS
370 *
371 * The offset into the GUID segment of the guid, or -1 if not found.
372 */
373 static int ctl2_find_guid(
374 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
375 int hash_key, /* [I] The hash key for the guid. */
376 REFGUID guid) /* [I] The guid to find. */
377 {
378 int offset;
379 MSFT_GuidEntry *guidentry;
380
381 offset = This->typelib_guidhash_segment[hash_key];
382 while (offset != -1) {
383 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
384
385 if (IsEqualGUID(guidentry, guid)) return offset;
386
387 offset = guidentry->next_hash;
388 }
389
390 return offset;
391 }
392
393 /****************************************************************************
394 * ctl2_find_name
395 *
396 * Locates a name in a type library.
397 *
398 * RETURNS
399 *
400 * The offset into the NAME segment of the name, or -1 if not found.
401 *
402 * NOTES
403 *
404 * The name must be encoded as with ctl2_encode_name().
405 */
406 static int ctl2_find_name(
407 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
408 const char *name) /* [I] The encoded name to find. */
409 {
410 int offset;
411 int *namestruct;
412
413 offset = This->typelib_namehash_segment[name[2] & 0x7f];
414 while (offset != -1) {
415 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
416
417 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
418 /* hash codes and lengths match, final test */
419 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
420 }
421
422 /* move to next item in hash bucket */
423 offset = namestruct[1];
424 }
425
426 return offset;
427 }
428
429 /****************************************************************************
430 * ctl2_encode_name
431 *
432 * Encodes a name string to a form suitable for storing into a type library
433 * or comparing to a name stored in a type library.
434 *
435 * RETURNS
436 *
437 * The length of the encoded name, including padding and length+hash fields.
438 *
439 * NOTES
440 *
441 * Will throw an exception if name or result are NULL. Is not multithread
442 * safe in the slightest.
443 */
444 static int ctl2_encode_name(
445 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
446 const WCHAR *name, /* [I] The name string to encode. */
447 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
448 {
449 int length;
450 static char converted_name[0x104];
451 int offset;
452 int value;
453
454 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
455 converted_name[0] = length & 0xff;
456
457 converted_name[length + 4] = 0;
458
459 converted_name[1] = 0x00;
460
461 value = LHashValOfNameSysA(ctl2_get_syskind(This), This->typelib_header.lcid, converted_name + 4);
462
463 converted_name[2] = value;
464 converted_name[3] = value >> 8;
465
466 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
467
468 *result = converted_name;
469
470 return (length + 7) & ~3;
471 }
472
473 /****************************************************************************
474 * ctl2_decode_name
475 *
476 * Converts string stored in typelib data to unicode.
477 */
478 static void ctl2_decode_name(
479 char *data, /* [I] String to be decoded */
480 WCHAR **string) /* [O] Decoded string */
481 {
482 int i, length;
483 static WCHAR converted_string[0x104];
484
485 length = data[0];
486
487 for(i=0; i<length; i++)
488 converted_string[i] = data[i+4];
489 converted_string[length] = '\0';
490
491 *string = converted_string;
492 }
493
494 /****************************************************************************
495 * ctl2_encode_string
496 *
497 * Encodes a string to a form suitable for storing into a type library or
498 * comparing to a string stored in a type library.
499 *
500 * RETURNS
501 *
502 * The length of the encoded string, including padding and length fields.
503 *
504 * NOTES
505 *
506 * Will throw an exception if string or result are NULL. Is not multithread
507 * safe in the slightest.
508 */
509 static int ctl2_encode_string(
510 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
511 const WCHAR *string, /* [I] The string to encode. */
512 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
513 {
514 int length;
515 static char converted_string[0x104];
516 int offset;
517
518 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
519 converted_string[0] = length & 0xff;
520 converted_string[1] = (length >> 8) & 0xff;
521
522 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
523
524 *result = converted_string;
525
526 return (length + 5) & ~3;
527 }
528
529 /****************************************************************************
530 * ctl2_decode_string
531 *
532 * Converts string stored in typelib data to unicode.
533 */
534 static void ctl2_decode_string(
535 unsigned char *data,/* [I] String to be decoded */
536 WCHAR **string) /* [O] Decoded string */
537 {
538 int i, length;
539 static WCHAR converted_string[0x104];
540
541 length = data[0] + (data[1]<<8);
542 if((length&0x3) == 1)
543 length >>= 2;
544
545 for(i=0; i<length; i++)
546 converted_string[i] = data[i+2];
547 converted_string[length] = '\0';
548
549 *string = converted_string;
550 }
551
552 /****************************************************************************
553 * ctl2_alloc_segment
554 *
555 * Allocates memory from a segment in a type library.
556 *
557 * RETURNS
558 *
559 * Success: The offset within the segment of the new data area.
560 * Failure: -1 (this is invariably an out of memory condition).
561 *
562 * BUGS
563 *
564 * Does not (yet) handle the case where the allocated segment memory needs to grow.
565 */
566 static int ctl2_alloc_segment(
567 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
568 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
569 int size, /* [I] The amount to allocate. */
570 int block_size) /* [I] Initial allocation block size, or 0 for default. */
571 {
572 int offset;
573
574 if(!This->typelib_segment_data[segment]) {
575 if (!block_size) block_size = 0x2000;
576
577 This->typelib_segment_block_length[segment] = block_size;
578 This->typelib_segment_data[segment] = heap_alloc(block_size);
579 if (!This->typelib_segment_data[segment]) return -1;
580 memset(This->typelib_segment_data[segment], 0x57, block_size);
581 }
582
583 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
584 unsigned char *block;
585
586 block_size = This->typelib_segment_block_length[segment];
587 block = heap_realloc(This->typelib_segment_data[segment], block_size << 1);
588 if (!block) return -1;
589
590 if (segment == MSFT_SEG_TYPEINFO) {
591 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
592 ICreateTypeInfo2Impl *typeinfo;
593
594 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
595 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
596 }
597 }
598
599 memset(block + block_size, 0x57, block_size);
600 This->typelib_segment_block_length[segment] = block_size << 1;
601 This->typelib_segment_data[segment] = block;
602 }
603
604 offset = This->typelib_segdir[segment].length;
605 This->typelib_segdir[segment].length += size;
606
607 return offset;
608 }
609
610 /****************************************************************************
611 * ctl2_alloc_typeinfo
612 *
613 * Allocates and initializes a typeinfo structure in a type library.
614 *
615 * RETURNS
616 *
617 * Success: The offset of the new typeinfo.
618 * Failure: -1 (this is invariably an out of memory condition).
619 */
620 static int ctl2_alloc_typeinfo(
621 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
622 int nameoffset) /* [I] The offset of the name for this typeinfo. */
623 {
624 int offset;
625 MSFT_TypeInfoBase *typeinfo;
626
627 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
628 if (offset == -1) return -1;
629
630 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
631
632 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
633
634 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
635 typeinfo->memoffset = -1; /* should be EOF if no elements */
636 typeinfo->res2 = 0;
637 typeinfo->res3 = 0;
638 typeinfo->res4 = 3;
639 typeinfo->res5 = 0;
640 typeinfo->cElement = 0;
641 typeinfo->res7 = 0;
642 typeinfo->res8 = 0;
643 typeinfo->res9 = 0;
644 typeinfo->resA = 0;
645 typeinfo->posguid = -1;
646 typeinfo->flags = 0;
647 typeinfo->NameOffset = nameoffset;
648 typeinfo->version = 0;
649 typeinfo->docstringoffs = -1;
650 typeinfo->helpstringcontext = 0;
651 typeinfo->helpcontext = 0;
652 typeinfo->oCustData = -1;
653 typeinfo->cbSizeVft = 0;
654 typeinfo->cImplTypes = 0;
655 typeinfo->size = 0;
656 typeinfo->datatype1 = -1;
657 typeinfo->datatype2 = 0;
658 typeinfo->res18 = 0;
659 typeinfo->res19 = -1;
660
661 return offset;
662 }
663
664 /****************************************************************************
665 * ctl2_alloc_guid
666 *
667 * Allocates and initializes a GUID structure in a type library. Also updates
668 * the GUID hash table as needed.
669 *
670 * RETURNS
671 *
672 * Success: The offset of the new GUID.
673 * Failure: -1 (this is invariably an out of memory condition).
674 */
675 static int ctl2_alloc_guid(
676 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
677 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
678 {
679 int offset;
680 MSFT_GuidEntry *guid_space;
681 int hash_key;
682
683 hash_key = ctl2_hash_guid(&guid->guid);
684
685 offset = ctl2_find_guid(This, hash_key, &guid->guid);
686 if (offset != -1) return offset;
687
688 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
689 if (offset == -1) return -1;
690
691 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
692 *guid_space = *guid;
693
694 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
695 This->typelib_guidhash_segment[hash_key] = offset;
696
697 return offset;
698 }
699
700 /****************************************************************************
701 * ctl2_alloc_name
702 *
703 * Allocates and initializes a name within a type library. Also updates the
704 * name hash table as needed.
705 *
706 * RETURNS
707 *
708 * Success: The offset within the segment of the new name.
709 * Failure: -1 (this is invariably an out of memory condition).
710 */
711 static int ctl2_alloc_name(
712 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
713 const WCHAR *name) /* [I] The name to store. */
714 {
715 int length;
716 int offset;
717 MSFT_NameIntro *name_space;
718 char *encoded_name;
719
720 length = ctl2_encode_name(This, name, &encoded_name);
721
722 offset = ctl2_find_name(This, encoded_name);
723 if (offset != -1) return offset;
724
725 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
726 if (offset == -1) return -1;
727
728 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
729 name_space->hreftype = -1;
730 name_space->next_hash = -1;
731 memcpy(&name_space->namelen, encoded_name, length);
732
733 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
734 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
735
736 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
737
738 This->typelib_header.nametablecount += 1;
739 This->typelib_header.nametablechars += *encoded_name;
740
741 return offset;
742 }
743
744 /****************************************************************************
745 * ctl2_alloc_string
746 *
747 * Allocates and initializes a string in a type library.
748 *
749 * RETURNS
750 *
751 * Success: The offset within the segment of the new string.
752 * Failure: -1 (this is invariably an out of memory condition).
753 */
754 static int ctl2_alloc_string(
755 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
756 const WCHAR *string) /* [I] The string to store. */
757 {
758 int length;
759 int offset;
760 unsigned char *string_space;
761 char *encoded_string;
762
763 length = ctl2_encode_string(This, string, &encoded_string);
764
765 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
766 offset += (((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
767 This->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
768 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
769 }
770
771 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
772 if (offset == -1) return -1;
773
774 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
775 memcpy(string_space, encoded_string, length);
776
777 return offset;
778 }
779
780 /****************************************************************************
781 * ctl2_alloc_importinfo
782 *
783 * Allocates and initializes an import information structure in a type library.
784 *
785 * RETURNS
786 *
787 * Success: The offset of the new importinfo.
788 * Failure: -1 (this is invariably an out of memory condition).
789 */
790 static int ctl2_alloc_importinfo(
791 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
792 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
793 {
794 int offset;
795 MSFT_ImpInfo *impinfo_space;
796
797 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
798 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
799 offset+=sizeof(MSFT_ImpInfo)) {
800 if(impinfo_space->oImpFile == impinfo->oImpFile
801 && impinfo_space->oGuid == impinfo->oGuid)
802 return offset;
803
804 impinfo_space += 1;
805 }
806
807 impinfo->flags |= This->typelib_header.nimpinfos++;
808
809 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
810 if (offset == -1) return -1;
811
812 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
813 *impinfo_space = *impinfo;
814
815 return offset;
816 }
817
818 /****************************************************************************
819 * ctl2_alloc_importfile
820 *
821 * Allocates and initializes an import file definition in a type library.
822 *
823 * RETURNS
824 *
825 * Success: The offset of the new importinfo.
826 * Failure: -1 (this is invariably an out of memory condition).
827 */
828 static int ctl2_alloc_importfile(
829 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
830 int guidoffset, /* [I] The offset to the GUID for the imported library. */
831 LCID lcid, /* [I] The LCID of imported library. */
832 int major_version, /* [I] The major version number of the imported library. */
833 int minor_version, /* [I] The minor version number of the imported library. */
834 const WCHAR *filename) /* [I] The filename of the imported library. */
835 {
836 int length;
837 int offset;
838 MSFT_ImpFile *importfile;
839 char *encoded_string;
840
841 length = ctl2_encode_string(This, filename, &encoded_string);
842
843 encoded_string[0] <<= 2;
844 encoded_string[0] |= 1;
845
846 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
847 offset += (((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
848 This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 5) & 0xfffc) + 0xc) {
849 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
850 }
851
852 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
853 if (offset == -1) return -1;
854
855 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
856 importfile->guid = guidoffset;
857 importfile->lcid = lcid;
858 importfile->version = major_version | (minor_version << 16);
859 memcpy(importfile->filename, encoded_string, length);
860
861 return offset;
862 }
863
864 /****************************************************************************
865 * ctl2_encode_variant
866 *
867 * Encodes a variant, inline if possible or in custom data segment
868 *
869 * RETURNS
870 *
871 * Success: S_OK
872 * Failure: Error code from winerror.h
873 */
874 static HRESULT ctl2_encode_variant(
875 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
876 int *encoded_value, /* [O] The encoded default value or data offset */
877 VARIANT *value, /* [I] Default value to be encoded */
878 VARTYPE arg_type) /* [I] Argument type */
879 {
880 VARIANT v;
881 HRESULT hres;
882 int mask = 0;
883
884 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
885
886 if(arg_type == VT_INT)
887 arg_type = VT_I4;
888 if(arg_type == VT_UINT)
889 arg_type = VT_UI4;
890
891 v = *value;
892 if(V_VT(value) != arg_type) {
893 hres = VariantChangeType(&v, value, 0, arg_type);
894 if(FAILED(hres))
895 return hres;
896 }
897
898 /* Check if default value can be stored in encoded_value */
899 switch(arg_type) {
900 case VT_I4:
901 case VT_UI4:
902 mask = 0x3ffffff;
903 if(V_UI4(&v)>0x3ffffff)
904 break;
905 /* fall through */
906 case VT_I1:
907 case VT_UI1:
908 case VT_BOOL:
909 if(!mask)
910 mask = 0xff;
911 /* fall through */
912 case VT_I2:
913 case VT_UI2:
914 if(!mask)
915 mask = 0xffff;
916 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
917 return S_OK;
918 }
919
920 switch(arg_type) {
921 case VT_I4:
922 case VT_R4:
923 case VT_UI4:
924 case VT_INT:
925 case VT_UINT:
926 case VT_HRESULT:
927 case VT_PTR: {
928 /* Construct the data to be allocated */
929 int data[2];
930 data[0] = arg_type + (V_UI4(&v)<<16);
931 data[1] = (V_UI4(&v)>>16) + 0x57570000;
932
933 /* Check if the data was already allocated */
934 /* Currently the structures doesn't allow to do it in a nice way */
935 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
936 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
937 return S_OK;
938
939 /* Allocate the data */
940 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
941 if(*encoded_value == -1)
942 return E_OUTOFMEMORY;
943
944 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
945 return S_OK;
946 }
947 case VT_BSTR: {
948 /* Construct the data */
949 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
950 char *data = heap_alloc(len);
951
952 if(!data)
953 return E_OUTOFMEMORY;
954
955 *((unsigned short*)data) = arg_type;
956 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
957 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
958 if(V_BSTR(&v)[i] <= 0x7f)
959 data[i+6] = V_BSTR(&v)[i];
960 else
961 data[i+6] = '?';
962 }
963 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
964 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
965 data[i] = 0x57;
966
967 /* Check if the data was already allocated */
968 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
969 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
970 heap_free(data);
971 return S_OK;
972 }
973
974 /* Allocate the data */
975 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
976 if(*encoded_value == -1) {
977 heap_free(data);
978 return E_OUTOFMEMORY;
979 }
980
981 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
982 heap_free(data);
983 return S_OK;
984 }
985 default:
986 FIXME("Argument type not yet handled\n");
987 return E_NOTIMPL;
988 }
989 }
990
991 static int ctl2_find_custdata(
992 ICreateTypeLib2Impl *This,
993 REFGUID guid,
994 int offset)
995 {
996 while (offset != -1) {
997 MSFT_CDGuid *cdentry =
998 (MSFT_CDGuid *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
999 MSFT_GuidEntry *guidentry =
1000 (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][cdentry->GuidOffset];
1001
1002 if (IsEqualGUID(guidentry, guid))
1003 return offset;
1004
1005 offset = cdentry->next;
1006 }
1007
1008 return -1;
1009 }
1010
1011 /****************************************************************************
1012 * ctl2_decode_variant
1013 *
1014 * Decodes a variant
1015 *
1016 * RETURNS
1017 *
1018 * Success: S_OK
1019 * Failure: Error code from winerror.h
1020 */
1021 static HRESULT ctl2_decode_variant(
1022 ICreateTypeLib2Impl *This, /* [I] The typelib that contains the variant */
1023 int data_offs, /* [I] Offset within the data array, or the encoded value itself */
1024 VARIANT *value) /* [O] Decoded value */
1025 {
1026 unsigned char *encoded_data;
1027 VARTYPE type;
1028
1029 if (data_offs & 0x80000000) {
1030 /* data_offs contains the encoded value */
1031 V_VT(value) = (data_offs & ~0x80000000) >> 26;
1032 V_UI4(value) = data_offs & ~0xFF000000;
1033 return S_OK;
1034 }
1035
1036 encoded_data = &This->typelib_segment_data[MSFT_SEG_CUSTDATA][data_offs];
1037 type = *encoded_data;
1038
1039 switch(type) {
1040 case VT_I4:
1041 case VT_R4:
1042 case VT_UI4:
1043 case VT_INT:
1044 case VT_UINT:
1045 case VT_HRESULT:
1046 case VT_PTR: {
1047 V_VT(value) = type;
1048 V_UI4(value) = *(unsigned*)(encoded_data + 2);
1049 return S_OK;
1050 }
1051 case VT_BSTR: {
1052 unsigned len, i;
1053
1054 len = *(unsigned*)(encoded_data + 2);
1055
1056 V_VT(value) = type;
1057 V_BSTR(value) = SysAllocStringByteLen(NULL, len * sizeof(OLECHAR));
1058 for (i = 0; i < len; ++i)
1059 V_BSTR(value)[i] = *(encoded_data + 6 + i);
1060
1061 return S_OK;
1062 }
1063 default:
1064 FIXME("Don't yet have decoder for this VARTYPE: %u\n", type);
1065 return E_NOTIMPL;
1066 }
1067 }
1068
1069 /****************************************************************************
1070 * ctl2_set_custdata
1071 *
1072 * Adds a custom data element to an object in a type library.
1073 *
1074 * RETURNS
1075 *
1076 * Success: S_OK.
1077 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
1078 */
1079 static HRESULT ctl2_set_custdata(
1080 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
1081 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
1082 VARIANT *pVarVal, /* [I] The custom data itself. */
1083 int *offset) /* [I/O] The list of custom data to prepend to. */
1084 {
1085 MSFT_GuidEntry guidentry;
1086 HRESULT status;
1087 int dataoffset;
1088 int guidoffset;
1089 int custoffset;
1090 int *custdata;
1091 BOOL new_segment = FALSE;
1092
1093 switch(V_VT(pVarVal))
1094 {
1095 case VT_I4:
1096 case VT_R4:
1097 case VT_UI4:
1098 case VT_INT:
1099 case VT_UINT:
1100 case VT_HRESULT:
1101 case VT_BSTR:
1102 /* empty */
1103 break;
1104 default:
1105 return DISP_E_BADVARTYPE;
1106 }
1107
1108 guidentry.guid = *guid;
1109
1110 guidentry.hreftype = -1;
1111 guidentry.next_hash = -1;
1112
1113 guidoffset = ctl2_alloc_guid(This, &guidentry);
1114 if (guidoffset == -1) return E_OUTOFMEMORY;
1115
1116 status = ctl2_encode_variant(This, &dataoffset, pVarVal, V_VT(pVarVal));
1117 if (status)
1118 return status;
1119
1120 custoffset = ctl2_find_custdata(This, guid, *offset);
1121 if (custoffset == -1) {
1122 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
1123 if (custoffset == -1)
1124 return E_OUTOFMEMORY;
1125 new_segment = TRUE;
1126 }
1127
1128 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1129 custdata[0] = guidoffset;
1130 custdata[1] = dataoffset;
1131 if (new_segment) {
1132 custdata[2] = *offset;
1133 *offset = custoffset;
1134 }
1135
1136 return S_OK;
1137 }
1138
1139 /****************************************************************************
1140 * ctl2_encode_typedesc
1141 *
1142 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
1143 * segments as needed.
1144 *
1145 * RETURNS
1146 *
1147 * Success: 0.
1148 * Failure: -1.
1149 */
1150 static int ctl2_encode_typedesc(
1151 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
1152 const TYPEDESC *tdesc, /* [I] The type description to encode. */
1153 int *encoded_tdesc, /* [O] The encoded type description. */
1154 int *width, /* [O] The width of the type, or NULL. */
1155 int *alignment, /* [O] The alignment of the type, or NULL. */
1156 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1157 {
1158 int default_tdesc;
1159 int scratch;
1160 int typeoffset;
1161 int arrayoffset;
1162 int *typedata;
1163 int *arraydata;
1164 int target_type;
1165 int child_size;
1166
1167 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
1168 if (!width) width = &scratch;
1169 if (!alignment) alignment = &scratch;
1170 if (!decoded_size) decoded_size = &scratch;
1171
1172 *decoded_size = 0;
1173
1174 switch (tdesc->vt) {
1175 case VT_UI1:
1176 case VT_I1:
1177 *encoded_tdesc = default_tdesc;
1178 *width = 1;
1179 *alignment = 1;
1180 break;
1181
1182 case VT_INT:
1183 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
1184 if (ctl2_get_syskind(This) == SYS_WIN16) {
1185 *width = 2;
1186 *alignment = 2;
1187 } else {
1188 *width = 4;
1189 *alignment = 4;
1190 }
1191 break;
1192
1193 case VT_UINT:
1194 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
1195 if (ctl2_get_syskind(This) == SYS_WIN16) {
1196 *width = 2;
1197 *alignment = 2;
1198 } else {
1199 *width = 4;
1200 *alignment = 4;
1201 }
1202 break;
1203
1204 case VT_UI2:
1205 case VT_I2:
1206 case VT_BOOL:
1207 *encoded_tdesc = default_tdesc;
1208 *width = 2;
1209 *alignment = 2;
1210 break;
1211
1212 case VT_I4:
1213 case VT_UI4:
1214 case VT_R4:
1215 case VT_ERROR:
1216 case VT_BSTR:
1217 case VT_HRESULT:
1218 *encoded_tdesc = default_tdesc;
1219 *width = 4;
1220 *alignment = 4;
1221 break;
1222
1223 case VT_CY:
1224 *encoded_tdesc = default_tdesc;
1225 *width = 8;
1226 *alignment = 4; /* guess? */
1227 break;
1228
1229 case VT_VOID:
1230 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1231 *width = 0;
1232 *alignment = 1;
1233 break;
1234
1235 case VT_PTR:
1236 case VT_SAFEARRAY:
1237 /* FIXME: Make with the error checking. */
1238 FIXME("PTR or SAFEARRAY vartype, may not work correctly.\n");
1239
1240 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1241
1242 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1243 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1244 if (((typedata[0] & 0xffff) == tdesc->vt) && (typedata[1] == target_type)) break;
1245 }
1246
1247 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1248 int mix_field;
1249
1250 if (target_type & 0x80000000) {
1251 mix_field = (target_type >> 16) & VT_TYPEMASK;
1252 } else {
1253 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1254 switch((typedata[0]>>16) & ~VT_ARRAY)
1255 {
1256 case VT_UI1:
1257 case VT_I1:
1258 case VT_UI2:
1259 case VT_I2:
1260 case VT_I4:
1261 case VT_UI4:
1262 mix_field = typedata[0]>>16;
1263 break;
1264 default:
1265 mix_field = ((typedata[0] >> 16) == 0x7fff) ? 0x7fff : 0x7ffe;
1266 break;
1267 }
1268 }
1269
1270 if (tdesc->vt == VT_PTR)
1271 mix_field |= VT_BYREF;
1272 else if (tdesc->vt == VT_SAFEARRAY)
1273 mix_field |= VT_ARRAY;
1274
1275 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1276 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1277
1278 typedata[0] = (mix_field << 16) | tdesc->vt;
1279 typedata[1] = target_type;
1280 }
1281
1282 *encoded_tdesc = typeoffset;
1283
1284 *width = 4;
1285 *alignment = 4;
1286 *decoded_size = sizeof(TYPEDESC) + child_size;
1287 break;
1288
1289 case VT_CARRAY:
1290 {
1291 /* FIXME: Make with the error checking. */
1292 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1293
1294 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1295 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1296 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1297
1298 arraydata[0] = target_type;
1299 arraydata[1] = num_dims;
1300 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1301 arraydata += 2;
1302
1303 for(dim = 0; dim < num_dims; dim++) {
1304 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1305 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1306 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1307 arraydata += 2;
1308 }
1309 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1310 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1311
1312 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1313 typedata[1] = arrayoffset;
1314
1315 *encoded_tdesc = typeoffset;
1316 *width = *width * elements;
1317 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1318
1319 break;
1320 }
1321 case VT_USERDEFINED:
1322 {
1323 const MSFT_TypeInfoBase *basetype;
1324 INT basevt = 0x7fff;
1325
1326 TRACE("USERDEFINED.\n");
1327 if (tdesc->u.hreftype % sizeof(*basetype) == 0 && tdesc->u.hreftype < This->typelib_segdir[MSFT_SEG_TYPEINFO].length)
1328 {
1329 basetype = (MSFT_TypeInfoBase*)&(This->typelib_segment_data[MSFT_SEG_TYPEINFO][tdesc->u.hreftype]);
1330 switch(basetype->typekind & 0xf)
1331 {
1332 case TKIND_ENUM:
1333 basevt = VT_I4;
1334 break;
1335 default:
1336 FIXME("USERDEFINED basetype %d not handled\n", basetype->typekind & 0xf);
1337 break;
1338 }
1339 }
1340 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1341 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1342 if ((typedata[0] == ((basevt << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1343 }
1344
1345 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1346 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1347 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1348
1349 typedata[0] = (basevt << 16) | VT_USERDEFINED;
1350 typedata[1] = tdesc->u.hreftype;
1351 }
1352
1353 *encoded_tdesc = typeoffset;
1354 *width = 0;
1355 *alignment = 1;
1356 break;
1357 }
1358
1359 default:
1360 FIXME("Unrecognized type %d.\n", tdesc->vt);
1361 *encoded_tdesc = default_tdesc;
1362 *width = 0;
1363 *alignment = 1;
1364 break;
1365 }
1366
1367 return 0;
1368 }
1369
1370 /****************************************************************************
1371 * ctl2_decode_typedesc
1372 *
1373 * Decodes a type description from an ICreateTypeLib2Impl.
1374 *
1375 * RETURNS
1376 *
1377 * Success: S_OK.
1378 * Failure: HRESULT error code.
1379 */
1380 static HRESULT ctl2_decode_typedesc(
1381 ICreateTypeLib2Impl *This, /* [I] The type library from which to decode the TYPEDESC. */
1382 int encoded_tdesc, /* [I] The encoded type description. */
1383 TYPEDESC *tdesc) /* [O] The decoded type description. */
1384 {
1385 int *typedata, i;
1386 HRESULT hres;
1387
1388 if (encoded_tdesc & 0x80000000) {
1389 tdesc->vt = encoded_tdesc & VT_TYPEMASK;
1390 tdesc->u.lptdesc = NULL;
1391 return S_OK;
1392 }
1393
1394 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][encoded_tdesc];
1395
1396 tdesc->vt = typedata[0] & 0xFFFF;
1397
1398 switch(tdesc->vt) {
1399 case VT_PTR:
1400 case VT_SAFEARRAY:
1401 tdesc->u.lptdesc = heap_alloc_zero(sizeof(TYPEDESC));
1402 if (!tdesc->u.lptdesc)
1403 return E_OUTOFMEMORY;
1404
1405 hres = ctl2_decode_typedesc(This, typedata[1], tdesc->u.lptdesc);
1406 if (FAILED(hres)) {
1407 heap_free(tdesc->u.lptdesc);
1408 return hres;
1409 }
1410
1411 return S_OK;
1412
1413 case VT_CARRAY: {
1414 int arrayoffset, *arraydata, num_dims;
1415
1416 arrayoffset = typedata[1];
1417 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1418 num_dims = arraydata[1] & 0xFFFF;
1419
1420 tdesc->u.lpadesc = heap_alloc_zero(sizeof(ARRAYDESC) + sizeof(SAFEARRAYBOUND) * (num_dims - 1));
1421 if (!tdesc->u.lpadesc)
1422 return E_OUTOFMEMORY;
1423
1424 hres = ctl2_decode_typedesc(This, arraydata[0], &tdesc->u.lpadesc->tdescElem);
1425 if (FAILED(hres)) {
1426 heap_free(tdesc->u.lpadesc);
1427 return E_OUTOFMEMORY;
1428 }
1429
1430 for (i = 0; i < num_dims; ++i) {
1431 tdesc->u.lpadesc->rgbounds[i].cElements = arraydata[2 + i * 2];
1432 tdesc->u.lpadesc->rgbounds[i].lLbound = arraydata[3 + i * 2];
1433 }
1434
1435 return S_OK;
1436 }
1437 case VT_USERDEFINED:
1438 tdesc->u.hreftype = typedata[1];
1439 return S_OK;
1440 default:
1441 FIXME("unable to decode typedesc (%08x): unknown VT: %d\n", encoded_tdesc, tdesc->vt);
1442 return E_NOTIMPL;
1443 }
1444 }
1445
1446 /****************************************************************************
1447 * ctl2_find_nth_reference
1448 *
1449 * Finds a reference by index into the linked list of reference records.
1450 *
1451 * RETURNS
1452 *
1453 * Success: Offset of the desired reference record.
1454 * Failure: -1.
1455 */
1456 static int ctl2_find_nth_reference(
1457 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1458 int offset, /* [I] The starting offset of the reference list. */
1459 int index) /* [I] The index of the reference to find. */
1460 {
1461 MSFT_RefRecord *ref;
1462
1463 for (; index && (offset != -1); index--) {
1464 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1465 offset = ref->onext;
1466 }
1467
1468 return offset;
1469 }
1470
1471 /****************************************************************************
1472 * ctl2_find_typeinfo_from_offset
1473 *
1474 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1475 *
1476 * RETURNS
1477 *
1478 * Success: S_OK.
1479 * Failure: TYPE_E_ELEMENTNOTFOUND.
1480 */
1481 static HRESULT ctl2_find_typeinfo_from_offset(
1482 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1483 int offset, /* [I] The offset of the desired typeinfo. */
1484 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1485 {
1486 void *typeinfodata;
1487 ICreateTypeInfo2Impl *typeinfo;
1488
1489 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1490
1491 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1492 if (typeinfo->typeinfo == typeinfodata) {
1493 *ppTinfo = (ITypeInfo *)&typeinfo->ITypeInfo2_iface;
1494 ITypeInfo_AddRef(*ppTinfo);
1495 return S_OK;
1496 }
1497 }
1498
1499 ERR("Failed to find typeinfo, invariant varied.\n");
1500
1501 return TYPE_E_ELEMENTNOTFOUND;
1502 }
1503
1504 /****************************************************************************
1505 * funcrecord_reallochdr
1506 *
1507 * Ensure FuncRecord data block contains header of required size
1508 *
1509 * PARAMS
1510 *
1511 * typedata [IO] - reference to pointer to data block
1512 * need [I] - required size of block in bytes
1513 *
1514 * RETURNS
1515 *
1516 * Number of additionally allocated bytes
1517 */
1518 static INT funcrecord_reallochdr(INT **typedata, int need)
1519 {
1520 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1521 int hdr = (*typedata)[0] - tail;
1522 int i;
1523
1524 if (hdr >= need)
1525 return 0;
1526
1527 *typedata = heap_realloc(*typedata, need + tail);
1528 if (!*typedata)
1529 return -1;
1530
1531 if (tail)
1532 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1533 (*typedata)[0] = need + tail;
1534
1535 /* fill in default values */
1536 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1537 {
1538 switch(i)
1539 {
1540 case 2:
1541 (*typedata)[i] = 0;
1542 break;
1543 case 7:
1544 (*typedata)[i] = -1;
1545 break;
1546 case 8:
1547 (*typedata)[i] = -1;
1548 break;
1549 case 9:
1550 (*typedata)[i] = -1;
1551 break;
1552 case 10:
1553 (*typedata)[i] = -1;
1554 break;
1555 case 11:
1556 (*typedata)[i] = 0;
1557 break;
1558 case 12:
1559 (*typedata)[i] = -1;
1560 break;
1561 }
1562 }
1563
1564 return need - hdr;
1565 }
1566
1567 /*================== ICreateTypeInfo2 Implementation ===================================*/
1568
1569 /******************************************************************************
1570 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1571 *
1572 */
1573 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1574 ICreateTypeInfo2 * iface,
1575 REFIID riid,
1576 VOID **ppvObject)
1577 {
1578 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1579
1580 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1581
1582 *ppvObject=NULL;
1583 if(IsEqualIID(riid, &IID_IUnknown) ||
1584 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1585 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1586 {
1587 *ppvObject = This;
1588 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1589 IsEqualIID(riid, &IID_ITypeInfo2)) {
1590 *ppvObject = &This->ITypeInfo2_iface;
1591 }
1592
1593 if(*ppvObject)
1594 {
1595 ICreateTypeInfo2_AddRef(iface);
1596 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1597 return S_OK;
1598 }
1599 TRACE("-- Interface: E_NOINTERFACE\n");
1600 return E_NOINTERFACE;
1601 }
1602
1603 /******************************************************************************
1604 * ICreateTypeInfo2_AddRef {OLEAUT32}
1605 */
1606 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1607 {
1608 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1609 ULONG ref = InterlockedIncrement(&This->ref);
1610
1611 TRACE("(%p)->ref was %u\n",This, ref - 1);
1612
1613 if(ref==1 && This->typelib)
1614 ICreateTypeLib2_AddRef(&This->typelib->ICreateTypeLib2_iface);
1615
1616 return ref;
1617 }
1618
1619 /******************************************************************************
1620 * ICreateTypeInfo2_Release {OLEAUT32}
1621 */
1622 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1623 {
1624 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1625 ULONG ref = InterlockedDecrement(&This->ref);
1626
1627 TRACE("(%p)->(%u)\n",This, ref);
1628
1629 if (!ref) {
1630 if (This->typelib) {
1631 ICreateTypeLib2_fnRelease(&This->typelib->ICreateTypeLib2_iface);
1632 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1633 /* This->typelib = NULL; */
1634 }
1635
1636 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1637 /* HeapFree(GetProcessHeap(),0,This); */
1638 return 0;
1639 }
1640
1641 return ref;
1642 }
1643
1644
1645 /******************************************************************************
1646 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1647 */
1648 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1649 {
1650 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1651
1652 MSFT_GuidEntry guidentry;
1653 int offset;
1654
1655 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1656
1657 guidentry.guid = *guid;
1658 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1659 guidentry.next_hash = -1;
1660
1661 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1662
1663 if (offset == -1) return E_OUTOFMEMORY;
1664
1665 This->typeinfo->posguid = offset;
1666
1667 if (IsEqualIID(guid, &IID_IDispatch)) {
1668 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1669 }
1670
1671 return S_OK;
1672 }
1673
1674 /******************************************************************************
1675 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1676 */
1677 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1678 {
1679 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1680
1681 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1682
1683 if(uTypeFlags & TYPEFLAG_FDUAL) {
1684 This->typeinfo->typekind |= 0x10;
1685 This->typeinfo->typekind &= ~0x0f;
1686 This->typeinfo->typekind |= TKIND_DISPATCH;
1687
1688 if(!This->dual) {
1689 This->dual = heap_alloc(sizeof(ICreateTypeInfo2Impl));
1690 if(!This->dual)
1691 return E_OUTOFMEMORY;
1692
1693 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1694 This->dual->ref = 0;
1695 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1696 TKIND_INTERFACE : TKIND_DISPATCH;
1697 This->dual->dual = This;
1698 }
1699
1700 /* Make sure dispatch is in typeinfos queue */
1701 if(This->typekind != TKIND_DISPATCH) {
1702 if(This->typelib->last_typeinfo == This)
1703 This->typelib->last_typeinfo = This->dual;
1704
1705 if(This->typelib->typeinfos == This)
1706 This->typelib->typeinfos = This->dual;
1707 else {
1708 ICreateTypeInfo2Impl *iter;
1709
1710 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1711 iter->next_typeinfo = This->dual;
1712 }
1713 } else
1714 iface = &This->dual->ICreateTypeInfo2_iface;
1715 }
1716
1717 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1718 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1719 ITypeLib *stdole;
1720 ITypeInfo *dispatch;
1721 HREFTYPE hreftype;
1722 HRESULT hres;
1723
1724 hres = LoadTypeLib(stdole2tlb, &stdole);
1725 if(FAILED(hres))
1726 return hres;
1727
1728 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1729 ITypeLib_Release(stdole);
1730 if(FAILED(hres))
1731 return hres;
1732
1733 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1734 ITypeInfo_Release(dispatch);
1735 if(FAILED(hres))
1736 return hres;
1737 }
1738
1739 This->typeinfo->flags = uTypeFlags;
1740 return S_OK;
1741 }
1742
1743 /******************************************************************************
1744 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1745 */
1746 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1747 ICreateTypeInfo2* iface,
1748 LPOLESTR pStrDoc)
1749 {
1750 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1751
1752 int offset;
1753
1754 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1755 if (!pStrDoc)
1756 return E_INVALIDARG;
1757
1758 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1759 if (offset == -1) return E_OUTOFMEMORY;
1760 This->typeinfo->docstringoffs = offset;
1761 return S_OK;
1762 }
1763
1764 /******************************************************************************
1765 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1766 */
1767 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1768 ICreateTypeInfo2* iface,
1769 DWORD dwHelpContext)
1770 {
1771 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1772
1773 TRACE("(%p,%d)\n", iface, dwHelpContext);
1774
1775 This->typeinfo->helpcontext = dwHelpContext;
1776
1777 return S_OK;
1778 }
1779
1780 /******************************************************************************
1781 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1782 */
1783 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1784 ICreateTypeInfo2* iface,
1785 WORD wMajorVerNum,
1786 WORD wMinorVerNum)
1787 {
1788 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1789
1790 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1791
1792 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1793 return S_OK;
1794 }
1795
1796 /******************************************************************************
1797 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1798 */
1799 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1800 ICreateTypeInfo2* iface,
1801 ITypeInfo* pTInfo,
1802 HREFTYPE* phRefType)
1803 {
1804 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1805
1806 ITypeLib *container;
1807 UINT index;
1808 HRESULT res;
1809
1810 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1811
1812 if(!pTInfo || !phRefType)
1813 return E_INVALIDARG;
1814
1815 /*
1816 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1817 * same internal structure as one of ours. It could be from another
1818 * implementation of ITypeInfo. So we need to do the following...
1819 */
1820 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1821 if (FAILED(res)) {
1822 TRACE("failed to find containing typelib.\n");
1823 return res;
1824 }
1825
1826 if (container == (ITypeLib *)&This->typelib->ITypeLib2_iface) {
1827 /* Process locally defined TypeInfo */
1828 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1829 } else {
1830 BSTR name;
1831 TLIBATTR *tlibattr;
1832 TYPEATTR *typeattr;
1833 TYPEKIND typekind;
1834 MSFT_GuidEntry guid, *check_guid;
1835 MSFT_ImpInfo impinfo;
1836 int guid_offset, import_offset;
1837 HRESULT hres;
1838
1839 /* Allocate container GUID */
1840 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1841 if(FAILED(hres)) {
1842 ITypeLib_Release(container);
1843 return hres;
1844 }
1845
1846 guid.guid = tlibattr->guid;
1847 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1848 guid.next_hash = -1;
1849
1850 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1851 if(guid_offset == -1) {
1852 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1853 ITypeLib_Release(container);
1854 return E_OUTOFMEMORY;
1855 }
1856
1857 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1858 if(check_guid->hreftype == guid.hreftype)
1859 This->typelib->typelib_guids++;
1860
1861 /* Get import file name */
1862 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1863 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1864 if(FAILED(hres)) {
1865 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1866 ITypeLib_Release(container);
1867 return hres;
1868 }
1869
1870 /* Import file */
1871 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1872 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1873 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1874 SysFreeString(name);
1875
1876 if(import_offset == -1) {
1877 ITypeLib_Release(container);
1878 return E_OUTOFMEMORY;
1879 }
1880
1881 /* Allocate referenced guid */
1882 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1883 if(FAILED(hres)) {
1884 ITypeLib_Release(container);
1885 return hres;
1886 }
1887
1888 guid.guid = typeattr->guid;
1889 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1890 guid.next_hash = -1;
1891 typekind = typeattr->typekind;
1892 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1893
1894 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1895 if(guid_offset == -1) {
1896 ITypeLib_Release(container);
1897 return E_OUTOFMEMORY;
1898 }
1899
1900 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1901 if(check_guid->hreftype == guid.hreftype)
1902 This->typelib->typeinfo_guids++;
1903
1904 /* Allocate importinfo */
1905 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1906 impinfo.oImpFile = import_offset;
1907 impinfo.oGuid = guid_offset;
1908 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1909
1910 if(IsEqualGUID(&guid.guid, &IID_IDispatch))
1911 This->typelib->typelib_header.dispatchpos = *phRefType;
1912 }
1913
1914 ITypeLib_Release(container);
1915 return S_OK;
1916 }
1917
1918 /******************************************************************************
1919 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1920 */
1921 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1922 ICreateTypeInfo2* iface,
1923 UINT index,
1924 FUNCDESC* pFuncDesc)
1925 {
1926 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1927
1928 CyclicList *iter, *insert;
1929 int *typedata;
1930 int i, num_defaults = 0, num_retval = 0;
1931 int decoded_size;
1932 HRESULT hres;
1933
1934 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1935
1936 if(!pFuncDesc || pFuncDesc->oVft&3)
1937 return E_INVALIDARG;
1938
1939 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1940 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1941 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1942 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1943 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1944
1945 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1946 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1947
1948 switch(This->typekind) {
1949 case TKIND_MODULE:
1950 if(pFuncDesc->funckind != FUNC_STATIC)
1951 return TYPE_E_BADMODULEKIND;
1952 break;
1953 case TKIND_DISPATCH:
1954 if(pFuncDesc->funckind != FUNC_DISPATCH)
1955 return TYPE_E_BADMODULEKIND;
1956 break;
1957 default:
1958 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1959 return TYPE_E_BADMODULEKIND;
1960 }
1961
1962 if(cti2_get_func_count(This->typeinfo) < index)
1963 return TYPE_E_ELEMENTNOTFOUND;
1964
1965 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1966 !pFuncDesc->cParams)
1967 return TYPE_E_INCONSISTENTPROPFUNCS;
1968
1969 /* get number of arguments with default values specified */
1970 for (i = 0; i < pFuncDesc->cParams; i++) {
1971 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1972 num_defaults++;
1973 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1974 num_retval++;
1975 }
1976
1977 if (!This->typedata) {
1978 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
1979 if(!This->typedata)
1980 return E_OUTOFMEMORY;
1981
1982 This->typedata->next = This->typedata;
1983
1984 if(This->dual)
1985 This->dual->typedata = This->typedata;
1986 }
1987
1988 /* allocate type data space for us */
1989 insert = alloc_cyclic_list_item(CyclicListFunc);
1990 if(!insert)
1991 return E_OUTOFMEMORY;
1992 insert->u.data = heap_alloc(FIELD_OFFSET(MSFT_FuncRecord, HelpContext) +
1993 sizeof(int)*(num_defaults?4:3)*pFuncDesc->cParams);
1994 if(!insert->u.data) {
1995 heap_free(insert);
1996 return E_OUTOFMEMORY;
1997 }
1998
1999 /* fill out the basic type information */
2000 typedata = insert->u.data;
2001 typedata[0] = FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
2002 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
2003 typedata[2] = pFuncDesc->wFuncFlags;
2004 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
2005 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
2006 if(num_defaults) typedata[4] |= 0x1000;
2007 if (num_retval) typedata[4] |= 0x4000;
2008 typedata[5] = pFuncDesc->cParams;
2009
2010 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
2011 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
2012 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
2013 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
2014
2015 /* add default values */
2016 if(num_defaults) {
2017 for (i = 0; i < pFuncDesc->cParams; i++)
2018 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
2019 hres = ctl2_encode_variant(This->typelib, typedata+6+i,
2020 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
2021 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
2022
2023 if(FAILED(hres)) {
2024 heap_free(insert->u.data);
2025 heap_free(insert);
2026 return hres;
2027 }
2028 } else
2029 typedata[6+i] = 0xffffffff;
2030
2031 num_defaults = pFuncDesc->cParams;
2032 }
2033
2034 /* add arguments */
2035 for (i = 0; i < pFuncDesc->cParams; i++) {
2036 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
2037 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
2038 typedata[7+num_defaults+(i*3)] = -1;
2039 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
2040 typedata[3] += decoded_size << 16;
2041 }
2042
2043 /* update the index data */
2044 insert->indice = pFuncDesc->memid;
2045 insert->name = -1;
2046
2047 /* insert type data to list */
2048 if(index == cti2_get_func_count(This->typeinfo)) {
2049 insert->next = This->typedata->next;
2050 This->typedata->next = insert;
2051 This->typedata = insert;
2052
2053 if(This->dual)
2054 This->dual->typedata = This->typedata;
2055 } else {
2056 unsigned int j;
2057
2058 iter = This->typedata->next;
2059 for (j = 0; j < index; j++)
2060 iter = iter->next;
2061
2062 insert->next = iter->next;
2063 iter->next = insert;
2064 }
2065
2066 /* update type data size */
2067 This->typedata->next->u.val += FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
2068
2069 /* Increment the number of function elements */
2070 This->typeinfo->cElement += 1;
2071
2072 return S_OK;
2073 }
2074
2075 /******************************************************************************
2076 * ICreateTypeInfo2_AddImplType {OLEAUT32}
2077 */
2078 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
2079 ICreateTypeInfo2* iface,
2080 UINT index,
2081 HREFTYPE hRefType)
2082 {
2083 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2084
2085 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
2086
2087 if (This->typekind == TKIND_COCLASS) {
2088 int offset;
2089 MSFT_RefRecord *ref;
2090
2091 if (index == 0) {
2092 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
2093
2094 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2095 if (offset == -1) return E_OUTOFMEMORY;
2096
2097 This->typeinfo->datatype1 = offset;
2098 } else {
2099 int lastoffset;
2100
2101 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
2102 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
2103
2104 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
2105 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
2106
2107 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2108 if (offset == -1) return E_OUTOFMEMORY;
2109
2110 ref->onext = offset;
2111 }
2112
2113 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2114
2115 ref->reftype = hRefType;
2116 ref->flags = 0;
2117 ref->oCustData = -1;
2118 ref->onext = -1;
2119 This->typeinfo->cImplTypes++;
2120 } else if (This->typekind == TKIND_INTERFACE) {
2121 if (This->typeinfo->cImplTypes && index==1)
2122 return TYPE_E_BADMODULEKIND;
2123
2124 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
2125
2126 This->typeinfo->datatype1 = hRefType;
2127 This->typeinfo->cImplTypes = 1;
2128 } else if (This->typekind == TKIND_DISPATCH) {
2129 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
2130
2131 /* FIXME: Check if referenced typeinfo is IDispatch */
2132 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2133 This->typeinfo->cImplTypes = 1;
2134 } else {
2135 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
2136 return E_OUTOFMEMORY;
2137 }
2138
2139 return S_OK;
2140 }
2141
2142 /******************************************************************************
2143 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
2144 */
2145 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
2146 ICreateTypeInfo2* iface,
2147 UINT index,
2148 INT implTypeFlags)
2149 {
2150 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2151 int offset;
2152 MSFT_RefRecord *ref;
2153
2154 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
2155
2156 if (This->typekind != TKIND_COCLASS) {
2157 return TYPE_E_BADMODULEKIND;
2158 }
2159
2160 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2161 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2162
2163 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2164 ref->flags = implTypeFlags;
2165
2166 return S_OK;
2167 }
2168
2169 /******************************************************************************
2170 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2171 */
2172 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2173 ICreateTypeInfo2* iface,
2174 WORD cbAlignment)
2175 {
2176 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2177
2178 TRACE("(%p,%d)\n", iface, cbAlignment);
2179
2180 if (!cbAlignment) return E_INVALIDARG;
2181 if (cbAlignment > 16) return E_INVALIDARG;
2182
2183 This->typeinfo->typekind &= ~0xffc0;
2184 This->typeinfo->typekind |= cbAlignment << 6;
2185
2186 /* FIXME: There's probably some way to simplify this. */
2187 switch (This->typekind) {
2188 case TKIND_ALIAS:
2189 default:
2190 break;
2191
2192 case TKIND_ENUM:
2193 case TKIND_INTERFACE:
2194 case TKIND_DISPATCH:
2195 case TKIND_COCLASS:
2196 if (cbAlignment > 4) cbAlignment = 4;
2197 break;
2198
2199 case TKIND_RECORD:
2200 case TKIND_MODULE:
2201 case TKIND_UNION:
2202 cbAlignment = 1;
2203 break;
2204 }
2205
2206 This->typeinfo->typekind |= cbAlignment << 11;
2207
2208 return S_OK;
2209 }
2210
2211 /******************************************************************************
2212 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2213 */
2214 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2215 ICreateTypeInfo2* iface,
2216 LPOLESTR pStrSchema)
2217 {
2218 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2219 return E_OUTOFMEMORY;
2220 }
2221
2222 /******************************************************************************
2223 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2224 */
2225 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2226 ICreateTypeInfo2* iface,
2227 UINT index,
2228 VARDESC* pVarDesc)
2229 {
2230 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2231
2232 HRESULT status = S_OK;
2233 CyclicList *insert;
2234 INT *typedata;
2235 int var_datawidth;
2236 int var_alignment;
2237 int var_type_size;
2238 int alignment;
2239
2240 TRACE("(%p,%d,%p)\n", iface, index, pVarDesc);
2241 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2242 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2243 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2244 pVarDesc->wVarFlags, pVarDesc->varkind);
2245
2246 if (cti2_get_var_count(This->typeinfo) != index)
2247 return TYPE_E_ELEMENTNOTFOUND;
2248
2249 if (!This->typedata) {
2250 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
2251 if(!This->typedata)
2252 return E_OUTOFMEMORY;
2253
2254 This->typedata->next = This->typedata;
2255
2256 if(This->dual)
2257 This->dual->typedata = This->typedata;
2258 }
2259
2260 insert = alloc_cyclic_list_item(CyclicListVar);
2261 if(!insert)
2262 return E_OUTOFMEMORY;
2263
2264 /* allocate whole structure, it's fixed size always */
2265 insert->u.data = heap_alloc(sizeof(MSFT_VarRecord));
2266 if(!insert->u.data) {
2267 heap_free(insert);
2268 return E_OUTOFMEMORY;
2269 }
2270
2271 insert->next = This->typedata->next;
2272 This->typedata->next = insert;
2273 This->typedata = insert;
2274
2275 if(This->dual)
2276 This->dual->typedata = This->typedata;
2277
2278 This->typedata->next->u.val += FIELD_OFFSET(MSFT_VarRecord, HelpContext);
2279 typedata = This->typedata->u.data;
2280
2281 /* fill out the basic type information */
2282
2283 /* no optional fields initially */
2284 typedata[0] = FIELD_OFFSET(MSFT_VarRecord, HelpContext) | (index << 16);
2285 typedata[2] = pVarDesc->wVarFlags;
2286 typedata[3] = (sizeof(VARDESC) << 16) | pVarDesc->varkind;
2287
2288 /* update the index data */
2289 insert->indice = 0x40000000 + index;
2290 insert->name = -1;
2291
2292 /* figure out type widths and whatnot */
2293 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2294 &typedata[1], &var_datawidth, &var_alignment,
2295 &var_type_size);
2296
2297 if (pVarDesc->varkind != VAR_CONST)
2298 {
2299 /* pad out starting position to data width */
2300 This->datawidth += var_alignment - 1;
2301 This->datawidth &= ~(var_alignment - 1);
2302 typedata[4] = This->datawidth;
2303
2304 /* add the new variable to the total data width */
2305 This->datawidth += var_datawidth;
2306 if(This->dual)
2307 This->dual->datawidth = This->datawidth;
2308
2309 /* add type description size to total required allocation */
2310 typedata[3] += var_type_size << 16;
2311
2312 /* fix type alignment */
2313 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2314 if (alignment < var_alignment) {
2315 alignment = var_alignment;
2316 This->typeinfo->typekind &= ~0xf800;
2317 This->typeinfo->typekind |= alignment << 11;
2318 }
2319
2320 /* ??? */
2321 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2322 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2323 This->typeinfo->res2 <<= 1;
2324 }
2325
2326 /* ??? */
2327 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2328 This->typeinfo->res3 += 0x2c;
2329
2330 /* pad data width to alignment */
2331 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2332 } else {
2333 VARIANT *value = pVarDesc->DUMMYUNIONNAME.lpvarValue;
2334 status = ctl2_encode_variant(This->typelib, typedata+4, value, V_VT(value));
2335 /* ??? native sets size 0x34 */
2336 typedata[3] += 0x10 << 16;
2337 }
2338
2339 /* increment the number of variable elements */
2340 This->typeinfo->cElement += 0x10000;
2341
2342 return status;
2343 }
2344
2345 /******************************************************************************
2346 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2347 */
2348 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2349 ICreateTypeInfo2* iface,
2350 UINT index,
2351 LPOLESTR* names,
2352 UINT cNames)
2353 {
2354 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2355 CyclicList *iter, *iter2;
2356 int offset, len, i;
2357 unsigned char *namedata;
2358
2359 TRACE("(%p %d %p %d)\n", This, index, names, cNames);
2360
2361 if(!names)
2362 return E_INVALIDARG;
2363
2364 if(index >= cti2_get_func_count(This->typeinfo) || cNames == 0)
2365 return TYPE_E_ELEMENTNOTFOUND;
2366
2367 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2368 if (iter->type == CyclicListFunc)
2369 if (i++ >= index)
2370 break;
2371
2372 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2373 if(cNames != iter->u.data[5] + (ctl2_get_invokekind(iter) & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2374 return TYPE_E_ELEMENTNOTFOUND;
2375
2376 TRACE("function name %s\n", debugstr_w(names[0]));
2377 len = ctl2_encode_name(This->typelib, names[0], (char**)&namedata);
2378 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2379
2380 int cmp = memcmp(namedata, This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len);
2381 if (iter2->name != -1 && cmp == 0) {
2382 if (iter2->type == CyclicListFunc) {
2383 INVOKEKIND inv1 = ctl2_get_invokekind(iter);
2384 INVOKEKIND inv2 = ctl2_get_invokekind(iter2);
2385
2386 /* it's allowed to have PUT, PUTREF and GET methods with the same name */
2387 if ((inv1 != inv2) &&
2388 (inv1 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)) &&
2389 (inv2 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)))
2390 continue;
2391 }
2392
2393 return TYPE_E_AMBIGUOUSNAME;
2394 }
2395 }
2396
2397 offset = ctl2_alloc_name(This->typelib, names[0]);
2398 if(offset == -1)
2399 return E_OUTOFMEMORY;
2400
2401 iter->name = offset;
2402
2403 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2404 if (*((INT*)namedata) == -1)
2405 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2406
2407 len = ctl2_get_record_size(iter)/4 - iter->u.data[5]*3;
2408
2409 for (i = 1; i < cNames; i++) {
2410 offset = ctl2_alloc_name(This->typelib, names[i]);
2411 iter->u.data[len + ((i-1)*3) + 1] = offset;
2412 }
2413
2414 return S_OK;
2415 }
2416
2417 /******************************************************************************
2418 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2419 */
2420 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2421 ICreateTypeInfo2* iface,
2422 UINT index,
2423 LPOLESTR szName)
2424 {
2425 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2426 CyclicList *iter;
2427 int offset, i;
2428 unsigned char *namedata;
2429
2430 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(szName));
2431
2432 if (cti2_get_var_count(This->typeinfo) <= index)
2433 return TYPE_E_ELEMENTNOTFOUND;
2434
2435 offset = ctl2_alloc_name(This->typelib, szName);
2436 if (offset == -1) return E_OUTOFMEMORY;
2437
2438 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2439 if (*((INT *)namedata) == -1) {
2440 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2441 namedata[9] |= 0x10;
2442 }
2443 if (This->typekind == TKIND_ENUM) {
2444 namedata[9] |= 0x20;
2445 }
2446
2447 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2448 if (iter->type == CyclicListVar)
2449 if (i++ >= index)
2450 break;
2451
2452 iter->name = offset;
2453 return S_OK;
2454 }
2455
2456 /******************************************************************************
2457 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2458 */
2459 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2460 ICreateTypeInfo2* iface,
2461 TYPEDESC* pTDescAlias)
2462 {
2463 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2464
2465 int encoded_typedesc;
2466 int width;
2467
2468 if (This->typekind != TKIND_ALIAS) {
2469 return TYPE_E_WRONGTYPEKIND;
2470 }
2471
2472 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2473
2474 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2475 return E_OUTOFMEMORY;
2476 }
2477
2478 This->typeinfo->size = width;
2479 This->typeinfo->datatype1 = encoded_typedesc;
2480
2481 return S_OK;
2482 }
2483
2484 /******************************************************************************
2485 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2486 */
2487 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2488 ICreateTypeInfo2* iface,
2489 UINT index,
2490 LPOLESTR szDllName,
2491 LPOLESTR szProcName)
2492 {
2493 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2494 return E_OUTOFMEMORY;
2495 }
2496
2497 /******************************************************************************
2498 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2499 */
2500 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2501 ICreateTypeInfo2* iface,
2502 UINT index,
2503 LPOLESTR szDocString)
2504 {
2505 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2506 return E_OUTOFMEMORY;
2507 }
2508
2509 /******************************************************************************
2510 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2511 */
2512 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2513 ICreateTypeInfo2* iface,
2514 UINT index,
2515 LPOLESTR docstring)
2516 {
2517 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2518 CyclicList *iter;
2519
2520 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(docstring));
2521
2522 if (!docstring) return E_INVALIDARG;
2523
2524 if (cti2_get_var_count(This->typeinfo) <= index)
2525 return TYPE_E_ELEMENTNOTFOUND;
2526
2527 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2528 if (iter->type == CyclicListVar)
2529 {
2530 if (index-- == 0)
2531 {
2532 int offset = ctl2_alloc_string(This->typelib, docstring);
2533
2534 if (offset == -1) return E_OUTOFMEMORY;
2535 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, res9));
2536 iter->u.data[6] = offset;
2537 return S_OK;
2538 }
2539 }
2540
2541 return TYPE_E_ELEMENTNOTFOUND;
2542 }
2543
2544 /******************************************************************************
2545 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2546 */
2547 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2548 ICreateTypeInfo2* iface,
2549 UINT index,
2550 DWORD dwHelpContext)
2551 {
2552 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2553 CyclicList *func;
2554
2555 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2556
2557 if(cti2_get_func_count(This->typeinfo) < index)
2558 return TYPE_E_ELEMENTNOTFOUND;
2559
2560 if(cti2_get_func_count(This->typeinfo) == index && This->typedata->type == CyclicListFunc)
2561 func = This->typedata;
2562 else
2563 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2564 if (func->type == CyclicListFunc)
2565 if(index-- == 0)
2566 break;
2567
2568 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2569 if(!func->u.data)
2570 return E_OUTOFMEMORY;
2571
2572 func->u.data[6] = dwHelpContext;
2573 return S_OK;
2574 }
2575
2576 /******************************************************************************
2577 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2578 */
2579 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2580 ICreateTypeInfo2* iface,
2581 UINT index,
2582 DWORD context)
2583 {
2584 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2585 CyclicList *iter;
2586
2587 TRACE("(%p,%d,%d)\n", This, index, context);
2588
2589 if (cti2_get_var_count(This->typeinfo) <= index)
2590 return TYPE_E_ELEMENTNOTFOUND;
2591
2592 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2593 if (iter->type == CyclicListVar)
2594 {
2595 if (index-- == 0)
2596 {
2597 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, HelpString));
2598 iter->u.data[5] = context;
2599 return S_OK;
2600 }
2601 }
2602
2603 return TYPE_E_ELEMENTNOTFOUND;
2604 }
2605
2606 /******************************************************************************
2607 * ICreateTypeInfo2_SetMops {OLEAUT32}
2608 */
2609 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2610 ICreateTypeInfo2* iface,
2611 UINT index,
2612 BSTR bstrMops)
2613 {
2614 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2615 return E_OUTOFMEMORY;
2616 }
2617
2618 /******************************************************************************
2619 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2620 */
2621 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2622 ICreateTypeInfo2* iface,
2623 IDLDESC* pIdlDesc)
2624 {
2625 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2626 return E_OUTOFMEMORY;
2627 }
2628
2629 /******************************************************************************
2630 * ICreateTypeInfo2_LayOut {OLEAUT32}
2631 */
2632 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2633 ICreateTypeInfo2* iface)
2634 {
2635 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2636 CyclicList *iter, *iter2, *last = NULL, **typedata;
2637 HREFTYPE hreftype;
2638 HRESULT hres;
2639 unsigned user_vft = 0;
2640 int i;
2641
2642 TRACE("(%p)\n", This);
2643
2644 /* FIXME: LayOut should be run on all ImplTypes */
2645 if(This->typekind == TKIND_COCLASS || This->typekind == TKIND_ALIAS)
2646 return S_OK;
2647
2648 /* Validate inheritance */
2649 This->typeinfo->datatype2 = 0;
2650 hreftype = This->typeinfo->datatype1;
2651
2652 /* Process internally defined interfaces */
2653 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2654 MSFT_TypeInfoBase *header;
2655
2656 if(hreftype&1)
2657 break;
2658
2659 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2660 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2661 hreftype = header->datatype1;
2662 }
2663 if(i == This->typelib->typelib_header.nrtypeinfos)
2664 return TYPE_E_CIRCULARTYPE;
2665
2666 /* Process externally defined interfaces */
2667 if(hreftype != -1) {
2668 ITypeInfo *cur, *next;
2669 TYPEATTR *typeattr;
2670
2671 hres = ICreateTypeInfo2_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2672 if(FAILED(hres))
2673 return hres;
2674
2675 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2676 ITypeInfo_Release(next);
2677 if(FAILED(hres))
2678 return hres;
2679
2680
2681 while(1) {
2682 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2683 if(FAILED(hres)) {
2684 ITypeInfo_Release(cur);
2685 return hres;
2686 }
2687
2688 if(IsEqualGUID(&typeattr->guid, &IID_IDispatch))
2689 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2690
2691 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2692 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2693
2694 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2695 if(hres == TYPE_E_ELEMENTNOTFOUND)
2696 break;
2697 if(FAILED(hres)) {
2698 ITypeInfo_Release(cur);
2699 return hres;
2700 }
2701
2702 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2703 if(FAILED(hres)) {
2704 ITypeInfo_Release(cur);
2705 return hres;
2706 }
2707
2708 ITypeInfo_Release(cur);
2709 cur = next;
2710 }
2711 ITypeInfo_Release(cur);
2712 }
2713
2714 /* Get cbSizeVft of inherited interface */
2715 /* Makes LayOut running recursively */
2716 if(This->typeinfo->datatype1 != -1) {
2717 ITypeInfo *cur, *inherited;
2718 TYPEATTR *typeattr;
2719
2720 hres = ICreateTypeInfo2_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2721 if(FAILED(hres))
2722 return hres;
2723
2724 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2725 ITypeInfo_Release(cur);
2726 if(FAILED(hres))
2727 return hres;
2728
2729 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2730 if(FAILED(hres)) {
2731 ITypeInfo_Release(inherited);
2732 return hres;
2733 }
2734
2735 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2736
2737 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2738 ITypeInfo_Release(inherited);
2739 } else
2740 This->typeinfo->cbSizeVft = 0;
2741
2742 if(!This->typedata)
2743 return S_OK;
2744
2745 typedata = heap_alloc(sizeof(CyclicList*)*cti2_get_func_count(This->typeinfo));
2746 if(!typedata)
2747 return E_OUTOFMEMORY;
2748
2749 /* Assign IDs and VTBL entries */
2750 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2751 if (iter->type == CyclicListFunc)
2752 last = iter;
2753
2754 if(last && last->u.data[3]&1)
2755 user_vft = last->u.data[3]&0xffff;
2756
2757 i = 0;
2758 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2759 /* Assign MEMBERID if MEMBERID_NIL was specified */
2760 if(iter->indice == MEMBERID_NIL) {
2761 iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2762
2763 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2764 if(iter == iter2) continue;
2765 if(iter2->indice == iter->indice) {
2766 iter->indice = 0x60000000 + This->typeinfo->cElement + (This->typeinfo->datatype2<<16);
2767
2768 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2769 if(iter == iter2) continue;
2770 if(iter2->indice == iter->indice) {
2771 ++iter->indice;
2772 iter2 = This->typedata->next;
2773 }
2774 }
2775
2776 break;
2777 }
2778 }
2779 }
2780
2781 if (iter->type != CyclicListFunc)
2782 continue;
2783
2784 typedata[i] = iter;
2785
2786 iter->u.data[0] = ctl2_get_record_size(iter) | (i<<16);
2787
2788 if((iter->u.data[3]&1) != (user_vft&1)) {
2789 heap_free(typedata);
2790 return TYPE_E_INVALIDID;
2791 }
2792
2793 if(user_vft&1) {
2794 if(user_vft < (iter->u.data[3]&0xffff))
2795 user_vft = (iter->u.data[3]&0xffff);
2796
2797 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2798 heap_free(typedata);
2799 return TYPE_E_INVALIDID;
2800 }
2801 } else if(This->typekind != TKIND_MODULE) {
2802 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2803 This->typeinfo->cbSizeVft += 4;
2804 }
2805
2806 /* Construct a list of elements with the same memberid */
2807 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2808 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2809 if(iter->indice == iter2->indice) {
2810 int v1, v2;
2811
2812 v1 = iter->u.data[4] >> 16;
2813 v2 = iter2->u.data[4] >> 16;
2814
2815 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2816 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2817 break;
2818 }
2819 }
2820
2821 i++;
2822 }
2823
2824 if(user_vft)
2825 This->typeinfo->cbSizeVft = user_vft+3;
2826
2827 for(i=0; i< cti2_get_func_count(This->typeinfo); i++) {
2828 if(typedata[i]->u.data[4]>>16 > i) {
2829 INVOKEKIND inv = ctl2_get_invokekind(typedata[i]);
2830
2831 i = typedata[i]->u.data[4] >> 16;
2832
2833 while(i > typedata[i]->u.data[4]>>16) {
2834 INVOKEKIND invkind = ctl2_get_invokekind(typedata[i]);
2835
2836 if(inv & invkind) {
2837 heap_free(typedata);
2838 return TYPE_E_DUPLICATEID;
2839 }
2840
2841 i = typedata[i]->u.data[4] >> 16;
2842 inv |= invkind;
2843 }
2844
2845 if(inv & INVOKE_FUNC) {
2846 heap_free(typedata);
2847 return TYPE_E_INCONSISTENTPROPFUNCS;
2848 }
2849 }
2850 }
2851
2852 heap_free(typedata);
2853 return S_OK;
2854 }
2855
2856 /******************************************************************************
2857 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2858 *
2859 * Delete a function description from a type.
2860 *
2861 * RETURNS
2862 *
2863 * Success: S_OK.
2864 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2865 */
2866 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2867 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2868 UINT index) /* [I] The index of the function to delete. */
2869 {
2870 FIXME("(%p,%d), stub!\n", iface, index);
2871 return E_OUTOFMEMORY;
2872 }
2873
2874 /******************************************************************************
2875 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2876 *
2877 * Delete a function description from a type.
2878 *
2879 * RETURNS
2880 *
2881 * Success: S_OK.
2882 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2883 */
2884 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2885 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2886 MEMBERID memid, /* [I] The member id of the function to delete. */
2887 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2888 {
2889 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2890 return E_OUTOFMEMORY;
2891 }
2892
2893 /******************************************************************************
2894 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2895 *
2896 * Delete a variable description from a type.
2897 *
2898 * RETURNS
2899 *
2900 * Success: S_OK.
2901 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2902 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2903 */
2904 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2905 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2906 UINT index) /* [I] The index of the variable description to delete. */
2907 {
2908 FIXME("(%p,%d), stub!\n", iface, index);
2909 return E_OUTOFMEMORY;
2910 }
2911
2912 /******************************************************************************
2913 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2914 *
2915 * Delete a variable description from a type.
2916 *
2917 * RETURNS
2918 *
2919 * Success: S_OK.
2920 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2921 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2922 */
2923 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2924 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2925 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2926 {
2927 FIXME("(%p,%d), stub!\n", iface, memid);
2928 return E_OUTOFMEMORY;
2929 }
2930
2931 /******************************************************************************
2932 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2933 *
2934 * Delete an interface implementation from a type. (?)
2935 *
2936 * RETURNS
2937 *
2938 * Success: S_OK.
2939 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2940 */
2941 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2942 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2943 UINT index) /* [I] The index of the interface to delete. */
2944 {
2945 FIXME("(%p,%d), stub!\n", iface, index);
2946 return E_OUTOFMEMORY;
2947 }
2948
2949 /******************************************************************************
2950 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2951 *
2952 * Set the custom data for a type.
2953 *
2954 * RETURNS
2955 *
2956 * Success: S_OK.
2957 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2958 */
2959 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2960 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2961 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2962 VARIANT* pVarVal) /* [I] The custom data. */
2963 {
2964 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2965
2966 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2967
2968 if (!pVarVal)
2969 return E_INVALIDARG;
2970
2971 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2972 }
2973
2974 /******************************************************************************
2975 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2976 *
2977 * Set the custom data for a function.
2978 *
2979 * RETURNS
2980 *
2981 * Success: S_OK.
2982 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2983 */
2984 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2985 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2986 UINT index, /* [I] The index of the function for which to set the custom data. */
2987 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2988 VARIANT* pVarVal) /* [I] The custom data. */
2989 {
2990 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2991 CyclicList *iter;
2992
2993 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2994
2995 if(index >= cti2_get_func_count(This->typeinfo))
2996 return TYPE_E_ELEMENTNOTFOUND;
2997
2998 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2999 if (iter->type == CyclicListFunc)
3000 if (index-- == 0)
3001 break;
3002
3003 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
3004 if(!iter->u.data)
3005 return E_OUTOFMEMORY;
3006
3007 iter->u.data[4] |= 0x80;
3008 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
3009 }
3010
3011 /******************************************************************************
3012 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
3013 *
3014 * Set the custom data for a function parameter.
3015 *
3016 * RETURNS
3017 *
3018 * Success: S_OK.
3019 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3020 */
3021 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
3022 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3023 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
3024 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
3025 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3026 VARIANT* pVarVal) /* [I] The custom data. */
3027 {
3028 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3029 return E_OUTOFMEMORY;
3030 }
3031
3032 /******************************************************************************
3033 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
3034 *
3035 * Set the custom data for a variable.
3036 *
3037 * RETURNS
3038 *
3039 * Success: S_OK.
3040 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3041 */
3042 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
3043 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3044 UINT index, /* [I] The index of the variable on which to set the custom data. */
3045 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3046 VARIANT* pVarVal) /* [I] The custom data. */
3047 {
3048 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3049 return E_OUTOFMEMORY;
3050 }
3051
3052 /******************************************************************************
3053 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
3054 *
3055 * Set the custom data for an implemented interface.
3056 *
3057 * RETURNS
3058 *
3059 * Success: S_OK.
3060 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3061 */
3062 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
3063 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
3064 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
3065 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3066 VARIANT* pVarVal) /* [I] The custom data. */
3067 {
3068 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3069 return E_OUTOFMEMORY;
3070 }
3071
3072 /******************************************************************************
3073 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
3074 *
3075 * Set the help string context for the typeinfo.
3076 *
3077 * RETURNS
3078 *
3079 * Success: S_OK.
3080 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3081 */
3082 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
3083 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3084 ULONG helpcontext) /* [I] The help string context. */
3085 {
3086 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
3087
3088 TRACE("(%p, %d)\n", iface, helpcontext);
3089
3090 This->typelib->typelib_header.helpcontext = helpcontext;
3091
3092 return S_OK;
3093 }
3094
3095 /******************************************************************************
3096 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
3097 *
3098 * Set the help string context for a function.
3099 *
3100 * RETURNS
3101 *
3102 * Success: S_OK.
3103 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3104 */
3105 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
3106 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3107 UINT index, /* [I] The index for the function on which to set the help string context. */
3108 ULONG dwHelpStringContext) /* [I] The help string context. */
3109 {
3110 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3111 return E_OUTOFMEMORY;
3112 }
3113
3114 /******************************************************************************
3115 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
3116 *
3117 * Set the help string context for a variable.
3118 *
3119 * RETURNS
3120 *
3121 * Success: S_OK.
3122 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3123 */
3124 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
3125 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3126 UINT index, /* [I] The index of the variable on which to set the help string context. */
3127 ULONG dwHelpStringContext) /* [I] The help string context */
3128 {
3129 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3130 return E_OUTOFMEMORY;
3131 }
3132
3133 /******************************************************************************
3134 * ICreateTypeInfo2_Invalidate {OLEAUT32}
3135 *
3136 * Undocumented function. (!)
3137 */
3138 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
3139 ICreateTypeInfo2* iface)
3140 {
3141 FIXME("(%p), stub!\n", iface);
3142 return E_OUTOFMEMORY;
3143 }
3144
3145 /******************************************************************************
3146 * ICreateTypeInfo2_SetName {OLEAUT32}
3147 *
3148 * Set the name for a typeinfo.
3149 *
3150 * RETURNS
3151 *
3152 * Success: S_OK.
3153 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
3154 */
3155 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
3156 ICreateTypeInfo2* iface,
3157 LPOLESTR szName)
3158 {
3159 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
3160 return E_OUTOFMEMORY;
3161 }
3162
3163 /*================== ITypeInfo2 Implementation ===================================*/
3164
3165 /******************************************************************************
3166 * ITypeInfo2_QueryInterface {OLEAUT32}
3167 */
3168 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
3169 {
3170 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3171
3172 return ICreateTypeInfo2_QueryInterface(&This->ICreateTypeInfo2_iface, riid, ppv);
3173 }
3174
3175 /******************************************************************************
3176 * ITypeInfo2_AddRef {OLEAUT32}
3177 */
3178 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3179 {
3180 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3181
3182 return ICreateTypeInfo2_AddRef(&This->ICreateTypeInfo2_iface);
3183 }
3184
3185 /******************************************************************************
3186 * ITypeInfo2_Release {OLEAUT32}
3187 */
3188 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3189 {
3190 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3191
3192 return ICreateTypeInfo2_Release(&This->ICreateTypeInfo2_iface);
3193 }
3194
3195 /******************************************************************************
3196 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3197 */
3198 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3199 ITypeInfo2* iface,
3200 TYPEATTR** ppTypeAttr)
3201 {
3202 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3203 HRESULT hres;
3204
3205 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3206
3207 if(!ppTypeAttr)
3208 return E_INVALIDARG;
3209
3210 hres = ICreateTypeInfo2_LayOut(&This->ICreateTypeInfo2_iface);
3211 if(FAILED(hres))
3212 return hres;
3213
3214 *ppTypeAttr = heap_alloc_zero(sizeof(TYPEATTR));
3215 if(!*ppTypeAttr)
3216 return E_OUTOFMEMORY;
3217
3218 if(This->typeinfo->posguid != -1) {
3219 MSFT_GuidEntry *guid;
3220
3221 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3222 (*ppTypeAttr)->guid = guid->guid;
3223 }
3224
3225 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3226 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3227 (*ppTypeAttr)->typekind = This->typekind;
3228 (*ppTypeAttr)->cFuncs = cti2_get_func_count(This->typeinfo);
3229 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3230 (*ppTypeAttr)->cFuncs += sizeof(IDispatchVtbl)/sizeof(void*);
3231 (*ppTypeAttr)->cVars = cti2_get_var_count(This->typeinfo);
3232 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3233 (*ppTypeAttr)->cbSizeVft = This->typekind == TKIND_DISPATCH ? sizeof(IDispatchVtbl) : This->typeinfo->cbSizeVft;
3234 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3235 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3236 (*ppTypeAttr)->wMajorVerNum = LOWORD(This->typeinfo->version);
3237 (*ppTypeAttr)->wMinorVerNum = HIWORD(This->typeinfo->version);
3238
3239 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3240 FIXME("TKIND_ALIAS handling not implemented\n");
3241
3242 return S_OK;
3243 }
3244
3245 /******************************************************************************
3246 * ITypeInfo2_GetTypeComp {OLEAUT32}
3247 */
3248 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3249 ITypeInfo2* iface,
3250 ITypeComp** ppTComp)
3251 {
3252 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3253 return E_OUTOFMEMORY;
3254 }
3255
3256 /******************************************************************************
3257 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3258 */
3259 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3260 ITypeInfo2* iface,
3261 UINT index,
3262 FUNCDESC** ppFuncDesc)
3263 {
3264 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3265 int i, *typedata, num_defaults = 0, hdr_len, tail, has_defaults;
3266 CyclicList *desc;
3267 HRESULT hres;
3268
3269 TRACE("(%p,%d,%p), semi-stub\n", iface, index, ppFuncDesc);
3270
3271 if (!ppFuncDesc)
3272 return E_INVALIDARG;
3273
3274 if (index >= cti2_get_func_count(This->typeinfo))
3275 return TYPE_E_ELEMENTNOTFOUND;
3276
3277 hres = ICreateTypeInfo2_LayOut(&This->ICreateTypeInfo2_iface);
3278 if (FAILED(hres))
3279 return hres;
3280
3281 desc = This->typedata->next;
3282 for (i = index; i >= 0; ) {
3283 desc = desc->next;
3284 if (desc->type == CyclicListFunc)
3285 --i;
3286 }
3287
3288 typedata = desc->u.data;
3289
3290 *ppFuncDesc = heap_alloc_zero(sizeof(FUNCDESC));
3291 if (!*ppFuncDesc)
3292 return E_OUTOFMEMORY;
3293
3294 (*ppFuncDesc)->memid = desc->indice;
3295 (*ppFuncDesc)->lprgscode = NULL; /* FIXME: Unimplemented */
3296 (*ppFuncDesc)->funckind = typedata[4] & 0x7;
3297 (*ppFuncDesc)->invkind = (typedata[4] >> 3) & 0xF;
3298 (*ppFuncDesc)->callconv = (typedata[4] >> 8) & 0xF;
3299 (*ppFuncDesc)->cParams = typedata[5];
3300 (*ppFuncDesc)->cParamsOpt = 0; /* FIXME: Unimplemented*/
3301 (*ppFuncDesc)->oVft = typedata[3] & 0xFFFF;
3302 if ((*ppFuncDesc)->oVft)
3303 --(*ppFuncDesc)->oVft;
3304 (*ppFuncDesc)->cScodes = 0; /* FIXME: Unimplemented*/
3305 hres = ctl2_decode_typedesc(This->typelib, typedata[1],
3306 &(*ppFuncDesc)->elemdescFunc.tdesc);
3307 if (FAILED(hres)) {
3308 heap_free(*ppFuncDesc);
3309 return hres;
3310 }
3311 (*ppFuncDesc)->wFuncFlags = typedata[2];
3312
3313 has_defaults = typedata[4] & 0x1000;
3314 tail = typedata[5] * (has_defaults ? 16 : 12);
3315 hdr_len = (ctl2_get_record_size(desc) - tail) / sizeof(int);
3316
3317 if ((*ppFuncDesc)->cParams > 0) {
3318 (*ppFuncDesc)->lprgelemdescParam = heap_alloc_zero((*ppFuncDesc)->cParams * sizeof(ELEMDESC));
3319 if (!(*ppFuncDesc)->lprgelemdescParam) {
3320 heap_free(*ppFuncDesc);
3321 return E_OUTOFMEMORY;
3322 }
3323 if (has_defaults) {
3324 num_defaults = (*ppFuncDesc)->cParams;
3325
3326 for (i = 0; i < num_defaults; ++i) {
3327 if (typedata[hdr_len + i] != 0xFFFFFFFF) {
3328 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags |= PARAMFLAG_FHASDEFAULT;
3329
3330 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex = heap_alloc(sizeof(PARAMDESCEX));
3331 if (!(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex) {
3332 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3333 return E_OUTOFMEMORY;
3334 }
3335
3336 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->cBytes = sizeof(PARAMDESCEX);
3337 VariantInit(&(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3338 hres = ctl2_decode_variant(This->typelib, typedata[hdr_len + i],
3339 &(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3340 if (FAILED(hres)) {
3341 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3342 return hres;
3343 }
3344 }
3345 }
3346 }
3347
3348 for (i = 0; i < (*ppFuncDesc)->cParams; ++i) {
3349 hres = ctl2_decode_typedesc(This->typelib, typedata[hdr_len + num_defaults + (i * 3)],
3350 &((*ppFuncDesc)->lprgelemdescParam + i)->tdesc);
3351 if (FAILED(hres)) {
3352 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3353 return hres;
3354 }
3355 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags = typedata[hdr_len + num_defaults + (i * 3) + 2];
3356 }
3357 }
3358
3359 return S_OK;
3360 }
3361
3362 /******************************************************************************
3363 * ITypeInfo2_GetVarDesc {OLEAUT32}
3364 */
3365 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3366 ITypeInfo2* iface,
3367 UINT index,
3368 VARDESC** ppVarDesc)
3369 {
3370 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3371 return E_OUTOFMEMORY;
3372 }
3373
3374 /******************************************************************************
3375 * ITypeInfo2_GetNames {OLEAUT32}
3376 */
3377 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3378 ITypeInfo2* iface,
3379 MEMBERID memid,
3380 BSTR* rgBstrNames,
3381 UINT cMaxNames,
3382 UINT* pcNames)
3383 {
3384 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3385 return E_OUTOFMEMORY;
3386 }
3387
3388 /******************************************************************************
3389 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3390 */
3391 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3392 ITypeInfo2* iface,
3393 UINT index,
3394 HREFTYPE* pRefType)
3395 {
3396 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3397 MSFT_RefRecord *ref;
3398 int offset;
3399
3400 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3401
3402 if(!pRefType)
3403 return E_INVALIDARG;
3404
3405 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3406 if(index == -1) {
3407 *pRefType = -2;
3408 return S_OK;
3409 }
3410
3411 if(This->typekind == TKIND_DISPATCH)
3412 return ITypeInfo2_GetRefTypeOfImplType(&This->dual->ITypeInfo2_iface,
3413 index, pRefType);
3414 }
3415
3416 if(index>=This->typeinfo->cImplTypes)
3417 return TYPE_E_ELEMENTNOTFOUND;
3418
3419 if(This->typekind == TKIND_INTERFACE) {
3420 *pRefType = This->typeinfo->datatype1 + 2;
3421 return S_OK;
3422 }
3423
3424 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3425 if(offset == -1)
3426 return TYPE_E_ELEMENTNOTFOUND;
3427
3428 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3429 *pRefType = ref->reftype;
3430 return S_OK;
3431 }
3432
3433 /******************************************************************************
3434 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3435 */
3436 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3437 ITypeInfo2* iface,
3438 UINT index,
3439 INT* pImplTypeFlags)
3440 {
3441 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3442 int offset;
3443 MSFT_RefRecord *ref;
3444
3445 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3446
3447 if(!pImplTypeFlags)
3448 return E_INVALIDARG;
3449
3450 if(index >= This->typeinfo->cImplTypes)
3451 return TYPE_E_ELEMENTNOTFOUND;
3452
3453 if(This->typekind != TKIND_COCLASS) {
3454 *pImplTypeFlags = 0;
3455 return S_OK;
3456 }
3457
3458 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3459 if(offset == -1)
3460 return TYPE_E_ELEMENTNOTFOUND;
3461
3462 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3463 *pImplTypeFlags = ref->flags;
3464 return S_OK;
3465 }
3466
3467 /******************************************************************************
3468 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3469 */
3470 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3471 ITypeInfo2* iface,
3472 LPOLESTR* rgszNames,
3473 UINT cNames,
3474 MEMBERID* pMemId)
3475 {
3476 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3477 return E_OUTOFMEMORY;
3478 }
3479
3480 /******************************************************************************
3481 * ITypeInfo2_Invoke {OLEAUT32}
3482 */
3483 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3484 ITypeInfo2* iface,
3485 PVOID pvInstance,
3486 MEMBERID memid,
3487 WORD wFlags,
3488 DISPPARAMS* pDispParams,
3489 VARIANT* pVarResult,
3490 EXCEPINFO* pExcepInfo,
3491 UINT* puArgErr)
3492 {
3493 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3494 return E_OUTOFMEMORY;
3495 }
3496
3497 /******************************************************************************
3498 * ITypeInfo2_GetDocumentation {OLEAUT32}
3499 */
3500 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3501 ITypeInfo2* iface,
3502 MEMBERID memid,
3503 BSTR* pBstrName,
3504 BSTR* pBstrDocString,
3505 DWORD* pdwHelpContext,
3506 BSTR* pBstrHelpFile)
3507 {
3508 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3509 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3510 INT nameoffset, docstringoffset, helpcontext;
3511
3512 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3513
3514 if (memid == -1)
3515 {
3516 nameoffset = This->typeinfo->NameOffset;
3517 docstringoffset = This->typeinfo->docstringoffs;
3518 helpcontext = This->typeinfo->helpcontext;
3519 status = S_OK;
3520 } else {
3521 CyclicList *iter;
3522 if (This->typedata) {
3523 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3524 if (iter->indice == memid) {
3525 if (iter->type == CyclicListFunc) {
3526 const int *typedata = iter->u.data;
3527 int size = ctl2_get_record_size(iter) - typedata[5]*(typedata[4]&0x1000?16:12);
3528
3529 nameoffset = iter->name;
3530 /* FIXME implement this once SetFuncDocString is implemented */
3531 docstringoffset = -1;
3532 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3533
3534 status = S_OK;
3535 } else {
3536 FIXME("Not implemented for variable members\n");
3537 }
3538
3539 break;
3540 }
3541 }
3542 }
3543 }
3544
3545 if (!status) {
3546 WCHAR *string;
3547 if (pBstrName) {
3548 if (nameoffset == -1)
3549 *pBstrName = NULL;
3550 else {
3551 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3552 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3553 ctl2_decode_name((char*)&name->namelen, &string);
3554 *pBstrName = SysAllocString(string);
3555 if(!*pBstrName)
3556 return E_OUTOFMEMORY;
3557 }
3558 }
3559
3560 if (pBstrDocString) {
3561 if (docstringoffset == -1)
3562 *pBstrDocString = NULL;
3563 else {
3564 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3565 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3566 ctl2_decode_name((char*)&name->namelen, &string);
3567 *pBstrDocString = SysAllocString(string);
3568 if(!*pBstrDocString) {
3569 if (pBstrName) SysFreeString(*pBstrName);
3570 return E_OUTOFMEMORY;
3571 }
3572 }
3573 }
3574
3575 if (pdwHelpContext) {
3576 *pdwHelpContext = helpcontext;
3577 }
3578
3579 if (pBstrHelpFile) {
3580 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->ITypeLib2_iface,
3581 -1, NULL, NULL, NULL, pBstrHelpFile);
3582 if (status) {
3583 if (pBstrName) SysFreeString(*pBstrName);
3584 if (pBstrDocString) SysFreeString(*pBstrDocString);
3585 }
3586 }
3587 }
3588
3589 return status;
3590 }
3591
3592 /******************************************************************************
3593 * ITypeInfo2_GetDllEntry {OLEAUT32}
3594 */
3595 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3596 ITypeInfo2* iface,
3597 MEMBERID memid,
3598 INVOKEKIND invKind,
3599 BSTR* pBstrDllName,
3600 BSTR* pBstrName,
3601 WORD* pwOrdinal)
3602 {
3603 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3604 return E_OUTOFMEMORY;
3605 }
3606
3607 /******************************************************************************
3608 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3609 */
3610 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3611 ITypeInfo2* iface,
3612 HREFTYPE hRefType,
3613 ITypeInfo** ppTInfo)
3614 {
3615 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3616
3617 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3618
3619 if(!ppTInfo)
3620 return E_INVALIDARG;
3621
3622 if(hRefType==-2 && This->dual) {
3623 *ppTInfo = (ITypeInfo *)&This->dual->ITypeInfo2_iface;
3624 ITypeInfo_AddRef(*ppTInfo);
3625 return S_OK;
3626 }
3627
3628 if(hRefType&1) {
3629 ITypeLib *tl;
3630 MSFT_ImpInfo *impinfo;
3631 MSFT_ImpFile *impfile;
3632 MSFT_GuidEntry *guid;
3633 WCHAR *filename;
3634 HRESULT hres;
3635
3636 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3637 return E_FAIL;
3638
3639 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3640 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3641 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3642
3643 ctl2_decode_string((unsigned char*)impfile->filename, &filename);
3644
3645 hres = LoadTypeLib(filename, &tl);
3646 if(FAILED(hres))
3647 return hres;
3648
3649 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3650
3651 ITypeLib_Release(tl);
3652 return hres;
3653 } else {
3654 ICreateTypeInfo2Impl *iter;
3655 int i = 0;
3656
3657 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3658 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3659 *ppTInfo = (ITypeInfo *)&iter->ITypeInfo2_iface;
3660
3661 ITypeInfo_AddRef(*ppTInfo);
3662 return S_OK;
3663 }
3664 i++;
3665 }
3666 }
3667
3668 return E_FAIL;
3669 }
3670
3671 /******************************************************************************
3672 * ITypeInfo2_AddressOfMember {OLEAUT32}
3673 */
3674 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3675 ITypeInfo2* iface,
3676 MEMBERID memid,
3677 INVOKEKIND invKind,
3678 PVOID* ppv)
3679 {
3680 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3681 return E_OUTOFMEMORY;
3682 }
3683
3684 /******************************************************************************
3685 * ITypeInfo2_CreateInstance {OLEAUT32}
3686 */
3687 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3688 ITypeInfo2* iface,
3689 IUnknown* pUnkOuter,
3690 REFIID riid,
3691 PVOID* ppvObj)
3692 {
3693 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3694 return E_OUTOFMEMORY;
3695 }
3696
3697 /******************************************************************************
3698 * ITypeInfo2_GetMops {OLEAUT32}
3699 */
3700 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3701 ITypeInfo2* iface,
3702 MEMBERID memid,
3703 BSTR* pBstrMops)
3704 {
3705 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3706 return E_OUTOFMEMORY;
3707 }
3708
3709 /******************************************************************************
3710 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3711 */
3712 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3713 ITypeInfo2* iface,
3714 ITypeLib** ppTLib,
3715 UINT* pIndex)
3716 {
3717 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3718
3719 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3720
3721 *ppTLib = (ITypeLib *)&This->typelib->ITypeLib2_iface;
3722 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3723 *pIndex = This->typeinfo->typekind >> 16;
3724
3725 return S_OK;
3726 }
3727
3728 static void release_typedesc(TYPEDESC *tdesc)
3729 {
3730 while (tdesc) {
3731 TYPEDESC *next;
3732 if (tdesc->vt == VT_USERDEFINED)
3733 next = NULL;
3734 else
3735 next = tdesc->u.lptdesc;
3736 heap_free(tdesc);
3737 tdesc = next;
3738 }
3739 }
3740
3741 /******************************************************************************
3742 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3743 */
3744 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3745 ITypeInfo2* iface,
3746 TYPEATTR* pTypeAttr)
3747 {
3748 TRACE("(%p,%p)\n", iface, pTypeAttr);
3749
3750 if (pTypeAttr->tdescAlias.vt != VT_USERDEFINED)
3751 release_typedesc(pTypeAttr->tdescAlias.u.lptdesc);
3752
3753 heap_free(pTypeAttr);
3754 }
3755
3756 /******************************************************************************
3757 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3758 */
3759 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3760 ITypeInfo2* iface,
3761 FUNCDESC* pFuncDesc)
3762 {
3763 int i;
3764
3765 TRACE("(%p,%p)\n", iface, pFuncDesc);
3766
3767 heap_free(pFuncDesc->lprgscode);
3768
3769 if (pFuncDesc->lprgelemdescParam) {
3770 for (i = 0; i < pFuncDesc->cParams; ++i) {
3771 if (pFuncDesc->lprgelemdescParam[i].tdesc.vt != VT_USERDEFINED)
3772 release_typedesc(pFuncDesc->lprgelemdescParam[i].tdesc.u.lptdesc);
3773
3774 if (pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex)
3775 {
3776 VariantClear(&pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3777 heap_free(pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex);
3778 }
3779 }
3780 heap_free(pFuncDesc->lprgelemdescParam);
3781 }
3782
3783 heap_free(pFuncDesc->elemdescFunc.u.paramdesc.pparamdescex);
3784
3785 if (pFuncDesc->elemdescFunc.tdesc.vt != VT_USERDEFINED)
3786 release_typedesc(pFuncDesc->elemdescFunc.tdesc.u.lptdesc);
3787
3788 heap_free(pFuncDesc);
3789 }
3790
3791 /******************************************************************************
3792 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3793 */
3794 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3795 ITypeInfo2* iface,
3796 VARDESC* pVarDesc)
3797 {
3798 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3799 }
3800
3801 /******************************************************************************
3802 * ITypeInfo2_GetTypeKind {OLEAUT32}
3803 *
3804 * Get the TYPEKIND value for a TypeInfo.
3805 *
3806 * RETURNS
3807 *
3808 * Success: S_OK.
3809 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3810 */
3811 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3812 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3813 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3814 {
3815 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3816 return E_OUTOFMEMORY;
3817 }
3818
3819 /******************************************************************************
3820 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3821 *
3822 * Get the Type Flags for a TypeInfo.
3823 *
3824 * RETURNS
3825 *
3826 * Success: S_OK.
3827 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3828 */
3829 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3830 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3831 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3832 {
3833 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3834 return E_OUTOFMEMORY;
3835 }
3836
3837 /******************************************************************************
3838 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3839 *
3840 * Gets the index of a function given its member id.
3841 *
3842 * RETURNS
3843 *
3844 * Success: S_OK.
3845 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3846 */
3847 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3848 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3849 MEMBERID memid, /* [I] The member id for the function. */
3850 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3851 UINT* pFuncIndex) /* [O] The index of the function. */
3852 {
3853 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3854 return E_OUTOFMEMORY;
3855 }
3856
3857 /******************************************************************************
3858 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3859 *
3860 * Gets the index of a variable given its member id.
3861 *
3862 * RETURNS
3863 *
3864 * Success: S_OK.
3865 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3866 */
3867 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3868 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3869 MEMBERID memid, /* [I] The member id for the variable. */
3870 UINT* pVarIndex) /* [O] The index of the variable. */
3871 {
3872 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3873 return E_OUTOFMEMORY;
3874 }
3875
3876 /******************************************************************************
3877 * ITypeInfo2_GetCustData {OLEAUT32}
3878 *
3879 * Gets a custom data element from a TypeInfo.
3880 *
3881 * RETURNS
3882 *
3883 * Success: S_OK.
3884 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3885 */
3886 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3887 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3888 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3889 VARIANT* pVarVal) /* [O] The custom data. */
3890 {
3891 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3892 MSFT_CDGuid *cdentry;
3893 int offset;
3894
3895 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
3896
3897 if (!guid || !pVarVal)
3898 return E_INVALIDARG;
3899
3900 VariantClear(pVarVal);
3901
3902 offset = ctl2_find_custdata(This->typelib, guid, This->typeinfo->oCustData);
3903 if (offset == -1)
3904 return S_OK;
3905
3906 cdentry = (MSFT_CDGuid *)&This->typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
3907 return ctl2_decode_variant(This->typelib, cdentry->DataOffset, pVarVal);
3908 }
3909
3910 /******************************************************************************
3911 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3912 *
3913 * Gets a custom data element from a function.
3914 *
3915 * RETURNS
3916 *
3917 * Success: S_OK.
3918 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3919 */
3920 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3921 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3922 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3923 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3924 VARIANT* pVarVal) /* [O] The custom data. */
3925 {
3926 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3927 return E_OUTOFMEMORY;
3928 }
3929
3930 /******************************************************************************
3931 * ITypeInfo2_GetParamCustData {OLEAUT32}
3932 *
3933 * Gets a custom data element from a parameter.
3934 *
3935 * RETURNS
3936 *
3937 * Success: S_OK.
3938 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3939 */
3940 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3941 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3942 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3943 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3944 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3945 VARIANT* pVarVal) /* [O] The custom data. */
3946 {
3947 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3948 return E_OUTOFMEMORY;
3949 }
3950
3951 /******************************************************************************
3952 * ITypeInfo2_GetVarCustData {OLEAUT32}
3953 *
3954 * Gets a custom data element from a variable.
3955 *
3956 * RETURNS
3957 *
3958 * Success: S_OK.
3959 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3960 */
3961 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3962 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3963 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3964 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3965 VARIANT* pVarVal) /* [O] The custom data. */
3966 {
3967 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3968 return E_OUTOFMEMORY;
3969 }
3970
3971 /******************************************************************************
3972 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3973 *
3974 * Gets a custom data element from an implemented type of a TypeInfo.
3975 *
3976 * RETURNS
3977 *
3978 * Success: S_OK.
3979 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3980 */
3981 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3982 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3983 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3984 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3985 VARIANT* pVarVal) /* [O] The custom data. */
3986 {
3987 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3988 return E_OUTOFMEMORY;
3989 }
3990
3991 /******************************************************************************
3992 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3993 *
3994 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3995 *
3996 * RETURNS
3997 *
3998 * Success: S_OK.
3999 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
4000 */
4001 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
4002 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
4003 MEMBERID memid, /* [I] The member id (why?). */
4004 LCID lcid, /* [I] The locale (why?). */
4005 BSTR* pbstrHelpString, /* [O] The help string. */
4006 DWORD* pdwHelpStringContext, /* [O] The help string context. */
4007 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
4008 {
4009 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
4010 return E_OUTOFMEMORY;
4011 }
4012
4013 /******************************************************************************
4014 * ITypeInfo2_GetAllCustData {OLEAUT32}
4015 *
4016 * Gets all of the custom data associated with a TypeInfo.
4017 *
4018 * RETURNS
4019 *
4020 * Success: S_OK.
4021 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4022 */
4023 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
4024 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4025 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4026 {
4027 FIXME("(%p,%p), stub!\n", iface, pCustData);
4028 return E_OUTOFMEMORY;
4029 }
4030
4031 /******************************************************************************
4032 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
4033 *
4034 * Gets all of the custom data associated with a function.
4035 *
4036 * RETURNS
4037 *
4038 * Success: S_OK.
4039 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4040 */
4041 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
4042 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4043 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
4044 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4045 {
4046 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4047 return E_OUTOFMEMORY;
4048 }
4049
4050 /******************************************************************************
4051 * ITypeInfo2_GetAllParamCustData {OLEAUT32}
4052 *
4053 * Gets all of the custom data associated with a parameter.
4054 *
4055 * RETURNS
4056 *
4057 * Success: S_OK.
4058 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4059 */
4060 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
4061 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4062 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
4063 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
4064 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4065 {
4066 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
4067 return E_OUTOFMEMORY;
4068 }
4069
4070 /******************************************************************************
4071 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
4072 *
4073 * Gets all of the custom data associated with a variable.
4074 *
4075 * RETURNS
4076 *
4077 * Success: S_OK.
4078 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4079 */
4080 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
4081 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4082 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
4083 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4084 {
4085 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4086 return E_OUTOFMEMORY;
4087 }
4088
4089 /******************************************************************************
4090 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
4091 *
4092 * Gets all of the custom data associated with an implemented type.
4093 *
4094 * RETURNS
4095 *
4096 * Success: S_OK.
4097 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4098 */
4099 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
4100 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4101 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
4102 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4103 {
4104 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4105 return E_OUTOFMEMORY;
4106 }
4107
4108
4109 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
4110
4111 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
4112 {
4113
4114 ICreateTypeInfo2_fnQueryInterface,
4115 ICreateTypeInfo2_fnAddRef,
4116 ICreateTypeInfo2_fnRelease,
4117
4118 ICreateTypeInfo2_fnSetGuid,
4119 ICreateTypeInfo2_fnSetTypeFlags,
4120 ICreateTypeInfo2_fnSetDocString,
4121 ICreateTypeInfo2_fnSetHelpContext,
4122 ICreateTypeInfo2_fnSetVersion,
4123 ICreateTypeInfo2_fnAddRefTypeInfo,
4124 ICreateTypeInfo2_fnAddFuncDesc,
4125 ICreateTypeInfo2_fnAddImplType,
4126 ICreateTypeInfo2_fnSetImplTypeFlags,
4127 ICreateTypeInfo2_fnSetAlignment,
4128 ICreateTypeInfo2_fnSetSchema,
4129 ICreateTypeInfo2_fnAddVarDesc,
4130 ICreateTypeInfo2_fnSetFuncAndParamNames,
4131 ICreateTypeInfo2_fnSetVarName,
4132 ICreateTypeInfo2_fnSetTypeDescAlias,
4133 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
4134 ICreateTypeInfo2_fnSetFuncDocString,
4135 ICreateTypeInfo2_fnSetVarDocString,
4136 ICreateTypeInfo2_fnSetFuncHelpContext,
4137 ICreateTypeInfo2_fnSetVarHelpContext,
4138 ICreateTypeInfo2_fnSetMops,
4139 ICreateTypeInfo2_fnSetTypeIdldesc,
4140 ICreateTypeInfo2_fnLayOut,
4141
4142 ICreateTypeInfo2_fnDeleteFuncDesc,
4143 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
4144 ICreateTypeInfo2_fnDeleteVarDesc,
4145 ICreateTypeInfo2_fnDeleteVarDescByMemId,
4146 ICreateTypeInfo2_fnDeleteImplType,
4147 ICreateTypeInfo2_fnSetCustData,
4148 ICreateTypeInfo2_fnSetFuncCustData,
4149 ICreateTypeInfo2_fnSetParamCustData,
4150 ICreateTypeInfo2_fnSetVarCustData,
4151 ICreateTypeInfo2_fnSetImplTypeCustData,
4152 ICreateTypeInfo2_fnSetHelpStringContext,
4153 ICreateTypeInfo2_fnSetFuncHelpStringContext,
4154 ICreateTypeInfo2_fnSetVarHelpStringContext,
4155 ICreateTypeInfo2_fnInvalidate,
4156 ICreateTypeInfo2_fnSetName
4157 };
4158
4159 static const ITypeInfo2Vtbl typeinfo2vt =
4160 {
4161
4162 ITypeInfo2_fnQueryInterface,
4163 ITypeInfo2_fnAddRef,
4164 ITypeInfo2_fnRelease,
4165
4166 ITypeInfo2_fnGetTypeAttr,
4167 ITypeInfo2_fnGetTypeComp,
4168 ITypeInfo2_fnGetFuncDesc,
4169 ITypeInfo2_fnGetVarDesc,
4170 ITypeInfo2_fnGetNames,
4171 ITypeInfo2_fnGetRefTypeOfImplType,
4172 ITypeInfo2_fnGetImplTypeFlags,
4173 ITypeInfo2_fnGetIDsOfNames,
4174 ITypeInfo2_fnInvoke,
4175 ITypeInfo2_fnGetDocumentation,
4176 ITypeInfo2_fnGetDllEntry,
4177 ITypeInfo2_fnGetRefTypeInfo,
4178 ITypeInfo2_fnAddressOfMember,
4179 ITypeInfo2_fnCreateInstance,
4180 ITypeInfo2_fnGetMops,
4181 ITypeInfo2_fnGetContainingTypeLib,
4182 ITypeInfo2_fnReleaseTypeAttr,
4183 ITypeInfo2_fnReleaseFuncDesc,
4184 ITypeInfo2_fnReleaseVarDesc,
4185
4186 ITypeInfo2_fnGetTypeKind,
4187 ITypeInfo2_fnGetTypeFlags,
4188 ITypeInfo2_fnGetFuncIndexOfMemId,
4189 ITypeInfo2_fnGetVarIndexOfMemId,
4190 ITypeInfo2_fnGetCustData,
4191 ITypeInfo2_fnGetFuncCustData,
4192 ITypeInfo2_fnGetParamCustData,
4193 ITypeInfo2_fnGetVarCustData,
4194 ITypeInfo2_fnGetImplTypeCustData,
4195 ITypeInfo2_fnGetDocumentation2,
4196 ITypeInfo2_fnGetAllCustData,
4197 ITypeInfo2_fnGetAllFuncCustData,
4198 ITypeInfo2_fnGetAllParamCustData,
4199 ITypeInfo2_fnGetAllVarCustData,
4200 ITypeInfo2_fnGetAllImplTypeCustData,
4201 };
4202
4203 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
4204 {
4205 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
4206
4207 int nameoffset;
4208 int typeinfo_offset;
4209 MSFT_TypeInfoBase *typeinfo;
4210
4211 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
4212
4213 pCreateTypeInfo2Impl = heap_alloc_zero(sizeof(ICreateTypeInfo2Impl));
4214 if (!pCreateTypeInfo2Impl) return NULL;
4215
4216 pCreateTypeInfo2Impl->ICreateTypeInfo2_iface.lpVtbl = &ctypeinfo2vt;
4217 pCreateTypeInfo2Impl->ITypeInfo2_iface.lpVtbl = &typeinfo2vt;
4218 pCreateTypeInfo2Impl->ref = 1;
4219
4220 pCreateTypeInfo2Impl->typelib = typelib;
4221 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
4222
4223 nameoffset = ctl2_alloc_name(typelib, szName);
4224 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
4225 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
4226
4227 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
4228 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
4229
4230 pCreateTypeInfo2Impl->typeinfo = typeinfo;
4231
4232 pCreateTypeInfo2Impl->typekind = tkind;
4233 typeinfo->typekind |= tkind | 0x20;
4234 ICreateTypeInfo2_SetAlignment(&pCreateTypeInfo2Impl->ICreateTypeInfo2_iface, 4);
4235
4236 switch (tkind) {
4237 case TKIND_ENUM:
4238 case TKIND_INTERFACE:
4239 case TKIND_DISPATCH:
4240 case TKIND_COCLASS:
4241 typeinfo->size = 4;
4242 break;
4243
4244 case TKIND_RECORD:
4245 case TKIND_UNION:
4246 typeinfo->size = 0;
4247 break;
4248
4249 case TKIND_MODULE:
4250 typeinfo->size = 2;
4251 break;
4252
4253 case TKIND_ALIAS:
4254 typeinfo->size = -0x75;
4255 break;
4256
4257 default:
4258 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
4259 typeinfo->size = 0xdeadbeef;
4260 break;
4261 }
4262
4263 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
4264 typelib->last_typeinfo = pCreateTypeInfo2Impl;
4265 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
4266
4267 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
4268
4269 return &pCreateTypeInfo2Impl->ICreateTypeInfo2_iface;
4270 }
4271
4272
4273 /*================== ICreateTypeLib2 Implementation ===================================*/
4274
4275 /******************************************************************************
4276 * ICreateTypeLib2_QueryInterface {OLEAUT32}
4277 */
4278 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
4279 ICreateTypeLib2 * iface,
4280 REFIID riid,
4281 VOID **ppvObject)
4282 {
4283 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4284
4285 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4286
4287 *ppvObject=NULL;
4288 if(IsEqualIID(riid, &IID_IUnknown) ||
4289 IsEqualIID(riid,&IID_ICreateTypeLib)||
4290 IsEqualIID(riid,&IID_ICreateTypeLib2))
4291 {
4292 *ppvObject = This;
4293 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4294 IsEqualIID(riid, &IID_ITypeLib2)) {
4295 *ppvObject = &This->ITypeLib2_iface;
4296 }
4297
4298 if(*ppvObject)
4299 {
4300 ICreateTypeLib2_AddRef(iface);
4301 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4302 return S_OK;
4303 }
4304 TRACE("-- Interface: E_NOINTERFACE\n");
4305 return E_NOINTERFACE;
4306 }
4307
4308 /******************************************************************************
4309 * ICreateTypeLib2_AddRef {OLEAUT32}
4310 */
4311 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4312 {
4313 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4314 ULONG ref = InterlockedIncrement(&This->ref);
4315
4316 TRACE("(%p)->(%u)\n", This, ref);
4317
4318 return ref;
4319 }
4320
4321 /******************************************************************************
4322 * ICreateTypeLib2_Release {OLEAUT32}
4323 */
4324 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4325 {
4326 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4327 ULONG ref = InterlockedDecrement(&This->ref);
4328
4329 TRACE("(%p)->(%u)\n", This, ref);
4330
4331 if (!ref) {
4332 int i;
4333
4334 for (i = 0; i < MSFT_SEG_MAX; i++) {
4335 heap_free(This->typelib_segment_data[i]);
4336 This->typelib_segment_data[i] = NULL;
4337 }
4338
4339 SysFreeString(This->filename);
4340 This->filename = NULL;
4341
4342 while (This->typeinfos) {
4343 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4344 This->typeinfos = typeinfo->next_typeinfo;
4345 if(typeinfo->typedata) {
4346 CyclicList *iter, *rem;
4347
4348 rem = typeinfo->typedata->next;
4349 typeinfo->typedata->next = NULL;
4350 iter = rem->next;
4351 heap_free(rem);
4352
4353 while(iter) {
4354 rem = iter;
4355 iter = iter->next;
4356 heap_free(rem->u.data);
4357 heap_free(rem);
4358 }
4359 }
4360
4361 heap_free(typeinfo->dual);
4362 heap_free(typeinfo);
4363 }
4364
4365 heap_free(This);
4366 }
4367
4368 return ref;
4369 }
4370
4371
4372 /******************************************************************************
4373 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4374 */
4375 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4376 ICreateTypeLib2 * iface,
4377 LPOLESTR szName,
4378 TYPEKIND tkind,
4379 ICreateTypeInfo **tinfo)
4380 {
4381 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4382 char *name;
4383
4384 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, tinfo);
4385
4386 if (!szName || !tinfo) return E_INVALIDARG;
4387
4388 ctl2_encode_name(This, szName, &name);
4389 if(ctl2_find_name(This, name) != -1)
4390 return TYPE_E_NAMECONFLICT;
4391
4392 *tinfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4393 if (!*tinfo) return E_OUTOFMEMORY;
4394
4395 return S_OK;
4396 }
4397
4398 /******************************************************************************
4399 * ICreateTypeLib2_SetName {OLEAUT32}
4400 */
4401 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4402 ICreateTypeLib2 * iface,
4403 LPOLESTR szName)
4404 {
4405 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4406 int offset;
4407
4408 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4409
4410 offset = ctl2_alloc_name(This, szName);
4411 if (offset == -1) return E_OUTOFMEMORY;
4412 This->typelib_header.NameOffset = offset;
4413 return S_OK;
4414 }
4415
4416 /******************************************************************************
4417 * ICreateTypeLib2_SetVersion {OLEAUT32}
4418 */
4419 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4420 {
4421 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4422
4423 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4424
4425 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4426 return S_OK;
4427 }
4428
4429 /******************************************************************************
4430 * ICreateTypeLib2_SetGuid {OLEAUT32}
4431 */
4432 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4433 {
4434 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4435 MSFT_GuidEntry guidentry;
4436 int offset;
4437
4438 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4439
4440 guidentry.guid = *guid;
4441 guidentry.hreftype = -2;
4442 guidentry.next_hash = -1;
4443
4444 offset = ctl2_alloc_guid(This, &guidentry);
4445
4446 if (offset == -1) return E_OUTOFMEMORY;
4447
4448 This->typelib_header.posguid = offset;
4449
4450 return S_OK;
4451 }
4452
4453 /******************************************************************************
4454 * ICreateTypeLib2_SetDocString {OLEAUT32}
4455 */
4456 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4457 {
4458 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4459 int offset;
4460
4461 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4462 if (!szDoc)
4463 return E_INVALIDARG;
4464
4465 offset = ctl2_alloc_string(This, szDoc);
4466 if (offset == -1) return E_OUTOFMEMORY;
4467 This->typelib_header.helpstring = offset;
4468 return S_OK;
4469 }
4470
4471 /******************************************************************************
4472 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4473 */
4474 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4475 {
4476 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4477 int offset;
4478
4479 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4480
4481 offset = ctl2_alloc_string(This, szHelpFileName);
4482 if (offset == -1) return E_OUTOFMEMORY;
4483 This->typelib_header.helpfile = offset;
4484 This->typelib_header.varflags |= 0x10;
4485 return S_OK;
4486 }
4487
4488 /******************************************************************************
4489 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4490 */
4491 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4492 {
4493 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4494
4495 TRACE("(%p,%d)\n", iface, dwHelpContext);
4496 This->typelib_header.helpcontext = dwHelpContext;
4497 return S_OK;
4498 }
4499
4500 /******************************************************************************
4501 * ICreateTypeLib2_SetLcid {OLEAUT32}
4502 *
4503 * Sets both the lcid and lcid2 members in the header to lcid.
4504 *
4505 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4506 * is set to US English while the second one is set to 0.
4507 */
4508 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4509 {
4510 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4511
4512 TRACE("(%p,%d)\n", iface, lcid);
4513
4514 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4515
4516 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4517
4518 return S_OK;
4519 }
4520
4521 /******************************************************************************
4522 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4523 */
4524 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4525 {
4526 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4527
4528 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4529
4530 This->typelib_header.flags = uLibFlags;
4531
4532 return S_OK;
4533 }
4534
4535 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4536 {
4537 DWORD dwWritten;
4538 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4539 TRACE("Writefile(%p, %d) failed, lasterror %d\n", segment, length, GetLastError());
4540 CloseHandle(hFile);
4541 return 0;
4542 }
4543 return -1;
4544 }
4545
4546 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4547 {
4548 DWORD dwWritten;
4549 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4550 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4551 CloseHandle(hFile);
4552 return 0;
4553 }
4554
4555 return -1;
4556 }
4557
4558 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4559 {
4560 ICreateTypeInfo2Impl *typeinfo;
4561 HRESULT hres;
4562
4563 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4564 typeinfo->typeinfo->memoffset = filesize;
4565
4566 hres = ICreateTypeInfo2_fnLayOut(&typeinfo->ICreateTypeInfo2_iface);
4567 if(FAILED(hres))
4568 return hres;
4569
4570 if (typeinfo->typedata)
4571 filesize += typeinfo->typedata->next->u.val
4572 + cti2_get_var_count(typeinfo->typeinfo) * 12
4573 + cti2_get_func_count(typeinfo->typeinfo) * 12 + 4;
4574 }
4575
4576 return S_OK;
4577 }
4578
4579 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4580 {
4581 if (This->typelib_segdir[segment].length) {
4582 This->typelib_segdir[segment].offset = filepos;
4583 } else {
4584 This->typelib_segdir[segment].offset = -1;
4585 }
4586
4587 return This->typelib_segdir[segment].length;
4588 }
4589
4590 static int ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4591 {
4592 ICreateTypeInfo2Impl *typeinfo;
4593
4594 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4595 CyclicList *iter;
4596 int offset = 0;
4597
4598 if (!typeinfo->typedata) continue;
4599
4600 iter = typeinfo->typedata->next;
4601 if (!ctl2_write_chunk(hFile, &iter->u.val, sizeof(int)))
4602 return 0;
4603 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4604 if (!ctl2_write_chunk(hFile, iter->u.data, ctl2_get_record_size(iter)))
4605 return 0;
4606
4607 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4608 if (!ctl2_write_chunk(hFile, &iter->indice, sizeof(int)))
4609 return 0;
4610
4611 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4612 if (!ctl2_write_chunk(hFile, &iter->name, sizeof(int)))
4613 return 0;
4614
4615 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4616 if (!ctl2_write_chunk(hFile, &offset, sizeof(int)))
4617 return 0;
4618 offset += ctl2_get_record_size(iter);
4619 }
4620 }
4621 return 1;
4622 }
4623
4624 /******************************************************************************
4625 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4626 */
4627 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4628 {
4629 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4630 int retval;
4631 int filepos;
4632 HANDLE hFile;
4633 HRESULT hres;
4634
4635 TRACE("(%p)\n", iface);
4636
4637 retval = TYPE_E_IOERROR;
4638
4639 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4640 if (hFile == INVALID_HANDLE_VALUE) return retval;
4641
4642 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4643 filepos += This->typelib_header.nrtypeinfos * 4;
4644
4645 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4646 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4647 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4648 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4649 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4650 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4651 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4652 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4653 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4654 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4655 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4656 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4657 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4658
4659 hres = ctl2_finalize_typeinfos(This, filepos);
4660 if(FAILED(hres)) {
4661 CloseHandle(hFile);
4662 return hres;
4663 }
4664
4665 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4666 if (This->typelib_header.varflags & HELPDLLFLAG)
4667 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4668 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4669 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4670 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4671 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4672 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4673 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4674 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4675 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4676 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4677 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4678 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4679 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4680 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4681 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4682 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4683
4684 if (!ctl2_write_typeinfos(This, hFile)) return retval;
4685
4686 if (!CloseHandle(hFile)) return retval;
4687
4688 return S_OK;
4689 }
4690
4691
4692 /******************************************************************************
4693 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4694 *
4695 * Deletes a named TypeInfo from a type library.
4696 *
4697 * RETURNS
4698 *
4699 * Success: S_OK
4700 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4701 */
4702 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4703 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4704 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4705 {
4706 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4707 return E_OUTOFMEMORY;
4708 }
4709
4710 /******************************************************************************
4711 * ICreateTypeLib2_SetCustData {OLEAUT32}
4712 *
4713 * Sets custom data for a type library.
4714 *
4715 * RETURNS
4716 *
4717 * Success: S_OK
4718 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4719 */
4720 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4721 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4722 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4723 VARIANT *pVarVal) /* [I] The custom data itself. */
4724 {
4725 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4726
4727 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4728
4729 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4730 }
4731
4732 /******************************************************************************
4733 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4734 *
4735 * Sets a context number for the library help string.
4736 *
4737 * PARAMS
4738 * iface [I] The type library to set the help string context for.
4739 * dwContext [I] The help string context.
4740 *
4741 * RETURNS
4742 * Success: S_OK
4743 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4744 */
4745 static
4746 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4747 ULONG dwContext)
4748 {
4749 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4750
4751 TRACE("(%p,%d)\n", iface, dwContext);
4752
4753 This->typelib_header.helpstringcontext = dwContext;
4754 return S_OK;
4755 }
4756
4757 /******************************************************************************
4758 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4759 *
4760 * Set the DLL used to look up localized help strings.
4761 *
4762 * PARAMS
4763 * iface [I] The type library to set the help DLL for.
4764 * szDllName [I] The name of the help DLL.
4765 *
4766 * RETURNS
4767 * Success: S_OK
4768 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4769 */
4770 static
4771 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4772 LPOLESTR szDllName)
4773 {
4774 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4775 int offset;
4776
4777 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4778 if (!szDllName)
4779 return E_INVALIDARG;
4780
4781 offset = ctl2_alloc_string(This, szDllName);
4782 if (offset == -1)
4783 return E_OUTOFMEMORY;
4784 This->typelib_header.varflags |= HELPDLLFLAG;
4785 This->helpStringDll = offset;
4786 return S_OK;
4787 }
4788
4789 /*================== ITypeLib2 Implementation ===================================*/
4790
4791 /******************************************************************************
4792 * ITypeLib2_QueryInterface {OLEAUT32}
4793 */
4794 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4795 {
4796 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4797
4798 return ICreateTypeLib2_QueryInterface(&This->ICreateTypeLib2_iface, riid, ppv);
4799 }
4800
4801 /******************************************************************************
4802 * ITypeLib2_AddRef {OLEAUT32}
4803 */
4804 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4805 {
4806 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4807
4808 return ICreateTypeLib2_AddRef(&This->ICreateTypeLib2_iface);
4809 }
4810
4811 /******************************************************************************
4812 * ITypeLib2_Release {OLEAUT32}
4813 */
4814 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4815 {
4816 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4817
4818 return ICreateTypeLib2_Release(&This->ICreateTypeLib2_iface);
4819 }
4820
4821 /******************************************************************************
4822 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4823 */
4824 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4825 ITypeLib2 * iface)
4826 {
4827 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4828
4829 TRACE("(%p)\n", iface);
4830
4831 return This->typelib_header.nrtypeinfos;
4832 }
4833
4834 /******************************************************************************
4835 * ITypeLib2_GetTypeInfo {OLEAUT32}
4836 */
4837 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4838 ITypeLib2 * iface,
4839 UINT index,
4840 ITypeInfo** ppTInfo)
4841 {
4842 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4843
4844 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4845
4846 if (!ppTInfo) return E_INVALIDARG;
4847
4848 if (index >= This->typelib_header.nrtypeinfos) {
4849 return TYPE_E_ELEMENTNOTFOUND;
4850 }
4851
4852 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4853 }
4854
4855 /******************************************************************************
4856 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4857 */
4858 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4859 ITypeLib2 * iface,
4860 UINT index,
4861 TYPEKIND* kind)
4862 {
4863 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4864
4865 TRACE("(%p,%d,%p)\n", iface, index, kind);
4866
4867 if (!kind) return E_INVALIDARG;
4868
4869 if (index >= This->typelib_header.nrtypeinfos) {
4870 return TYPE_E_ELEMENTNOTFOUND;
4871 }
4872
4873 *kind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 0xF;
4874
4875 return S_OK;
4876 }
4877
4878 /******************************************************************************
4879 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4880 */
4881 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4882 ITypeLib2 * iface,
4883 REFGUID guid,
4884 ITypeInfo** ppTinfo)
4885 {
4886 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4887
4888 int guidoffset;
4889 int typeinfo;
4890
4891 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4892
4893 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4894 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4895
4896 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4897 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4898
4899 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4900 }
4901
4902 /******************************************************************************
4903 * ITypeLib2_GetLibAttr {OLEAUT32}
4904 */
4905 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4906 ITypeLib2 * iface,
4907 TLIBATTR** ppTLibAttr)
4908 {
4909 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4910
4911 TRACE("(%p,%p)\n", This, ppTLibAttr);
4912
4913 if(!ppTLibAttr)
4914 return E_INVALIDARG;
4915
4916 *ppTLibAttr = heap_alloc_zero(sizeof(TLIBATTR));
4917 if(!*ppTLibAttr)
4918 return E_OUTOFMEMORY;
4919
4920 if(This->typelib_header.posguid != -1) {
4921 MSFT_GuidEntry *guid;
4922
4923 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4924 (*ppTLibAttr)->guid = guid->guid;
4925 }
4926
4927 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4928 (*ppTLibAttr)->syskind = ctl2_get_syskind(This);
4929 (*ppTLibAttr)->wMajorVerNum = LOWORD(This->typelib_header.version);
4930 (*ppTLibAttr)->wMinorVerNum = HIWORD(This->typelib_header.version);
4931 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4932 return S_OK;
4933 }
4934
4935 /******************************************************************************
4936 * ITypeLib2_GetTypeComp {OLEAUT32}
4937 */
4938 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4939 ITypeLib2 * iface,
4940 ITypeComp** ppTComp)
4941 {
4942 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4943
4944 FIXME("(%p,%p), stub!\n", This, ppTComp);
4945
4946 return E_OUTOFMEMORY;
4947 }
4948
4949 /******************************************************************************
4950 * ITypeLib2_GetDocumentation {OLEAUT32}
4951 */
4952 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4953 ITypeLib2 * iface,
4954 INT index,
4955 BSTR* pBstrName,
4956 BSTR* pBstrDocString,
4957 DWORD* pdwHelpContext,
4958 BSTR* pBstrHelpFile)
4959 {
4960 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4961 WCHAR *string;
4962
4963 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4964
4965 if(index != -1) {
4966 ICreateTypeInfo2Impl *iter;
4967
4968 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4969 index--;
4970
4971 if(!iter)
4972 return TYPE_E_ELEMENTNOTFOUND;
4973
4974 return ITypeInfo2_GetDocumentation(&iter->ITypeInfo2_iface,
4975 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4976 }
4977
4978 if(pBstrName) {
4979 if(This->typelib_header.NameOffset == -1)
4980 *pBstrName = NULL;
4981 else {
4982 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4983 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4984
4985 ctl2_decode_name((char*)&name->namelen, &string);
4986
4987 *pBstrName = SysAllocString(string);
4988 if(!*pBstrName)
4989 return E_OUTOFMEMORY;
4990 }
4991 }
4992
4993 if(pBstrDocString) {
4994 if(This->typelib_header.helpstring == -1)
4995 *pBstrDocString = NULL;
4996 else {
4997 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4998
4999 *pBstrDocString = SysAllocString(string);
5000 if(!*pBstrDocString) {
5001 if(pBstrName) SysFreeString(*pBstrName);
5002 return E_OUTOFMEMORY;
5003 }
5004 }
5005 }
5006
5007 if(pdwHelpContext)
5008 *pdwHelpContext = This->typelib_header.helpcontext;
5009
5010 if(pBstrHelpFile) {
5011 if(This->typelib_header.helpfile == -1)
5012 *pBstrHelpFile = NULL;
5013 else {
5014 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
5015
5016 *pBstrHelpFile = SysAllocString(string);
5017 if(!*pBstrHelpFile) {
5018 if(pBstrName) SysFreeString(*pBstrName);
5019 if(pBstrDocString) SysFreeString(*pBstrDocString);
5020 return E_OUTOFMEMORY;
5021 }
5022 }
5023 }
5024
5025 return S_OK;
5026 }
5027
5028 /******************************************************************************
5029 * ITypeLib2_IsName {OLEAUT32}
5030 */
5031 static HRESULT WINAPI ITypeLib2_fnIsName(
5032 ITypeLib2 * iface,
5033 LPOLESTR szNameBuf,
5034 ULONG lHashVal,
5035 BOOL* pfName)
5036 {
5037 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5038
5039 char *encoded_name;
5040 int nameoffset;
5041 MSFT_NameIntro *nameintro;
5042
5043 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
5044
5045 ctl2_encode_name(This, szNameBuf, &encoded_name);
5046 nameoffset = ctl2_find_name(This, encoded_name);
5047
5048 *pfName = 0;
5049
5050 if (nameoffset == -1) return S_OK;
5051
5052 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
5053 if (nameintro->hreftype == -1) return S_OK;
5054
5055 *pfName = 1;
5056
5057 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
5058
5059 return S_OK;
5060 }
5061
5062 /******************************************************************************
5063 * ITypeLib2_FindName {OLEAUT32}
5064 */
5065 static HRESULT WINAPI ITypeLib2_fnFindName(
5066 ITypeLib2 * iface,
5067 LPOLESTR szNameBuf,
5068 ULONG lHashVal,
5069 ITypeInfo** ppTInfo,
5070 MEMBERID* rgMemId,
5071 USHORT* pcFound)
5072 {
5073 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5074
5075 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
5076
5077 return E_OUTOFMEMORY;
5078 }
5079
5080 /******************************************************************************
5081 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
5082 */
5083 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
5084 ITypeLib2 * iface,
5085 TLIBATTR* attr)
5086 {
5087 TRACE("(%p,%p)\n", iface, attr);
5088 heap_free(attr);
5089 }
5090
5091 /******************************************************************************
5092 * ICreateTypeLib2_GetCustData {OLEAUT32}
5093 *
5094 * Retrieves a custom data value stored on a type library.
5095 *
5096 * RETURNS
5097 *
5098 * Success: S_OK
5099 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5100 */
5101 static HRESULT WINAPI ITypeLib2_fnGetCustData(
5102 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5103 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
5104 VARIANT* pVarVal) /* [O] The custom data. */
5105 {
5106 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5107
5108 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
5109
5110 return E_OUTOFMEMORY;
5111 }
5112
5113 /******************************************************************************
5114 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
5115 *
5116 * Retrieves some statistics about names in a type library, supposedly for
5117 * hash table optimization purposes.
5118 *
5119 * RETURNS
5120 *
5121 * Success: S_OK
5122 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5123 */
5124 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
5125 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
5126 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
5127 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
5128 {
5129 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5130
5131 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
5132
5133 return E_OUTOFMEMORY;
5134 }
5135
5136 /******************************************************************************
5137 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
5138 *
5139 * Obtain locale-aware help string information.
5140 *
5141 * RETURNS
5142 *
5143 * Success: S_OK
5144 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
5145 */
5146 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
5147 ITypeLib2 * iface,
5148 INT index,
5149 LCID lcid,
5150 BSTR* pbstrHelpString,
5151 DWORD* pdwHelpStringContext,
5152 BSTR* pbstrHelpStringDll)
5153 {
5154 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5155
5156 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
5157
5158 return E_OUTOFMEMORY;
5159 }
5160
5161 /******************************************************************************
5162 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
5163 *
5164 * Retrieve all of the custom data for a type library.
5165 *
5166 * RETURNS
5167 *
5168 * Success: S_OK
5169 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5170 */
5171 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
5172 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5173 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
5174 {
5175 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5176
5177 FIXME("(%p,%p), stub!\n", This, pCustData);
5178
5179 return E_OUTOFMEMORY;
5180 }
5181
5182
5183 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
5184
5185 static const ICreateTypeLib2Vtbl ctypelib2vt =
5186 {
5187
5188 ICreateTypeLib2_fnQueryInterface,
5189 ICreateTypeLib2_fnAddRef,
5190 ICreateTypeLib2_fnRelease,
5191
5192 ICreateTypeLib2_fnCreateTypeInfo,
5193 ICreateTypeLib2_fnSetName,
5194 ICreateTypeLib2_fnSetVersion,
5195 ICreateTypeLib2_fnSetGuid,
5196 ICreateTypeLib2_fnSetDocString,
5197 ICreateTypeLib2_fnSetHelpFileName,
5198 ICreateTypeLib2_fnSetHelpContext,
5199 ICreateTypeLib2_fnSetLcid,
5200 ICreateTypeLib2_fnSetLibFlags,
5201 ICreateTypeLib2_fnSaveAllChanges,
5202
5203 ICreateTypeLib2_fnDeleteTypeInfo,
5204 ICreateTypeLib2_fnSetCustData,
5205 ICreateTypeLib2_fnSetHelpStringContext,
5206 ICreateTypeLib2_fnSetHelpStringDll
5207 };
5208
5209 static const ITypeLib2Vtbl typelib2vt =
5210 {
5211
5212 ITypeLib2_fnQueryInterface,
5213 ITypeLib2_fnAddRef,
5214 ITypeLib2_fnRelease,
5215
5216 ITypeLib2_fnGetTypeInfoCount,
5217 ITypeLib2_fnGetTypeInfo,
5218 ITypeLib2_fnGetTypeInfoType,
5219 ITypeLib2_fnGetTypeInfoOfGuid,
5220 ITypeLib2_fnGetLibAttr,
5221 ITypeLib2_fnGetTypeComp,
5222 ITypeLib2_fnGetDocumentation,
5223 ITypeLib2_fnIsName,
5224 ITypeLib2_fnFindName,
5225 ITypeLib2_fnReleaseTLibAttr,
5226
5227 ITypeLib2_fnGetCustData,
5228 ITypeLib2_fnGetLibStatistics,
5229 ITypeLib2_fnGetDocumentation2,
5230 ITypeLib2_fnGetAllCustData,
5231 };
5232
5233 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR filename)
5234 {
5235 ICreateTypeLib2Impl *create_tlib2;
5236 int failed = 0;
5237
5238 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(filename));
5239
5240 create_tlib2 = heap_alloc_zero(sizeof(ICreateTypeLib2Impl));
5241 if (!create_tlib2) return NULL;
5242
5243 create_tlib2->filename = SysAllocString(filename);
5244 if (!create_tlib2->filename) {
5245 heap_free(create_tlib2);
5246 return NULL;
5247 }
5248
5249 ctl2_init_header(create_tlib2);
5250 ctl2_init_segdir(create_tlib2);
5251
5252 create_tlib2->typelib_header.varflags |= syskind;
5253
5254 /*
5255 * The following two calls return an offset or -1 if out of memory. We
5256 * specifically need an offset of 0, however, so...
5257 */
5258 if (ctl2_alloc_segment(create_tlib2, MSFT_SEG_GUIDHASH, 0x80, 0x80) == 0)
5259 {
5260 create_tlib2->typelib_guidhash_segment = (int *)create_tlib2->typelib_segment_data[MSFT_SEG_GUIDHASH];
5261 memset(create_tlib2->typelib_guidhash_segment, 0xff, 0x80);
5262 }
5263 else
5264 failed = 1;
5265
5266 if (ctl2_alloc_segment(create_tlib2, MSFT_SEG_NAMEHASH, 0x200, 0x200) == 0)
5267 {
5268 create_tlib2->typelib_namehash_segment = (int *)create_tlib2->typelib_segment_data[MSFT_SEG_NAMEHASH];
5269 memset(create_tlib2->typelib_namehash_segment, 0xff, 0x200);
5270 }
5271 else
5272 failed = 1;
5273
5274 create_tlib2->ICreateTypeLib2_iface.lpVtbl = &ctypelib2vt;
5275 create_tlib2->ITypeLib2_iface.lpVtbl = &typelib2vt;
5276 create_tlib2->ref = 1;
5277
5278 if (failed) {
5279 ICreateTypeLib2_fnRelease(&create_tlib2->ICreateTypeLib2_iface);
5280 heap_free(create_tlib2);
5281 return NULL;
5282 }
5283
5284 return &create_tlib2->ICreateTypeLib2_iface;
5285 }
5286
5287 /******************************************************************************
5288 * CreateTypeLib2 [OLEAUT32.180]
5289 *
5290 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5291 * library.
5292 *
5293 * NOTES
5294 *
5295 * See also CreateTypeLib.
5296 *
5297 * RETURNS
5298 * Success: S_OK
5299 * Failure: Status
5300 */
5301 HRESULT WINAPI CreateTypeLib2(
5302 SYSKIND syskind, /* [I] System type library is for */
5303 LPCOLESTR szFile, /* [I] Type library file name */
5304 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5305 {
5306 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5307
5308 if (!szFile) return E_INVALIDARG;
5309 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5310 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5311 }
5312
5313 /******************************************************************************
5314 * ClearCustData (OLEAUT32.171)
5315 *
5316 * Clear a custom data types' data.
5317 *
5318 * PARAMS
5319 * lpCust [I] The custom data type instance
5320 *
5321 * RETURNS
5322 * Nothing.
5323 */
5324 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5325 {
5326 if (lpCust && lpCust->cCustData)
5327 {
5328 if (lpCust->prgCustData)
5329 {
5330 DWORD i;
5331
5332 for (i = 0; i < lpCust->cCustData; i++)
5333 VariantClear(&lpCust->prgCustData[i].varValue);
5334
5335 /* FIXME - Should be using a per-thread IMalloc */
5336 heap_free(lpCust->prgCustData);
5337 lpCust->prgCustData = NULL;
5338 }
5339 lpCust->cCustData = 0;
5340 }
5341 }