oleaut32: Implement ICreateTypeInfo2 SetHelpStringContext.
[wine/multimedia.git] / dlls / oleaut32 / typelib2.c
blobd319ede36027a1096f94db21c56ebfecd4f0d064
1 /*
2 * TYPELIB2
4 * Copyright 2004 Alastair Bridgewater
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.
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.
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
20 * --------------------------------------------------------------------------------------
21 * Known problems:
23 * Badly incomplete.
25 * Only works on little-endian systems.
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <ctype.h>
38 #define COBJMACROS
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "winreg.h"
47 #include "winuser.h"
49 #include "wine/unicode.h"
50 #include "objbase.h"
51 #include "typelib.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(typelib2);
55 /* WINE_DEFAULT_DEBUG_CHANNEL(ole); */
58 /******************************************************************************
59 * ICreateTypeLib2 {OLEAUT32}
61 * NOTES
62 * The ICreateTypeLib2 interface provides an interface whereby one may create
63 * new type library (.tlb) files.
65 * This interface inherits from ICreateTypeLib, and can be freely cast back
66 * and forth between an ICreateTypeLib and an ICreateTypeLib2 on local clients.
67 * This dispensation applies only to ICreateTypeLib objects obtained on MSFT
68 * format type libraries (those made through CreateTypeLib2).
70 * METHODS
73 /******************************************************************************
74 * ICreateTypeInfo2 {OLEAUT32}
76 * NOTES
77 * The ICreateTypeInfo2 interface provides an interface whereby one may add
78 * type information to type library (.tlb) files.
80 * This interface inherits from ICreateTypeInfo, and can be freely cast back
81 * and forth between an ICreateTypeInfo and an ICreateTypeInfo2 on local clients.
82 * This dispensation applies only to ICreateTypeInfo objects obtained on MSFT
83 * format type libraries (those made through CreateTypeLib2).
85 * METHODS
88 /******************************************************************************
89 * ITypeLib2 {OLEAUT32}
91 * NOTES
92 * The ITypeLib2 interface provides an interface whereby one may query MSFT
93 * format type library (.tlb) files.
95 * This interface inherits from ITypeLib, and can be freely cast back and
96 * forth between an ITypeLib and an ITypeLib2 on local clients. This
97 * dispensation applies only to ITypeLib objects obtained on MSFT format type
98 * libraries (those made through CreateTypeLib2).
100 * METHODS
103 /******************************************************************************
104 * ITypeInfo2 {OLEAUT32}
106 * NOTES
107 * The ITypeInfo2 interface provides an interface whereby one may query type
108 * information stored in MSFT format type library (.tlb) files.
110 * This interface inherits from ITypeInfo, and can be freely cast back and
111 * forth between an ITypeInfo and an ITypeInfo2 on local clients. This
112 * dispensation applies only to ITypeInfo objects obtained on MSFT format type
113 * libraries (those made through CreateTypeLib2).
115 * METHODS
118 /*================== Implementation Structures ===================================*/
120 /* Used for storing cyclic list. Tail address is kept */
121 typedef enum tagCyclicListElementType {
122 CyclicListSentinel,
123 CyclicListFunc,
124 CyclicListVar
125 } CyclicListElementType;
126 typedef struct tagCyclicList {
127 struct tagCyclicList *next;
128 int indice;
129 int name;
130 CyclicListElementType type;
132 union {
133 int val;
134 int *data;
136 } CyclicList;
138 enum MSFT_segment_index {
139 MSFT_SEG_TYPEINFO = 0, /* type information */
140 MSFT_SEG_IMPORTINFO, /* import information */
141 MSFT_SEG_IMPORTFILES, /* import filenames */
142 MSFT_SEG_REFERENCES, /* references (?) */
143 MSFT_SEG_GUIDHASH, /* hash table for guids? */
144 MSFT_SEG_GUID, /* guid storage */
145 MSFT_SEG_NAMEHASH, /* hash table for names */
146 MSFT_SEG_NAME, /* name storage */
147 MSFT_SEG_STRING, /* string storage */
148 MSFT_SEG_TYPEDESC, /* type descriptions */
149 MSFT_SEG_ARRAYDESC, /* array descriptions */
150 MSFT_SEG_CUSTDATA, /* custom data */
151 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
152 MSFT_SEG_UNKNOWN, /* ??? */
153 MSFT_SEG_UNKNOWN2, /* ??? */
154 MSFT_SEG_MAX /* total number of segments */
157 typedef struct tagMSFT_ImpFile {
158 int guid;
159 LCID lcid;
160 int version;
161 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
162 } MSFT_ImpFile;
164 typedef struct tagICreateTypeLib2Impl
166 const ICreateTypeLib2Vtbl *lpVtbl;
167 const ITypeLib2Vtbl *lpVtblTypeLib2;
169 LONG ref;
171 WCHAR *filename;
173 MSFT_Header typelib_header;
174 INT helpStringDll;
175 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
176 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
177 int typelib_segment_block_length[MSFT_SEG_MAX];
179 int typelib_guids; /* Number of defined typelib guids */
180 int typeinfo_guids; /* Number of defined typeinfo guids */
182 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
184 INT *typelib_namehash_segment;
185 INT *typelib_guidhash_segment;
187 struct tagICreateTypeInfo2Impl *typeinfos;
188 struct tagICreateTypeInfo2Impl *last_typeinfo;
189 } ICreateTypeLib2Impl;
191 static inline ICreateTypeLib2Impl *impl_from_ITypeLib2( ITypeLib2 *iface )
193 return (ICreateTypeLib2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeLib2Impl, lpVtblTypeLib2));
196 typedef struct tagICreateTypeInfo2Impl
198 ICreateTypeInfo2 ICreateTypeInfo2_iface;
199 ITypeInfo2 ITypeInfo2_iface;
201 LONG ref;
203 ICreateTypeLib2Impl *typelib;
204 MSFT_TypeInfoBase *typeinfo;
206 struct tagCyclicList *typedata; /* tail of cyclic list */
208 TYPEKIND typekind;
209 int datawidth;
211 struct tagICreateTypeInfo2Impl *next_typeinfo;
212 struct tagICreateTypeInfo2Impl *dual;
213 } ICreateTypeInfo2Impl;
215 static inline ICreateTypeInfo2Impl *impl_from_ICreateTypeInfo2(ICreateTypeInfo2 *iface)
217 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ICreateTypeInfo2_iface);
220 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
222 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ITypeInfo2_iface);
225 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
227 static CyclicList *alloc_cyclic_list_item(CyclicListElementType type)
229 CyclicList *ret = heap_alloc_zero(sizeof(CyclicList));
230 if (!ret)
231 return NULL;
232 ret->type = type;
233 return ret;
236 /*================== Internal functions ===================================*/
238 static inline UINT cti2_get_var_count(const MSFT_TypeInfoBase *typeinfo)
240 return typeinfo->cElement >> 16;
243 static inline UINT cti2_get_func_count(const MSFT_TypeInfoBase *typeinfo)
245 return typeinfo->cElement & 0xFFFF;
248 static inline INT ctl2_get_record_size(const CyclicList *iter)
250 return iter->u.data[0] & 0xFFFF;
253 static void ctl2_update_var_size(const ICreateTypeInfo2Impl *This, CyclicList *var, int size)
255 int old = ctl2_get_record_size(var), i;
257 if (old >= size) return;
259 /* initialize fields included in size but currently unused */
260 for (i = old/sizeof(int); i < (size/sizeof(int) - 1); i++)
262 /* HelpContext/HelpStringContext being 0 means it's not set */
263 var->u.data[i] = (i == 5 || i == 9) ? 0 : -1;
266 var->u.data[0] += size - old;
267 This->typedata->next->u.val += size - old;
270 /* NOTE: entry always assumed to be a function */
271 static inline INVOKEKIND ctl2_get_invokekind(const CyclicList *func)
273 /* INVOKEKIND uses bit flags up to 8 */
274 return (func->u.data[4] >> 3) & 0xF;
277 static inline SYSKIND ctl2_get_syskind(const ICreateTypeLib2Impl *This)
279 return This->typelib_header.varflags & 0xF;
282 /****************************************************************************
283 * ctl2_init_header
285 * Initializes the type library header of a new typelib.
287 static void ctl2_init_header(
288 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
290 This->typelib_header.magic1 = MSFT_SIGNATURE;
291 This->typelib_header.magic2 = 0x00010002;
292 This->typelib_header.posguid = -1;
293 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
294 This->typelib_header.varflags = 0x40;
295 This->typelib_header.version = 0;
296 This->typelib_header.flags = 0;
297 This->typelib_header.nrtypeinfos = 0;
298 This->typelib_header.helpstring = -1;
299 This->typelib_header.helpstringcontext = 0;
300 This->typelib_header.helpcontext = 0;
301 This->typelib_header.nametablecount = 0;
302 This->typelib_header.nametablechars = 0;
303 This->typelib_header.NameOffset = -1;
304 This->typelib_header.helpfile = -1;
305 This->typelib_header.CustomDataOffset = -1;
306 This->typelib_header.res44 = 0x20;
307 This->typelib_header.res48 = 0x80;
308 This->typelib_header.dispatchpos = -1;
309 This->typelib_header.nimpinfos = 0;
310 This->helpStringDll = -1;
313 /****************************************************************************
314 * ctl2_init_segdir
316 * Initializes the segment directory of a new typelib.
318 static void ctl2_init_segdir(
319 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
321 int i;
322 MSFT_pSeg *segdir;
324 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
326 for (i = 0; i < 15; i++) {
327 segdir[i].offset = -1;
328 segdir[i].length = 0;
329 segdir[i].res08 = -1;
330 segdir[i].res0c = 0x0f;
334 /****************************************************************************
335 * ctl2_hash_guid
337 * Generates a hash key from a GUID.
339 * RETURNS
341 * The hash key for the GUID.
343 static int ctl2_hash_guid(
344 REFGUID guid) /* [I] The guid to find. */
346 int hash;
347 int i;
349 hash = 0;
350 for (i = 0; i < 8; i ++) {
351 hash ^= ((const short *)guid)[i];
354 return hash & 0x1f;
357 /****************************************************************************
358 * ctl2_find_guid
360 * Locates a guid in a type library.
362 * RETURNS
364 * The offset into the GUID segment of the guid, or -1 if not found.
366 static int ctl2_find_guid(
367 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
368 int hash_key, /* [I] The hash key for the guid. */
369 REFGUID guid) /* [I] The guid to find. */
371 int offset;
372 MSFT_GuidEntry *guidentry;
374 offset = This->typelib_guidhash_segment[hash_key];
375 while (offset != -1) {
376 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
378 if (IsEqualGUID(guidentry, guid)) return offset;
380 offset = guidentry->next_hash;
383 return offset;
386 /****************************************************************************
387 * ctl2_find_name
389 * Locates a name in a type library.
391 * RETURNS
393 * The offset into the NAME segment of the name, or -1 if not found.
395 * NOTES
397 * The name must be encoded as with ctl2_encode_name().
399 static int ctl2_find_name(
400 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
401 const char *name) /* [I] The encoded name to find. */
403 int offset;
404 int *namestruct;
406 offset = This->typelib_namehash_segment[name[2] & 0x7f];
407 while (offset != -1) {
408 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
410 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
411 /* hash codes and lengths match, final test */
412 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
415 /* move to next item in hash bucket */
416 offset = namestruct[1];
419 return offset;
422 /****************************************************************************
423 * ctl2_encode_name
425 * Encodes a name string to a form suitable for storing into a type library
426 * or comparing to a name stored in a type library.
428 * RETURNS
430 * The length of the encoded name, including padding and length+hash fields.
432 * NOTES
434 * Will throw an exception if name or result are NULL. Is not multithread
435 * safe in the slightest.
437 static int ctl2_encode_name(
438 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
439 const WCHAR *name, /* [I] The name string to encode. */
440 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
442 int length;
443 static char converted_name[0x104];
444 int offset;
445 int value;
447 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
448 converted_name[0] = length & 0xff;
450 converted_name[length + 4] = 0;
452 converted_name[1] = 0x00;
454 value = LHashValOfNameSysA(ctl2_get_syskind(This), This->typelib_header.lcid, converted_name + 4);
456 converted_name[2] = value;
457 converted_name[3] = value >> 8;
459 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
461 *result = converted_name;
463 return (length + 7) & ~3;
466 /****************************************************************************
467 * ctl2_decode_name
469 * Converts string stored in typelib data to unicode.
471 static void ctl2_decode_name(
472 char *data, /* [I] String to be decoded */
473 WCHAR **string) /* [O] Decoded string */
475 int i, length;
476 static WCHAR converted_string[0x104];
478 length = data[0];
480 for(i=0; i<length; i++)
481 converted_string[i] = data[i+4];
482 converted_string[length] = '\0';
484 *string = converted_string;
487 /****************************************************************************
488 * ctl2_encode_string
490 * Encodes a string to a form suitable for storing into a type library or
491 * comparing to a string stored in a type library.
493 * RETURNS
495 * The length of the encoded string, including padding and length fields.
497 * NOTES
499 * Will throw an exception if string or result are NULL. Is not multithread
500 * safe in the slightest.
502 static int ctl2_encode_string(
503 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
504 const WCHAR *string, /* [I] The string to encode. */
505 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
507 int length;
508 static char converted_string[0x104];
509 int offset;
511 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
512 converted_string[0] = length & 0xff;
513 converted_string[1] = (length >> 8) & 0xff;
515 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
517 *result = converted_string;
519 return (length + 5) & ~3;
522 /****************************************************************************
523 * ctl2_decode_string
525 * Converts string stored in typelib data to unicode.
527 static void ctl2_decode_string(
528 unsigned char *data,/* [I] String to be decoded */
529 WCHAR **string) /* [O] Decoded string */
531 int i, length;
532 static WCHAR converted_string[0x104];
534 length = data[0] + (data[1]<<8);
535 if((length&0x3) == 1)
536 length >>= 2;
538 for(i=0; i<length; i++)
539 converted_string[i] = data[i+2];
540 converted_string[length] = '\0';
542 *string = converted_string;
545 /****************************************************************************
546 * ctl2_alloc_segment
548 * Allocates memory from a segment in a type library.
550 * RETURNS
552 * Success: The offset within the segment of the new data area.
553 * Failure: -1 (this is invariably an out of memory condition).
555 * BUGS
557 * Does not (yet) handle the case where the allocated segment memory needs to grow.
559 static int ctl2_alloc_segment(
560 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
561 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
562 int size, /* [I] The amount to allocate. */
563 int block_size) /* [I] Initial allocation block size, or 0 for default. */
565 int offset;
567 if(!This->typelib_segment_data[segment]) {
568 if (!block_size) block_size = 0x2000;
570 This->typelib_segment_block_length[segment] = block_size;
571 This->typelib_segment_data[segment] = heap_alloc(block_size);
572 if (!This->typelib_segment_data[segment]) return -1;
573 memset(This->typelib_segment_data[segment], 0x57, block_size);
576 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
577 unsigned char *block;
579 block_size = This->typelib_segment_block_length[segment];
580 block = heap_realloc(This->typelib_segment_data[segment], block_size << 1);
581 if (!block) return -1;
583 if (segment == MSFT_SEG_TYPEINFO) {
584 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
585 ICreateTypeInfo2Impl *typeinfo;
587 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
588 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
592 memset(block + block_size, 0x57, block_size);
593 This->typelib_segment_block_length[segment] = block_size << 1;
594 This->typelib_segment_data[segment] = block;
597 offset = This->typelib_segdir[segment].length;
598 This->typelib_segdir[segment].length += size;
600 return offset;
603 /****************************************************************************
604 * ctl2_alloc_typeinfo
606 * Allocates and initializes a typeinfo structure in a type library.
608 * RETURNS
610 * Success: The offset of the new typeinfo.
611 * Failure: -1 (this is invariably an out of memory condition).
613 static int ctl2_alloc_typeinfo(
614 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
615 int nameoffset) /* [I] The offset of the name for this typeinfo. */
617 int offset;
618 MSFT_TypeInfoBase *typeinfo;
620 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
621 if (offset == -1) return -1;
623 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
625 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
627 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
628 typeinfo->memoffset = -1; /* should be EOF if no elements */
629 typeinfo->res2 = 0;
630 typeinfo->res3 = 0;
631 typeinfo->res4 = 3;
632 typeinfo->res5 = 0;
633 typeinfo->cElement = 0;
634 typeinfo->res7 = 0;
635 typeinfo->res8 = 0;
636 typeinfo->res9 = 0;
637 typeinfo->resA = 0;
638 typeinfo->posguid = -1;
639 typeinfo->flags = 0;
640 typeinfo->NameOffset = nameoffset;
641 typeinfo->version = 0;
642 typeinfo->docstringoffs = -1;
643 typeinfo->helpstringcontext = 0;
644 typeinfo->helpcontext = 0;
645 typeinfo->oCustData = -1;
646 typeinfo->cbSizeVft = 0;
647 typeinfo->cImplTypes = 0;
648 typeinfo->size = 0;
649 typeinfo->datatype1 = -1;
650 typeinfo->datatype2 = 0;
651 typeinfo->res18 = 0;
652 typeinfo->res19 = -1;
654 return offset;
657 /****************************************************************************
658 * ctl2_alloc_guid
660 * Allocates and initializes a GUID structure in a type library. Also updates
661 * the GUID hash table as needed.
663 * RETURNS
665 * Success: The offset of the new GUID.
666 * Failure: -1 (this is invariably an out of memory condition).
668 static int ctl2_alloc_guid(
669 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
670 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
672 int offset;
673 MSFT_GuidEntry *guid_space;
674 int hash_key;
676 hash_key = ctl2_hash_guid(&guid->guid);
678 offset = ctl2_find_guid(This, hash_key, &guid->guid);
679 if (offset != -1) return offset;
681 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
682 if (offset == -1) return -1;
684 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
685 *guid_space = *guid;
687 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
688 This->typelib_guidhash_segment[hash_key] = offset;
690 return offset;
693 /****************************************************************************
694 * ctl2_alloc_name
696 * Allocates and initializes a name within a type library. Also updates the
697 * name hash table as needed.
699 * RETURNS
701 * Success: The offset within the segment of the new name.
702 * Failure: -1 (this is invariably an out of memory condition).
704 static int ctl2_alloc_name(
705 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
706 const WCHAR *name) /* [I] The name to store. */
708 int length;
709 int offset;
710 MSFT_NameIntro *name_space;
711 char *encoded_name;
713 length = ctl2_encode_name(This, name, &encoded_name);
715 offset = ctl2_find_name(This, encoded_name);
716 if (offset != -1) return offset;
718 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
719 if (offset == -1) return -1;
721 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
722 name_space->hreftype = -1;
723 name_space->next_hash = -1;
724 memcpy(&name_space->namelen, encoded_name, length);
726 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
727 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
729 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
731 This->typelib_header.nametablecount += 1;
732 This->typelib_header.nametablechars += *encoded_name;
734 return offset;
737 /****************************************************************************
738 * ctl2_alloc_string
740 * Allocates and initializes a string in a type library.
742 * RETURNS
744 * Success: The offset within the segment of the new string.
745 * Failure: -1 (this is invariably an out of memory condition).
747 static int ctl2_alloc_string(
748 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
749 const WCHAR *string) /* [I] The string to store. */
751 int length;
752 int offset;
753 unsigned char *string_space;
754 char *encoded_string;
756 length = ctl2_encode_string(This, string, &encoded_string);
758 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
759 offset += (((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
760 This->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
761 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
764 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
765 if (offset == -1) return -1;
767 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
768 memcpy(string_space, encoded_string, length);
770 return offset;
773 /****************************************************************************
774 * ctl2_alloc_importinfo
776 * Allocates and initializes an import information structure in a type library.
778 * RETURNS
780 * Success: The offset of the new importinfo.
781 * Failure: -1 (this is invariably an out of memory condition).
783 static int ctl2_alloc_importinfo(
784 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
785 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
787 int offset;
788 MSFT_ImpInfo *impinfo_space;
790 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
791 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
792 offset+=sizeof(MSFT_ImpInfo)) {
793 if(impinfo_space->oImpFile == impinfo->oImpFile
794 && impinfo_space->oGuid == impinfo->oGuid)
795 return offset;
797 impinfo_space += 1;
800 impinfo->flags |= This->typelib_header.nimpinfos++;
802 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
803 if (offset == -1) return -1;
805 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
806 *impinfo_space = *impinfo;
808 return offset;
811 /****************************************************************************
812 * ctl2_alloc_importfile
814 * Allocates and initializes an import file definition in a type library.
816 * RETURNS
818 * Success: The offset of the new importinfo.
819 * Failure: -1 (this is invariably an out of memory condition).
821 static int ctl2_alloc_importfile(
822 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
823 int guidoffset, /* [I] The offset to the GUID for the imported library. */
824 LCID lcid, /* [I] The LCID of imported library. */
825 int major_version, /* [I] The major version number of the imported library. */
826 int minor_version, /* [I] The minor version number of the imported library. */
827 const WCHAR *filename) /* [I] The filename of the imported library. */
829 int length;
830 int offset;
831 MSFT_ImpFile *importfile;
832 char *encoded_string;
834 length = ctl2_encode_string(This, filename, &encoded_string);
836 encoded_string[0] <<= 2;
837 encoded_string[0] |= 1;
839 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
840 offset += (((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
841 This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 5) & 0xfffc) + 0xc) {
842 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
845 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
846 if (offset == -1) return -1;
848 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
849 importfile->guid = guidoffset;
850 importfile->lcid = lcid;
851 importfile->version = major_version | (minor_version << 16);
852 memcpy(importfile->filename, encoded_string, length);
854 return offset;
857 /****************************************************************************
858 * ctl2_encode_variant
860 * Encodes a variant, inline if possible or in custom data segment
862 * RETURNS
864 * Success: S_OK
865 * Failure: Error code from winerror.h
867 static HRESULT ctl2_encode_variant(
868 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
869 int *encoded_value, /* [O] The encoded default value or data offset */
870 VARIANT *value, /* [I] Default value to be encoded */
871 VARTYPE arg_type) /* [I] Argument type */
873 VARIANT v;
874 HRESULT hres;
875 int mask = 0;
877 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
879 if(arg_type == VT_INT)
880 arg_type = VT_I4;
881 if(arg_type == VT_UINT)
882 arg_type = VT_UI4;
884 v = *value;
885 if(V_VT(value) != arg_type) {
886 hres = VariantChangeType(&v, value, 0, arg_type);
887 if(FAILED(hres))
888 return hres;
891 /* Check if default value can be stored in encoded_value */
892 switch(arg_type) {
893 case VT_I4:
894 case VT_UI4:
895 mask = 0x3ffffff;
896 if(V_UI4(&v)>0x3ffffff)
897 break;
898 case VT_I1:
899 case VT_UI1:
900 case VT_BOOL:
901 if(!mask)
902 mask = 0xff;
903 case VT_I2:
904 case VT_UI2:
905 if(!mask)
906 mask = 0xffff;
907 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
908 return S_OK;
911 switch(arg_type) {
912 case VT_I4:
913 case VT_R4:
914 case VT_UI4:
915 case VT_INT:
916 case VT_UINT:
917 case VT_HRESULT:
918 case VT_PTR: {
919 /* Construct the data to be allocated */
920 int data[2];
921 data[0] = arg_type + (V_UI4(&v)<<16);
922 data[1] = (V_UI4(&v)>>16) + 0x57570000;
924 /* Check if the data was already allocated */
925 /* Currently the structures doesn't allow to do it in a nice way */
926 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
927 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
928 return S_OK;
930 /* Allocate the data */
931 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
932 if(*encoded_value == -1)
933 return E_OUTOFMEMORY;
935 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
936 return S_OK;
938 case VT_BSTR: {
939 /* Construct the data */
940 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
941 char *data = heap_alloc(len);
943 if(!data)
944 return E_OUTOFMEMORY;
946 *((unsigned short*)data) = arg_type;
947 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
948 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
949 if(V_BSTR(&v)[i] <= 0x7f)
950 data[i+6] = V_BSTR(&v)[i];
951 else
952 data[i+6] = '?';
954 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
955 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
956 data[i] = 0x57;
958 /* Check if the data was already allocated */
959 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
960 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
961 heap_free(data);
962 return S_OK;
965 /* Allocate the data */
966 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
967 if(*encoded_value == -1) {
968 heap_free(data);
969 return E_OUTOFMEMORY;
972 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
973 heap_free(data);
974 return S_OK;
976 default:
977 FIXME("Argument type not yet handled\n");
978 return E_NOTIMPL;
982 static int ctl2_find_custdata(
983 ICreateTypeLib2Impl *This,
984 REFGUID guid,
985 int offset)
987 while (offset != -1) {
988 MSFT_CDGuid *cdentry =
989 (MSFT_CDGuid *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
990 MSFT_GuidEntry *guidentry =
991 (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][cdentry->GuidOffset];
993 if (IsEqualGUID(guidentry, guid))
994 return offset;
996 offset = cdentry->next;
999 return -1;
1002 /****************************************************************************
1003 * ctl2_decode_variant
1005 * Decodes a variant
1007 * RETURNS
1009 * Success: S_OK
1010 * Failure: Error code from winerror.h
1012 static HRESULT ctl2_decode_variant(
1013 ICreateTypeLib2Impl *This, /* [I] The typelib that contains the variant */
1014 int data_offs, /* [I] Offset within the data array, or the encoded value itself */
1015 VARIANT *value) /* [O] Decoded value */
1017 unsigned char *encoded_data;
1018 VARTYPE type;
1020 if (data_offs & 0x80000000) {
1021 /* data_offs contains the encoded value */
1022 V_VT(value) = (data_offs & ~0x80000000) >> 26;
1023 V_UI4(value) = data_offs & ~0xFF000000;
1024 return S_OK;
1027 encoded_data = &This->typelib_segment_data[MSFT_SEG_CUSTDATA][data_offs];
1028 type = *encoded_data;
1030 switch(type) {
1031 case VT_I4:
1032 case VT_R4:
1033 case VT_UI4:
1034 case VT_INT:
1035 case VT_UINT:
1036 case VT_HRESULT:
1037 case VT_PTR: {
1038 V_VT(value) = type;
1039 V_UI4(value) = *(unsigned*)(encoded_data + 2);
1040 return S_OK;
1042 case VT_BSTR: {
1043 unsigned len, i;
1045 len = *(unsigned*)(encoded_data + 2);
1047 V_VT(value) = type;
1048 V_BSTR(value) = SysAllocStringByteLen(NULL, len * sizeof(OLECHAR));
1049 for (i = 0; i < len; ++i)
1050 V_BSTR(value)[i] = *(encoded_data + 6 + i);
1052 return S_OK;
1054 default:
1055 FIXME("Don't yet have decoder for this VARTYPE: %u\n", type);
1056 return E_NOTIMPL;
1060 /****************************************************************************
1061 * ctl2_set_custdata
1063 * Adds a custom data element to an object in a type library.
1065 * RETURNS
1067 * Success: S_OK.
1068 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
1070 static HRESULT ctl2_set_custdata(
1071 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
1072 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
1073 VARIANT *pVarVal, /* [I] The custom data itself. */
1074 int *offset) /* [I/O] The list of custom data to prepend to. */
1076 MSFT_GuidEntry guidentry;
1077 HRESULT status;
1078 int dataoffset;
1079 int guidoffset;
1080 int custoffset;
1081 int *custdata;
1082 BOOL new_segment = FALSE;
1084 switch(V_VT(pVarVal))
1086 case VT_I4:
1087 case VT_R4:
1088 case VT_UI4:
1089 case VT_INT:
1090 case VT_UINT:
1091 case VT_HRESULT:
1092 case VT_BSTR:
1093 /* empty */
1094 break;
1095 default:
1096 return DISP_E_BADVARTYPE;
1099 guidentry.guid = *guid;
1101 guidentry.hreftype = -1;
1102 guidentry.next_hash = -1;
1104 guidoffset = ctl2_alloc_guid(This, &guidentry);
1105 if (guidoffset == -1) return E_OUTOFMEMORY;
1107 status = ctl2_encode_variant(This, &dataoffset, pVarVal, V_VT(pVarVal));
1108 if (status)
1109 return status;
1111 custoffset = ctl2_find_custdata(This, guid, *offset);
1112 if (custoffset == -1) {
1113 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
1114 if (custoffset == -1)
1115 return E_OUTOFMEMORY;
1116 new_segment = TRUE;
1119 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1120 custdata[0] = guidoffset;
1121 custdata[1] = dataoffset;
1122 if (new_segment) {
1123 custdata[2] = *offset;
1124 *offset = custoffset;
1127 return S_OK;
1130 /****************************************************************************
1131 * ctl2_encode_typedesc
1133 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
1134 * segments as needed.
1136 * RETURNS
1138 * Success: 0.
1139 * Failure: -1.
1141 static int ctl2_encode_typedesc(
1142 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
1143 const TYPEDESC *tdesc, /* [I] The type description to encode. */
1144 int *encoded_tdesc, /* [O] The encoded type description. */
1145 int *width, /* [O] The width of the type, or NULL. */
1146 int *alignment, /* [O] The alignment of the type, or NULL. */
1147 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1149 int default_tdesc;
1150 int scratch;
1151 int typeoffset;
1152 int arrayoffset;
1153 int *typedata;
1154 int *arraydata;
1155 int target_type;
1156 int child_size;
1158 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
1159 if (!width) width = &scratch;
1160 if (!alignment) alignment = &scratch;
1161 if (!decoded_size) decoded_size = &scratch;
1163 *decoded_size = 0;
1165 switch (tdesc->vt) {
1166 case VT_UI1:
1167 case VT_I1:
1168 *encoded_tdesc = default_tdesc;
1169 *width = 1;
1170 *alignment = 1;
1171 break;
1173 case VT_INT:
1174 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
1175 if (ctl2_get_syskind(This) == SYS_WIN16) {
1176 *width = 2;
1177 *alignment = 2;
1178 } else {
1179 *width = 4;
1180 *alignment = 4;
1182 break;
1184 case VT_UINT:
1185 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
1186 if (ctl2_get_syskind(This) == SYS_WIN16) {
1187 *width = 2;
1188 *alignment = 2;
1189 } else {
1190 *width = 4;
1191 *alignment = 4;
1193 break;
1195 case VT_UI2:
1196 case VT_I2:
1197 case VT_BOOL:
1198 *encoded_tdesc = default_tdesc;
1199 *width = 2;
1200 *alignment = 2;
1201 break;
1203 case VT_I4:
1204 case VT_UI4:
1205 case VT_R4:
1206 case VT_ERROR:
1207 case VT_BSTR:
1208 case VT_HRESULT:
1209 *encoded_tdesc = default_tdesc;
1210 *width = 4;
1211 *alignment = 4;
1212 break;
1214 case VT_CY:
1215 *encoded_tdesc = default_tdesc;
1216 *width = 8;
1217 *alignment = 4; /* guess? */
1218 break;
1220 case VT_VOID:
1221 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1222 *width = 0;
1223 *alignment = 1;
1224 break;
1226 case VT_PTR:
1227 case VT_SAFEARRAY:
1228 /* FIXME: Make with the error checking. */
1229 FIXME("PTR or SAFEARRAY vartype, may not work correctly.\n");
1231 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1233 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1234 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1235 if (((typedata[0] & 0xffff) == tdesc->vt) && (typedata[1] == target_type)) break;
1238 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1239 int mix_field;
1241 if (target_type & 0x80000000) {
1242 mix_field = (target_type >> 16) & VT_TYPEMASK;
1243 } else {
1244 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1245 switch((typedata[0]>>16) & ~VT_ARRAY)
1247 case VT_UI1:
1248 case VT_I1:
1249 case VT_UI2:
1250 case VT_I2:
1251 case VT_I4:
1252 case VT_UI4:
1253 mix_field = typedata[0]>>16;
1254 break;
1255 default:
1256 mix_field = ((typedata[0] >> 16) == 0x7fff) ? 0x7fff : 0x7ffe;
1257 break;
1261 if (tdesc->vt == VT_PTR)
1262 mix_field |= VT_BYREF;
1263 else if (tdesc->vt == VT_SAFEARRAY)
1264 mix_field |= VT_ARRAY;
1266 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1267 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1269 typedata[0] = (mix_field << 16) | tdesc->vt;
1270 typedata[1] = target_type;
1273 *encoded_tdesc = typeoffset;
1275 *width = 4;
1276 *alignment = 4;
1277 *decoded_size = sizeof(TYPEDESC) + child_size;
1278 break;
1280 case VT_CARRAY:
1282 /* FIXME: Make with the error checking. */
1283 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1285 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1286 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1287 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1289 arraydata[0] = target_type;
1290 arraydata[1] = num_dims;
1291 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1292 arraydata += 2;
1294 for(dim = 0; dim < num_dims; dim++) {
1295 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1296 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1297 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1298 arraydata += 2;
1300 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1301 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1303 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1304 typedata[1] = arrayoffset;
1306 *encoded_tdesc = typeoffset;
1307 *width = *width * elements;
1308 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1310 break;
1312 case VT_USERDEFINED:
1314 const MSFT_TypeInfoBase *basetype;
1315 INT basevt = 0x7fff;
1317 TRACE("USERDEFINED.\n");
1318 if (tdesc->u.hreftype % sizeof(*basetype) == 0 && tdesc->u.hreftype < This->typelib_segdir[MSFT_SEG_TYPEINFO].length)
1320 basetype = (MSFT_TypeInfoBase*)&(This->typelib_segment_data[MSFT_SEG_TYPEINFO][tdesc->u.hreftype]);
1321 switch(basetype->typekind & 0xf)
1323 case TKIND_ENUM:
1324 basevt = VT_I4;
1325 break;
1326 default:
1327 FIXME("USERDEFINED basetype %d not handled\n", basetype->typekind & 0xf);
1328 break;
1331 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1332 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1333 if ((typedata[0] == ((basevt << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1336 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1337 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1338 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1340 typedata[0] = (basevt << 16) | VT_USERDEFINED;
1341 typedata[1] = tdesc->u.hreftype;
1344 *encoded_tdesc = typeoffset;
1345 *width = 0;
1346 *alignment = 1;
1347 break;
1350 default:
1351 FIXME("Unrecognized type %d.\n", tdesc->vt);
1352 *encoded_tdesc = default_tdesc;
1353 *width = 0;
1354 *alignment = 1;
1355 break;
1358 return 0;
1361 /****************************************************************************
1362 * ctl2_decode_typedesc
1364 * Decodes a type description from an ICreateTypeLib2Impl.
1366 * RETURNS
1368 * Success: S_OK.
1369 * Failure: HRESULT error code.
1371 static HRESULT ctl2_decode_typedesc(
1372 ICreateTypeLib2Impl *This, /* [I] The type library from which to decode the TYPEDESC. */
1373 int encoded_tdesc, /* [I] The encoded type description. */
1374 TYPEDESC *tdesc) /* [O] The decoded type description. */
1376 int *typedata, i;
1377 HRESULT hres;
1379 if (encoded_tdesc & 0x80000000) {
1380 tdesc->vt = encoded_tdesc & VT_TYPEMASK;
1381 tdesc->u.lptdesc = NULL;
1382 return S_OK;
1385 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][encoded_tdesc];
1387 tdesc->vt = typedata[0] & 0xFFFF;
1389 switch(tdesc->vt) {
1390 case VT_PTR:
1391 case VT_SAFEARRAY:
1392 tdesc->u.lptdesc = heap_alloc_zero(sizeof(TYPEDESC));
1393 if (!tdesc->u.lptdesc)
1394 return E_OUTOFMEMORY;
1396 hres = ctl2_decode_typedesc(This, typedata[1], tdesc->u.lptdesc);
1397 if (FAILED(hres)) {
1398 heap_free(tdesc->u.lptdesc);
1399 return hres;
1402 return S_OK;
1404 case VT_CARRAY: {
1405 int arrayoffset, *arraydata, num_dims;
1407 arrayoffset = typedata[1];
1408 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1409 num_dims = arraydata[1] & 0xFFFF;
1411 tdesc->u.lpadesc = heap_alloc_zero(sizeof(ARRAYDESC) + sizeof(SAFEARRAYBOUND) * (num_dims - 1));
1412 if (!tdesc->u.lpadesc)
1413 return E_OUTOFMEMORY;
1415 hres = ctl2_decode_typedesc(This, arraydata[0], &tdesc->u.lpadesc->tdescElem);
1416 if (FAILED(hres)) {
1417 heap_free(tdesc->u.lpadesc);
1418 return E_OUTOFMEMORY;
1421 for (i = 0; i < num_dims; ++i) {
1422 tdesc->u.lpadesc->rgbounds[i].cElements = arraydata[2 + i * 2];
1423 tdesc->u.lpadesc->rgbounds[i].lLbound = arraydata[3 + i * 2];
1426 return S_OK;
1428 case VT_USERDEFINED:
1429 tdesc->u.hreftype = typedata[1];
1430 return S_OK;
1431 default:
1432 FIXME("unable to decode typedesc (%08x): unknown VT: %d\n", encoded_tdesc, tdesc->vt);
1433 return E_NOTIMPL;
1437 /****************************************************************************
1438 * ctl2_find_nth_reference
1440 * Finds a reference by index into the linked list of reference records.
1442 * RETURNS
1444 * Success: Offset of the desired reference record.
1445 * Failure: -1.
1447 static int ctl2_find_nth_reference(
1448 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1449 int offset, /* [I] The starting offset of the reference list. */
1450 int index) /* [I] The index of the reference to find. */
1452 MSFT_RefRecord *ref;
1454 for (; index && (offset != -1); index--) {
1455 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1456 offset = ref->onext;
1459 return offset;
1462 /****************************************************************************
1463 * ctl2_find_typeinfo_from_offset
1465 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1467 * RETURNS
1469 * Success: S_OK.
1470 * Failure: TYPE_E_ELEMENTNOTFOUND.
1472 static HRESULT ctl2_find_typeinfo_from_offset(
1473 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1474 int offset, /* [I] The offset of the desired typeinfo. */
1475 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1477 void *typeinfodata;
1478 ICreateTypeInfo2Impl *typeinfo;
1480 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1482 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1483 if (typeinfo->typeinfo == typeinfodata) {
1484 *ppTinfo = (ITypeInfo *)&typeinfo->ITypeInfo2_iface;
1485 ITypeInfo2_AddRef(*ppTinfo);
1486 return S_OK;
1490 ERR("Failed to find typeinfo, invariant varied.\n");
1492 return TYPE_E_ELEMENTNOTFOUND;
1495 /****************************************************************************
1496 * funcrecord_reallochdr
1498 * Ensure FuncRecord data block contains header of required size
1500 * PARAMS
1502 * typedata [IO] - reference to pointer to data block
1503 * need [I] - required size of block in bytes
1505 * RETURNS
1507 * Number of additionally allocated bytes
1509 static INT funcrecord_reallochdr(INT **typedata, int need)
1511 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1512 int hdr = (*typedata)[0] - tail;
1513 int i;
1515 if (hdr >= need)
1516 return 0;
1518 *typedata = heap_realloc(*typedata, need + tail);
1519 if (!*typedata)
1520 return -1;
1522 if (tail)
1523 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1524 (*typedata)[0] = need + tail;
1526 /* fill in default values */
1527 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1529 switch(i)
1531 case 2:
1532 (*typedata)[i] = 0;
1533 break;
1534 case 7:
1535 (*typedata)[i] = -1;
1536 break;
1537 case 8:
1538 (*typedata)[i] = -1;
1539 break;
1540 case 9:
1541 (*typedata)[i] = -1;
1542 break;
1543 case 10:
1544 (*typedata)[i] = -1;
1545 break;
1546 case 11:
1547 (*typedata)[i] = 0;
1548 break;
1549 case 12:
1550 (*typedata)[i] = -1;
1551 break;
1555 return need - hdr;
1558 /*================== ICreateTypeInfo2 Implementation ===================================*/
1560 /******************************************************************************
1561 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1564 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1565 ICreateTypeInfo2 * iface,
1566 REFIID riid,
1567 VOID **ppvObject)
1569 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1571 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1573 *ppvObject=NULL;
1574 if(IsEqualIID(riid, &IID_IUnknown) ||
1575 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1576 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1578 *ppvObject = This;
1579 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1580 IsEqualIID(riid, &IID_ITypeInfo2)) {
1581 *ppvObject = &This->ITypeInfo2_iface;
1584 if(*ppvObject)
1586 ICreateTypeInfo2_AddRef(iface);
1587 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1588 return S_OK;
1590 TRACE("-- Interface: E_NOINTERFACE\n");
1591 return E_NOINTERFACE;
1594 /******************************************************************************
1595 * ICreateTypeInfo2_AddRef {OLEAUT32}
1597 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1599 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1600 ULONG ref = InterlockedIncrement(&This->ref);
1602 TRACE("(%p)->ref was %u\n",This, ref - 1);
1604 if(ref==1 && This->typelib)
1605 ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This->typelib);
1607 return ref;
1610 /******************************************************************************
1611 * ICreateTypeInfo2_Release {OLEAUT32}
1613 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1615 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1616 ULONG ref = InterlockedDecrement(&This->ref);
1618 TRACE("(%p)->(%u)\n",This, ref);
1620 if (!ref) {
1621 if (This->typelib) {
1622 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)This->typelib);
1623 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1624 /* This->typelib = NULL; */
1627 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1628 /* HeapFree(GetProcessHeap(),0,This); */
1629 return 0;
1632 return ref;
1636 /******************************************************************************
1637 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1639 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1641 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1643 MSFT_GuidEntry guidentry;
1644 int offset;
1646 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1648 guidentry.guid = *guid;
1649 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1650 guidentry.next_hash = -1;
1652 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1654 if (offset == -1) return E_OUTOFMEMORY;
1656 This->typeinfo->posguid = offset;
1658 if (IsEqualIID(guid, &IID_IDispatch)) {
1659 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1662 return S_OK;
1665 /******************************************************************************
1666 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1668 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1670 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1672 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1674 if(uTypeFlags & TYPEFLAG_FDUAL) {
1675 This->typeinfo->typekind |= 0x10;
1676 This->typeinfo->typekind &= ~0x0f;
1677 This->typeinfo->typekind |= TKIND_DISPATCH;
1679 if(!This->dual) {
1680 This->dual = heap_alloc(sizeof(ICreateTypeInfo2Impl));
1681 if(!This->dual)
1682 return E_OUTOFMEMORY;
1684 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1685 This->dual->ref = 0;
1686 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1687 TKIND_INTERFACE : TKIND_DISPATCH;
1688 This->dual->dual = This;
1691 /* Make sure dispatch is in typeinfos queue */
1692 if(This->typekind != TKIND_DISPATCH) {
1693 if(This->typelib->last_typeinfo == This)
1694 This->typelib->last_typeinfo = This->dual;
1696 if(This->typelib->typeinfos == This)
1697 This->typelib->typeinfos = This->dual;
1698 else {
1699 ICreateTypeInfo2Impl *iter;
1701 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1702 iter->next_typeinfo = This->dual;
1704 } else
1705 iface = &This->dual->ICreateTypeInfo2_iface;
1708 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1709 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1710 ITypeLib *stdole;
1711 ITypeInfo *dispatch;
1712 HREFTYPE hreftype;
1713 HRESULT hres;
1715 hres = LoadTypeLib(stdole2tlb, &stdole);
1716 if(FAILED(hres))
1717 return hres;
1719 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1720 ITypeLib_Release(stdole);
1721 if(FAILED(hres))
1722 return hres;
1724 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1725 ITypeInfo_Release(dispatch);
1726 if(FAILED(hres))
1727 return hres;
1730 This->typeinfo->flags = uTypeFlags;
1731 return S_OK;
1734 /******************************************************************************
1735 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1737 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1738 ICreateTypeInfo2* iface,
1739 LPOLESTR pStrDoc)
1741 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1743 int offset;
1745 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1746 if (!pStrDoc)
1747 return E_INVALIDARG;
1749 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1750 if (offset == -1) return E_OUTOFMEMORY;
1751 This->typeinfo->docstringoffs = offset;
1752 return S_OK;
1755 /******************************************************************************
1756 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1758 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1759 ICreateTypeInfo2* iface,
1760 DWORD dwHelpContext)
1762 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1764 TRACE("(%p,%d)\n", iface, dwHelpContext);
1766 This->typeinfo->helpcontext = dwHelpContext;
1768 return S_OK;
1771 /******************************************************************************
1772 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1774 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1775 ICreateTypeInfo2* iface,
1776 WORD wMajorVerNum,
1777 WORD wMinorVerNum)
1779 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1781 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1783 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1784 return S_OK;
1787 /******************************************************************************
1788 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1790 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1791 ICreateTypeInfo2* iface,
1792 ITypeInfo* pTInfo,
1793 HREFTYPE* phRefType)
1795 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1797 ITypeLib *container;
1798 UINT index;
1799 HRESULT res;
1801 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1803 if(!pTInfo || !phRefType)
1804 return E_INVALIDARG;
1807 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1808 * same internal structure as one of ours. It could be from another
1809 * implementation of ITypeInfo. So we need to do the following...
1811 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1812 if (FAILED(res)) {
1813 TRACE("failed to find containing typelib.\n");
1814 return res;
1817 if (container == (ITypeLib *)&This->typelib->lpVtblTypeLib2) {
1818 /* Process locally defined TypeInfo */
1819 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1820 } else {
1821 BSTR name;
1822 TLIBATTR *tlibattr;
1823 TYPEATTR *typeattr;
1824 TYPEKIND typekind;
1825 MSFT_GuidEntry guid, *check_guid;
1826 MSFT_ImpInfo impinfo;
1827 int guid_offset, import_offset;
1828 HRESULT hres;
1830 /* Allocate container GUID */
1831 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1832 if(FAILED(hres)) {
1833 ITypeLib_Release(container);
1834 return hres;
1837 guid.guid = tlibattr->guid;
1838 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1839 guid.next_hash = -1;
1841 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1842 if(guid_offset == -1) {
1843 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1844 ITypeLib_Release(container);
1845 return E_OUTOFMEMORY;
1848 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1849 if(check_guid->hreftype == guid.hreftype)
1850 This->typelib->typelib_guids++;
1852 /* Get import file name */
1853 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1854 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1855 if(FAILED(hres)) {
1856 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1857 ITypeLib_Release(container);
1858 return hres;
1861 /* Import file */
1862 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1863 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1864 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1865 SysFreeString(name);
1867 if(import_offset == -1) {
1868 ITypeLib_Release(container);
1869 return E_OUTOFMEMORY;
1872 /* Allocate referenced guid */
1873 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1874 if(FAILED(hres)) {
1875 ITypeLib_Release(container);
1876 return hres;
1879 guid.guid = typeattr->guid;
1880 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1881 guid.next_hash = -1;
1882 typekind = typeattr->typekind;
1883 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1885 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1886 if(guid_offset == -1) {
1887 ITypeLib_Release(container);
1888 return E_OUTOFMEMORY;
1891 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1892 if(check_guid->hreftype == guid.hreftype)
1893 This->typelib->typeinfo_guids++;
1895 /* Allocate importinfo */
1896 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1897 impinfo.oImpFile = import_offset;
1898 impinfo.oGuid = guid_offset;
1899 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1901 if(IsEqualGUID(&guid.guid, &IID_IDispatch))
1902 This->typelib->typelib_header.dispatchpos = *phRefType;
1905 ITypeLib_Release(container);
1906 return S_OK;
1909 /******************************************************************************
1910 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1912 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1913 ICreateTypeInfo2* iface,
1914 UINT index,
1915 FUNCDESC* pFuncDesc)
1917 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1919 CyclicList *iter, *insert;
1920 int *typedata;
1921 int i, num_defaults = 0, num_retval = 0;
1922 int decoded_size;
1923 HRESULT hres;
1925 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1927 if(!pFuncDesc || pFuncDesc->oVft&3)
1928 return E_INVALIDARG;
1930 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1931 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1932 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1933 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1934 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1936 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1937 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1939 switch(This->typekind) {
1940 case TKIND_MODULE:
1941 if(pFuncDesc->funckind != FUNC_STATIC)
1942 return TYPE_E_BADMODULEKIND;
1943 break;
1944 case TKIND_DISPATCH:
1945 if(pFuncDesc->funckind != FUNC_DISPATCH)
1946 return TYPE_E_BADMODULEKIND;
1947 break;
1948 default:
1949 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1950 return TYPE_E_BADMODULEKIND;
1953 if(cti2_get_func_count(This->typeinfo) < index)
1954 return TYPE_E_ELEMENTNOTFOUND;
1956 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1957 !pFuncDesc->cParams)
1958 return TYPE_E_INCONSISTENTPROPFUNCS;
1960 /* get number of arguments with default values specified */
1961 for (i = 0; i < pFuncDesc->cParams; i++) {
1962 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1963 num_defaults++;
1964 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1965 num_retval++;
1968 if (!This->typedata) {
1969 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
1970 if(!This->typedata)
1971 return E_OUTOFMEMORY;
1973 This->typedata->next = This->typedata;
1975 if(This->dual)
1976 This->dual->typedata = This->typedata;
1979 /* allocate type data space for us */
1980 insert = alloc_cyclic_list_item(CyclicListFunc);
1981 if(!insert)
1982 return E_OUTOFMEMORY;
1983 insert->u.data = heap_alloc(FIELD_OFFSET(MSFT_FuncRecord, HelpContext) +
1984 sizeof(int[(num_defaults?4:3)])*pFuncDesc->cParams);
1985 if(!insert->u.data) {
1986 heap_free(insert);
1987 return E_OUTOFMEMORY;
1990 /* fill out the basic type information */
1991 typedata = insert->u.data;
1992 typedata[0] = FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
1993 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
1994 typedata[2] = pFuncDesc->wFuncFlags;
1995 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
1996 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
1997 if(num_defaults) typedata[4] |= 0x1000;
1998 if (num_retval) typedata[4] |= 0x4000;
1999 typedata[5] = pFuncDesc->cParams;
2001 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
2002 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
2003 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
2004 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
2006 /* add default values */
2007 if(num_defaults) {
2008 for (i = 0; i < pFuncDesc->cParams; i++)
2009 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
2010 hres = ctl2_encode_variant(This->typelib, typedata+6+i,
2011 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
2012 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
2014 if(FAILED(hres)) {
2015 heap_free(insert->u.data);
2016 heap_free(insert);
2017 return hres;
2019 } else
2020 typedata[6+i] = 0xffffffff;
2022 num_defaults = pFuncDesc->cParams;
2025 /* add arguments */
2026 for (i = 0; i < pFuncDesc->cParams; i++) {
2027 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
2028 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
2029 typedata[7+num_defaults+(i*3)] = -1;
2030 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
2031 typedata[3] += decoded_size << 16;
2034 /* update the index data */
2035 insert->indice = pFuncDesc->memid;
2036 insert->name = -1;
2038 /* insert type data to list */
2039 if(index == cti2_get_func_count(This->typeinfo)) {
2040 insert->next = This->typedata->next;
2041 This->typedata->next = insert;
2042 This->typedata = insert;
2044 if(This->dual)
2045 This->dual->typedata = This->typedata;
2046 } else {
2047 iter = This->typedata->next;
2048 for(i=0; i<index; i++)
2049 iter = iter->next;
2051 insert->next = iter->next;
2052 iter->next = insert;
2055 /* update type data size */
2056 This->typedata->next->u.val += FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
2058 /* Increment the number of function elements */
2059 This->typeinfo->cElement += 1;
2061 return S_OK;
2064 /******************************************************************************
2065 * ICreateTypeInfo2_AddImplType {OLEAUT32}
2067 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
2068 ICreateTypeInfo2* iface,
2069 UINT index,
2070 HREFTYPE hRefType)
2072 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2074 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
2076 if (This->typekind == TKIND_COCLASS) {
2077 int offset;
2078 MSFT_RefRecord *ref;
2080 if (index == 0) {
2081 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
2083 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2084 if (offset == -1) return E_OUTOFMEMORY;
2086 This->typeinfo->datatype1 = offset;
2087 } else {
2088 int lastoffset;
2090 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
2091 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
2093 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
2094 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
2096 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2097 if (offset == -1) return E_OUTOFMEMORY;
2099 ref->onext = offset;
2102 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2104 ref->reftype = hRefType;
2105 ref->flags = 0;
2106 ref->oCustData = -1;
2107 ref->onext = -1;
2108 This->typeinfo->cImplTypes++;
2109 } else if (This->typekind == TKIND_INTERFACE) {
2110 if (This->typeinfo->cImplTypes && index==1)
2111 return TYPE_E_BADMODULEKIND;
2113 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
2115 This->typeinfo->datatype1 = hRefType;
2116 This->typeinfo->cImplTypes = 1;
2117 } else if (This->typekind == TKIND_DISPATCH) {
2118 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
2120 /* FIXME: Check if referenced typeinfo is IDispatch */
2121 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2122 This->typeinfo->cImplTypes = 1;
2123 } else {
2124 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
2125 return E_OUTOFMEMORY;
2128 return S_OK;
2131 /******************************************************************************
2132 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
2134 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
2135 ICreateTypeInfo2* iface,
2136 UINT index,
2137 INT implTypeFlags)
2139 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2140 int offset;
2141 MSFT_RefRecord *ref;
2143 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
2145 if (This->typekind != TKIND_COCLASS) {
2146 return TYPE_E_BADMODULEKIND;
2149 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2150 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2152 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2153 ref->flags = implTypeFlags;
2155 return S_OK;
2158 /******************************************************************************
2159 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2161 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2162 ICreateTypeInfo2* iface,
2163 WORD cbAlignment)
2165 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2167 TRACE("(%p,%d)\n", iface, cbAlignment);
2169 if (!cbAlignment) return E_INVALIDARG;
2170 if (cbAlignment > 16) return E_INVALIDARG;
2172 This->typeinfo->typekind &= ~0xffc0;
2173 This->typeinfo->typekind |= cbAlignment << 6;
2175 /* FIXME: There's probably some way to simplify this. */
2176 switch (This->typekind) {
2177 case TKIND_ALIAS:
2178 default:
2179 break;
2181 case TKIND_ENUM:
2182 case TKIND_INTERFACE:
2183 case TKIND_DISPATCH:
2184 case TKIND_COCLASS:
2185 if (cbAlignment > 4) cbAlignment = 4;
2186 break;
2188 case TKIND_RECORD:
2189 case TKIND_MODULE:
2190 case TKIND_UNION:
2191 cbAlignment = 1;
2192 break;
2195 This->typeinfo->typekind |= cbAlignment << 11;
2197 return S_OK;
2200 /******************************************************************************
2201 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2203 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2204 ICreateTypeInfo2* iface,
2205 LPOLESTR pStrSchema)
2207 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2208 return E_OUTOFMEMORY;
2211 /******************************************************************************
2212 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2214 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2215 ICreateTypeInfo2* iface,
2216 UINT index,
2217 VARDESC* pVarDesc)
2219 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2221 HRESULT status = S_OK;
2222 CyclicList *insert;
2223 INT *typedata;
2224 int var_datawidth;
2225 int var_alignment;
2226 int var_type_size;
2227 int alignment;
2229 TRACE("(%p,%d,%p)\n", iface, index, pVarDesc);
2230 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2231 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2232 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2233 pVarDesc->wVarFlags, pVarDesc->varkind);
2235 if (cti2_get_var_count(This->typeinfo) != index)
2236 return TYPE_E_ELEMENTNOTFOUND;
2238 if (!This->typedata) {
2239 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
2240 if(!This->typedata)
2241 return E_OUTOFMEMORY;
2243 This->typedata->next = This->typedata;
2245 if(This->dual)
2246 This->dual->typedata = This->typedata;
2249 insert = alloc_cyclic_list_item(CyclicListVar);
2250 if(!insert)
2251 return E_OUTOFMEMORY;
2253 /* allocate whole structure, it's fixed size always */
2254 insert->u.data = heap_alloc(sizeof(MSFT_VarRecord));
2255 if(!insert->u.data) {
2256 heap_free(insert);
2257 return E_OUTOFMEMORY;
2260 insert->next = This->typedata->next;
2261 This->typedata->next = insert;
2262 This->typedata = insert;
2264 if(This->dual)
2265 This->dual->typedata = This->typedata;
2267 This->typedata->next->u.val += FIELD_OFFSET(MSFT_VarRecord, HelpContext);
2268 typedata = This->typedata->u.data;
2270 /* fill out the basic type information */
2272 /* no optional fields initially */
2273 typedata[0] = FIELD_OFFSET(MSFT_VarRecord, HelpContext) | (index << 16);
2274 typedata[2] = pVarDesc->wVarFlags;
2275 typedata[3] = (sizeof(VARDESC) << 16) | pVarDesc->varkind;
2277 /* update the index data */
2278 insert->indice = 0x40000000 + index;
2279 insert->name = -1;
2281 /* figure out type widths and whatnot */
2282 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2283 &typedata[1], &var_datawidth, &var_alignment,
2284 &var_type_size);
2286 if (pVarDesc->varkind != VAR_CONST)
2288 /* pad out starting position to data width */
2289 This->datawidth += var_alignment - 1;
2290 This->datawidth &= ~(var_alignment - 1);
2291 typedata[4] = This->datawidth;
2293 /* add the new variable to the total data width */
2294 This->datawidth += var_datawidth;
2295 if(This->dual)
2296 This->dual->datawidth = This->datawidth;
2298 /* add type description size to total required allocation */
2299 typedata[3] += var_type_size << 16;
2301 /* fix type alignment */
2302 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2303 if (alignment < var_alignment) {
2304 alignment = var_alignment;
2305 This->typeinfo->typekind &= ~0xf800;
2306 This->typeinfo->typekind |= alignment << 11;
2309 /* ??? */
2310 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2311 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2312 This->typeinfo->res2 <<= 1;
2315 /* ??? */
2316 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2317 This->typeinfo->res3 += 0x2c;
2319 /* pad data width to alignment */
2320 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2321 } else {
2322 VARIANT *value = pVarDesc->DUMMYUNIONNAME.lpvarValue;
2323 status = ctl2_encode_variant(This->typelib, typedata+4, value, V_VT(value));
2324 /* ??? native sets size 0x34 */
2325 typedata[3] += 0x10 << 16;
2328 /* increment the number of variable elements */
2329 This->typeinfo->cElement += 0x10000;
2331 return status;
2334 /******************************************************************************
2335 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2337 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2338 ICreateTypeInfo2* iface,
2339 UINT index,
2340 LPOLESTR* names,
2341 UINT cNames)
2343 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2344 CyclicList *iter, *iter2;
2345 int offset, len, i;
2346 unsigned char *namedata;
2348 TRACE("(%p %d %p %d)\n", This, index, names, cNames);
2350 if(!names)
2351 return E_INVALIDARG;
2353 if(index >= cti2_get_func_count(This->typeinfo) || cNames == 0)
2354 return TYPE_E_ELEMENTNOTFOUND;
2356 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2357 if (iter->type == CyclicListFunc)
2358 if (i++ >= index)
2359 break;
2361 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2362 if(cNames != iter->u.data[5] + (ctl2_get_invokekind(iter) & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2363 return TYPE_E_ELEMENTNOTFOUND;
2365 TRACE("function name %s\n", debugstr_w(names[0]));
2366 len = ctl2_encode_name(This->typelib, names[0], (char**)&namedata);
2367 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2369 int cmp = memcmp(namedata, This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len);
2370 if (iter2->name != -1 && cmp == 0) {
2371 if (iter2->type == CyclicListFunc) {
2372 INVOKEKIND inv1 = ctl2_get_invokekind(iter);
2373 INVOKEKIND inv2 = ctl2_get_invokekind(iter2);
2375 /* it's allowed to have PUT, PUTREF and GET methods with the same name */
2376 if ((inv1 != inv2) &&
2377 (inv1 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)) &&
2378 (inv2 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)))
2379 continue;
2382 return TYPE_E_AMBIGUOUSNAME;
2386 offset = ctl2_alloc_name(This->typelib, names[0]);
2387 if(offset == -1)
2388 return E_OUTOFMEMORY;
2390 iter->name = offset;
2392 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2393 if (*((INT*)namedata) == -1)
2394 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2396 len = ctl2_get_record_size(iter)/4 - iter->u.data[5]*3;
2398 for (i = 1; i < cNames; i++) {
2399 offset = ctl2_alloc_name(This->typelib, names[i]);
2400 iter->u.data[len + ((i-1)*3) + 1] = offset;
2403 return S_OK;
2406 /******************************************************************************
2407 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2409 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2410 ICreateTypeInfo2* iface,
2411 UINT index,
2412 LPOLESTR szName)
2414 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2415 CyclicList *iter;
2416 int offset, i;
2417 unsigned char *namedata;
2419 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(szName));
2421 if (cti2_get_var_count(This->typeinfo) <= index)
2422 return TYPE_E_ELEMENTNOTFOUND;
2424 offset = ctl2_alloc_name(This->typelib, szName);
2425 if (offset == -1) return E_OUTOFMEMORY;
2427 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2428 if (*((INT *)namedata) == -1) {
2429 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2430 namedata[9] |= 0x10;
2432 if (This->typekind == TKIND_ENUM) {
2433 namedata[9] |= 0x20;
2436 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2437 if (iter->type == CyclicListVar)
2438 if (i++ >= index)
2439 break;
2441 iter->name = offset;
2442 return S_OK;
2445 /******************************************************************************
2446 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2448 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2449 ICreateTypeInfo2* iface,
2450 TYPEDESC* pTDescAlias)
2452 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2454 int encoded_typedesc;
2455 int width;
2457 if (This->typekind != TKIND_ALIAS) {
2458 return TYPE_E_WRONGTYPEKIND;
2461 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2463 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2464 return E_OUTOFMEMORY;
2467 This->typeinfo->size = width;
2468 This->typeinfo->datatype1 = encoded_typedesc;
2470 return S_OK;
2473 /******************************************************************************
2474 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2476 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2477 ICreateTypeInfo2* iface,
2478 UINT index,
2479 LPOLESTR szDllName,
2480 LPOLESTR szProcName)
2482 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2483 return E_OUTOFMEMORY;
2486 /******************************************************************************
2487 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2489 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2490 ICreateTypeInfo2* iface,
2491 UINT index,
2492 LPOLESTR szDocString)
2494 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2495 return E_OUTOFMEMORY;
2498 /******************************************************************************
2499 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2501 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2502 ICreateTypeInfo2* iface,
2503 UINT index,
2504 LPOLESTR docstring)
2506 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2507 CyclicList *iter;
2509 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(docstring));
2511 if (!docstring) return E_INVALIDARG;
2513 if (cti2_get_var_count(This->typeinfo) <= index)
2514 return TYPE_E_ELEMENTNOTFOUND;
2516 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2517 if (iter->type == CyclicListVar)
2519 if (index-- == 0)
2521 int offset = ctl2_alloc_string(This->typelib, docstring);
2523 if (offset == -1) return E_OUTOFMEMORY;
2524 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, res9));
2525 iter->u.data[6] = offset;
2526 return S_OK;
2530 return TYPE_E_ELEMENTNOTFOUND;
2533 /******************************************************************************
2534 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2536 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2537 ICreateTypeInfo2* iface,
2538 UINT index,
2539 DWORD dwHelpContext)
2541 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2542 CyclicList *func;
2544 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2546 if(cti2_get_func_count(This->typeinfo) < index)
2547 return TYPE_E_ELEMENTNOTFOUND;
2549 if(cti2_get_func_count(This->typeinfo) == index && This->typedata->type == CyclicListFunc)
2550 func = This->typedata;
2551 else
2552 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2553 if (func->type == CyclicListFunc)
2554 if(index-- == 0)
2555 break;
2557 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2558 if(!func->u.data)
2559 return E_OUTOFMEMORY;
2561 func->u.data[6] = dwHelpContext;
2562 return S_OK;
2565 /******************************************************************************
2566 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2568 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2569 ICreateTypeInfo2* iface,
2570 UINT index,
2571 DWORD context)
2573 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2574 CyclicList *iter;
2576 TRACE("(%p,%d,%d)\n", This, index, context);
2578 if (cti2_get_var_count(This->typeinfo) <= index)
2579 return TYPE_E_ELEMENTNOTFOUND;
2581 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2582 if (iter->type == CyclicListVar)
2584 if (index-- == 0)
2586 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, HelpString));
2587 iter->u.data[5] = context;
2588 return S_OK;
2592 return TYPE_E_ELEMENTNOTFOUND;
2595 /******************************************************************************
2596 * ICreateTypeInfo2_SetMops {OLEAUT32}
2598 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2599 ICreateTypeInfo2* iface,
2600 UINT index,
2601 BSTR bstrMops)
2603 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2604 return E_OUTOFMEMORY;
2607 /******************************************************************************
2608 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2610 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2611 ICreateTypeInfo2* iface,
2612 IDLDESC* pIdlDesc)
2614 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2615 return E_OUTOFMEMORY;
2618 /******************************************************************************
2619 * ICreateTypeInfo2_LayOut {OLEAUT32}
2621 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2622 ICreateTypeInfo2* iface)
2624 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2625 CyclicList *iter, *iter2, *last = NULL, **typedata;
2626 HREFTYPE hreftype;
2627 HRESULT hres;
2628 unsigned user_vft = 0;
2629 int i;
2631 TRACE("(%p)\n", This);
2633 /* FIXME: LayOut should be run on all ImplTypes */
2634 if(This->typekind == TKIND_COCLASS || This->typekind == TKIND_ALIAS)
2635 return S_OK;
2637 /* Validate inheritance */
2638 This->typeinfo->datatype2 = 0;
2639 hreftype = This->typeinfo->datatype1;
2641 /* Process internally defined interfaces */
2642 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2643 MSFT_TypeInfoBase *header;
2645 if(hreftype&1)
2646 break;
2648 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2649 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2650 hreftype = header->datatype1;
2652 if(i == This->typelib->typelib_header.nrtypeinfos)
2653 return TYPE_E_CIRCULARTYPE;
2655 /* Process externally defined interfaces */
2656 if(hreftype != -1) {
2657 ITypeInfo *cur, *next;
2658 TYPEATTR *typeattr;
2660 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2661 if(FAILED(hres))
2662 return hres;
2664 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2665 ITypeInfo_Release(next);
2666 if(FAILED(hres))
2667 return hres;
2670 while(1) {
2671 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2672 if(FAILED(hres)) {
2673 ITypeInfo_Release(cur);
2674 return hres;
2677 if(IsEqualGUID(&typeattr->guid, &IID_IDispatch))
2678 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2680 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2681 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2683 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2684 if(hres == TYPE_E_ELEMENTNOTFOUND)
2685 break;
2686 if(FAILED(hres)) {
2687 ITypeInfo_Release(cur);
2688 return hres;
2691 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2692 if(FAILED(hres)) {
2693 ITypeInfo_Release(cur);
2694 return hres;
2697 ITypeInfo_Release(cur);
2698 cur = next;
2700 ITypeInfo_Release(cur);
2703 /* Get cbSizeVft of inherited interface */
2704 /* Makes LayOut running recursively */
2705 if(This->typeinfo->datatype1 != -1) {
2706 ITypeInfo *cur, *inherited;
2707 TYPEATTR *typeattr;
2709 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2710 if(FAILED(hres))
2711 return hres;
2713 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2714 ITypeInfo_Release(cur);
2715 if(FAILED(hres))
2716 return hres;
2718 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2719 if(FAILED(hres)) {
2720 ITypeInfo_Release(inherited);
2721 return hres;
2724 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2726 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2727 ITypeInfo_Release(inherited);
2728 } else
2729 This->typeinfo->cbSizeVft = 0;
2731 if(!This->typedata)
2732 return S_OK;
2734 typedata = heap_alloc(sizeof(CyclicList*)*cti2_get_func_count(This->typeinfo));
2735 if(!typedata)
2736 return E_OUTOFMEMORY;
2738 /* Assign IDs and VTBL entries */
2739 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2740 if (iter->type == CyclicListFunc)
2741 last = iter;
2743 if(last && last->u.data[3]&1)
2744 user_vft = last->u.data[3]&0xffff;
2746 i = 0;
2747 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2748 /* Assign MEMBERID if MEMBERID_NIL was specified */
2749 if(iter->indice == MEMBERID_NIL) {
2750 iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2752 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2753 if(iter == iter2) continue;
2754 if(iter2->indice == iter->indice) {
2755 iter->indice = 0x60000000 + This->typeinfo->cElement + (This->typeinfo->datatype2<<16);
2757 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2758 if(iter == iter2) continue;
2759 if(iter2->indice == iter->indice) {
2760 ++iter->indice;
2761 iter2 = This->typedata->next;
2765 break;
2770 if (iter->type != CyclicListFunc)
2771 continue;
2773 typedata[i] = iter;
2775 iter->u.data[0] = ctl2_get_record_size(iter) | (i<<16);
2777 if((iter->u.data[3]&1) != (user_vft&1)) {
2778 heap_free(typedata);
2779 return TYPE_E_INVALIDID;
2782 if(user_vft&1) {
2783 if(user_vft < (iter->u.data[3]&0xffff))
2784 user_vft = (iter->u.data[3]&0xffff);
2786 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2787 heap_free(typedata);
2788 return TYPE_E_INVALIDID;
2790 } else if(This->typekind != TKIND_MODULE) {
2791 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2792 This->typeinfo->cbSizeVft += 4;
2795 /* Construct a list of elements with the same memberid */
2796 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2797 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2798 if(iter->indice == iter2->indice) {
2799 int v1, v2;
2801 v1 = iter->u.data[4] >> 16;
2802 v2 = iter2->u.data[4] >> 16;
2804 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2805 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2806 break;
2810 i++;
2813 if(user_vft)
2814 This->typeinfo->cbSizeVft = user_vft+3;
2816 for(i=0; i< cti2_get_func_count(This->typeinfo); i++) {
2817 if(typedata[i]->u.data[4]>>16 > i) {
2818 INVOKEKIND inv = ctl2_get_invokekind(typedata[i]);
2820 i = typedata[i]->u.data[4] >> 16;
2822 while(i > typedata[i]->u.data[4]>>16) {
2823 INVOKEKIND invkind = ctl2_get_invokekind(typedata[i]);
2825 if(inv & invkind) {
2826 heap_free(typedata);
2827 return TYPE_E_DUPLICATEID;
2830 i = typedata[i]->u.data[4] >> 16;
2831 inv |= invkind;
2834 if(inv & INVOKE_FUNC) {
2835 heap_free(typedata);
2836 return TYPE_E_INCONSISTENTPROPFUNCS;
2841 heap_free(typedata);
2842 return S_OK;
2845 /******************************************************************************
2846 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2848 * Delete a function description from a type.
2850 * RETURNS
2852 * Success: S_OK.
2853 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2855 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2856 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2857 UINT index) /* [I] The index of the function to delete. */
2859 FIXME("(%p,%d), stub!\n", iface, index);
2860 return E_OUTOFMEMORY;
2863 /******************************************************************************
2864 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2866 * Delete a function description from a type.
2868 * RETURNS
2870 * Success: S_OK.
2871 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2873 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2874 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2875 MEMBERID memid, /* [I] The member id of the function to delete. */
2876 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2878 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2879 return E_OUTOFMEMORY;
2882 /******************************************************************************
2883 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2885 * Delete a variable description from a type.
2887 * RETURNS
2889 * Success: S_OK.
2890 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2891 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2893 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2894 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2895 UINT index) /* [I] The index of the variable description to delete. */
2897 FIXME("(%p,%d), stub!\n", iface, index);
2898 return E_OUTOFMEMORY;
2901 /******************************************************************************
2902 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2904 * Delete a variable description from a type.
2906 * RETURNS
2908 * Success: S_OK.
2909 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2910 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2912 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2913 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2914 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2916 FIXME("(%p,%d), stub!\n", iface, memid);
2917 return E_OUTOFMEMORY;
2920 /******************************************************************************
2921 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2923 * Delete an interface implementation from a type. (?)
2925 * RETURNS
2927 * Success: S_OK.
2928 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2930 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2931 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2932 UINT index) /* [I] The index of the interface to delete. */
2934 FIXME("(%p,%d), stub!\n", iface, index);
2935 return E_OUTOFMEMORY;
2938 /******************************************************************************
2939 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2941 * Set the custom data for a type.
2943 * RETURNS
2945 * Success: S_OK.
2946 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2948 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2949 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2950 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2951 VARIANT* pVarVal) /* [I] The custom data. */
2953 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2955 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2957 if (!pVarVal)
2958 return E_INVALIDARG;
2960 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2963 /******************************************************************************
2964 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2966 * Set the custom data for a function.
2968 * RETURNS
2970 * Success: S_OK.
2971 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2973 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2974 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2975 UINT index, /* [I] The index of the function for which to set the custom data. */
2976 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2977 VARIANT* pVarVal) /* [I] The custom data. */
2979 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2980 CyclicList *iter;
2982 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2984 if(index >= cti2_get_func_count(This->typeinfo))
2985 return TYPE_E_ELEMENTNOTFOUND;
2987 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2988 if (iter->type == CyclicListFunc)
2989 if (index-- == 0)
2990 break;
2992 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
2993 if(!iter->u.data)
2994 return E_OUTOFMEMORY;
2996 iter->u.data[4] |= 0x80;
2997 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
3000 /******************************************************************************
3001 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
3003 * Set the custom data for a function parameter.
3005 * RETURNS
3007 * Success: S_OK.
3008 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3010 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
3011 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3012 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
3013 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
3014 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3015 VARIANT* pVarVal) /* [I] The custom data. */
3017 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3018 return E_OUTOFMEMORY;
3021 /******************************************************************************
3022 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
3024 * Set the custom data for a variable.
3026 * RETURNS
3028 * Success: S_OK.
3029 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3031 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
3032 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3033 UINT index, /* [I] The index of the variable on which to set the custom data. */
3034 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3035 VARIANT* pVarVal) /* [I] The custom data. */
3037 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3038 return E_OUTOFMEMORY;
3041 /******************************************************************************
3042 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
3044 * Set the custom data for an implemented interface.
3046 * RETURNS
3048 * Success: S_OK.
3049 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3051 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
3052 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
3053 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
3054 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3055 VARIANT* pVarVal) /* [I] The custom data. */
3057 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3058 return E_OUTOFMEMORY;
3061 /******************************************************************************
3062 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
3064 * Set the help string context for the typeinfo.
3066 * RETURNS
3068 * Success: S_OK.
3069 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3071 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
3072 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3073 ULONG helpcontext) /* [I] The help string context. */
3075 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
3077 TRACE("(%p, %d)\n", iface, helpcontext);
3079 This->typelib->typelib_header.helpcontext = helpcontext;
3081 return S_OK;
3084 /******************************************************************************
3085 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
3087 * Set the help string context for a function.
3089 * RETURNS
3091 * Success: S_OK.
3092 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3094 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
3095 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3096 UINT index, /* [I] The index for the function on which to set the help string context. */
3097 ULONG dwHelpStringContext) /* [I] The help string context. */
3099 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3100 return E_OUTOFMEMORY;
3103 /******************************************************************************
3104 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
3106 * Set the help string context for a variable.
3108 * RETURNS
3110 * Success: S_OK.
3111 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3113 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
3114 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3115 UINT index, /* [I] The index of the variable on which to set the help string context. */
3116 ULONG dwHelpStringContext) /* [I] The help string context */
3118 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3119 return E_OUTOFMEMORY;
3122 /******************************************************************************
3123 * ICreateTypeInfo2_Invalidate {OLEAUT32}
3125 * Undocumented function. (!)
3127 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
3128 ICreateTypeInfo2* iface)
3130 FIXME("(%p), stub!\n", iface);
3131 return E_OUTOFMEMORY;
3134 /******************************************************************************
3135 * ICreateTypeInfo2_SetName {OLEAUT32}
3137 * Set the name for a typeinfo.
3139 * RETURNS
3141 * Success: S_OK.
3142 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
3144 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
3145 ICreateTypeInfo2* iface,
3146 LPOLESTR szName)
3148 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
3149 return E_OUTOFMEMORY;
3152 /*================== ITypeInfo2 Implementation ===================================*/
3154 /******************************************************************************
3155 * ITypeInfo2_QueryInterface {OLEAUT32}
3157 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
3159 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3161 return ICreateTypeInfo2_QueryInterface(&This->ICreateTypeInfo2_iface, riid, ppv);
3164 /******************************************************************************
3165 * ITypeInfo2_AddRef {OLEAUT32}
3167 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3169 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3171 return ICreateTypeInfo2_AddRef(&This->ICreateTypeInfo2_iface);
3174 /******************************************************************************
3175 * ITypeInfo2_Release {OLEAUT32}
3177 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3179 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3181 return ICreateTypeInfo2_Release(&This->ICreateTypeInfo2_iface);
3184 /******************************************************************************
3185 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3187 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3188 ITypeInfo2* iface,
3189 TYPEATTR** ppTypeAttr)
3191 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3192 HRESULT hres;
3194 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3196 if(!ppTypeAttr)
3197 return E_INVALIDARG;
3199 hres = ICreateTypeInfo_LayOut(&This->ICreateTypeInfo2_iface);
3200 if(FAILED(hres))
3201 return hres;
3203 *ppTypeAttr = heap_alloc_zero(sizeof(TYPEATTR));
3204 if(!*ppTypeAttr)
3205 return E_OUTOFMEMORY;
3207 if(This->typeinfo->posguid != -1) {
3208 MSFT_GuidEntry *guid;
3210 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3211 (*ppTypeAttr)->guid = guid->guid;
3214 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3215 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3216 (*ppTypeAttr)->typekind = This->typekind;
3217 (*ppTypeAttr)->cFuncs = cti2_get_func_count(This->typeinfo);
3218 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3219 (*ppTypeAttr)->cFuncs += sizeof(IDispatchVtbl)/sizeof(void*);
3220 (*ppTypeAttr)->cVars = cti2_get_var_count(This->typeinfo);
3221 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3222 (*ppTypeAttr)->cbSizeVft = This->typekind == TKIND_DISPATCH ? sizeof(IDispatchVtbl) : This->typeinfo->cbSizeVft;
3223 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3224 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3225 (*ppTypeAttr)->wMajorVerNum = LOWORD(This->typeinfo->version);
3226 (*ppTypeAttr)->wMinorVerNum = HIWORD(This->typeinfo->version);
3228 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3229 FIXME("TKIND_ALIAS handling not implemented\n");
3231 return S_OK;
3234 /******************************************************************************
3235 * ITypeInfo2_GetTypeComp {OLEAUT32}
3237 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3238 ITypeInfo2* iface,
3239 ITypeComp** ppTComp)
3241 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3242 return E_OUTOFMEMORY;
3245 /******************************************************************************
3246 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3248 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3249 ITypeInfo2* iface,
3250 UINT index,
3251 FUNCDESC** ppFuncDesc)
3253 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3254 int i, *typedata, num_defaults = 0, hdr_len, tail, has_defaults;
3255 CyclicList *desc;
3256 HRESULT hres;
3258 TRACE("(%p,%d,%p), semi-stub\n", iface, index, ppFuncDesc);
3260 if (!ppFuncDesc)
3261 return E_INVALIDARG;
3263 if (index >= cti2_get_func_count(This->typeinfo))
3264 return TYPE_E_ELEMENTNOTFOUND;
3266 hres = ICreateTypeInfo2_LayOut(&This->ICreateTypeInfo2_iface);
3267 if (FAILED(hres))
3268 return hres;
3270 desc = This->typedata->next;
3271 for (i = index; i >= 0; ) {
3272 desc = desc->next;
3273 if (desc->type == CyclicListFunc)
3274 --i;
3277 typedata = desc->u.data;
3279 *ppFuncDesc = heap_alloc_zero(sizeof(FUNCDESC));
3280 if (!*ppFuncDesc)
3281 return E_OUTOFMEMORY;
3283 (*ppFuncDesc)->memid = desc->indice;
3284 (*ppFuncDesc)->lprgscode = NULL; /* FIXME: Unimplemented */
3285 (*ppFuncDesc)->funckind = typedata[4] & 0x7;
3286 (*ppFuncDesc)->invkind = (typedata[4] >> 3) & 0xF;
3287 (*ppFuncDesc)->callconv = (typedata[4] >> 8) & 0xF;
3288 (*ppFuncDesc)->cParams = typedata[5];
3289 (*ppFuncDesc)->cParamsOpt = 0; /* FIXME: Unimplemented*/
3290 (*ppFuncDesc)->oVft = typedata[3] & 0xFFFF;
3291 if ((*ppFuncDesc)->oVft)
3292 --(*ppFuncDesc)->oVft;
3293 (*ppFuncDesc)->cScodes = 0; /* FIXME: Unimplemented*/
3294 hres = ctl2_decode_typedesc(This->typelib, typedata[1],
3295 &(*ppFuncDesc)->elemdescFunc.tdesc);
3296 if (FAILED(hres)) {
3297 heap_free(*ppFuncDesc);
3298 return hres;
3300 (*ppFuncDesc)->wFuncFlags = typedata[2];
3302 has_defaults = typedata[4] & 0x1000;
3303 tail = typedata[5] * (has_defaults ? 16 : 12);
3304 hdr_len = (ctl2_get_record_size(desc) - tail) / sizeof(int);
3306 if ((*ppFuncDesc)->cParams > 0) {
3307 (*ppFuncDesc)->lprgelemdescParam = heap_alloc_zero((*ppFuncDesc)->cParams * sizeof(ELEMDESC));
3308 if (!(*ppFuncDesc)->lprgelemdescParam) {
3309 heap_free(*ppFuncDesc);
3310 return E_OUTOFMEMORY;
3312 if (has_defaults) {
3313 num_defaults = (*ppFuncDesc)->cParams;
3315 for (i = 0; i < num_defaults; ++i) {
3316 if (typedata[hdr_len + i] != 0xFFFFFFFF) {
3317 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags |= PARAMFLAG_FHASDEFAULT;
3319 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex = heap_alloc(sizeof(PARAMDESCEX));
3320 if (!(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex) {
3321 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3322 return E_OUTOFMEMORY;
3325 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->cBytes = sizeof(PARAMDESCEX);
3326 VariantInit(&(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3327 hres = ctl2_decode_variant(This->typelib, typedata[hdr_len + i],
3328 &(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3329 if (FAILED(hres)) {
3330 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3331 return hres;
3337 for (i = 0; i < (*ppFuncDesc)->cParams; ++i) {
3338 hres = ctl2_decode_typedesc(This->typelib, typedata[hdr_len + num_defaults + (i * 3)],
3339 &((*ppFuncDesc)->lprgelemdescParam + i)->tdesc);
3340 if (FAILED(hres)) {
3341 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3342 return hres;
3344 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags = typedata[hdr_len + num_defaults + (i * 3) + 2];
3348 return S_OK;
3351 /******************************************************************************
3352 * ITypeInfo2_GetVarDesc {OLEAUT32}
3354 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3355 ITypeInfo2* iface,
3356 UINT index,
3357 VARDESC** ppVarDesc)
3359 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3360 return E_OUTOFMEMORY;
3363 /******************************************************************************
3364 * ITypeInfo2_GetNames {OLEAUT32}
3366 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3367 ITypeInfo2* iface,
3368 MEMBERID memid,
3369 BSTR* rgBstrNames,
3370 UINT cMaxNames,
3371 UINT* pcNames)
3373 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3374 return E_OUTOFMEMORY;
3377 /******************************************************************************
3378 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3380 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3381 ITypeInfo2* iface,
3382 UINT index,
3383 HREFTYPE* pRefType)
3385 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3386 MSFT_RefRecord *ref;
3387 int offset;
3389 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3391 if(!pRefType)
3392 return E_INVALIDARG;
3394 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3395 if(index == -1) {
3396 *pRefType = -2;
3397 return S_OK;
3400 if(This->typekind == TKIND_DISPATCH)
3401 return ITypeInfo2_GetRefTypeOfImplType(&This->dual->ITypeInfo2_iface,
3402 index, pRefType);
3405 if(index>=This->typeinfo->cImplTypes)
3406 return TYPE_E_ELEMENTNOTFOUND;
3408 if(This->typekind == TKIND_INTERFACE) {
3409 *pRefType = This->typeinfo->datatype1 + 2;
3410 return S_OK;
3413 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3414 if(offset == -1)
3415 return TYPE_E_ELEMENTNOTFOUND;
3417 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3418 *pRefType = ref->reftype;
3419 return S_OK;
3422 /******************************************************************************
3423 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3425 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3426 ITypeInfo2* iface,
3427 UINT index,
3428 INT* pImplTypeFlags)
3430 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3431 int offset;
3432 MSFT_RefRecord *ref;
3434 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3436 if(!pImplTypeFlags)
3437 return E_INVALIDARG;
3439 if(index >= This->typeinfo->cImplTypes)
3440 return TYPE_E_ELEMENTNOTFOUND;
3442 if(This->typekind != TKIND_COCLASS) {
3443 *pImplTypeFlags = 0;
3444 return S_OK;
3447 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3448 if(offset == -1)
3449 return TYPE_E_ELEMENTNOTFOUND;
3451 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3452 *pImplTypeFlags = ref->flags;
3453 return S_OK;
3456 /******************************************************************************
3457 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3459 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3460 ITypeInfo2* iface,
3461 LPOLESTR* rgszNames,
3462 UINT cNames,
3463 MEMBERID* pMemId)
3465 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3466 return E_OUTOFMEMORY;
3469 /******************************************************************************
3470 * ITypeInfo2_Invoke {OLEAUT32}
3472 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3473 ITypeInfo2* iface,
3474 PVOID pvInstance,
3475 MEMBERID memid,
3476 WORD wFlags,
3477 DISPPARAMS* pDispParams,
3478 VARIANT* pVarResult,
3479 EXCEPINFO* pExcepInfo,
3480 UINT* puArgErr)
3482 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3483 return E_OUTOFMEMORY;
3486 /******************************************************************************
3487 * ITypeInfo2_GetDocumentation {OLEAUT32}
3489 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3490 ITypeInfo2* iface,
3491 MEMBERID memid,
3492 BSTR* pBstrName,
3493 BSTR* pBstrDocString,
3494 DWORD* pdwHelpContext,
3495 BSTR* pBstrHelpFile)
3497 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3498 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3499 INT nameoffset, docstringoffset, helpcontext;
3501 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3503 if (memid == -1)
3505 nameoffset = This->typeinfo->NameOffset;
3506 docstringoffset = This->typeinfo->docstringoffs;
3507 helpcontext = This->typeinfo->helpcontext;
3508 status = S_OK;
3509 } else {
3510 CyclicList *iter;
3511 if (This->typedata) {
3512 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3513 if (iter->indice == memid) {
3514 if (iter->type == CyclicListFunc) {
3515 const int *typedata = iter->u.data;
3516 int size = ctl2_get_record_size(iter) - typedata[5]*(typedata[4]&0x1000?16:12);
3518 nameoffset = iter->name;
3519 /* FIXME implement this once SetFuncDocString is implemented */
3520 docstringoffset = -1;
3521 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3523 status = S_OK;
3524 } else {
3525 FIXME("Not implemented for variable members\n");
3528 break;
3534 if (!status) {
3535 WCHAR *string;
3536 if (pBstrName) {
3537 if (nameoffset == -1)
3538 *pBstrName = NULL;
3539 else {
3540 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3541 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3542 ctl2_decode_name((char*)&name->namelen, &string);
3543 *pBstrName = SysAllocString(string);
3544 if(!*pBstrName)
3545 return E_OUTOFMEMORY;
3549 if (pBstrDocString) {
3550 if (docstringoffset == -1)
3551 *pBstrDocString = NULL;
3552 else {
3553 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3554 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3555 ctl2_decode_name((char*)&name->namelen, &string);
3556 *pBstrDocString = SysAllocString(string);
3557 if(!*pBstrDocString) {
3558 if (pBstrName) SysFreeString(*pBstrName);
3559 return E_OUTOFMEMORY;
3564 if (pdwHelpContext) {
3565 *pdwHelpContext = helpcontext;
3568 if (pBstrHelpFile) {
3569 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->lpVtblTypeLib2,
3570 -1, NULL, NULL, NULL, pBstrHelpFile);
3571 if (status) {
3572 if (pBstrName) SysFreeString(*pBstrName);
3573 if (pBstrDocString) SysFreeString(*pBstrDocString);
3578 return status;
3581 /******************************************************************************
3582 * ITypeInfo2_GetDllEntry {OLEAUT32}
3584 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3585 ITypeInfo2* iface,
3586 MEMBERID memid,
3587 INVOKEKIND invKind,
3588 BSTR* pBstrDllName,
3589 BSTR* pBstrName,
3590 WORD* pwOrdinal)
3592 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3593 return E_OUTOFMEMORY;
3596 /******************************************************************************
3597 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3599 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3600 ITypeInfo2* iface,
3601 HREFTYPE hRefType,
3602 ITypeInfo** ppTInfo)
3604 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3606 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3608 if(!ppTInfo)
3609 return E_INVALIDARG;
3611 if(hRefType==-2 && This->dual) {
3612 *ppTInfo = (ITypeInfo *)&This->dual->ITypeInfo2_iface;
3613 ITypeInfo_AddRef(*ppTInfo);
3614 return S_OK;
3617 if(hRefType&1) {
3618 ITypeLib *tl;
3619 MSFT_ImpInfo *impinfo;
3620 MSFT_ImpFile *impfile;
3621 MSFT_GuidEntry *guid;
3622 WCHAR *filename;
3623 HRESULT hres;
3625 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3626 return E_FAIL;
3628 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3629 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3630 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3632 ctl2_decode_string((unsigned char*)impfile->filename, &filename);
3634 hres = LoadTypeLib(filename, &tl);
3635 if(FAILED(hres))
3636 return hres;
3638 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3640 ITypeLib_Release(tl);
3641 return hres;
3642 } else {
3643 ICreateTypeInfo2Impl *iter;
3644 int i = 0;
3646 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3647 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3648 *ppTInfo = (ITypeInfo *)&iter->ITypeInfo2_iface;
3650 ITypeLib_AddRef(*ppTInfo);
3651 return S_OK;
3653 i++;
3657 return E_FAIL;
3660 /******************************************************************************
3661 * ITypeInfo2_AddressOfMember {OLEAUT32}
3663 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3664 ITypeInfo2* iface,
3665 MEMBERID memid,
3666 INVOKEKIND invKind,
3667 PVOID* ppv)
3669 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3670 return E_OUTOFMEMORY;
3673 /******************************************************************************
3674 * ITypeInfo2_CreateInstance {OLEAUT32}
3676 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3677 ITypeInfo2* iface,
3678 IUnknown* pUnkOuter,
3679 REFIID riid,
3680 PVOID* ppvObj)
3682 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3683 return E_OUTOFMEMORY;
3686 /******************************************************************************
3687 * ITypeInfo2_GetMops {OLEAUT32}
3689 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3690 ITypeInfo2* iface,
3691 MEMBERID memid,
3692 BSTR* pBstrMops)
3694 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3695 return E_OUTOFMEMORY;
3698 /******************************************************************************
3699 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3701 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3702 ITypeInfo2* iface,
3703 ITypeLib** ppTLib,
3704 UINT* pIndex)
3706 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3708 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3710 *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3711 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3712 *pIndex = This->typeinfo->typekind >> 16;
3714 return S_OK;
3717 static void release_typedesc(TYPEDESC *tdesc)
3719 while (tdesc) {
3720 TYPEDESC *next;
3721 if (tdesc->vt == VT_USERDEFINED)
3722 next = NULL;
3723 else
3724 next = tdesc->u.lptdesc;
3725 heap_free(tdesc);
3726 tdesc = next;
3730 /******************************************************************************
3731 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3733 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3734 ITypeInfo2* iface,
3735 TYPEATTR* pTypeAttr)
3737 TRACE("(%p,%p)\n", iface, pTypeAttr);
3739 if (pTypeAttr->tdescAlias.vt != VT_USERDEFINED)
3740 release_typedesc(pTypeAttr->tdescAlias.u.lptdesc);
3742 heap_free(pTypeAttr);
3745 /******************************************************************************
3746 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3748 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3749 ITypeInfo2* iface,
3750 FUNCDESC* pFuncDesc)
3752 int i;
3754 TRACE("(%p,%p)\n", iface, pFuncDesc);
3756 heap_free(pFuncDesc->lprgscode);
3758 if (pFuncDesc->lprgelemdescParam) {
3759 for (i = 0; i < pFuncDesc->cParams; ++i) {
3760 if (pFuncDesc->lprgelemdescParam[i].tdesc.vt != VT_USERDEFINED)
3761 release_typedesc(pFuncDesc->lprgelemdescParam[i].tdesc.u.lptdesc);
3763 if (pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex)
3765 VariantClear(&pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3766 heap_free(pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex);
3769 heap_free(pFuncDesc->lprgelemdescParam);
3772 heap_free(pFuncDesc->elemdescFunc.u.paramdesc.pparamdescex);
3774 if (pFuncDesc->elemdescFunc.tdesc.vt != VT_USERDEFINED)
3775 release_typedesc(pFuncDesc->elemdescFunc.tdesc.u.lptdesc);
3777 heap_free(pFuncDesc);
3780 /******************************************************************************
3781 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3783 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3784 ITypeInfo2* iface,
3785 VARDESC* pVarDesc)
3787 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3790 /******************************************************************************
3791 * ITypeInfo2_GetTypeKind {OLEAUT32}
3793 * Get the TYPEKIND value for a TypeInfo.
3795 * RETURNS
3797 * Success: S_OK.
3798 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3800 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3801 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3802 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3804 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3805 return E_OUTOFMEMORY;
3808 /******************************************************************************
3809 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3811 * Get the Type Flags for a TypeInfo.
3813 * RETURNS
3815 * Success: S_OK.
3816 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3818 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3819 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3820 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3822 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3823 return E_OUTOFMEMORY;
3826 /******************************************************************************
3827 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3829 * Gets the index of a function given its member id.
3831 * RETURNS
3833 * Success: S_OK.
3834 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3836 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3837 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3838 MEMBERID memid, /* [I] The member id for the function. */
3839 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3840 UINT* pFuncIndex) /* [O] The index of the function. */
3842 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3843 return E_OUTOFMEMORY;
3846 /******************************************************************************
3847 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3849 * Gets the index of a variable given its member id.
3851 * RETURNS
3853 * Success: S_OK.
3854 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3856 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3857 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3858 MEMBERID memid, /* [I] The member id for the variable. */
3859 UINT* pVarIndex) /* [O] The index of the variable. */
3861 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3862 return E_OUTOFMEMORY;
3865 /******************************************************************************
3866 * ITypeInfo2_GetCustData {OLEAUT32}
3868 * Gets a custom data element from a TypeInfo.
3870 * RETURNS
3872 * Success: S_OK.
3873 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3875 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3876 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3877 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3878 VARIANT* pVarVal) /* [O] The custom data. */
3880 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3881 MSFT_CDGuid *cdentry;
3882 int offset;
3884 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
3886 if (!guid || !pVarVal)
3887 return E_INVALIDARG;
3889 VariantClear(pVarVal);
3891 offset = ctl2_find_custdata(This->typelib, guid, This->typeinfo->oCustData);
3892 if (offset == -1)
3893 return S_OK;
3895 cdentry = (MSFT_CDGuid *)&This->typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
3896 return ctl2_decode_variant(This->typelib, cdentry->DataOffset, pVarVal);
3899 /******************************************************************************
3900 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3902 * Gets a custom data element from a function.
3904 * RETURNS
3906 * Success: S_OK.
3907 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3909 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3910 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3911 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3912 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3913 VARIANT* pVarVal) /* [O] The custom data. */
3915 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3916 return E_OUTOFMEMORY;
3919 /******************************************************************************
3920 * ITypeInfo2_GetParamCustData {OLEAUT32}
3922 * Gets a custom data element from a parameter.
3924 * RETURNS
3926 * Success: S_OK.
3927 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3929 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3930 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3931 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3932 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3933 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3934 VARIANT* pVarVal) /* [O] The custom data. */
3936 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3937 return E_OUTOFMEMORY;
3940 /******************************************************************************
3941 * ITypeInfo2_GetVarCustData {OLEAUT32}
3943 * Gets a custom data element from a variable.
3945 * RETURNS
3947 * Success: S_OK.
3948 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3950 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3951 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3952 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3953 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3954 VARIANT* pVarVal) /* [O] The custom data. */
3956 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3957 return E_OUTOFMEMORY;
3960 /******************************************************************************
3961 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3963 * Gets a custom data element from an implemented type of a TypeInfo.
3965 * RETURNS
3967 * Success: S_OK.
3968 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3970 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3971 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3972 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3973 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3974 VARIANT* pVarVal) /* [O] The custom data. */
3976 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3977 return E_OUTOFMEMORY;
3980 /******************************************************************************
3981 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3983 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3985 * RETURNS
3987 * Success: S_OK.
3988 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3990 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3991 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
3992 MEMBERID memid, /* [I] The member id (why?). */
3993 LCID lcid, /* [I] The locale (why?). */
3994 BSTR* pbstrHelpString, /* [O] The help string. */
3995 DWORD* pdwHelpStringContext, /* [O] The help string context. */
3996 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
3998 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3999 return E_OUTOFMEMORY;
4002 /******************************************************************************
4003 * ITypeInfo2_GetAllCustData {OLEAUT32}
4005 * Gets all of the custom data associated with a TypeInfo.
4007 * RETURNS
4009 * Success: S_OK.
4010 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4012 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
4013 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4014 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4016 FIXME("(%p,%p), stub!\n", iface, pCustData);
4017 return E_OUTOFMEMORY;
4020 /******************************************************************************
4021 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
4023 * Gets all of the custom data associated with a function.
4025 * RETURNS
4027 * Success: S_OK.
4028 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4030 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
4031 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4032 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
4033 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4035 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4036 return E_OUTOFMEMORY;
4039 /******************************************************************************
4040 * ITypeInfo2_GetAllParamCustData {OLEAUT32}
4042 * Gets all of the custom data associated with a parameter.
4044 * RETURNS
4046 * Success: S_OK.
4047 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4049 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
4050 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4051 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
4052 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
4053 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4055 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
4056 return E_OUTOFMEMORY;
4059 /******************************************************************************
4060 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
4062 * Gets all of the custom data associated with a variable.
4064 * RETURNS
4066 * Success: S_OK.
4067 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4069 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
4070 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4071 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
4072 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4074 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4075 return E_OUTOFMEMORY;
4078 /******************************************************************************
4079 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
4081 * Gets all of the custom data associated with an implemented type.
4083 * RETURNS
4085 * Success: S_OK.
4086 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4088 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
4089 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4090 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
4091 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4093 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4094 return E_OUTOFMEMORY;
4098 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
4100 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
4103 ICreateTypeInfo2_fnQueryInterface,
4104 ICreateTypeInfo2_fnAddRef,
4105 ICreateTypeInfo2_fnRelease,
4107 ICreateTypeInfo2_fnSetGuid,
4108 ICreateTypeInfo2_fnSetTypeFlags,
4109 ICreateTypeInfo2_fnSetDocString,
4110 ICreateTypeInfo2_fnSetHelpContext,
4111 ICreateTypeInfo2_fnSetVersion,
4112 ICreateTypeInfo2_fnAddRefTypeInfo,
4113 ICreateTypeInfo2_fnAddFuncDesc,
4114 ICreateTypeInfo2_fnAddImplType,
4115 ICreateTypeInfo2_fnSetImplTypeFlags,
4116 ICreateTypeInfo2_fnSetAlignment,
4117 ICreateTypeInfo2_fnSetSchema,
4118 ICreateTypeInfo2_fnAddVarDesc,
4119 ICreateTypeInfo2_fnSetFuncAndParamNames,
4120 ICreateTypeInfo2_fnSetVarName,
4121 ICreateTypeInfo2_fnSetTypeDescAlias,
4122 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
4123 ICreateTypeInfo2_fnSetFuncDocString,
4124 ICreateTypeInfo2_fnSetVarDocString,
4125 ICreateTypeInfo2_fnSetFuncHelpContext,
4126 ICreateTypeInfo2_fnSetVarHelpContext,
4127 ICreateTypeInfo2_fnSetMops,
4128 ICreateTypeInfo2_fnSetTypeIdldesc,
4129 ICreateTypeInfo2_fnLayOut,
4131 ICreateTypeInfo2_fnDeleteFuncDesc,
4132 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
4133 ICreateTypeInfo2_fnDeleteVarDesc,
4134 ICreateTypeInfo2_fnDeleteVarDescByMemId,
4135 ICreateTypeInfo2_fnDeleteImplType,
4136 ICreateTypeInfo2_fnSetCustData,
4137 ICreateTypeInfo2_fnSetFuncCustData,
4138 ICreateTypeInfo2_fnSetParamCustData,
4139 ICreateTypeInfo2_fnSetVarCustData,
4140 ICreateTypeInfo2_fnSetImplTypeCustData,
4141 ICreateTypeInfo2_fnSetHelpStringContext,
4142 ICreateTypeInfo2_fnSetFuncHelpStringContext,
4143 ICreateTypeInfo2_fnSetVarHelpStringContext,
4144 ICreateTypeInfo2_fnInvalidate,
4145 ICreateTypeInfo2_fnSetName
4148 static const ITypeInfo2Vtbl typeinfo2vt =
4151 ITypeInfo2_fnQueryInterface,
4152 ITypeInfo2_fnAddRef,
4153 ITypeInfo2_fnRelease,
4155 ITypeInfo2_fnGetTypeAttr,
4156 ITypeInfo2_fnGetTypeComp,
4157 ITypeInfo2_fnGetFuncDesc,
4158 ITypeInfo2_fnGetVarDesc,
4159 ITypeInfo2_fnGetNames,
4160 ITypeInfo2_fnGetRefTypeOfImplType,
4161 ITypeInfo2_fnGetImplTypeFlags,
4162 ITypeInfo2_fnGetIDsOfNames,
4163 ITypeInfo2_fnInvoke,
4164 ITypeInfo2_fnGetDocumentation,
4165 ITypeInfo2_fnGetDllEntry,
4166 ITypeInfo2_fnGetRefTypeInfo,
4167 ITypeInfo2_fnAddressOfMember,
4168 ITypeInfo2_fnCreateInstance,
4169 ITypeInfo2_fnGetMops,
4170 ITypeInfo2_fnGetContainingTypeLib,
4171 ITypeInfo2_fnReleaseTypeAttr,
4172 ITypeInfo2_fnReleaseFuncDesc,
4173 ITypeInfo2_fnReleaseVarDesc,
4175 ITypeInfo2_fnGetTypeKind,
4176 ITypeInfo2_fnGetTypeFlags,
4177 ITypeInfo2_fnGetFuncIndexOfMemId,
4178 ITypeInfo2_fnGetVarIndexOfMemId,
4179 ITypeInfo2_fnGetCustData,
4180 ITypeInfo2_fnGetFuncCustData,
4181 ITypeInfo2_fnGetParamCustData,
4182 ITypeInfo2_fnGetVarCustData,
4183 ITypeInfo2_fnGetImplTypeCustData,
4184 ITypeInfo2_fnGetDocumentation2,
4185 ITypeInfo2_fnGetAllCustData,
4186 ITypeInfo2_fnGetAllFuncCustData,
4187 ITypeInfo2_fnGetAllParamCustData,
4188 ITypeInfo2_fnGetAllVarCustData,
4189 ITypeInfo2_fnGetAllImplTypeCustData,
4192 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
4194 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
4196 int nameoffset;
4197 int typeinfo_offset;
4198 MSFT_TypeInfoBase *typeinfo;
4200 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
4202 pCreateTypeInfo2Impl = heap_alloc_zero(sizeof(ICreateTypeInfo2Impl));
4203 if (!pCreateTypeInfo2Impl) return NULL;
4205 pCreateTypeInfo2Impl->ICreateTypeInfo2_iface.lpVtbl = &ctypeinfo2vt;
4206 pCreateTypeInfo2Impl->ITypeInfo2_iface.lpVtbl = &typeinfo2vt;
4207 pCreateTypeInfo2Impl->ref = 1;
4209 pCreateTypeInfo2Impl->typelib = typelib;
4210 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
4212 nameoffset = ctl2_alloc_name(typelib, szName);
4213 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
4214 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
4216 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
4217 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
4219 pCreateTypeInfo2Impl->typeinfo = typeinfo;
4221 pCreateTypeInfo2Impl->typekind = tkind;
4222 typeinfo->typekind |= tkind | 0x20;
4223 ICreateTypeInfo2_SetAlignment(&pCreateTypeInfo2Impl->ICreateTypeInfo2_iface, 4);
4225 switch (tkind) {
4226 case TKIND_ENUM:
4227 case TKIND_INTERFACE:
4228 case TKIND_DISPATCH:
4229 case TKIND_COCLASS:
4230 typeinfo->size = 4;
4231 break;
4233 case TKIND_RECORD:
4234 case TKIND_UNION:
4235 typeinfo->size = 0;
4236 break;
4238 case TKIND_MODULE:
4239 typeinfo->size = 2;
4240 break;
4242 case TKIND_ALIAS:
4243 typeinfo->size = -0x75;
4244 break;
4246 default:
4247 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
4248 typeinfo->size = 0xdeadbeef;
4249 break;
4252 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
4253 typelib->last_typeinfo = pCreateTypeInfo2Impl;
4254 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
4256 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
4258 return &pCreateTypeInfo2Impl->ICreateTypeInfo2_iface;
4262 /*================== ICreateTypeLib2 Implementation ===================================*/
4264 /******************************************************************************
4265 * ICreateTypeLib2_QueryInterface {OLEAUT32}
4267 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
4268 ICreateTypeLib2 * iface,
4269 REFIID riid,
4270 VOID **ppvObject)
4272 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4274 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4276 *ppvObject=NULL;
4277 if(IsEqualIID(riid, &IID_IUnknown) ||
4278 IsEqualIID(riid,&IID_ICreateTypeLib)||
4279 IsEqualIID(riid,&IID_ICreateTypeLib2))
4281 *ppvObject = This;
4282 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4283 IsEqualIID(riid, &IID_ITypeLib2)) {
4284 *ppvObject = &This->lpVtblTypeLib2;
4287 if(*ppvObject)
4289 ICreateTypeLib2_AddRef(iface);
4290 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4291 return S_OK;
4293 TRACE("-- Interface: E_NOINTERFACE\n");
4294 return E_NOINTERFACE;
4297 /******************************************************************************
4298 * ICreateTypeLib2_AddRef {OLEAUT32}
4300 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4302 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4303 ULONG ref = InterlockedIncrement(&This->ref);
4305 TRACE("(%p)->(%u)\n", This, ref);
4307 return ref;
4310 /******************************************************************************
4311 * ICreateTypeLib2_Release {OLEAUT32}
4313 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4315 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4316 ULONG ref = InterlockedDecrement(&This->ref);
4318 TRACE("(%p)->(%u)\n", This, ref);
4320 if (!ref) {
4321 int i;
4323 for (i = 0; i < MSFT_SEG_MAX; i++) {
4324 heap_free(This->typelib_segment_data[i]);
4325 This->typelib_segment_data[i] = NULL;
4328 heap_free(This->filename);
4329 This->filename = NULL;
4331 while (This->typeinfos) {
4332 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4333 This->typeinfos = typeinfo->next_typeinfo;
4334 if(typeinfo->typedata) {
4335 CyclicList *iter, *rem;
4337 rem = typeinfo->typedata->next;
4338 typeinfo->typedata->next = NULL;
4339 iter = rem->next;
4340 heap_free(rem);
4342 while(iter) {
4343 rem = iter;
4344 iter = iter->next;
4345 heap_free(rem->u.data);
4346 heap_free(rem);
4350 heap_free(typeinfo->dual);
4351 heap_free(typeinfo);
4354 heap_free(This);
4357 return ref;
4361 /******************************************************************************
4362 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4364 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4365 ICreateTypeLib2 * iface,
4366 LPOLESTR szName,
4367 TYPEKIND tkind,
4368 ICreateTypeInfo **tinfo)
4370 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4371 char *name;
4373 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, tinfo);
4375 if (!szName || !tinfo) return E_INVALIDARG;
4377 ctl2_encode_name(This, szName, &name);
4378 if(ctl2_find_name(This, name) != -1)
4379 return TYPE_E_NAMECONFLICT;
4381 *tinfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4382 if (!*tinfo) return E_OUTOFMEMORY;
4384 return S_OK;
4387 /******************************************************************************
4388 * ICreateTypeLib2_SetName {OLEAUT32}
4390 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4391 ICreateTypeLib2 * iface,
4392 LPOLESTR szName)
4394 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4396 int offset;
4398 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4400 offset = ctl2_alloc_name(This, szName);
4401 if (offset == -1) return E_OUTOFMEMORY;
4402 This->typelib_header.NameOffset = offset;
4403 return S_OK;
4406 /******************************************************************************
4407 * ICreateTypeLib2_SetVersion {OLEAUT32}
4409 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4411 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4413 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4415 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4416 return S_OK;
4419 /******************************************************************************
4420 * ICreateTypeLib2_SetGuid {OLEAUT32}
4422 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4424 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4426 MSFT_GuidEntry guidentry;
4427 int offset;
4429 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4431 guidentry.guid = *guid;
4432 guidentry.hreftype = -2;
4433 guidentry.next_hash = -1;
4435 offset = ctl2_alloc_guid(This, &guidentry);
4437 if (offset == -1) return E_OUTOFMEMORY;
4439 This->typelib_header.posguid = offset;
4441 return S_OK;
4444 /******************************************************************************
4445 * ICreateTypeLib2_SetDocString {OLEAUT32}
4447 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4449 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4451 int offset;
4453 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4454 if (!szDoc)
4455 return E_INVALIDARG;
4457 offset = ctl2_alloc_string(This, szDoc);
4458 if (offset == -1) return E_OUTOFMEMORY;
4459 This->typelib_header.helpstring = offset;
4460 return S_OK;
4463 /******************************************************************************
4464 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4466 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4468 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4470 int offset;
4472 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4474 offset = ctl2_alloc_string(This, szHelpFileName);
4475 if (offset == -1) return E_OUTOFMEMORY;
4476 This->typelib_header.helpfile = offset;
4477 This->typelib_header.varflags |= 0x10;
4478 return S_OK;
4481 /******************************************************************************
4482 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4484 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4486 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4488 TRACE("(%p,%d)\n", iface, dwHelpContext);
4489 This->typelib_header.helpcontext = dwHelpContext;
4490 return S_OK;
4493 /******************************************************************************
4494 * ICreateTypeLib2_SetLcid {OLEAUT32}
4496 * Sets both the lcid and lcid2 members in the header to lcid.
4498 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4499 * is set to US English while the second one is set to 0.
4501 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4503 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4505 TRACE("(%p,%d)\n", iface, lcid);
4507 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4509 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4511 return S_OK;
4514 /******************************************************************************
4515 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4517 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4519 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4521 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4523 This->typelib_header.flags = uLibFlags;
4525 return S_OK;
4528 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4530 DWORD dwWritten;
4531 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4532 CloseHandle(hFile);
4533 return 0;
4535 return -1;
4538 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4540 DWORD dwWritten;
4541 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4542 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4543 CloseHandle(hFile);
4544 return 0;
4547 return -1;
4550 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4552 ICreateTypeInfo2Impl *typeinfo;
4553 HRESULT hres;
4555 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4556 typeinfo->typeinfo->memoffset = filesize;
4558 hres = ICreateTypeInfo2_fnLayOut(&typeinfo->ICreateTypeInfo2_iface);
4559 if(FAILED(hres))
4560 return hres;
4562 if (typeinfo->typedata)
4563 filesize += typeinfo->typedata->next->u.val
4564 + cti2_get_var_count(typeinfo->typeinfo) * 12
4565 + cti2_get_func_count(typeinfo->typeinfo) * 12 + 4;
4568 return S_OK;
4571 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4573 if (This->typelib_segdir[segment].length) {
4574 This->typelib_segdir[segment].offset = filepos;
4575 } else {
4576 This->typelib_segdir[segment].offset = -1;
4579 return This->typelib_segdir[segment].length;
4582 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4584 ICreateTypeInfo2Impl *typeinfo;
4586 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4587 CyclicList *iter;
4588 int offset = 0;
4590 if (!typeinfo->typedata) continue;
4592 iter = typeinfo->typedata->next;
4593 ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4594 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4595 ctl2_write_chunk(hFile, iter->u.data, ctl2_get_record_size(iter));
4597 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4598 ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4600 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4601 ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4603 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4604 ctl2_write_chunk(hFile, &offset, sizeof(int));
4605 offset += ctl2_get_record_size(iter);
4610 /******************************************************************************
4611 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4613 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4615 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4617 int retval;
4618 int filepos;
4619 HANDLE hFile;
4620 HRESULT hres;
4622 TRACE("(%p)\n", iface);
4624 retval = TYPE_E_IOERROR;
4626 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4627 if (hFile == INVALID_HANDLE_VALUE) return retval;
4629 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4630 filepos += This->typelib_header.nrtypeinfos * 4;
4632 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4633 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4634 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4635 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4636 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4637 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4638 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4639 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4640 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4641 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4642 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4643 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4644 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4646 hres = ctl2_finalize_typeinfos(This, filepos);
4647 if(FAILED(hres)) {
4648 CloseHandle(hFile);
4649 return hres;
4652 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4653 if (This->typelib_header.varflags & HELPDLLFLAG)
4654 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4655 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4656 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4657 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4658 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4659 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4660 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4661 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4662 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4663 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4664 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4665 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4666 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4667 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4668 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4669 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4671 ctl2_write_typeinfos(This, hFile);
4673 if (!CloseHandle(hFile)) return retval;
4675 return S_OK;
4679 /******************************************************************************
4680 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4682 * Deletes a named TypeInfo from a type library.
4684 * RETURNS
4686 * Success: S_OK
4687 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4689 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4690 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4691 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4693 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4694 return E_OUTOFMEMORY;
4697 /******************************************************************************
4698 * ICreateTypeLib2_SetCustData {OLEAUT32}
4700 * Sets custom data for a type library.
4702 * RETURNS
4704 * Success: S_OK
4705 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4707 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4708 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4709 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4710 VARIANT *pVarVal) /* [I] The custom data itself. */
4712 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4714 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4716 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4719 /******************************************************************************
4720 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4722 * Sets a context number for the library help string.
4724 * PARAMS
4725 * iface [I] The type library to set the help string context for.
4726 * dwContext [I] The help string context.
4728 * RETURNS
4729 * Success: S_OK
4730 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4732 static
4733 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4734 ULONG dwContext)
4736 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4738 TRACE("(%p,%d)\n", iface, dwContext);
4740 This->typelib_header.helpstringcontext = dwContext;
4741 return S_OK;
4744 /******************************************************************************
4745 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4747 * Set the DLL used to look up localized help strings.
4749 * PARAMS
4750 * iface [I] The type library to set the help DLL for.
4751 * szDllName [I] The name of the help DLL.
4753 * RETURNS
4754 * Success: S_OK
4755 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4757 static
4758 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4759 LPOLESTR szDllName)
4761 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4762 int offset;
4764 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4765 if (!szDllName)
4766 return E_INVALIDARG;
4768 offset = ctl2_alloc_string(This, szDllName);
4769 if (offset == -1)
4770 return E_OUTOFMEMORY;
4771 This->typelib_header.varflags |= HELPDLLFLAG;
4772 This->helpStringDll = offset;
4773 return S_OK;
4776 /*================== ITypeLib2 Implementation ===================================*/
4778 /******************************************************************************
4779 * ITypeLib2_QueryInterface {OLEAUT32}
4781 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4783 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4785 return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4788 /******************************************************************************
4789 * ITypeLib2_AddRef {OLEAUT32}
4791 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4793 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4795 return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4798 /******************************************************************************
4799 * ITypeLib2_Release {OLEAUT32}
4801 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4803 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4805 return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4808 /******************************************************************************
4809 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4811 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4812 ITypeLib2 * iface)
4814 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4816 TRACE("(%p)\n", iface);
4818 return This->typelib_header.nrtypeinfos;
4821 /******************************************************************************
4822 * ITypeLib2_GetTypeInfo {OLEAUT32}
4824 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4825 ITypeLib2 * iface,
4826 UINT index,
4827 ITypeInfo** ppTInfo)
4829 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4831 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4833 if (!ppTInfo) return E_INVALIDARG;
4835 if (index >= This->typelib_header.nrtypeinfos) {
4836 return TYPE_E_ELEMENTNOTFOUND;
4839 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4842 /******************************************************************************
4843 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4845 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4846 ITypeLib2 * iface,
4847 UINT index,
4848 TYPEKIND* kind)
4850 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4852 TRACE("(%p,%d,%p)\n", iface, index, kind);
4854 if (!kind) return E_INVALIDARG;
4856 if (index >= This->typelib_header.nrtypeinfos) {
4857 return TYPE_E_ELEMENTNOTFOUND;
4860 *kind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 0xF;
4862 return S_OK;
4865 /******************************************************************************
4866 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4868 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4869 ITypeLib2 * iface,
4870 REFGUID guid,
4871 ITypeInfo** ppTinfo)
4873 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4875 int guidoffset;
4876 int typeinfo;
4878 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4880 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4881 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4883 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4884 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4886 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4889 /******************************************************************************
4890 * ITypeLib2_GetLibAttr {OLEAUT32}
4892 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4893 ITypeLib2 * iface,
4894 TLIBATTR** ppTLibAttr)
4896 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4898 TRACE("(%p,%p)\n", This, ppTLibAttr);
4900 if(!ppTLibAttr)
4901 return E_INVALIDARG;
4903 *ppTLibAttr = heap_alloc_zero(sizeof(TLIBATTR));
4904 if(!*ppTLibAttr)
4905 return E_OUTOFMEMORY;
4907 if(This->typelib_header.posguid != -1) {
4908 MSFT_GuidEntry *guid;
4910 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4911 (*ppTLibAttr)->guid = guid->guid;
4914 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4915 (*ppTLibAttr)->syskind = ctl2_get_syskind(This);
4916 (*ppTLibAttr)->wMajorVerNum = LOWORD(This->typelib_header.version);
4917 (*ppTLibAttr)->wMinorVerNum = HIWORD(This->typelib_header.version);
4918 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4919 return S_OK;
4922 /******************************************************************************
4923 * ITypeLib2_GetTypeComp {OLEAUT32}
4925 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4926 ITypeLib2 * iface,
4927 ITypeComp** ppTComp)
4929 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4931 FIXME("(%p,%p), stub!\n", This, ppTComp);
4933 return E_OUTOFMEMORY;
4936 /******************************************************************************
4937 * ITypeLib2_GetDocumentation {OLEAUT32}
4939 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4940 ITypeLib2 * iface,
4941 INT index,
4942 BSTR* pBstrName,
4943 BSTR* pBstrDocString,
4944 DWORD* pdwHelpContext,
4945 BSTR* pBstrHelpFile)
4947 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4948 WCHAR *string;
4950 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4952 if(index != -1) {
4953 ICreateTypeInfo2Impl *iter;
4955 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4956 index--;
4958 if(!iter)
4959 return TYPE_E_ELEMENTNOTFOUND;
4961 return ITypeInfo_GetDocumentation(&iter->ITypeInfo2_iface,
4962 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4965 if(pBstrName) {
4966 if(This->typelib_header.NameOffset == -1)
4967 *pBstrName = NULL;
4968 else {
4969 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4970 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4972 ctl2_decode_name((char*)&name->namelen, &string);
4974 *pBstrName = SysAllocString(string);
4975 if(!*pBstrName)
4976 return E_OUTOFMEMORY;
4980 if(pBstrDocString) {
4981 if(This->typelib_header.helpstring == -1)
4982 *pBstrDocString = NULL;
4983 else {
4984 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4986 *pBstrDocString = SysAllocString(string);
4987 if(!*pBstrDocString) {
4988 if(pBstrName) SysFreeString(*pBstrName);
4989 return E_OUTOFMEMORY;
4994 if(pdwHelpContext)
4995 *pdwHelpContext = This->typelib_header.helpcontext;
4997 if(pBstrHelpFile) {
4998 if(This->typelib_header.helpfile == -1)
4999 *pBstrHelpFile = NULL;
5000 else {
5001 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
5003 *pBstrHelpFile = SysAllocString(string);
5004 if(!*pBstrHelpFile) {
5005 if(pBstrName) SysFreeString(*pBstrName);
5006 if(pBstrDocString) SysFreeString(*pBstrDocString);
5007 return E_OUTOFMEMORY;
5012 return S_OK;
5015 /******************************************************************************
5016 * ITypeLib2_IsName {OLEAUT32}
5018 static HRESULT WINAPI ITypeLib2_fnIsName(
5019 ITypeLib2 * iface,
5020 LPOLESTR szNameBuf,
5021 ULONG lHashVal,
5022 BOOL* pfName)
5024 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5026 char *encoded_name;
5027 int nameoffset;
5028 MSFT_NameIntro *nameintro;
5030 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
5032 ctl2_encode_name(This, szNameBuf, &encoded_name);
5033 nameoffset = ctl2_find_name(This, encoded_name);
5035 *pfName = 0;
5037 if (nameoffset == -1) return S_OK;
5039 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
5040 if (nameintro->hreftype == -1) return S_OK;
5042 *pfName = 1;
5044 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
5046 return S_OK;
5049 /******************************************************************************
5050 * ITypeLib2_FindName {OLEAUT32}
5052 static HRESULT WINAPI ITypeLib2_fnFindName(
5053 ITypeLib2 * iface,
5054 LPOLESTR szNameBuf,
5055 ULONG lHashVal,
5056 ITypeInfo** ppTInfo,
5057 MEMBERID* rgMemId,
5058 USHORT* pcFound)
5060 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5062 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
5064 return E_OUTOFMEMORY;
5067 /******************************************************************************
5068 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
5070 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
5071 ITypeLib2 * iface,
5072 TLIBATTR* attr)
5074 TRACE("(%p,%p)\n", iface, attr);
5075 heap_free(attr);
5078 /******************************************************************************
5079 * ICreateTypeLib2_GetCustData {OLEAUT32}
5081 * Retrieves a custom data value stored on a type library.
5083 * RETURNS
5085 * Success: S_OK
5086 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5088 static HRESULT WINAPI ITypeLib2_fnGetCustData(
5089 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5090 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
5091 VARIANT* pVarVal) /* [O] The custom data. */
5093 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5095 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
5097 return E_OUTOFMEMORY;
5100 /******************************************************************************
5101 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
5103 * Retrieves some statistics about names in a type library, supposedly for
5104 * hash table optimization purposes.
5106 * RETURNS
5108 * Success: S_OK
5109 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5111 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
5112 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
5113 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
5114 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
5116 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5118 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
5120 return E_OUTOFMEMORY;
5123 /******************************************************************************
5124 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
5126 * Obtain locale-aware help string information.
5128 * RETURNS
5130 * Success: S_OK
5131 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
5133 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
5134 ITypeLib2 * iface,
5135 INT index,
5136 LCID lcid,
5137 BSTR* pbstrHelpString,
5138 DWORD* pdwHelpStringContext,
5139 BSTR* pbstrHelpStringDll)
5141 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5143 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
5145 return E_OUTOFMEMORY;
5148 /******************************************************************************
5149 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
5151 * Retrieve all of the custom data for a type library.
5153 * RETURNS
5155 * Success: S_OK
5156 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5158 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
5159 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5160 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
5162 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5164 FIXME("(%p,%p), stub!\n", This, pCustData);
5166 return E_OUTOFMEMORY;
5170 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
5172 static const ICreateTypeLib2Vtbl ctypelib2vt =
5175 ICreateTypeLib2_fnQueryInterface,
5176 ICreateTypeLib2_fnAddRef,
5177 ICreateTypeLib2_fnRelease,
5179 ICreateTypeLib2_fnCreateTypeInfo,
5180 ICreateTypeLib2_fnSetName,
5181 ICreateTypeLib2_fnSetVersion,
5182 ICreateTypeLib2_fnSetGuid,
5183 ICreateTypeLib2_fnSetDocString,
5184 ICreateTypeLib2_fnSetHelpFileName,
5185 ICreateTypeLib2_fnSetHelpContext,
5186 ICreateTypeLib2_fnSetLcid,
5187 ICreateTypeLib2_fnSetLibFlags,
5188 ICreateTypeLib2_fnSaveAllChanges,
5190 ICreateTypeLib2_fnDeleteTypeInfo,
5191 ICreateTypeLib2_fnSetCustData,
5192 ICreateTypeLib2_fnSetHelpStringContext,
5193 ICreateTypeLib2_fnSetHelpStringDll
5196 static const ITypeLib2Vtbl typelib2vt =
5199 ITypeLib2_fnQueryInterface,
5200 ITypeLib2_fnAddRef,
5201 ITypeLib2_fnRelease,
5203 ITypeLib2_fnGetTypeInfoCount,
5204 ITypeLib2_fnGetTypeInfo,
5205 ITypeLib2_fnGetTypeInfoType,
5206 ITypeLib2_fnGetTypeInfoOfGuid,
5207 ITypeLib2_fnGetLibAttr,
5208 ITypeLib2_fnGetTypeComp,
5209 ITypeLib2_fnGetDocumentation,
5210 ITypeLib2_fnIsName,
5211 ITypeLib2_fnFindName,
5212 ITypeLib2_fnReleaseTLibAttr,
5214 ITypeLib2_fnGetCustData,
5215 ITypeLib2_fnGetLibStatistics,
5216 ITypeLib2_fnGetDocumentation2,
5217 ITypeLib2_fnGetAllCustData,
5220 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
5222 ICreateTypeLib2Impl *pCreateTypeLib2Impl;
5223 int failed = 0;
5225 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
5227 pCreateTypeLib2Impl = heap_alloc_zero(sizeof(ICreateTypeLib2Impl));
5228 if (!pCreateTypeLib2Impl) return NULL;
5230 pCreateTypeLib2Impl->filename = heap_alloc((strlenW(szFile) + 1) * sizeof(WCHAR));
5231 if (!pCreateTypeLib2Impl->filename) {
5232 heap_free(pCreateTypeLib2Impl);
5233 return NULL;
5235 strcpyW(pCreateTypeLib2Impl->filename, szFile);
5237 ctl2_init_header(pCreateTypeLib2Impl);
5238 ctl2_init_segdir(pCreateTypeLib2Impl);
5240 pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
5243 * The following two calls return an offset or -1 if out of memory. We
5244 * specifically need an offset of 0, however, so...
5246 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
5247 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
5249 pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
5250 pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
5252 memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
5253 memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
5255 pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
5256 pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
5257 pCreateTypeLib2Impl->ref = 1;
5259 if (failed) {
5260 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
5261 return 0;
5264 return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
5267 /******************************************************************************
5268 * CreateTypeLib2 [OLEAUT32.180]
5270 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5271 * library.
5273 * NOTES
5275 * See also CreateTypeLib.
5277 * RETURNS
5278 * Success: S_OK
5279 * Failure: Status
5281 HRESULT WINAPI CreateTypeLib2(
5282 SYSKIND syskind, /* [I] System type library is for */
5283 LPCOLESTR szFile, /* [I] Type library file name */
5284 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5286 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5288 if (!szFile) return E_INVALIDARG;
5289 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5290 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5293 /******************************************************************************
5294 * ClearCustData (OLEAUT32.171)
5296 * Clear a custom data types' data.
5298 * PARAMS
5299 * lpCust [I] The custom data type instance
5301 * RETURNS
5302 * Nothing.
5304 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5306 if (lpCust && lpCust->cCustData)
5308 if (lpCust->prgCustData)
5310 DWORD i;
5312 for (i = 0; i < lpCust->cCustData; i++)
5313 VariantClear(&lpCust->prgCustData[i].varValue);
5315 /* FIXME - Should be using a per-thread IMalloc */
5316 heap_free(lpCust->prgCustData);
5317 lpCust->prgCustData = NULL;
5319 lpCust->cCustData = 0;