oleaut32: Update variable description and typeinfo size in ICreateTypeInfo2::SetVarHe...
[wine.git] / dlls / oleaut32 / typelib2.c
blob6e36d694d6e74044ee05141646c85ecfd160999d
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 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 const ICreateTypeInfo2Vtbl *lpVtbl;
199 const ITypeInfo2Vtbl *lpVtblTypeInfo2;
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_ITypeInfo2( ITypeInfo2 *iface )
217 return (ICreateTypeInfo2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeInfo2Impl, lpVtblTypeInfo2));
220 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
222 static CyclicList *alloc_cyclic_list_item(CyclicListElementType type)
224 CyclicList *ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CyclicList));
225 if (!ret)
226 return NULL;
227 ret->type = type;
228 return ret;
231 /*================== Internal functions ===================================*/
233 static inline UINT cti2_get_var_count(const MSFT_TypeInfoBase *typeinfo)
235 return typeinfo->cElement >> 16;
238 static inline UINT cti2_get_func_count(const MSFT_TypeInfoBase *typeinfo)
240 return typeinfo->cElement & 0xFFFF;
243 static inline INT ctl2_get_record_size(const CyclicList *iter)
245 return iter->u.data[0] & 0xFFFF;
248 static void ctl2_update_var_size(const ICreateTypeInfo2Impl *This, CyclicList *var, int size)
250 int old = ctl2_get_record_size(var), i;
252 if (old >= size) return;
254 /* initialize fields included in size but currently unused */
255 for (i = old/sizeof(int); i < (size/sizeof(int) - 1); i++)
257 /* HelpContext/HelpStringContext being 0 means it's not set */
258 var->u.data[i] = (i == 5 || i == 9) ? 0 : -1;
261 var->u.data[0] += size - old;
262 This->typedata->next->u.val += size - old;
265 /* NOTE: entry always assumed to be a function */
266 static inline INVOKEKIND ctl2_get_invokekind(const CyclicList *func)
268 /* INVOKEKIND uses bit flags up to 8 */
269 return (func->u.data[4] >> 3) & 0xF;
272 static inline SYSKIND ctl2_get_syskind(const ICreateTypeLib2Impl *This)
274 return This->typelib_header.varflags & 0xF;
277 /****************************************************************************
278 * ctl2_init_header
280 * Initializes the type library header of a new typelib.
282 static void ctl2_init_header(
283 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
285 This->typelib_header.magic1 = MSFT_SIGNATURE;
286 This->typelib_header.magic2 = 0x00010002;
287 This->typelib_header.posguid = -1;
288 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
289 This->typelib_header.varflags = 0x40;
290 This->typelib_header.version = 0;
291 This->typelib_header.flags = 0;
292 This->typelib_header.nrtypeinfos = 0;
293 This->typelib_header.helpstring = -1;
294 This->typelib_header.helpstringcontext = 0;
295 This->typelib_header.helpcontext = 0;
296 This->typelib_header.nametablecount = 0;
297 This->typelib_header.nametablechars = 0;
298 This->typelib_header.NameOffset = -1;
299 This->typelib_header.helpfile = -1;
300 This->typelib_header.CustomDataOffset = -1;
301 This->typelib_header.res44 = 0x20;
302 This->typelib_header.res48 = 0x80;
303 This->typelib_header.dispatchpos = -1;
304 This->typelib_header.nimpinfos = 0;
305 This->helpStringDll = -1;
308 /****************************************************************************
309 * ctl2_init_segdir
311 * Initializes the segment directory of a new typelib.
313 static void ctl2_init_segdir(
314 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
316 int i;
317 MSFT_pSeg *segdir;
319 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
321 for (i = 0; i < 15; i++) {
322 segdir[i].offset = -1;
323 segdir[i].length = 0;
324 segdir[i].res08 = -1;
325 segdir[i].res0c = 0x0f;
329 /****************************************************************************
330 * ctl2_hash_guid
332 * Generates a hash key from a GUID.
334 * RETURNS
336 * The hash key for the GUID.
338 static int ctl2_hash_guid(
339 REFGUID guid) /* [I] The guid to find. */
341 int hash;
342 int i;
344 hash = 0;
345 for (i = 0; i < 8; i ++) {
346 hash ^= ((const short *)guid)[i];
349 return hash & 0x1f;
352 /****************************************************************************
353 * ctl2_find_guid
355 * Locates a guid in a type library.
357 * RETURNS
359 * The offset into the GUID segment of the guid, or -1 if not found.
361 static int ctl2_find_guid(
362 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
363 int hash_key, /* [I] The hash key for the guid. */
364 REFGUID guid) /* [I] The guid to find. */
366 int offset;
367 MSFT_GuidEntry *guidentry;
369 offset = This->typelib_guidhash_segment[hash_key];
370 while (offset != -1) {
371 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
373 if (IsEqualGUID(guidentry, guid)) return offset;
375 offset = guidentry->next_hash;
378 return offset;
381 /****************************************************************************
382 * ctl2_find_name
384 * Locates a name in a type library.
386 * RETURNS
388 * The offset into the NAME segment of the name, or -1 if not found.
390 * NOTES
392 * The name must be encoded as with ctl2_encode_name().
394 static int ctl2_find_name(
395 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
396 const char *name) /* [I] The encoded name to find. */
398 int offset;
399 int *namestruct;
401 offset = This->typelib_namehash_segment[name[2] & 0x7f];
402 while (offset != -1) {
403 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
405 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
406 /* hash codes and lengths match, final test */
407 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
410 /* move to next item in hash bucket */
411 offset = namestruct[1];
414 return offset;
417 /****************************************************************************
418 * ctl2_encode_name
420 * Encodes a name string to a form suitable for storing into a type library
421 * or comparing to a name stored in a type library.
423 * RETURNS
425 * The length of the encoded name, including padding and length+hash fields.
427 * NOTES
429 * Will throw an exception if name or result are NULL. Is not multithread
430 * safe in the slightest.
432 static int ctl2_encode_name(
433 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
434 const WCHAR *name, /* [I] The name string to encode. */
435 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
437 int length;
438 static char converted_name[0x104];
439 int offset;
440 int value;
442 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
443 converted_name[0] = length & 0xff;
445 converted_name[length + 4] = 0;
447 converted_name[1] = 0x00;
449 value = LHashValOfNameSysA(ctl2_get_syskind(This), This->typelib_header.lcid, converted_name + 4);
451 converted_name[2] = value;
452 converted_name[3] = value >> 8;
454 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
456 *result = converted_name;
458 return (length + 7) & ~3;
461 /****************************************************************************
462 * ctl2_decode_name
464 * Converts string stored in typelib data to unicode.
466 static void ctl2_decode_name(
467 char *data, /* [I] String to be decoded */
468 WCHAR **string) /* [O] Decoded string */
470 int i, length;
471 static WCHAR converted_string[0x104];
473 length = data[0];
475 for(i=0; i<length; i++)
476 converted_string[i] = data[i+4];
477 converted_string[length] = '\0';
479 *string = converted_string;
482 /****************************************************************************
483 * ctl2_encode_string
485 * Encodes a string to a form suitable for storing into a type library or
486 * comparing to a string stored in a type library.
488 * RETURNS
490 * The length of the encoded string, including padding and length fields.
492 * NOTES
494 * Will throw an exception if string or result are NULL. Is not multithread
495 * safe in the slightest.
497 static int ctl2_encode_string(
498 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
499 const WCHAR *string, /* [I] The string to encode. */
500 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
502 int length;
503 static char converted_string[0x104];
504 int offset;
506 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
507 converted_string[0] = length & 0xff;
508 converted_string[1] = (length >> 8) & 0xff;
510 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
512 *result = converted_string;
514 return (length + 5) & ~3;
517 /****************************************************************************
518 * ctl2_decode_string
520 * Converts string stored in typelib data to unicode.
522 static void ctl2_decode_string(
523 char *data, /* [I] String to be decoded */
524 WCHAR **string) /* [O] Decoded string */
526 int i, length;
527 static WCHAR converted_string[0x104];
529 length = data[0] + (data[1]<<8);
530 if((length&0x3) == 1)
531 length >>= 2;
533 for(i=0; i<length; i++)
534 converted_string[i] = data[i+2];
535 converted_string[length] = '\0';
537 *string = converted_string;
540 /****************************************************************************
541 * ctl2_alloc_segment
543 * Allocates memory from a segment in a type library.
545 * RETURNS
547 * Success: The offset within the segment of the new data area.
548 * Failure: -1 (this is invariably an out of memory condition).
550 * BUGS
552 * Does not (yet) handle the case where the allocated segment memory needs to grow.
554 static int ctl2_alloc_segment(
555 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
556 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
557 int size, /* [I] The amount to allocate. */
558 int block_size) /* [I] Initial allocation block size, or 0 for default. */
560 int offset;
562 if(!This->typelib_segment_data[segment]) {
563 if (!block_size) block_size = 0x2000;
565 This->typelib_segment_block_length[segment] = block_size;
566 This->typelib_segment_data[segment] = HeapAlloc(GetProcessHeap(), 0, block_size);
567 if (!This->typelib_segment_data[segment]) return -1;
568 memset(This->typelib_segment_data[segment], 0x57, block_size);
571 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
572 char *block;
574 block_size = This->typelib_segment_block_length[segment];
575 block = HeapReAlloc(GetProcessHeap(), 0, This->typelib_segment_data[segment], block_size << 1);
576 if (!block) return -1;
578 if (segment == MSFT_SEG_TYPEINFO) {
579 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
580 ICreateTypeInfo2Impl *typeinfo;
582 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
583 typeinfo->typeinfo = (void *)&block[((char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
587 memset(block + block_size, 0x57, block_size);
588 This->typelib_segment_block_length[segment] = block_size << 1;
589 This->typelib_segment_data[segment] = block;
592 offset = This->typelib_segdir[segment].length;
593 This->typelib_segdir[segment].length += size;
595 return offset;
598 /****************************************************************************
599 * ctl2_alloc_typeinfo
601 * Allocates and initializes a typeinfo structure in a type library.
603 * RETURNS
605 * Success: The offset of the new typeinfo.
606 * Failure: -1 (this is invariably an out of memory condition).
608 static int ctl2_alloc_typeinfo(
609 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
610 int nameoffset) /* [I] The offset of the name for this typeinfo. */
612 int offset;
613 MSFT_TypeInfoBase *typeinfo;
615 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
616 if (offset == -1) return -1;
618 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
620 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
622 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
623 typeinfo->memoffset = -1; /* should be EOF if no elements */
624 typeinfo->res2 = 0;
625 typeinfo->res3 = 0;
626 typeinfo->res4 = 3;
627 typeinfo->res5 = 0;
628 typeinfo->cElement = 0;
629 typeinfo->res7 = 0;
630 typeinfo->res8 = 0;
631 typeinfo->res9 = 0;
632 typeinfo->resA = 0;
633 typeinfo->posguid = -1;
634 typeinfo->flags = 0;
635 typeinfo->NameOffset = nameoffset;
636 typeinfo->version = 0;
637 typeinfo->docstringoffs = -1;
638 typeinfo->helpstringcontext = 0;
639 typeinfo->helpcontext = 0;
640 typeinfo->oCustData = -1;
641 typeinfo->cbSizeVft = 0;
642 typeinfo->cImplTypes = 0;
643 typeinfo->size = 0;
644 typeinfo->datatype1 = -1;
645 typeinfo->datatype2 = 0;
646 typeinfo->res18 = 0;
647 typeinfo->res19 = -1;
649 return offset;
652 /****************************************************************************
653 * ctl2_alloc_guid
655 * Allocates and initializes a GUID structure in a type library. Also updates
656 * the GUID hash table as needed.
658 * RETURNS
660 * Success: The offset of the new GUID.
661 * Failure: -1 (this is invariably an out of memory condition).
663 static int ctl2_alloc_guid(
664 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
665 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
667 int offset;
668 MSFT_GuidEntry *guid_space;
669 int hash_key;
671 hash_key = ctl2_hash_guid(&guid->guid);
673 offset = ctl2_find_guid(This, hash_key, &guid->guid);
674 if (offset != -1) return offset;
676 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
677 if (offset == -1) return -1;
679 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
680 *guid_space = *guid;
682 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
683 This->typelib_guidhash_segment[hash_key] = offset;
685 return offset;
688 /****************************************************************************
689 * ctl2_alloc_name
691 * Allocates and initializes a name within a type library. Also updates the
692 * name hash table as needed.
694 * RETURNS
696 * Success: The offset within the segment of the new name.
697 * Failure: -1 (this is invariably an out of memory condition).
699 static int ctl2_alloc_name(
700 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
701 const WCHAR *name) /* [I] The name to store. */
703 int length;
704 int offset;
705 MSFT_NameIntro *name_space;
706 char *encoded_name;
708 length = ctl2_encode_name(This, name, &encoded_name);
710 offset = ctl2_find_name(This, encoded_name);
711 if (offset != -1) return offset;
713 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
714 if (offset == -1) return -1;
716 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
717 name_space->hreftype = -1;
718 name_space->next_hash = -1;
719 memcpy(&name_space->namelen, encoded_name, length);
721 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
722 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
724 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
726 This->typelib_header.nametablecount += 1;
727 This->typelib_header.nametablechars += *encoded_name;
729 return offset;
732 /****************************************************************************
733 * ctl2_alloc_string
735 * Allocates and initializes a string in a type library.
737 * RETURNS
739 * Success: The offset within the segment of the new string.
740 * Failure: -1 (this is invariably an out of memory condition).
742 static int ctl2_alloc_string(
743 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
744 const WCHAR *string) /* [I] The string to store. */
746 int length;
747 int offset;
748 char *string_space;
749 char *encoded_string;
751 length = ctl2_encode_string(This, string, &encoded_string);
753 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
754 offset += ((((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) & 0xff)
755 | (This->typelib_segment_data[MSFT_SEG_STRING][offset + 0] & 0xff)) + 5) & ~3) {
756 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
759 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
760 if (offset == -1) return -1;
762 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
763 memcpy(string_space, encoded_string, length);
765 return offset;
768 /****************************************************************************
769 * ctl2_alloc_importinfo
771 * Allocates and initializes an import information structure in a type library.
773 * RETURNS
775 * Success: The offset of the new importinfo.
776 * Failure: -1 (this is invariably an out of memory condition).
778 static int ctl2_alloc_importinfo(
779 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
780 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
782 int offset;
783 MSFT_ImpInfo *impinfo_space;
785 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
786 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
787 offset+=sizeof(MSFT_ImpInfo)) {
788 if(impinfo_space->oImpFile == impinfo->oImpFile
789 && impinfo_space->oGuid == impinfo->oGuid)
790 return offset;
792 impinfo_space += 1;
795 impinfo->flags |= This->typelib_header.nimpinfos++;
797 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
798 if (offset == -1) return -1;
800 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
801 *impinfo_space = *impinfo;
803 return offset;
806 /****************************************************************************
807 * ctl2_alloc_importfile
809 * Allocates and initializes an import file definition in a type library.
811 * RETURNS
813 * Success: The offset of the new importinfo.
814 * Failure: -1 (this is invariably an out of memory condition).
816 static int ctl2_alloc_importfile(
817 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
818 int guidoffset, /* [I] The offset to the GUID for the imported library. */
819 LCID lcid, /* [I] The LCID of imported library. */
820 int major_version, /* [I] The major version number of the imported library. */
821 int minor_version, /* [I] The minor version number of the imported library. */
822 const WCHAR *filename) /* [I] The filename of the imported library. */
824 int length;
825 int offset;
826 MSFT_ImpFile *importfile;
827 char *encoded_string;
829 length = ctl2_encode_string(This, filename, &encoded_string);
831 encoded_string[0] <<= 2;
832 encoded_string[0] |= 1;
834 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
835 offset += ((((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) & 0xff00)
836 | (This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc] & 0xff)) >> 2) + 5) & 0xfffc) + 0xc) {
837 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
840 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
841 if (offset == -1) return -1;
843 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
844 importfile->guid = guidoffset;
845 importfile->lcid = lcid;
846 importfile->version = major_version | (minor_version << 16);
847 memcpy(importfile->filename, encoded_string, length);
849 return offset;
852 /****************************************************************************
853 * ctl2_encode_variant
855 * Encodes a variant, inline if possible or in custom data segment
857 * RETURNS
859 * Success: S_OK
860 * Failure: Error code from winerror.h
862 static HRESULT ctl2_encode_variant(
863 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
864 int *encoded_value, /* [O] The encoded default value or data offset */
865 VARIANT *value, /* [I] Default value to be encoded */
866 VARTYPE arg_type) /* [I] Argument type */
868 VARIANT v;
869 HRESULT hres;
870 int mask = 0;
872 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
874 if(arg_type == VT_INT)
875 arg_type = VT_I4;
876 if(arg_type == VT_UINT)
877 arg_type = VT_UI4;
879 v = *value;
880 if(V_VT(value) != arg_type) {
881 hres = VariantChangeType(&v, value, 0, arg_type);
882 if(FAILED(hres))
883 return hres;
886 /* Check if default value can be stored in encoded_value */
887 switch(arg_type) {
888 case VT_I4:
889 case VT_UI4:
890 mask = 0x3ffffff;
891 if(V_UI4(&v)>0x3ffffff)
892 break;
893 case VT_I1:
894 case VT_UI1:
895 case VT_BOOL:
896 if(!mask)
897 mask = 0xff;
898 case VT_I2:
899 case VT_UI2:
900 if(!mask)
901 mask = 0xffff;
902 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
903 return S_OK;
906 switch(arg_type) {
907 case VT_I4:
908 case VT_R4:
909 case VT_UI4:
910 case VT_INT:
911 case VT_UINT:
912 case VT_HRESULT:
913 case VT_PTR: {
914 /* Construct the data to be allocated */
915 int data[2];
916 data[0] = arg_type + (V_UI4(&v)<<16);
917 data[1] = (V_UI4(&v)>>16) + 0x57570000;
919 /* Check if the data was already allocated */
920 /* Currently the structures doesn't allow to do it in a nice way */
921 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
922 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
923 return S_OK;
925 /* Allocate the data */
926 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
927 if(*encoded_value == -1)
928 return E_OUTOFMEMORY;
930 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
931 return S_OK;
933 case VT_BSTR: {
934 /* Construct the data */
935 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
936 char *data = HeapAlloc(GetProcessHeap(), 0, len);
938 if(!data)
939 return E_OUTOFMEMORY;
941 *((unsigned short*)data) = arg_type;
942 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
943 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
944 if(V_BSTR(&v)[i] <= 0x7f)
945 data[i+6] = V_BSTR(&v)[i];
946 else
947 data[i+6] = '?';
949 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
950 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
951 data[i] = 0x57;
953 /* Check if the data was already allocated */
954 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
955 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
956 HeapFree(GetProcessHeap(), 0, data);
957 return S_OK;
960 /* Allocate the data */
961 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
962 if(*encoded_value == -1) {
963 HeapFree(GetProcessHeap(), 0, data);
964 return E_OUTOFMEMORY;
967 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
968 HeapFree(GetProcessHeap(), 0, data);
969 return S_OK;
971 default:
972 FIXME("Argument type not yet handled\n");
973 return E_NOTIMPL;
977 static int ctl2_find_custdata(
978 ICreateTypeLib2Impl *This,
979 REFGUID guid,
980 int offset)
982 while (offset != -1) {
983 MSFT_CDGuid *cdentry =
984 (MSFT_CDGuid *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
985 MSFT_GuidEntry *guidentry =
986 (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][cdentry->GuidOffset];
988 if (IsEqualGUID(guidentry, guid))
989 return offset;
991 offset = cdentry->next;
994 return -1;
997 /****************************************************************************
998 * ctl2_decode_variant
1000 * Decodes a variant
1002 * RETURNS
1004 * Success: S_OK
1005 * Failure: Error code from winerror.h
1007 static HRESULT ctl2_decode_variant(
1008 ICreateTypeLib2Impl *This, /* [I] The typelib that contains the variant */
1009 int data_offs, /* [I] Offset within the data array, or the encoded value itself */
1010 VARIANT *value) /* [O] Decoded value */
1012 char *encoded_data;
1013 VARTYPE type;
1015 if (data_offs & 0x80000000) {
1016 /* data_offs contains the encoded value */
1017 V_VT(value) = (data_offs & ~0x80000000) >> 26;
1018 V_UI4(value) = data_offs & ~0xFF000000;
1019 return S_OK;
1022 encoded_data = &This->typelib_segment_data[MSFT_SEG_CUSTDATA][data_offs];
1023 type = *encoded_data;
1025 switch(type) {
1026 case VT_I4:
1027 case VT_R4:
1028 case VT_UI4:
1029 case VT_INT:
1030 case VT_UINT:
1031 case VT_HRESULT:
1032 case VT_PTR: {
1033 V_VT(value) = type;
1034 V_UI4(value) = *(unsigned*)(encoded_data + 2);
1035 return S_OK;
1037 case VT_BSTR: {
1038 unsigned len, i;
1040 len = *(unsigned*)(encoded_data + 2);
1042 V_VT(value) = type;
1043 V_BSTR(value) = SysAllocStringByteLen(NULL, len * sizeof(OLECHAR));
1044 for (i = 0; i < len; ++i)
1045 V_BSTR(value)[i] = *(encoded_data + 6 + i);
1047 return S_OK;
1049 default:
1050 FIXME("Don't yet have decoder for this VARTYPE: %u\n", type);
1051 return E_NOTIMPL;
1055 /****************************************************************************
1056 * ctl2_set_custdata
1058 * Adds a custom data element to an object in a type library.
1060 * RETURNS
1062 * Success: S_OK.
1063 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
1065 static HRESULT ctl2_set_custdata(
1066 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
1067 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
1068 VARIANT *pVarVal, /* [I] The custom data itself. */
1069 int *offset) /* [I/O] The list of custom data to prepend to. */
1071 MSFT_GuidEntry guidentry;
1072 HRESULT status;
1073 int dataoffset;
1074 int guidoffset;
1075 int custoffset;
1076 int *custdata;
1077 BOOL new_segment = FALSE;
1079 switch(V_VT(pVarVal))
1081 case VT_I4:
1082 case VT_R4:
1083 case VT_UI4:
1084 case VT_INT:
1085 case VT_UINT:
1086 case VT_HRESULT:
1087 case VT_BSTR:
1088 /* empty */
1089 break;
1090 default:
1091 return DISP_E_BADVARTYPE;
1094 guidentry.guid = *guid;
1096 guidentry.hreftype = -1;
1097 guidentry.next_hash = -1;
1099 guidoffset = ctl2_alloc_guid(This, &guidentry);
1100 if (guidoffset == -1) return E_OUTOFMEMORY;
1102 status = ctl2_encode_variant(This, &dataoffset, pVarVal, V_VT(pVarVal));
1103 if (status)
1104 return status;
1106 custoffset = ctl2_find_custdata(This, guid, *offset);
1107 if (custoffset == -1) {
1108 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
1109 if (custoffset == -1)
1110 return E_OUTOFMEMORY;
1111 new_segment = TRUE;
1114 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1115 custdata[0] = guidoffset;
1116 custdata[1] = dataoffset;
1117 if (new_segment) {
1118 custdata[2] = *offset;
1119 *offset = custoffset;
1122 return S_OK;
1125 /****************************************************************************
1126 * ctl2_encode_typedesc
1128 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
1129 * segments as needed.
1131 * RETURNS
1133 * Success: 0.
1134 * Failure: -1.
1136 static int ctl2_encode_typedesc(
1137 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
1138 const TYPEDESC *tdesc, /* [I] The type description to encode. */
1139 int *encoded_tdesc, /* [O] The encoded type description. */
1140 int *width, /* [O] The width of the type, or NULL. */
1141 int *alignment, /* [O] The alignment of the type, or NULL. */
1142 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1144 int default_tdesc;
1145 int scratch;
1146 int typeoffset;
1147 int arrayoffset;
1148 int *typedata;
1149 int *arraydata;
1150 int target_type;
1151 int child_size;
1153 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
1154 if (!width) width = &scratch;
1155 if (!alignment) alignment = &scratch;
1156 if (!decoded_size) decoded_size = &scratch;
1158 *decoded_size = 0;
1160 switch (tdesc->vt) {
1161 case VT_UI1:
1162 case VT_I1:
1163 *encoded_tdesc = default_tdesc;
1164 *width = 1;
1165 *alignment = 1;
1166 break;
1168 case VT_INT:
1169 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
1170 if (ctl2_get_syskind(This) == SYS_WIN16) {
1171 *width = 2;
1172 *alignment = 2;
1173 } else {
1174 *width = 4;
1175 *alignment = 4;
1177 break;
1179 case VT_UINT:
1180 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
1181 if (ctl2_get_syskind(This) == SYS_WIN16) {
1182 *width = 2;
1183 *alignment = 2;
1184 } else {
1185 *width = 4;
1186 *alignment = 4;
1188 break;
1190 case VT_UI2:
1191 case VT_I2:
1192 case VT_BOOL:
1193 *encoded_tdesc = default_tdesc;
1194 *width = 2;
1195 *alignment = 2;
1196 break;
1198 case VT_I4:
1199 case VT_UI4:
1200 case VT_R4:
1201 case VT_ERROR:
1202 case VT_BSTR:
1203 case VT_HRESULT:
1204 *encoded_tdesc = default_tdesc;
1205 *width = 4;
1206 *alignment = 4;
1207 break;
1209 case VT_CY:
1210 *encoded_tdesc = default_tdesc;
1211 *width = 8;
1212 *alignment = 4; /* guess? */
1213 break;
1215 case VT_VOID:
1216 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1217 *width = 0;
1218 *alignment = 1;
1219 break;
1221 case VT_PTR:
1222 case VT_SAFEARRAY:
1223 /* FIXME: Make with the error checking. */
1224 FIXME("PTR or SAFEARRAY vartype, may not work correctly.\n");
1226 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1228 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1229 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1230 if (((typedata[0] & 0xffff) == tdesc->vt) && (typedata[1] == target_type)) break;
1233 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1234 int mix_field;
1236 if (target_type & 0x80000000) {
1237 mix_field = (target_type >> 16) & VT_TYPEMASK;
1238 } else {
1239 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1240 switch((typedata[0]>>16) & ~VT_ARRAY)
1242 case VT_UI1:
1243 case VT_I1:
1244 case VT_UI2:
1245 case VT_I2:
1246 case VT_I4:
1247 case VT_UI4:
1248 mix_field = typedata[0]>>16;
1249 break;
1250 default:
1251 mix_field = ((typedata[0] >> 16) == 0x7fff) ? 0x7fff : 0x7ffe;
1252 break;
1256 if (tdesc->vt == VT_PTR)
1257 mix_field |= VT_BYREF;
1258 else if (tdesc->vt == VT_SAFEARRAY)
1259 mix_field |= VT_ARRAY;
1261 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1262 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1264 typedata[0] = (mix_field << 16) | tdesc->vt;
1265 typedata[1] = target_type;
1268 *encoded_tdesc = typeoffset;
1270 *width = 4;
1271 *alignment = 4;
1272 *decoded_size = sizeof(TYPEDESC) + child_size;
1273 break;
1275 case VT_CARRAY:
1277 /* FIXME: Make with the error checking. */
1278 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1280 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1281 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1282 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1284 arraydata[0] = target_type;
1285 arraydata[1] = num_dims;
1286 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1287 arraydata += 2;
1289 for(dim = 0; dim < num_dims; dim++) {
1290 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1291 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1292 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1293 arraydata += 2;
1295 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1296 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1298 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1299 typedata[1] = arrayoffset;
1301 *encoded_tdesc = typeoffset;
1302 *width = *width * elements;
1303 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1305 break;
1307 case VT_USERDEFINED:
1309 const MSFT_TypeInfoBase *basetype;
1310 INT basevt = 0x7fff;
1312 TRACE("USERDEFINED.\n");
1313 if (tdesc->u.hreftype % sizeof(*basetype) == 0 && tdesc->u.hreftype < This->typelib_segdir[MSFT_SEG_TYPEINFO].length)
1315 basetype = (MSFT_TypeInfoBase*)&(This->typelib_segment_data[MSFT_SEG_TYPEINFO][tdesc->u.hreftype]);
1316 switch(basetype->typekind & 0xf)
1318 case TKIND_ENUM:
1319 basevt = VT_I4;
1320 break;
1321 default:
1322 FIXME("USERDEFINED basetype %d not handled\n", basetype->typekind & 0xf);
1323 break;
1326 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1327 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1328 if ((typedata[0] == ((basevt << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1331 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1332 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1333 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1335 typedata[0] = (basevt << 16) | VT_USERDEFINED;
1336 typedata[1] = tdesc->u.hreftype;
1339 *encoded_tdesc = typeoffset;
1340 *width = 0;
1341 *alignment = 1;
1342 break;
1345 default:
1346 FIXME("Unrecognized type %d.\n", tdesc->vt);
1347 *encoded_tdesc = default_tdesc;
1348 *width = 0;
1349 *alignment = 1;
1350 break;
1353 return 0;
1356 /****************************************************************************
1357 * ctl2_decode_typedesc
1359 * Decodes a type description from an ICreateTypeLib2Impl.
1361 * RETURNS
1363 * Success: S_OK.
1364 * Failure: HRESULT error code.
1366 static HRESULT ctl2_decode_typedesc(
1367 ICreateTypeLib2Impl *This, /* [I] The type library from which to decode the TYPEDESC. */
1368 int encoded_tdesc, /* [I] The encoded type description. */
1369 TYPEDESC *tdesc) /* [O] The decoded type description. */
1371 int *typedata, i;
1372 HRESULT hres;
1374 if (encoded_tdesc & 0x80000000) {
1375 tdesc->vt = encoded_tdesc & VT_TYPEMASK;
1376 tdesc->u.lptdesc = NULL;
1377 return S_OK;
1380 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][encoded_tdesc];
1382 tdesc->vt = typedata[0] & 0xFFFF;
1384 switch(tdesc->vt) {
1385 case VT_PTR:
1386 case VT_SAFEARRAY:
1387 tdesc->u.lptdesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TYPEDESC));
1388 if (!tdesc->u.lptdesc)
1389 return E_OUTOFMEMORY;
1391 hres = ctl2_decode_typedesc(This, typedata[1], tdesc->u.lptdesc);
1392 if (FAILED(hres)) {
1393 HeapFree(GetProcessHeap(), 0, tdesc->u.lptdesc);
1394 return hres;
1397 return S_OK;
1399 case VT_CARRAY: {
1400 int arrayoffset, *arraydata, num_dims;
1402 arrayoffset = typedata[1];
1403 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1404 num_dims = arraydata[1] & 0xFFFF;
1406 tdesc->u.lpadesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1407 sizeof(ARRAYDESC) + sizeof(SAFEARRAYBOUND) * (num_dims - 1));
1408 if (!tdesc->u.lpadesc)
1409 return E_OUTOFMEMORY;
1411 hres = ctl2_decode_typedesc(This, arraydata[0], &tdesc->u.lpadesc->tdescElem);
1412 if (FAILED(hres)) {
1413 HeapFree(GetProcessHeap(), 0, tdesc->u.lpadesc);
1414 return E_OUTOFMEMORY;
1417 for (i = 0; i < num_dims; ++i) {
1418 tdesc->u.lpadesc->rgbounds[i].cElements = arraydata[2 + i * 2];
1419 tdesc->u.lpadesc->rgbounds[i].lLbound = arraydata[3 + i * 2];
1422 return S_OK;
1424 case VT_USERDEFINED:
1425 tdesc->u.hreftype = typedata[1];
1426 return S_OK;
1427 default:
1428 FIXME("unable to decode typedesc (%08x): unknown VT: %d\n", encoded_tdesc, tdesc->vt);
1429 return E_NOTIMPL;
1433 /****************************************************************************
1434 * ctl2_find_nth_reference
1436 * Finds a reference by index into the linked list of reference records.
1438 * RETURNS
1440 * Success: Offset of the desired reference record.
1441 * Failure: -1.
1443 static int ctl2_find_nth_reference(
1444 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1445 int offset, /* [I] The starting offset of the reference list. */
1446 int index) /* [I] The index of the reference to find. */
1448 MSFT_RefRecord *ref;
1450 for (; index && (offset != -1); index--) {
1451 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1452 offset = ref->onext;
1455 return offset;
1458 /****************************************************************************
1459 * ctl2_find_typeinfo_from_offset
1461 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1463 * RETURNS
1465 * Success: S_OK.
1466 * Failure: TYPE_E_ELEMENTNOTFOUND.
1468 static HRESULT ctl2_find_typeinfo_from_offset(
1469 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1470 int offset, /* [I] The offset of the desired typeinfo. */
1471 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1473 void *typeinfodata;
1474 ICreateTypeInfo2Impl *typeinfo;
1476 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1478 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1479 if (typeinfo->typeinfo == typeinfodata) {
1480 *ppTinfo = (ITypeInfo *)&typeinfo->lpVtblTypeInfo2;
1481 ITypeInfo2_AddRef(*ppTinfo);
1482 return S_OK;
1486 ERR("Failed to find typeinfo, invariant varied.\n");
1488 return TYPE_E_ELEMENTNOTFOUND;
1491 /****************************************************************************
1492 * funcrecord_reallochdr
1494 * Ensure FuncRecord data block contains header of required size
1496 * PARAMS
1498 * typedata [IO] - reference to pointer to data block
1499 * need [I] - required size of block in bytes
1501 * RETURNS
1503 * Number of additionally allocated bytes
1505 static INT funcrecord_reallochdr(INT **typedata, int need)
1507 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1508 int hdr = (*typedata)[0] - tail;
1509 int i;
1511 if (hdr >= need)
1512 return 0;
1514 *typedata = HeapReAlloc(GetProcessHeap(), 0, *typedata, need + tail);
1515 if (!*typedata)
1516 return -1;
1518 if (tail)
1519 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1520 (*typedata)[0] = need + tail;
1522 /* fill in default values */
1523 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1525 switch(i)
1527 case 2:
1528 (*typedata)[i] = 0;
1529 break;
1530 case 7:
1531 (*typedata)[i] = -1;
1532 break;
1533 case 8:
1534 (*typedata)[i] = -1;
1535 break;
1536 case 9:
1537 (*typedata)[i] = -1;
1538 break;
1539 case 10:
1540 (*typedata)[i] = -1;
1541 break;
1542 case 11:
1543 (*typedata)[i] = 0;
1544 break;
1545 case 12:
1546 (*typedata)[i] = -1;
1547 break;
1551 return need - hdr;
1554 /*================== ICreateTypeInfo2 Implementation ===================================*/
1556 /******************************************************************************
1557 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1560 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1561 ICreateTypeInfo2 * iface,
1562 REFIID riid,
1563 VOID **ppvObject)
1565 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1567 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1569 *ppvObject=NULL;
1570 if(IsEqualIID(riid, &IID_IUnknown) ||
1571 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1572 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1574 *ppvObject = This;
1575 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1576 IsEqualIID(riid, &IID_ITypeInfo2)) {
1577 *ppvObject = &This->lpVtblTypeInfo2;
1580 if(*ppvObject)
1582 ICreateTypeInfo2_AddRef(iface);
1583 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1584 return S_OK;
1586 TRACE("-- Interface: E_NOINTERFACE\n");
1587 return E_NOINTERFACE;
1590 /******************************************************************************
1591 * ICreateTypeInfo2_AddRef {OLEAUT32}
1593 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1595 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1596 ULONG ref = InterlockedIncrement(&This->ref);
1598 TRACE("(%p)->ref was %u\n",This, ref - 1);
1600 if(ref==1 && This->typelib)
1601 ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This->typelib);
1603 return ref;
1606 /******************************************************************************
1607 * ICreateTypeInfo2_Release {OLEAUT32}
1609 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1611 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1612 ULONG ref = InterlockedDecrement(&This->ref);
1614 TRACE("(%p)->(%u)\n",This, ref);
1616 if (!ref) {
1617 if (This->typelib) {
1618 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)This->typelib);
1619 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1620 /* This->typelib = NULL; */
1623 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1624 /* HeapFree(GetProcessHeap(),0,This); */
1625 return 0;
1628 return ref;
1632 /******************************************************************************
1633 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1635 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1637 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1639 MSFT_GuidEntry guidentry;
1640 int offset;
1642 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1644 guidentry.guid = *guid;
1645 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1646 guidentry.next_hash = -1;
1648 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1650 if (offset == -1) return E_OUTOFMEMORY;
1652 This->typeinfo->posguid = offset;
1654 if (IsEqualIID(guid, &IID_IDispatch)) {
1655 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1658 return S_OK;
1661 /******************************************************************************
1662 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1664 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1666 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1668 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1670 if(uTypeFlags & TYPEFLAG_FDUAL) {
1671 This->typeinfo->typekind |= 0x10;
1672 This->typeinfo->typekind &= ~0x0f;
1673 This->typeinfo->typekind |= TKIND_DISPATCH;
1675 if(!This->dual) {
1676 This->dual = HeapAlloc(GetProcessHeap(), 0, sizeof(ICreateTypeInfo2Impl));
1677 if(!This->dual)
1678 return E_OUTOFMEMORY;
1680 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1681 This->dual->ref = 0;
1682 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1683 TKIND_INTERFACE : TKIND_DISPATCH;
1684 This->dual->dual = This;
1687 /* Make sure dispatch is in typeinfos queue */
1688 if(This->typekind != TKIND_DISPATCH) {
1689 if(This->typelib->last_typeinfo == This)
1690 This->typelib->last_typeinfo = This->dual;
1692 if(This->typelib->typeinfos == This)
1693 This->typelib->typeinfos = This->dual;
1694 else {
1695 ICreateTypeInfo2Impl *iter;
1697 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1698 iter->next_typeinfo = This->dual;
1700 } else
1701 iface = (ICreateTypeInfo2*)&This->dual->lpVtbl;
1704 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1705 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1706 ITypeLib *stdole;
1707 ITypeInfo *dispatch;
1708 HREFTYPE hreftype;
1709 HRESULT hres;
1711 hres = LoadTypeLib(stdole2tlb, &stdole);
1712 if(FAILED(hres))
1713 return hres;
1715 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1716 ITypeLib_Release(stdole);
1717 if(FAILED(hres))
1718 return hres;
1720 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1721 ITypeInfo_Release(dispatch);
1722 if(FAILED(hres))
1723 return hres;
1726 This->typeinfo->flags = uTypeFlags;
1727 return S_OK;
1730 /******************************************************************************
1731 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1733 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1734 ICreateTypeInfo2* iface,
1735 LPOLESTR pStrDoc)
1737 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1739 int offset;
1741 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1742 if (!pStrDoc)
1743 return E_INVALIDARG;
1745 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1746 if (offset == -1) return E_OUTOFMEMORY;
1747 This->typeinfo->docstringoffs = offset;
1748 return S_OK;
1751 /******************************************************************************
1752 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1754 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1755 ICreateTypeInfo2* iface,
1756 DWORD dwHelpContext)
1758 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1760 TRACE("(%p,%d)\n", iface, dwHelpContext);
1762 This->typeinfo->helpcontext = dwHelpContext;
1764 return S_OK;
1767 /******************************************************************************
1768 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1770 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1771 ICreateTypeInfo2* iface,
1772 WORD wMajorVerNum,
1773 WORD wMinorVerNum)
1775 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1777 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1779 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1780 return S_OK;
1783 /******************************************************************************
1784 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1786 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1787 ICreateTypeInfo2* iface,
1788 ITypeInfo* pTInfo,
1789 HREFTYPE* phRefType)
1791 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1793 ITypeLib *container;
1794 UINT index;
1795 HRESULT res;
1797 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1799 if(!pTInfo || !phRefType)
1800 return E_INVALIDARG;
1803 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1804 * same internal structure as one of ours. It could be from another
1805 * implementation of ITypeInfo. So we need to do the following...
1807 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1808 if (FAILED(res)) {
1809 TRACE("failed to find containing typelib.\n");
1810 return res;
1813 if (container == (ITypeLib *)&This->typelib->lpVtblTypeLib2) {
1814 /* Process locally defined TypeInfo */
1815 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1816 } else {
1817 BSTR name;
1818 TLIBATTR *tlibattr;
1819 TYPEATTR *typeattr;
1820 TYPEKIND typekind;
1821 MSFT_GuidEntry guid, *check_guid;
1822 MSFT_ImpInfo impinfo;
1823 int guid_offset, import_offset;
1824 HRESULT hres;
1826 /* Allocate container GUID */
1827 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1828 if(FAILED(hres)) {
1829 ITypeLib_Release(container);
1830 return hres;
1833 guid.guid = tlibattr->guid;
1834 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1835 guid.next_hash = -1;
1837 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1838 if(guid_offset == -1) {
1839 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1840 ITypeLib_Release(container);
1841 return E_OUTOFMEMORY;
1844 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1845 if(check_guid->hreftype == guid.hreftype)
1846 This->typelib->typelib_guids++;
1848 /* Get import file name */
1849 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1850 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1851 if(FAILED(hres)) {
1852 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1853 ITypeLib_Release(container);
1854 return hres;
1857 /* Import file */
1858 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1859 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1860 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1861 SysFreeString(name);
1863 if(import_offset == -1) {
1864 ITypeLib_Release(container);
1865 return E_OUTOFMEMORY;
1868 /* Allocate referenced guid */
1869 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1870 if(FAILED(hres)) {
1871 ITypeLib_Release(container);
1872 return hres;
1875 guid.guid = typeattr->guid;
1876 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1877 guid.next_hash = -1;
1878 typekind = typeattr->typekind;
1879 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1881 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1882 if(guid_offset == -1) {
1883 ITypeLib_Release(container);
1884 return E_OUTOFMEMORY;
1887 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1888 if(check_guid->hreftype == guid.hreftype)
1889 This->typelib->typeinfo_guids++;
1891 /* Allocate importinfo */
1892 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1893 impinfo.oImpFile = import_offset;
1894 impinfo.oGuid = guid_offset;
1895 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1897 if(IsEqualGUID(&guid.guid, &IID_IDispatch))
1898 This->typelib->typelib_header.dispatchpos = *phRefType;
1901 ITypeLib_Release(container);
1902 return S_OK;
1905 /******************************************************************************
1906 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1908 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1909 ICreateTypeInfo2* iface,
1910 UINT index,
1911 FUNCDESC* pFuncDesc)
1913 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1915 CyclicList *iter, *insert;
1916 int *typedata;
1917 int i, num_defaults = 0, num_retval = 0;
1918 int decoded_size;
1919 HRESULT hres;
1921 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1923 if(!pFuncDesc || pFuncDesc->oVft&3)
1924 return E_INVALIDARG;
1926 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1927 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1928 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1929 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1930 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1932 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1933 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1935 switch(This->typekind) {
1936 case TKIND_MODULE:
1937 if(pFuncDesc->funckind != FUNC_STATIC)
1938 return TYPE_E_BADMODULEKIND;
1939 break;
1940 case TKIND_DISPATCH:
1941 if(pFuncDesc->funckind != FUNC_DISPATCH)
1942 return TYPE_E_BADMODULEKIND;
1943 break;
1944 default:
1945 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1946 return TYPE_E_BADMODULEKIND;
1949 if(cti2_get_func_count(This->typeinfo) < index)
1950 return TYPE_E_ELEMENTNOTFOUND;
1952 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1953 !pFuncDesc->cParams)
1954 return TYPE_E_INCONSISTENTPROPFUNCS;
1956 /* get number of arguments with default values specified */
1957 for (i = 0; i < pFuncDesc->cParams; i++) {
1958 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1959 num_defaults++;
1960 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1961 num_retval++;
1964 if (!This->typedata) {
1965 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
1966 if(!This->typedata)
1967 return E_OUTOFMEMORY;
1969 This->typedata->next = This->typedata;
1971 if(This->dual)
1972 This->dual->typedata = This->typedata;
1975 /* allocate type data space for us */
1976 insert = alloc_cyclic_list_item(CyclicListFunc);
1977 if(!insert)
1978 return E_OUTOFMEMORY;
1979 insert->u.data = HeapAlloc(GetProcessHeap(), 0, sizeof(int[6])+sizeof(int[(num_defaults?4:3)])*pFuncDesc->cParams);
1980 if(!insert->u.data) {
1981 HeapFree(GetProcessHeap(), 0, insert);
1982 return E_OUTOFMEMORY;
1985 /* fill out the basic type information */
1986 typedata = insert->u.data;
1987 typedata[0] = 0x18 + pFuncDesc->cParams*(num_defaults?16:12);
1988 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
1989 typedata[2] = pFuncDesc->wFuncFlags;
1990 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
1991 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
1992 if(num_defaults) typedata[4] |= 0x1000;
1993 if (num_retval) typedata[4] |= 0x4000;
1994 typedata[5] = pFuncDesc->cParams;
1996 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1997 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1998 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
1999 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
2001 /* add default values */
2002 if(num_defaults) {
2003 for (i = 0; i < pFuncDesc->cParams; i++)
2004 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
2005 hres = ctl2_encode_variant(This->typelib, typedata+6+i,
2006 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
2007 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
2009 if(FAILED(hres)) {
2010 HeapFree(GetProcessHeap(), 0, insert->u.data);
2011 HeapFree(GetProcessHeap(), 0, insert);
2012 return hres;
2014 } else
2015 typedata[6+i] = 0xffffffff;
2017 num_defaults = pFuncDesc->cParams;
2020 /* add arguments */
2021 for (i = 0; i < pFuncDesc->cParams; i++) {
2022 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
2023 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
2024 typedata[7+num_defaults+(i*3)] = -1;
2025 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
2026 typedata[3] += decoded_size << 16;
2029 /* update the index data */
2030 insert->indice = pFuncDesc->memid;
2031 insert->name = -1;
2033 /* insert type data to list */
2034 if(index == cti2_get_func_count(This->typeinfo)) {
2035 insert->next = This->typedata->next;
2036 This->typedata->next = insert;
2037 This->typedata = insert;
2039 if(This->dual)
2040 This->dual->typedata = This->typedata;
2041 } else {
2042 iter = This->typedata->next;
2043 for(i=0; i<index; i++)
2044 iter = iter->next;
2046 insert->next = iter->next;
2047 iter->next = insert;
2050 /* update type data size */
2051 This->typedata->next->u.val += 0x18 + pFuncDesc->cParams*(num_defaults?16:12);
2053 /* Increment the number of function elements */
2054 This->typeinfo->cElement += 1;
2056 return S_OK;
2059 /******************************************************************************
2060 * ICreateTypeInfo2_AddImplType {OLEAUT32}
2062 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
2063 ICreateTypeInfo2* iface,
2064 UINT index,
2065 HREFTYPE hRefType)
2067 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2069 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
2071 if (This->typekind == TKIND_COCLASS) {
2072 int offset;
2073 MSFT_RefRecord *ref;
2075 if (index == 0) {
2076 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
2078 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2079 if (offset == -1) return E_OUTOFMEMORY;
2081 This->typeinfo->datatype1 = offset;
2082 } else {
2083 int lastoffset;
2085 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
2086 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
2088 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
2089 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
2091 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2092 if (offset == -1) return E_OUTOFMEMORY;
2094 ref->onext = offset;
2097 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2099 ref->reftype = hRefType;
2100 ref->flags = 0;
2101 ref->oCustData = -1;
2102 ref->onext = -1;
2103 This->typeinfo->cImplTypes++;
2104 } else if (This->typekind == TKIND_INTERFACE) {
2105 if (This->typeinfo->cImplTypes && index==1)
2106 return TYPE_E_BADMODULEKIND;
2108 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
2110 This->typeinfo->datatype1 = hRefType;
2111 This->typeinfo->cImplTypes = 1;
2112 } else if (This->typekind == TKIND_DISPATCH) {
2113 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
2115 /* FIXME: Check if referenced typeinfo is IDispatch */
2116 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2117 This->typeinfo->cImplTypes = 1;
2118 } else {
2119 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
2120 return E_OUTOFMEMORY;
2123 return S_OK;
2126 /******************************************************************************
2127 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
2129 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
2130 ICreateTypeInfo2* iface,
2131 UINT index,
2132 INT implTypeFlags)
2134 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2135 int offset;
2136 MSFT_RefRecord *ref;
2138 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
2140 if (This->typekind != TKIND_COCLASS) {
2141 return TYPE_E_BADMODULEKIND;
2144 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2145 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2147 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2148 ref->flags = implTypeFlags;
2150 return S_OK;
2153 /******************************************************************************
2154 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2156 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2157 ICreateTypeInfo2* iface,
2158 WORD cbAlignment)
2160 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2162 TRACE("(%p,%d)\n", iface, cbAlignment);
2164 if (!cbAlignment) return E_INVALIDARG;
2165 if (cbAlignment > 16) return E_INVALIDARG;
2167 This->typeinfo->typekind &= ~0xffc0;
2168 This->typeinfo->typekind |= cbAlignment << 6;
2170 /* FIXME: There's probably some way to simplify this. */
2171 switch (This->typekind) {
2172 case TKIND_ALIAS:
2173 default:
2174 break;
2176 case TKIND_ENUM:
2177 case TKIND_INTERFACE:
2178 case TKIND_DISPATCH:
2179 case TKIND_COCLASS:
2180 if (cbAlignment > 4) cbAlignment = 4;
2181 break;
2183 case TKIND_RECORD:
2184 case TKIND_MODULE:
2185 case TKIND_UNION:
2186 cbAlignment = 1;
2187 break;
2190 This->typeinfo->typekind |= cbAlignment << 11;
2192 return S_OK;
2195 /******************************************************************************
2196 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2198 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2199 ICreateTypeInfo2* iface,
2200 LPOLESTR pStrSchema)
2202 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2203 return E_OUTOFMEMORY;
2206 /******************************************************************************
2207 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2209 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2210 ICreateTypeInfo2* iface,
2211 UINT index,
2212 VARDESC* pVarDesc)
2214 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2216 HRESULT status = S_OK;
2217 CyclicList *insert;
2218 INT *typedata;
2219 int var_datawidth;
2220 int var_alignment;
2221 int var_type_size;
2222 int alignment;
2224 TRACE("(%p,%d,%p)\n", iface, index, pVarDesc);
2225 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2226 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2227 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2228 pVarDesc->wVarFlags, pVarDesc->varkind);
2230 if (cti2_get_var_count(This->typeinfo) != index)
2231 return TYPE_E_ELEMENTNOTFOUND;
2233 if (!This->typedata) {
2234 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
2235 if(!This->typedata)
2236 return E_OUTOFMEMORY;
2238 This->typedata->next = This->typedata;
2240 if(This->dual)
2241 This->dual->typedata = This->typedata;
2244 insert = alloc_cyclic_list_item(CyclicListVar);
2245 if(!insert)
2246 return E_OUTOFMEMORY;
2248 /* allocate whole structure, it's fixed size always */
2249 insert->u.data = HeapAlloc(GetProcessHeap(), 0, sizeof(MSFT_VarRecord));
2250 if(!insert->u.data) {
2251 HeapFree(GetProcessHeap(), 0, insert);
2252 return E_OUTOFMEMORY;
2255 insert->next = This->typedata->next;
2256 This->typedata->next = insert;
2257 This->typedata = insert;
2259 if(This->dual)
2260 This->dual->typedata = This->typedata;
2262 This->typedata->next->u.val += FIELD_OFFSET(MSFT_VarRecord, HelpContext);
2263 typedata = This->typedata->u.data;
2265 /* fill out the basic type information */
2267 /* no optional fields initially */
2268 typedata[0] = FIELD_OFFSET(MSFT_VarRecord, HelpContext) | (index << 16);
2269 typedata[2] = pVarDesc->wVarFlags;
2270 typedata[3] = (sizeof(VARDESC) << 16) | pVarDesc->varkind;
2272 /* update the index data */
2273 insert->indice = 0x40000000 + index;
2274 insert->name = -1;
2276 /* figure out type widths and whatnot */
2277 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2278 &typedata[1], &var_datawidth, &var_alignment,
2279 &var_type_size);
2281 if (pVarDesc->varkind != VAR_CONST)
2283 /* pad out starting position to data width */
2284 This->datawidth += var_alignment - 1;
2285 This->datawidth &= ~(var_alignment - 1);
2286 typedata[4] = This->datawidth;
2288 /* add the new variable to the total data width */
2289 This->datawidth += var_datawidth;
2290 if(This->dual)
2291 This->dual->datawidth = This->datawidth;
2293 /* add type description size to total required allocation */
2294 typedata[3] += var_type_size << 16;
2296 /* fix type alignment */
2297 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2298 if (alignment < var_alignment) {
2299 alignment = var_alignment;
2300 This->typeinfo->typekind &= ~0xf800;
2301 This->typeinfo->typekind |= alignment << 11;
2304 /* ??? */
2305 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2306 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2307 This->typeinfo->res2 <<= 1;
2310 /* ??? */
2311 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2312 This->typeinfo->res3 += 0x2c;
2314 /* pad data width to alignment */
2315 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2316 } else {
2317 VARIANT *value = pVarDesc->DUMMYUNIONNAME.lpvarValue;
2318 status = ctl2_encode_variant(This->typelib, typedata+4, value, V_VT(value));
2319 /* ??? native sets size 0x34 */
2320 typedata[3] += 0x10 << 16;
2323 /* increment the number of variable elements */
2324 This->typeinfo->cElement += 0x10000;
2326 return status;
2329 /******************************************************************************
2330 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2332 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2333 ICreateTypeInfo2* iface,
2334 UINT index,
2335 LPOLESTR* names,
2336 UINT cNames)
2338 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2339 CyclicList *iter, *iter2;
2340 int offset, len, i;
2341 char *namedata;
2343 TRACE("(%p %d %p %d)\n", This, index, names, cNames);
2345 if(!names)
2346 return E_INVALIDARG;
2348 if(index >= cti2_get_func_count(This->typeinfo) || cNames == 0)
2349 return TYPE_E_ELEMENTNOTFOUND;
2351 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2352 if (iter->type == CyclicListFunc)
2353 if (i++ >= index)
2354 break;
2356 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2357 if(cNames != iter->u.data[5] + (ctl2_get_invokekind(iter) & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2358 return TYPE_E_ELEMENTNOTFOUND;
2360 TRACE("function name %s\n", debugstr_w(names[0]));
2361 len = ctl2_encode_name(This->typelib, names[0], &namedata);
2362 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2364 int cmp = memcmp(namedata, This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len);
2365 if (iter2->name != -1 && cmp == 0) {
2366 if (iter2->type == CyclicListFunc) {
2367 INVOKEKIND inv1 = ctl2_get_invokekind(iter);
2368 INVOKEKIND inv2 = ctl2_get_invokekind(iter2);
2370 /* it's allowed to have PUT, PUTREF and GET methods with the same name */
2371 if ((inv1 != inv2) &&
2372 (inv1 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)) &&
2373 (inv2 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)))
2374 continue;
2377 return TYPE_E_AMBIGUOUSNAME;
2381 offset = ctl2_alloc_name(This->typelib, names[0]);
2382 if(offset == -1)
2383 return E_OUTOFMEMORY;
2385 iter->name = offset;
2387 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2388 if (*((INT*)namedata) == -1)
2389 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2391 len = ctl2_get_record_size(iter)/4 - iter->u.data[5]*3;
2393 for (i = 1; i < cNames; i++) {
2394 offset = ctl2_alloc_name(This->typelib, names[i]);
2395 iter->u.data[len + ((i-1)*3) + 1] = offset;
2398 return S_OK;
2401 /******************************************************************************
2402 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2404 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2405 ICreateTypeInfo2* iface,
2406 UINT index,
2407 LPOLESTR szName)
2409 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2410 CyclicList *iter;
2411 int offset, i;
2412 char *namedata;
2414 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(szName));
2416 if (cti2_get_var_count(This->typeinfo) <= index)
2417 return TYPE_E_ELEMENTNOTFOUND;
2419 offset = ctl2_alloc_name(This->typelib, szName);
2420 if (offset == -1) return E_OUTOFMEMORY;
2422 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2423 if (*((INT *)namedata) == -1) {
2424 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2425 namedata[9] |= 0x10;
2427 if (This->typekind == TKIND_ENUM) {
2428 namedata[9] |= 0x20;
2431 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2432 if (iter->type == CyclicListVar)
2433 if (i++ >= index)
2434 break;
2436 iter->name = offset;
2437 return S_OK;
2440 /******************************************************************************
2441 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2443 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2444 ICreateTypeInfo2* iface,
2445 TYPEDESC* pTDescAlias)
2447 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2449 int encoded_typedesc;
2450 int width;
2452 if (This->typekind != TKIND_ALIAS) {
2453 return TYPE_E_WRONGTYPEKIND;
2456 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2458 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2459 return E_OUTOFMEMORY;
2462 This->typeinfo->size = width;
2463 This->typeinfo->datatype1 = encoded_typedesc;
2465 return S_OK;
2468 /******************************************************************************
2469 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2471 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2472 ICreateTypeInfo2* iface,
2473 UINT index,
2474 LPOLESTR szDllName,
2475 LPOLESTR szProcName)
2477 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2478 return E_OUTOFMEMORY;
2481 /******************************************************************************
2482 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2484 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2485 ICreateTypeInfo2* iface,
2486 UINT index,
2487 LPOLESTR szDocString)
2489 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2490 return E_OUTOFMEMORY;
2493 /******************************************************************************
2494 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2496 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2497 ICreateTypeInfo2* iface,
2498 UINT index,
2499 LPOLESTR szDocString)
2501 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2503 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2505 ctl2_alloc_string(This->typelib, szDocString);
2507 return E_OUTOFMEMORY;
2510 /******************************************************************************
2511 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2513 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2514 ICreateTypeInfo2* iface,
2515 UINT index,
2516 DWORD dwHelpContext)
2518 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2519 CyclicList *func;
2521 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2523 if(cti2_get_func_count(This->typeinfo) < index)
2524 return TYPE_E_ELEMENTNOTFOUND;
2526 if(cti2_get_func_count(This->typeinfo) == index && This->typedata->type == CyclicListFunc)
2527 func = This->typedata;
2528 else
2529 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2530 if (func->type == CyclicListFunc)
2531 if(index-- == 0)
2532 break;
2534 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2535 if(!func->u.data)
2536 return E_OUTOFMEMORY;
2538 func->u.data[6] = dwHelpContext;
2539 return S_OK;
2542 /******************************************************************************
2543 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2545 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2546 ICreateTypeInfo2* iface,
2547 UINT index,
2548 DWORD context)
2550 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2551 CyclicList *iter;
2553 TRACE("(%p,%d,%d)\n", This, index, context);
2555 if (cti2_get_var_count(This->typeinfo) <= index)
2556 return TYPE_E_ELEMENTNOTFOUND;
2558 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2559 if (iter->type == CyclicListVar)
2561 if (index-- == 0)
2563 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, HelpString));
2564 iter->u.data[5] = context;
2565 return S_OK;
2569 return TYPE_E_ELEMENTNOTFOUND;
2572 /******************************************************************************
2573 * ICreateTypeInfo2_SetMops {OLEAUT32}
2575 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2576 ICreateTypeInfo2* iface,
2577 UINT index,
2578 BSTR bstrMops)
2580 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2581 return E_OUTOFMEMORY;
2584 /******************************************************************************
2585 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2587 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2588 ICreateTypeInfo2* iface,
2589 IDLDESC* pIdlDesc)
2591 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2592 return E_OUTOFMEMORY;
2595 /******************************************************************************
2596 * ICreateTypeInfo2_LayOut {OLEAUT32}
2598 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2599 ICreateTypeInfo2* iface)
2601 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2602 CyclicList *iter, *iter2, *last = NULL, **typedata;
2603 HREFTYPE hreftype;
2604 HRESULT hres;
2605 unsigned user_vft = 0;
2606 int i;
2608 TRACE("(%p)\n", iface);
2610 /* FIXME: LayOut should be run on all ImplTypes */
2611 if(This->typekind == TKIND_COCLASS)
2612 return S_OK;
2614 /* Validate inheritance */
2615 This->typeinfo->datatype2 = 0;
2616 hreftype = This->typeinfo->datatype1;
2618 /* Process internally defined interfaces */
2619 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2620 MSFT_TypeInfoBase *header;
2622 if(hreftype&1)
2623 break;
2625 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2626 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2627 hreftype = header->datatype1;
2629 if(i == This->typelib->typelib_header.nrtypeinfos)
2630 return TYPE_E_CIRCULARTYPE;
2632 /* Process externally defined interfaces */
2633 if(hreftype != -1) {
2634 ITypeInfo *cur, *next;
2635 TYPEATTR *typeattr;
2637 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2638 if(FAILED(hres))
2639 return hres;
2641 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2642 ITypeInfo_Release(next);
2643 if(FAILED(hres))
2644 return hres;
2647 while(1) {
2648 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2649 if(FAILED(hres)) {
2650 ITypeInfo_Release(cur);
2651 return hres;
2654 if(IsEqualGUID(&typeattr->guid, &IID_IDispatch))
2655 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2657 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2658 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2660 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2661 if(hres == TYPE_E_ELEMENTNOTFOUND)
2662 break;
2663 if(FAILED(hres)) {
2664 ITypeInfo_Release(cur);
2665 return hres;
2668 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2669 if(FAILED(hres)) {
2670 ITypeInfo_Release(cur);
2671 return hres;
2674 ITypeInfo_Release(cur);
2675 cur = next;
2677 ITypeInfo_Release(cur);
2680 /* Get cbSizeVft of inherited interface */
2681 /* Makes LayOut running recursively */
2682 if(This->typeinfo->datatype1 != -1) {
2683 ITypeInfo *cur, *inherited;
2684 TYPEATTR *typeattr;
2686 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2687 if(FAILED(hres))
2688 return hres;
2690 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2691 ITypeInfo_Release(cur);
2692 if(FAILED(hres))
2693 return hres;
2695 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2696 if(FAILED(hres)) {
2697 ITypeInfo_Release(inherited);
2698 return hres;
2701 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2703 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2704 ITypeInfo_Release(inherited);
2705 } else
2706 This->typeinfo->cbSizeVft = 0;
2708 if(!This->typedata)
2709 return S_OK;
2711 typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList*)*cti2_get_func_count(This->typeinfo));
2712 if(!typedata)
2713 return E_OUTOFMEMORY;
2715 /* Assign IDs and VTBL entries */
2716 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2717 if (iter->type == CyclicListFunc)
2718 last = iter;
2720 if(last && last->u.data[3]&1)
2721 user_vft = last->u.data[3]&0xffff;
2723 i = 0;
2724 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2725 /* Assign MEMBERID if MEMBERID_NIL was specified */
2726 if(iter->indice == MEMBERID_NIL) {
2727 iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2729 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2730 if(iter == iter2) continue;
2731 if(iter2->indice == iter->indice) {
2732 iter->indice = 0x60000000 + This->typeinfo->cElement + (This->typeinfo->datatype2<<16);
2734 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2735 if(iter == iter2) continue;
2736 if(iter2->indice == iter->indice) {
2737 ++iter->indice;
2738 iter2 = This->typedata->next;
2742 break;
2747 if (iter->type != CyclicListFunc)
2748 continue;
2750 typedata[i] = iter;
2752 iter->u.data[0] = ctl2_get_record_size(iter) | (i<<16);
2754 if((iter->u.data[3]&1) != (user_vft&1)) {
2755 HeapFree(GetProcessHeap(), 0, typedata);
2756 return TYPE_E_INVALIDID;
2759 if(user_vft&1) {
2760 if(user_vft < (iter->u.data[3]&0xffff))
2761 user_vft = (iter->u.data[3]&0xffff);
2763 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2764 HeapFree(GetProcessHeap(), 0, typedata);
2765 return TYPE_E_INVALIDID;
2767 } else if(This->typekind != TKIND_MODULE) {
2768 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2769 This->typeinfo->cbSizeVft += 4;
2772 /* Construct a list of elements with the same memberid */
2773 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2774 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2775 if(iter->indice == iter2->indice) {
2776 int v1, v2;
2778 v1 = iter->u.data[4] >> 16;
2779 v2 = iter2->u.data[4] >> 16;
2781 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2782 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2783 break;
2787 i++;
2790 if(user_vft)
2791 This->typeinfo->cbSizeVft = user_vft+3;
2793 for(i=0; i< cti2_get_func_count(This->typeinfo); i++) {
2794 if(typedata[i]->u.data[4]>>16 > i) {
2795 INVOKEKIND inv = ctl2_get_invokekind(typedata[i]);
2797 i = typedata[i]->u.data[4] >> 16;
2799 while(i > typedata[i]->u.data[4]>>16) {
2800 INVOKEKIND invkind = ctl2_get_invokekind(typedata[i]);
2802 if(inv & invkind) {
2803 HeapFree(GetProcessHeap(), 0, typedata);
2804 return TYPE_E_DUPLICATEID;
2807 i = typedata[i]->u.data[4] >> 16;
2808 inv |= invkind;
2811 if(inv & INVOKE_FUNC) {
2812 HeapFree(GetProcessHeap(), 0, typedata);
2813 return TYPE_E_INCONSISTENTPROPFUNCS;
2818 HeapFree(GetProcessHeap(), 0, typedata);
2819 return S_OK;
2822 /******************************************************************************
2823 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2825 * Delete a function description from a type.
2827 * RETURNS
2829 * Success: S_OK.
2830 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2832 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2833 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2834 UINT index) /* [I] The index of the function to delete. */
2836 FIXME("(%p,%d), stub!\n", iface, index);
2837 return E_OUTOFMEMORY;
2840 /******************************************************************************
2841 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2843 * Delete a function description from a type.
2845 * RETURNS
2847 * Success: S_OK.
2848 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2850 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2851 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2852 MEMBERID memid, /* [I] The member id of the function to delete. */
2853 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2855 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2856 return E_OUTOFMEMORY;
2859 /******************************************************************************
2860 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2862 * Delete a variable description from a type.
2864 * RETURNS
2866 * Success: S_OK.
2867 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2868 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2870 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2871 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2872 UINT index) /* [I] The index of the variable description to delete. */
2874 FIXME("(%p,%d), stub!\n", iface, index);
2875 return E_OUTOFMEMORY;
2878 /******************************************************************************
2879 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2881 * Delete a variable description from a type.
2883 * RETURNS
2885 * Success: S_OK.
2886 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2887 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2889 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2890 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2891 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2893 FIXME("(%p,%d), stub!\n", iface, memid);
2894 return E_OUTOFMEMORY;
2897 /******************************************************************************
2898 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2900 * Delete an interface implementation from a type. (?)
2902 * RETURNS
2904 * Success: S_OK.
2905 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2907 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2908 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2909 UINT index) /* [I] The index of the interface to delete. */
2911 FIXME("(%p,%d), stub!\n", iface, index);
2912 return E_OUTOFMEMORY;
2915 /******************************************************************************
2916 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2918 * Set the custom data for a type.
2920 * RETURNS
2922 * Success: S_OK.
2923 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2925 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2926 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2927 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2928 VARIANT* pVarVal) /* [I] The custom data. */
2930 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2932 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2934 if (!pVarVal)
2935 return E_INVALIDARG;
2937 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2940 /******************************************************************************
2941 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2943 * Set the custom data for a function.
2945 * RETURNS
2947 * Success: S_OK.
2948 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2950 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2951 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2952 UINT index, /* [I] The index of the function for which to set the custom data. */
2953 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2954 VARIANT* pVarVal) /* [I] The custom data. */
2956 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2957 CyclicList *iter;
2959 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2961 if(index >= cti2_get_func_count(This->typeinfo))
2962 return TYPE_E_ELEMENTNOTFOUND;
2964 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2965 if (iter->type == CyclicListFunc)
2966 if (index-- == 0)
2967 break;
2969 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
2970 if(!iter->u.data)
2971 return E_OUTOFMEMORY;
2973 iter->u.data[4] |= 0x80;
2974 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
2977 /******************************************************************************
2978 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
2980 * Set the custom data for a function parameter.
2982 * RETURNS
2984 * Success: S_OK.
2985 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2987 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
2988 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2989 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
2990 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
2991 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2992 VARIANT* pVarVal) /* [I] The custom data. */
2994 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
2995 return E_OUTOFMEMORY;
2998 /******************************************************************************
2999 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
3001 * Set the custom data for a variable.
3003 * RETURNS
3005 * Success: S_OK.
3006 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3008 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
3009 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3010 UINT index, /* [I] The index of the variable on which to set the custom data. */
3011 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3012 VARIANT* pVarVal) /* [I] The custom data. */
3014 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3015 return E_OUTOFMEMORY;
3018 /******************************************************************************
3019 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
3021 * Set the custom data for an implemented interface.
3023 * RETURNS
3025 * Success: S_OK.
3026 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3028 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
3029 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
3030 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
3031 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3032 VARIANT* pVarVal) /* [I] The custom data. */
3034 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3035 return E_OUTOFMEMORY;
3038 /******************************************************************************
3039 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
3041 * Set the help string context for the typeinfo.
3043 * RETURNS
3045 * Success: S_OK.
3046 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3048 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
3049 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3050 ULONG dwHelpStringContext) /* [I] The help string context. */
3052 FIXME("(%p,%d), stub!\n", iface, dwHelpStringContext);
3053 return E_OUTOFMEMORY;
3056 /******************************************************************************
3057 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
3059 * Set the help string context for a function.
3061 * RETURNS
3063 * Success: S_OK.
3064 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3066 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
3067 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3068 UINT index, /* [I] The index for the function on which to set the help string context. */
3069 ULONG dwHelpStringContext) /* [I] The help string context. */
3071 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3072 return E_OUTOFMEMORY;
3075 /******************************************************************************
3076 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
3078 * Set the help string context for a variable.
3080 * RETURNS
3082 * Success: S_OK.
3083 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3085 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
3086 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3087 UINT index, /* [I] The index of the variable on which to set the help string context. */
3088 ULONG dwHelpStringContext) /* [I] The help string context */
3090 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3091 return E_OUTOFMEMORY;
3094 /******************************************************************************
3095 * ICreateTypeInfo2_Invalidate {OLEAUT32}
3097 * Undocumented function. (!)
3099 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
3100 ICreateTypeInfo2* iface)
3102 FIXME("(%p), stub!\n", iface);
3103 return E_OUTOFMEMORY;
3106 /******************************************************************************
3107 * ICreateTypeInfo2_SetName {OLEAUT32}
3109 * Set the name for a typeinfo.
3111 * RETURNS
3113 * Success: S_OK.
3114 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
3116 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
3117 ICreateTypeInfo2* iface,
3118 LPOLESTR szName)
3120 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
3121 return E_OUTOFMEMORY;
3124 /*================== ITypeInfo2 Implementation ===================================*/
3126 /******************************************************************************
3127 * ITypeInfo2_QueryInterface {OLEAUT32}
3129 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
3131 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3133 return ICreateTypeInfo2_QueryInterface((ICreateTypeInfo2 *)This, riid, ppv);
3136 /******************************************************************************
3137 * ITypeInfo2_AddRef {OLEAUT32}
3139 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3141 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3143 return ICreateTypeInfo2_AddRef((ICreateTypeInfo2 *)This);
3146 /******************************************************************************
3147 * ITypeInfo2_Release {OLEAUT32}
3149 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3151 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3153 return ICreateTypeInfo2_Release((ICreateTypeInfo2 *)This);
3156 /******************************************************************************
3157 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3159 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3160 ITypeInfo2* iface,
3161 TYPEATTR** ppTypeAttr)
3163 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3164 HRESULT hres;
3166 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3168 if(!ppTypeAttr)
3169 return E_INVALIDARG;
3171 hres = ICreateTypeInfo_LayOut((ICreateTypeInfo*)This);
3172 if(FAILED(hres))
3173 return hres;
3175 *ppTypeAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TYPEATTR));
3176 if(!*ppTypeAttr)
3177 return E_OUTOFMEMORY;
3179 if(This->typeinfo->posguid != -1) {
3180 MSFT_GuidEntry *guid;
3182 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3183 (*ppTypeAttr)->guid = guid->guid;
3186 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3187 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3188 (*ppTypeAttr)->typekind = This->typekind;
3189 (*ppTypeAttr)->cFuncs = cti2_get_func_count(This->typeinfo);
3190 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3191 (*ppTypeAttr)->cFuncs += 7;
3192 (*ppTypeAttr)->cVars = cti2_get_var_count(This->typeinfo);
3193 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3194 (*ppTypeAttr)->cbSizeVft = This->typekind==TKIND_DISPATCH ? 7 * sizeof(void*) : This->typeinfo->cbSizeVft;
3195 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3196 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3197 (*ppTypeAttr)->wMajorVerNum = LOWORD(This->typeinfo->version);
3198 (*ppTypeAttr)->wMinorVerNum = HIWORD(This->typeinfo->version);
3200 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3201 FIXME("TKIND_ALIAS handling not implemented\n");
3203 return S_OK;
3206 /******************************************************************************
3207 * ITypeInfo2_GetTypeComp {OLEAUT32}
3209 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3210 ITypeInfo2* iface,
3211 ITypeComp** ppTComp)
3213 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3214 return E_OUTOFMEMORY;
3217 /******************************************************************************
3218 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3220 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3221 ITypeInfo2* iface,
3222 UINT index,
3223 FUNCDESC** ppFuncDesc)
3225 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3226 int i, *typedata, num_defaults = 0, hdr_len, tail, has_defaults;
3227 CyclicList *desc;
3228 HRESULT hres;
3230 TRACE("(%p,%d,%p), semi-stub\n", iface, index, ppFuncDesc);
3232 if (!ppFuncDesc)
3233 return E_INVALIDARG;
3235 if (index >= cti2_get_func_count(This->typeinfo))
3236 return TYPE_E_ELEMENTNOTFOUND;
3238 hres = ICreateTypeInfo2_LayOut((ICreateTypeInfo2*)This);
3239 if (FAILED(hres))
3240 return hres;
3242 desc = This->typedata->next;
3243 for (i = index; i >= 0; ) {
3244 desc = desc->next;
3245 if (desc->type == CyclicListFunc)
3246 --i;
3249 typedata = desc->u.data;
3251 *ppFuncDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FUNCDESC));
3252 if (!*ppFuncDesc)
3253 return E_OUTOFMEMORY;
3255 (*ppFuncDesc)->memid = desc->indice;
3256 (*ppFuncDesc)->lprgscode = NULL; /* FIXME: Unimplemented */
3257 (*ppFuncDesc)->funckind = typedata[4] & 0x7;
3258 (*ppFuncDesc)->invkind = (typedata[4] >> 3) & 0xF;
3259 (*ppFuncDesc)->callconv = (typedata[4] >> 8) & 0xF;
3260 (*ppFuncDesc)->cParams = typedata[5];
3261 (*ppFuncDesc)->cParamsOpt = 0; /* FIXME: Unimplemented*/
3262 (*ppFuncDesc)->oVft = typedata[3] & 0xFFFF;
3263 if ((*ppFuncDesc)->oVft)
3264 --(*ppFuncDesc)->oVft;
3265 (*ppFuncDesc)->cScodes = 0; /* FIXME: Unimplemented*/
3266 hres = ctl2_decode_typedesc(This->typelib, typedata[1],
3267 &(*ppFuncDesc)->elemdescFunc.tdesc);
3268 if (FAILED(hres)) {
3269 HeapFree(GetProcessHeap(), 0, *ppFuncDesc);
3270 return hres;
3272 (*ppFuncDesc)->wFuncFlags = typedata[2];
3274 has_defaults = typedata[4] & 0x1000;
3275 tail = typedata[5] * (has_defaults ? 16 : 12);
3276 hdr_len = (ctl2_get_record_size(desc) - tail) / sizeof(int);
3278 if ((*ppFuncDesc)->cParams > 0) {
3279 (*ppFuncDesc)->lprgelemdescParam = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (*ppFuncDesc)->cParams * sizeof(ELEMDESC));
3280 if (!(*ppFuncDesc)->lprgelemdescParam) {
3281 HeapFree(GetProcessHeap(), 0, *ppFuncDesc);
3282 return E_OUTOFMEMORY;
3284 if (has_defaults) {
3285 num_defaults = (*ppFuncDesc)->cParams;
3287 for (i = 0; i < num_defaults; ++i) {
3288 if (typedata[hdr_len + i] != 0xFFFFFFFF) {
3289 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags |= PARAMFLAG_FHASDEFAULT;
3291 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex = HeapAlloc(GetProcessHeap(), 0, sizeof(PARAMDESCEX));
3292 if (!(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex) {
3293 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3294 return E_OUTOFMEMORY;
3297 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->cBytes = sizeof(PARAMDESCEX);
3298 hres = ctl2_decode_variant(This->typelib, typedata[hdr_len + i],
3299 &(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3300 if (FAILED(hres)) {
3301 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3302 return hres;
3308 for (i = 0; i < (*ppFuncDesc)->cParams; ++i) {
3309 hres = ctl2_decode_typedesc(This->typelib, typedata[hdr_len + num_defaults + (i * 3)],
3310 &((*ppFuncDesc)->lprgelemdescParam + i)->tdesc);
3311 if (FAILED(hres)) {
3312 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3313 return hres;
3315 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags = typedata[hdr_len + num_defaults + (i * 3) + 2];
3319 return S_OK;
3322 /******************************************************************************
3323 * ITypeInfo2_GetVarDesc {OLEAUT32}
3325 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3326 ITypeInfo2* iface,
3327 UINT index,
3328 VARDESC** ppVarDesc)
3330 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3331 return E_OUTOFMEMORY;
3334 /******************************************************************************
3335 * ITypeInfo2_GetNames {OLEAUT32}
3337 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3338 ITypeInfo2* iface,
3339 MEMBERID memid,
3340 BSTR* rgBstrNames,
3341 UINT cMaxNames,
3342 UINT* pcNames)
3344 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3345 return E_OUTOFMEMORY;
3348 /******************************************************************************
3349 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3351 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3352 ITypeInfo2* iface,
3353 UINT index,
3354 HREFTYPE* pRefType)
3356 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3357 MSFT_RefRecord *ref;
3358 int offset;
3360 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3362 if(!pRefType)
3363 return E_INVALIDARG;
3365 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3366 if(index == -1) {
3367 *pRefType = -2;
3368 return S_OK;
3371 if(This->typekind == TKIND_DISPATCH)
3372 return ITypeInfo2_GetRefTypeOfImplType((ITypeInfo2*)&This->dual->lpVtblTypeInfo2,
3373 index, pRefType);
3376 if(index>=This->typeinfo->cImplTypes)
3377 return TYPE_E_ELEMENTNOTFOUND;
3379 if(This->typekind == TKIND_INTERFACE) {
3380 *pRefType = This->typeinfo->datatype1 + 2;
3381 return S_OK;
3384 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3385 if(offset == -1)
3386 return TYPE_E_ELEMENTNOTFOUND;
3388 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3389 *pRefType = ref->reftype;
3390 return S_OK;
3393 /******************************************************************************
3394 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3396 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3397 ITypeInfo2* iface,
3398 UINT index,
3399 INT* pImplTypeFlags)
3401 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3402 int offset;
3403 MSFT_RefRecord *ref;
3405 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3407 if(!pImplTypeFlags)
3408 return E_INVALIDARG;
3410 if(index >= This->typeinfo->cImplTypes)
3411 return TYPE_E_ELEMENTNOTFOUND;
3413 if(This->typekind != TKIND_COCLASS) {
3414 *pImplTypeFlags = 0;
3415 return S_OK;
3418 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3419 if(offset == -1)
3420 return TYPE_E_ELEMENTNOTFOUND;
3422 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3423 *pImplTypeFlags = ref->flags;
3424 return S_OK;
3427 /******************************************************************************
3428 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3430 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3431 ITypeInfo2* iface,
3432 LPOLESTR* rgszNames,
3433 UINT cNames,
3434 MEMBERID* pMemId)
3436 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3437 return E_OUTOFMEMORY;
3440 /******************************************************************************
3441 * ITypeInfo2_Invoke {OLEAUT32}
3443 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3444 ITypeInfo2* iface,
3445 PVOID pvInstance,
3446 MEMBERID memid,
3447 WORD wFlags,
3448 DISPPARAMS* pDispParams,
3449 VARIANT* pVarResult,
3450 EXCEPINFO* pExcepInfo,
3451 UINT* puArgErr)
3453 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3454 return E_OUTOFMEMORY;
3457 /******************************************************************************
3458 * ITypeInfo2_GetDocumentation {OLEAUT32}
3460 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3461 ITypeInfo2* iface,
3462 MEMBERID memid,
3463 BSTR* pBstrName,
3464 BSTR* pBstrDocString,
3465 DWORD* pdwHelpContext,
3466 BSTR* pBstrHelpFile)
3468 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3469 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3470 INT nameoffset, docstringoffset, helpcontext;
3472 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3474 if (memid == -1)
3476 nameoffset = This->typeinfo->NameOffset;
3477 docstringoffset = This->typeinfo->docstringoffs;
3478 helpcontext = This->typeinfo->helpcontext;
3479 status = S_OK;
3480 } else {
3481 CyclicList *iter;
3482 if (This->typedata) {
3483 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3484 if (iter->indice == memid) {
3485 if (iter->type == CyclicListFunc) {
3486 const int *typedata = iter->u.data;
3487 int size = ctl2_get_record_size(iter) - typedata[5]*(typedata[4]&0x1000?16:12);
3489 nameoffset = iter->name;
3490 /* FIXME implement this once SetFuncDocString is implemented */
3491 docstringoffset = -1;
3492 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3494 status = S_OK;
3495 } else {
3496 FIXME("Not implemented for variable members\n");
3499 break;
3505 if (!status) {
3506 WCHAR *string;
3507 if (pBstrName) {
3508 if (nameoffset == -1)
3509 *pBstrName = NULL;
3510 else {
3511 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3512 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3513 ctl2_decode_name((char*)&name->namelen, &string);
3514 *pBstrName = SysAllocString(string);
3515 if(!*pBstrName)
3516 return E_OUTOFMEMORY;
3520 if (pBstrDocString) {
3521 if (docstringoffset == -1)
3522 *pBstrDocString = NULL;
3523 else {
3524 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3525 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3526 ctl2_decode_name((char*)&name->namelen, &string);
3527 *pBstrDocString = SysAllocString(string);
3528 if(!*pBstrDocString) {
3529 if (pBstrName) SysFreeString(*pBstrName);
3530 return E_OUTOFMEMORY;
3535 if (pdwHelpContext) {
3536 *pdwHelpContext = helpcontext;
3539 if (pBstrHelpFile) {
3540 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->lpVtblTypeLib2,
3541 -1, NULL, NULL, NULL, pBstrHelpFile);
3542 if (status) {
3543 if (pBstrName) SysFreeString(*pBstrName);
3544 if (pBstrDocString) SysFreeString(*pBstrDocString);
3549 return status;
3552 /******************************************************************************
3553 * ITypeInfo2_GetDllEntry {OLEAUT32}
3555 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3556 ITypeInfo2* iface,
3557 MEMBERID memid,
3558 INVOKEKIND invKind,
3559 BSTR* pBstrDllName,
3560 BSTR* pBstrName,
3561 WORD* pwOrdinal)
3563 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3564 return E_OUTOFMEMORY;
3567 /******************************************************************************
3568 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3570 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3571 ITypeInfo2* iface,
3572 HREFTYPE hRefType,
3573 ITypeInfo** ppTInfo)
3575 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3577 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3579 if(!ppTInfo)
3580 return E_INVALIDARG;
3582 if(hRefType==-2 && This->dual) {
3583 *ppTInfo = (ITypeInfo*)&This->dual->lpVtblTypeInfo2;
3584 ITypeInfo_AddRef(*ppTInfo);
3585 return S_OK;
3588 if(hRefType&1) {
3589 ITypeLib *tl;
3590 MSFT_ImpInfo *impinfo;
3591 MSFT_ImpFile *impfile;
3592 MSFT_GuidEntry *guid;
3593 WCHAR *filename;
3594 HRESULT hres;
3596 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3597 return E_FAIL;
3599 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3600 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3601 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3603 ctl2_decode_string(impfile->filename, &filename);
3605 hres = LoadTypeLib(filename, &tl);
3606 if(FAILED(hres))
3607 return hres;
3609 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3611 ITypeLib_Release(tl);
3612 return hres;
3613 } else {
3614 ICreateTypeInfo2Impl *iter;
3615 int i = 0;
3617 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3618 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3619 *ppTInfo = (ITypeInfo*)&iter->lpVtblTypeInfo2;
3621 ITypeLib_AddRef(*ppTInfo);
3622 return S_OK;
3624 i++;
3628 return E_FAIL;
3631 /******************************************************************************
3632 * ITypeInfo2_AddressOfMember {OLEAUT32}
3634 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3635 ITypeInfo2* iface,
3636 MEMBERID memid,
3637 INVOKEKIND invKind,
3638 PVOID* ppv)
3640 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3641 return E_OUTOFMEMORY;
3644 /******************************************************************************
3645 * ITypeInfo2_CreateInstance {OLEAUT32}
3647 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3648 ITypeInfo2* iface,
3649 IUnknown* pUnkOuter,
3650 REFIID riid,
3651 PVOID* ppvObj)
3653 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3654 return E_OUTOFMEMORY;
3657 /******************************************************************************
3658 * ITypeInfo2_GetMops {OLEAUT32}
3660 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3661 ITypeInfo2* iface,
3662 MEMBERID memid,
3663 BSTR* pBstrMops)
3665 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3666 return E_OUTOFMEMORY;
3669 /******************************************************************************
3670 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3672 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3673 ITypeInfo2* iface,
3674 ITypeLib** ppTLib,
3675 UINT* pIndex)
3677 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3679 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3681 *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3682 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3683 *pIndex = This->typeinfo->typekind >> 16;
3685 return S_OK;
3688 static void release_typedesc(TYPEDESC *tdesc)
3690 while (tdesc) {
3691 TYPEDESC *next;
3692 if (tdesc->vt == VT_USERDEFINED)
3693 next = NULL;
3694 else
3695 next = tdesc->u.lptdesc;
3696 HeapFree(GetProcessHeap(), 0, tdesc);
3697 tdesc = next;
3701 /******************************************************************************
3702 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3704 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3705 ITypeInfo2* iface,
3706 TYPEATTR* pTypeAttr)
3708 TRACE("(%p,%p)\n", iface, pTypeAttr);
3710 if (pTypeAttr->tdescAlias.vt != VT_USERDEFINED)
3711 release_typedesc(pTypeAttr->tdescAlias.u.lptdesc);
3713 HeapFree(GetProcessHeap(), 0, pTypeAttr);
3716 /******************************************************************************
3717 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3719 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3720 ITypeInfo2* iface,
3721 FUNCDESC* pFuncDesc)
3723 int i;
3725 TRACE("(%p,%p)\n", iface, pFuncDesc);
3727 HeapFree(GetProcessHeap(), 0, pFuncDesc->lprgscode);
3729 if (pFuncDesc->lprgelemdescParam) {
3730 for (i = 0; i < pFuncDesc->cParams; ++i) {
3731 if (pFuncDesc->lprgelemdescParam[i].tdesc.vt != VT_USERDEFINED)
3732 release_typedesc(pFuncDesc->lprgelemdescParam[i].tdesc.u.lptdesc);
3734 HeapFree(GetProcessHeap(), 0, pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex);
3736 HeapFree(GetProcessHeap(), 0, pFuncDesc->lprgelemdescParam);
3739 HeapFree(GetProcessHeap(), 0, pFuncDesc->elemdescFunc.u.paramdesc.pparamdescex);
3741 if (pFuncDesc->elemdescFunc.tdesc.vt != VT_USERDEFINED)
3742 release_typedesc(pFuncDesc->elemdescFunc.tdesc.u.lptdesc);
3744 HeapFree(GetProcessHeap(), 0, pFuncDesc);
3747 /******************************************************************************
3748 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3750 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3751 ITypeInfo2* iface,
3752 VARDESC* pVarDesc)
3754 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3757 /******************************************************************************
3758 * ITypeInfo2_GetTypeKind {OLEAUT32}
3760 * Get the TYPEKIND value for a TypeInfo.
3762 * RETURNS
3764 * Success: S_OK.
3765 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3767 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3768 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3769 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3771 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3772 return E_OUTOFMEMORY;
3775 /******************************************************************************
3776 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3778 * Get the Type Flags for a TypeInfo.
3780 * RETURNS
3782 * Success: S_OK.
3783 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3785 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3786 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3787 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3789 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3790 return E_OUTOFMEMORY;
3793 /******************************************************************************
3794 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3796 * Gets the index of a function given its member id.
3798 * RETURNS
3800 * Success: S_OK.
3801 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3803 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3804 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3805 MEMBERID memid, /* [I] The member id for the function. */
3806 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3807 UINT* pFuncIndex) /* [O] The index of the function. */
3809 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3810 return E_OUTOFMEMORY;
3813 /******************************************************************************
3814 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3816 * Gets the index of a variable given its member id.
3818 * RETURNS
3820 * Success: S_OK.
3821 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3823 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3824 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3825 MEMBERID memid, /* [I] The member id for the variable. */
3826 UINT* pVarIndex) /* [O] The index of the variable. */
3828 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3829 return E_OUTOFMEMORY;
3832 /******************************************************************************
3833 * ITypeInfo2_GetCustData {OLEAUT32}
3835 * Gets a custom data element from a TypeInfo.
3837 * RETURNS
3839 * Success: S_OK.
3840 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3842 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3843 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3844 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3845 VARIANT* pVarVal) /* [O] The custom data. */
3847 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3848 MSFT_CDGuid *cdentry;
3849 int offset;
3851 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
3853 if (!guid || !pVarVal)
3854 return E_INVALIDARG;
3856 VariantClear(pVarVal);
3858 offset = ctl2_find_custdata(This->typelib, guid, This->typeinfo->oCustData);
3859 if (offset == -1)
3860 return S_OK;
3862 cdentry = (MSFT_CDGuid *)&This->typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
3863 return ctl2_decode_variant(This->typelib, cdentry->DataOffset, pVarVal);
3866 /******************************************************************************
3867 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3869 * Gets a custom data element from a function.
3871 * RETURNS
3873 * Success: S_OK.
3874 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3876 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3877 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3878 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3879 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3880 VARIANT* pVarVal) /* [O] The custom data. */
3882 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3883 return E_OUTOFMEMORY;
3886 /******************************************************************************
3887 * ITypeInfo2_GetParamCustData {OLEAUT32}
3889 * Gets a custom data element from a parameter.
3891 * RETURNS
3893 * Success: S_OK.
3894 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3896 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3897 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3898 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3899 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3900 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3901 VARIANT* pVarVal) /* [O] The custom data. */
3903 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3904 return E_OUTOFMEMORY;
3907 /******************************************************************************
3908 * ITypeInfo2_GetVarCustData {OLEAUT32}
3910 * Gets a custom data element from a variable.
3912 * RETURNS
3914 * Success: S_OK.
3915 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3917 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3918 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3919 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3920 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3921 VARIANT* pVarVal) /* [O] The custom data. */
3923 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3924 return E_OUTOFMEMORY;
3927 /******************************************************************************
3928 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3930 * Gets a custom data element from an implemented type of a TypeInfo.
3932 * RETURNS
3934 * Success: S_OK.
3935 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3937 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3938 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3939 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3940 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3941 VARIANT* pVarVal) /* [O] The custom data. */
3943 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3944 return E_OUTOFMEMORY;
3947 /******************************************************************************
3948 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3950 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3952 * RETURNS
3954 * Success: S_OK.
3955 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3957 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3958 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
3959 MEMBERID memid, /* [I] The member id (why?). */
3960 LCID lcid, /* [I] The locale (why?). */
3961 BSTR* pbstrHelpString, /* [O] The help string. */
3962 DWORD* pdwHelpStringContext, /* [O] The help string context. */
3963 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
3965 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3966 return E_OUTOFMEMORY;
3969 /******************************************************************************
3970 * ITypeInfo2_GetAllCustData {OLEAUT32}
3972 * Gets all of the custom data associated with a TypeInfo.
3974 * RETURNS
3976 * Success: S_OK.
3977 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3979 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
3980 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3981 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3983 FIXME("(%p,%p), stub!\n", iface, pCustData);
3984 return E_OUTOFMEMORY;
3987 /******************************************************************************
3988 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
3990 * Gets all of the custom data associated with a function.
3992 * RETURNS
3994 * Success: S_OK.
3995 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3997 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
3998 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3999 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
4000 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4002 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4003 return E_OUTOFMEMORY;
4006 /******************************************************************************
4007 * ITypeInfo2_GetAllParamCustData {OLEAUT32}
4009 * Gets all of the custom data associated with a parameter.
4011 * RETURNS
4013 * Success: S_OK.
4014 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4016 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
4017 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4018 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
4019 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
4020 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4022 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
4023 return E_OUTOFMEMORY;
4026 /******************************************************************************
4027 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
4029 * Gets all of the custom data associated with a variable.
4031 * RETURNS
4033 * Success: S_OK.
4034 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4036 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
4037 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4038 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
4039 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4041 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4042 return E_OUTOFMEMORY;
4045 /******************************************************************************
4046 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
4048 * Gets all of the custom data associated with an implemented type.
4050 * RETURNS
4052 * Success: S_OK.
4053 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4055 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
4056 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4057 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
4058 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4060 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4061 return E_OUTOFMEMORY;
4065 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
4067 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
4070 ICreateTypeInfo2_fnQueryInterface,
4071 ICreateTypeInfo2_fnAddRef,
4072 ICreateTypeInfo2_fnRelease,
4074 ICreateTypeInfo2_fnSetGuid,
4075 ICreateTypeInfo2_fnSetTypeFlags,
4076 ICreateTypeInfo2_fnSetDocString,
4077 ICreateTypeInfo2_fnSetHelpContext,
4078 ICreateTypeInfo2_fnSetVersion,
4079 ICreateTypeInfo2_fnAddRefTypeInfo,
4080 ICreateTypeInfo2_fnAddFuncDesc,
4081 ICreateTypeInfo2_fnAddImplType,
4082 ICreateTypeInfo2_fnSetImplTypeFlags,
4083 ICreateTypeInfo2_fnSetAlignment,
4084 ICreateTypeInfo2_fnSetSchema,
4085 ICreateTypeInfo2_fnAddVarDesc,
4086 ICreateTypeInfo2_fnSetFuncAndParamNames,
4087 ICreateTypeInfo2_fnSetVarName,
4088 ICreateTypeInfo2_fnSetTypeDescAlias,
4089 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
4090 ICreateTypeInfo2_fnSetFuncDocString,
4091 ICreateTypeInfo2_fnSetVarDocString,
4092 ICreateTypeInfo2_fnSetFuncHelpContext,
4093 ICreateTypeInfo2_fnSetVarHelpContext,
4094 ICreateTypeInfo2_fnSetMops,
4095 ICreateTypeInfo2_fnSetTypeIdldesc,
4096 ICreateTypeInfo2_fnLayOut,
4098 ICreateTypeInfo2_fnDeleteFuncDesc,
4099 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
4100 ICreateTypeInfo2_fnDeleteVarDesc,
4101 ICreateTypeInfo2_fnDeleteVarDescByMemId,
4102 ICreateTypeInfo2_fnDeleteImplType,
4103 ICreateTypeInfo2_fnSetCustData,
4104 ICreateTypeInfo2_fnSetFuncCustData,
4105 ICreateTypeInfo2_fnSetParamCustData,
4106 ICreateTypeInfo2_fnSetVarCustData,
4107 ICreateTypeInfo2_fnSetImplTypeCustData,
4108 ICreateTypeInfo2_fnSetHelpStringContext,
4109 ICreateTypeInfo2_fnSetFuncHelpStringContext,
4110 ICreateTypeInfo2_fnSetVarHelpStringContext,
4111 ICreateTypeInfo2_fnInvalidate,
4112 ICreateTypeInfo2_fnSetName
4115 static const ITypeInfo2Vtbl typeinfo2vt =
4118 ITypeInfo2_fnQueryInterface,
4119 ITypeInfo2_fnAddRef,
4120 ITypeInfo2_fnRelease,
4122 ITypeInfo2_fnGetTypeAttr,
4123 ITypeInfo2_fnGetTypeComp,
4124 ITypeInfo2_fnGetFuncDesc,
4125 ITypeInfo2_fnGetVarDesc,
4126 ITypeInfo2_fnGetNames,
4127 ITypeInfo2_fnGetRefTypeOfImplType,
4128 ITypeInfo2_fnGetImplTypeFlags,
4129 ITypeInfo2_fnGetIDsOfNames,
4130 ITypeInfo2_fnInvoke,
4131 ITypeInfo2_fnGetDocumentation,
4132 ITypeInfo2_fnGetDllEntry,
4133 ITypeInfo2_fnGetRefTypeInfo,
4134 ITypeInfo2_fnAddressOfMember,
4135 ITypeInfo2_fnCreateInstance,
4136 ITypeInfo2_fnGetMops,
4137 ITypeInfo2_fnGetContainingTypeLib,
4138 ITypeInfo2_fnReleaseTypeAttr,
4139 ITypeInfo2_fnReleaseFuncDesc,
4140 ITypeInfo2_fnReleaseVarDesc,
4142 ITypeInfo2_fnGetTypeKind,
4143 ITypeInfo2_fnGetTypeFlags,
4144 ITypeInfo2_fnGetFuncIndexOfMemId,
4145 ITypeInfo2_fnGetVarIndexOfMemId,
4146 ITypeInfo2_fnGetCustData,
4147 ITypeInfo2_fnGetFuncCustData,
4148 ITypeInfo2_fnGetParamCustData,
4149 ITypeInfo2_fnGetVarCustData,
4150 ITypeInfo2_fnGetImplTypeCustData,
4151 ITypeInfo2_fnGetDocumentation2,
4152 ITypeInfo2_fnGetAllCustData,
4153 ITypeInfo2_fnGetAllFuncCustData,
4154 ITypeInfo2_fnGetAllParamCustData,
4155 ITypeInfo2_fnGetAllVarCustData,
4156 ITypeInfo2_fnGetAllImplTypeCustData,
4159 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
4161 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
4163 int nameoffset;
4164 int typeinfo_offset;
4165 MSFT_TypeInfoBase *typeinfo;
4167 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
4169 pCreateTypeInfo2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeInfo2Impl));
4170 if (!pCreateTypeInfo2Impl) return NULL;
4172 pCreateTypeInfo2Impl->lpVtbl = &ctypeinfo2vt;
4173 pCreateTypeInfo2Impl->lpVtblTypeInfo2 = &typeinfo2vt;
4174 pCreateTypeInfo2Impl->ref = 1;
4176 pCreateTypeInfo2Impl->typelib = typelib;
4177 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
4179 nameoffset = ctl2_alloc_name(typelib, szName);
4180 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
4181 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
4183 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
4184 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
4186 pCreateTypeInfo2Impl->typeinfo = typeinfo;
4188 pCreateTypeInfo2Impl->typekind = tkind;
4189 typeinfo->typekind |= tkind | 0x20;
4190 ICreateTypeInfo2_SetAlignment((ICreateTypeInfo2 *)pCreateTypeInfo2Impl, 4);
4192 switch (tkind) {
4193 case TKIND_ENUM:
4194 case TKIND_INTERFACE:
4195 case TKIND_DISPATCH:
4196 case TKIND_COCLASS:
4197 typeinfo->size = 4;
4198 break;
4200 case TKIND_RECORD:
4201 case TKIND_UNION:
4202 typeinfo->size = 0;
4203 break;
4205 case TKIND_MODULE:
4206 typeinfo->size = 2;
4207 break;
4209 case TKIND_ALIAS:
4210 typeinfo->size = -0x75;
4211 break;
4213 default:
4214 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
4215 typeinfo->size = 0xdeadbeef;
4216 break;
4219 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
4220 typelib->last_typeinfo = pCreateTypeInfo2Impl;
4221 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
4223 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
4225 return (ICreateTypeInfo2 *)pCreateTypeInfo2Impl;
4229 /*================== ICreateTypeLib2 Implementation ===================================*/
4231 /******************************************************************************
4232 * ICreateTypeLib2_QueryInterface {OLEAUT32}
4234 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
4235 ICreateTypeLib2 * iface,
4236 REFIID riid,
4237 VOID **ppvObject)
4239 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4241 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4243 *ppvObject=NULL;
4244 if(IsEqualIID(riid, &IID_IUnknown) ||
4245 IsEqualIID(riid,&IID_ICreateTypeLib)||
4246 IsEqualIID(riid,&IID_ICreateTypeLib2))
4248 *ppvObject = This;
4249 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4250 IsEqualIID(riid, &IID_ITypeLib2)) {
4251 *ppvObject = &This->lpVtblTypeLib2;
4254 if(*ppvObject)
4256 ICreateTypeLib2_AddRef(iface);
4257 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4258 return S_OK;
4260 TRACE("-- Interface: E_NOINTERFACE\n");
4261 return E_NOINTERFACE;
4264 /******************************************************************************
4265 * ICreateTypeLib2_AddRef {OLEAUT32}
4267 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4269 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4270 ULONG ref = InterlockedIncrement(&This->ref);
4272 TRACE("(%p)->(%u)\n", This, ref);
4274 return ref;
4277 /******************************************************************************
4278 * ICreateTypeLib2_Release {OLEAUT32}
4280 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4282 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4283 ULONG ref = InterlockedDecrement(&This->ref);
4285 TRACE("(%p)->(%u)\n", This, ref);
4287 if (!ref) {
4288 int i;
4290 for (i = 0; i < MSFT_SEG_MAX; i++) {
4291 HeapFree(GetProcessHeap(), 0, This->typelib_segment_data[i]);
4292 This->typelib_segment_data[i] = NULL;
4295 HeapFree(GetProcessHeap(), 0, This->filename);
4296 This->filename = NULL;
4298 while (This->typeinfos) {
4299 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4300 This->typeinfos = typeinfo->next_typeinfo;
4301 if(typeinfo->typedata) {
4302 CyclicList *iter, *rem;
4304 rem = typeinfo->typedata->next;
4305 typeinfo->typedata->next = NULL;
4306 iter = rem->next;
4307 HeapFree(GetProcessHeap(), 0, rem);
4309 while(iter) {
4310 rem = iter;
4311 iter = iter->next;
4312 HeapFree(GetProcessHeap(), 0, rem->u.data);
4313 HeapFree(GetProcessHeap(), 0, rem);
4317 HeapFree(GetProcessHeap(), 0, typeinfo->dual);
4318 HeapFree(GetProcessHeap(), 0, typeinfo);
4321 HeapFree(GetProcessHeap(),0,This);
4322 return 0;
4325 return ref;
4329 /******************************************************************************
4330 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4332 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4333 ICreateTypeLib2 * iface,
4334 LPOLESTR szName,
4335 TYPEKIND tkind,
4336 ICreateTypeInfo **tinfo)
4338 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4339 char *name;
4341 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, tinfo);
4343 if (!szName || !tinfo) return E_INVALIDARG;
4345 ctl2_encode_name(This, szName, &name);
4346 if(ctl2_find_name(This, name) != -1)
4347 return TYPE_E_NAMECONFLICT;
4349 *tinfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4350 if (!*tinfo) return E_OUTOFMEMORY;
4352 return S_OK;
4355 /******************************************************************************
4356 * ICreateTypeLib2_SetName {OLEAUT32}
4358 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4359 ICreateTypeLib2 * iface,
4360 LPOLESTR szName)
4362 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4364 int offset;
4366 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4368 offset = ctl2_alloc_name(This, szName);
4369 if (offset == -1) return E_OUTOFMEMORY;
4370 This->typelib_header.NameOffset = offset;
4371 return S_OK;
4374 /******************************************************************************
4375 * ICreateTypeLib2_SetVersion {OLEAUT32}
4377 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4379 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4381 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4383 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4384 return S_OK;
4387 /******************************************************************************
4388 * ICreateTypeLib2_SetGuid {OLEAUT32}
4390 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4392 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4394 MSFT_GuidEntry guidentry;
4395 int offset;
4397 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4399 guidentry.guid = *guid;
4400 guidentry.hreftype = -2;
4401 guidentry.next_hash = -1;
4403 offset = ctl2_alloc_guid(This, &guidentry);
4405 if (offset == -1) return E_OUTOFMEMORY;
4407 This->typelib_header.posguid = offset;
4409 return S_OK;
4412 /******************************************************************************
4413 * ICreateTypeLib2_SetDocString {OLEAUT32}
4415 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4417 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4419 int offset;
4421 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4422 if (!szDoc)
4423 return E_INVALIDARG;
4425 offset = ctl2_alloc_string(This, szDoc);
4426 if (offset == -1) return E_OUTOFMEMORY;
4427 This->typelib_header.helpstring = offset;
4428 return S_OK;
4431 /******************************************************************************
4432 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4434 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4436 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4438 int offset;
4440 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4442 offset = ctl2_alloc_string(This, szHelpFileName);
4443 if (offset == -1) return E_OUTOFMEMORY;
4444 This->typelib_header.helpfile = offset;
4445 This->typelib_header.varflags |= 0x10;
4446 return S_OK;
4449 /******************************************************************************
4450 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4452 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4454 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4456 TRACE("(%p,%d)\n", iface, dwHelpContext);
4457 This->typelib_header.helpcontext = dwHelpContext;
4458 return S_OK;
4461 /******************************************************************************
4462 * ICreateTypeLib2_SetLcid {OLEAUT32}
4464 * Sets both the lcid and lcid2 members in the header to lcid.
4466 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4467 * is set to US English while the second one is set to 0.
4469 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4471 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4473 TRACE("(%p,%d)\n", iface, lcid);
4475 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4477 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4479 return S_OK;
4482 /******************************************************************************
4483 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4485 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4487 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4489 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4491 This->typelib_header.flags = uLibFlags;
4493 return S_OK;
4496 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4498 DWORD dwWritten;
4499 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4500 CloseHandle(hFile);
4501 return 0;
4503 return -1;
4506 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4508 DWORD dwWritten;
4509 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4510 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4511 CloseHandle(hFile);
4512 return 0;
4515 return -1;
4518 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4520 ICreateTypeInfo2Impl *typeinfo;
4521 HRESULT hres;
4523 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4524 typeinfo->typeinfo->memoffset = filesize;
4526 hres = ICreateTypeInfo2_fnLayOut((ICreateTypeInfo2 *)typeinfo);
4527 if(FAILED(hres))
4528 return hres;
4530 if (typeinfo->typedata)
4531 filesize += typeinfo->typedata->next->u.val
4532 + cti2_get_var_count(typeinfo->typeinfo) * 12
4533 + cti2_get_func_count(typeinfo->typeinfo) * 12 + 4;
4536 return S_OK;
4539 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4541 if (This->typelib_segdir[segment].length) {
4542 This->typelib_segdir[segment].offset = filepos;
4543 } else {
4544 This->typelib_segdir[segment].offset = -1;
4547 return This->typelib_segdir[segment].length;
4550 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4552 ICreateTypeInfo2Impl *typeinfo;
4554 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4555 CyclicList *iter;
4556 int offset = 0;
4558 if (!typeinfo->typedata) continue;
4560 iter = typeinfo->typedata->next;
4561 ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4562 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4563 ctl2_write_chunk(hFile, iter->u.data, ctl2_get_record_size(iter));
4565 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4566 ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4568 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4569 ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4571 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4572 ctl2_write_chunk(hFile, &offset, sizeof(int));
4573 offset += ctl2_get_record_size(iter);
4578 /******************************************************************************
4579 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4581 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4583 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4585 int retval;
4586 int filepos;
4587 HANDLE hFile;
4588 HRESULT hres;
4590 TRACE("(%p)\n", iface);
4592 retval = TYPE_E_IOERROR;
4594 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4595 if (hFile == INVALID_HANDLE_VALUE) return retval;
4597 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4598 filepos += This->typelib_header.nrtypeinfos * 4;
4600 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4601 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4602 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4603 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4604 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4605 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4606 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4607 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4608 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4609 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4610 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4611 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4612 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4614 hres = ctl2_finalize_typeinfos(This, filepos);
4615 if(FAILED(hres)) {
4616 CloseHandle(hFile);
4617 return hres;
4620 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4621 if (This->typelib_header.varflags & HELPDLLFLAG)
4622 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4623 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4624 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4625 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4626 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4627 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4628 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4629 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4630 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4631 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4632 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4633 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4634 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4635 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4636 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4637 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4639 ctl2_write_typeinfos(This, hFile);
4641 if (!CloseHandle(hFile)) return retval;
4643 return S_OK;
4647 /******************************************************************************
4648 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4650 * Deletes a named TypeInfo from a type library.
4652 * RETURNS
4654 * Success: S_OK
4655 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4657 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4658 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4659 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4661 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4662 return E_OUTOFMEMORY;
4665 /******************************************************************************
4666 * ICreateTypeLib2_SetCustData {OLEAUT32}
4668 * Sets custom data for a type library.
4670 * RETURNS
4672 * Success: S_OK
4673 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4675 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4676 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4677 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4678 VARIANT *pVarVal) /* [I] The custom data itself. */
4680 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4682 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4684 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4687 /******************************************************************************
4688 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4690 * Sets a context number for the library help string.
4692 * PARAMS
4693 * iface [I] The type library to set the help string context for.
4694 * dwContext [I] The help string context.
4696 * RETURNS
4697 * Success: S_OK
4698 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4700 static
4701 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4702 ULONG dwContext)
4704 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4706 TRACE("(%p,%d)\n", iface, dwContext);
4708 This->typelib_header.helpstringcontext = dwContext;
4709 return S_OK;
4712 /******************************************************************************
4713 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4715 * Set the DLL used to look up localized help strings.
4717 * PARAMS
4718 * iface [I] The type library to set the help DLL for.
4719 * szDllName [I] The name of the help DLL.
4721 * RETURNS
4722 * Success: S_OK
4723 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4725 static
4726 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4727 LPOLESTR szDllName)
4729 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4730 int offset;
4732 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4733 if (!szDllName)
4734 return E_INVALIDARG;
4736 offset = ctl2_alloc_string(This, szDllName);
4737 if (offset == -1)
4738 return E_OUTOFMEMORY;
4739 This->typelib_header.varflags |= HELPDLLFLAG;
4740 This->helpStringDll = offset;
4741 return S_OK;
4744 /*================== ITypeLib2 Implementation ===================================*/
4746 /******************************************************************************
4747 * ITypeLib2_QueryInterface {OLEAUT32}
4749 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4751 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4753 return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4756 /******************************************************************************
4757 * ITypeLib2_AddRef {OLEAUT32}
4759 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4761 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4763 return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4766 /******************************************************************************
4767 * ITypeLib2_Release {OLEAUT32}
4769 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4771 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4773 return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4776 /******************************************************************************
4777 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4779 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4780 ITypeLib2 * iface)
4782 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4784 TRACE("(%p)\n", iface);
4786 return This->typelib_header.nrtypeinfos;
4789 /******************************************************************************
4790 * ITypeLib2_GetTypeInfo {OLEAUT32}
4792 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4793 ITypeLib2 * iface,
4794 UINT index,
4795 ITypeInfo** ppTInfo)
4797 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4799 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4801 if (!ppTInfo) return E_INVALIDARG;
4803 if (index >= This->typelib_header.nrtypeinfos) {
4804 return TYPE_E_ELEMENTNOTFOUND;
4807 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4810 /******************************************************************************
4811 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4813 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4814 ITypeLib2 * iface,
4815 UINT index,
4816 TYPEKIND* kind)
4818 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4820 TRACE("(%p,%d,%p)\n", iface, index, kind);
4822 if (!kind) return E_INVALIDARG;
4824 if (index >= This->typelib_header.nrtypeinfos) {
4825 return TYPE_E_ELEMENTNOTFOUND;
4828 *kind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 0xF;
4830 return S_OK;
4833 /******************************************************************************
4834 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4836 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4837 ITypeLib2 * iface,
4838 REFGUID guid,
4839 ITypeInfo** ppTinfo)
4841 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4843 int guidoffset;
4844 int typeinfo;
4846 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4848 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4849 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4851 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4852 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4854 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4857 /******************************************************************************
4858 * ITypeLib2_GetLibAttr {OLEAUT32}
4860 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4861 ITypeLib2 * iface,
4862 TLIBATTR** ppTLibAttr)
4864 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4866 TRACE("(%p,%p)\n", This, ppTLibAttr);
4868 if(!ppTLibAttr)
4869 return E_INVALIDARG;
4871 *ppTLibAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TLIBATTR));
4872 if(!*ppTLibAttr)
4873 return E_OUTOFMEMORY;
4875 if(This->typelib_header.posguid != -1) {
4876 MSFT_GuidEntry *guid;
4878 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4879 (*ppTLibAttr)->guid = guid->guid;
4882 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4883 (*ppTLibAttr)->syskind = ctl2_get_syskind(This);
4884 (*ppTLibAttr)->wMajorVerNum = LOWORD(This->typelib_header.version);
4885 (*ppTLibAttr)->wMinorVerNum = HIWORD(This->typelib_header.version);
4886 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4887 return S_OK;
4890 /******************************************************************************
4891 * ITypeLib2_GetTypeComp {OLEAUT32}
4893 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4894 ITypeLib2 * iface,
4895 ITypeComp** ppTComp)
4897 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4899 FIXME("(%p,%p), stub!\n", This, ppTComp);
4901 return E_OUTOFMEMORY;
4904 /******************************************************************************
4905 * ITypeLib2_GetDocumentation {OLEAUT32}
4907 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4908 ITypeLib2 * iface,
4909 INT index,
4910 BSTR* pBstrName,
4911 BSTR* pBstrDocString,
4912 DWORD* pdwHelpContext,
4913 BSTR* pBstrHelpFile)
4915 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4916 WCHAR *string;
4918 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4920 if(index != -1) {
4921 ICreateTypeInfo2Impl *iter;
4923 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4924 index--;
4926 if(!iter)
4927 return TYPE_E_ELEMENTNOTFOUND;
4929 return ITypeInfo_GetDocumentation((ITypeInfo*)&iter->lpVtblTypeInfo2,
4930 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4933 if(pBstrName) {
4934 if(This->typelib_header.NameOffset == -1)
4935 *pBstrName = NULL;
4936 else {
4937 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4938 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4940 ctl2_decode_name((char*)&name->namelen, &string);
4942 *pBstrName = SysAllocString(string);
4943 if(!*pBstrName)
4944 return E_OUTOFMEMORY;
4948 if(pBstrDocString) {
4949 if(This->typelib_header.helpstring == -1)
4950 *pBstrDocString = NULL;
4951 else {
4952 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4954 *pBstrDocString = SysAllocString(string);
4955 if(!*pBstrDocString) {
4956 if(pBstrName) SysFreeString(*pBstrName);
4957 return E_OUTOFMEMORY;
4962 if(pdwHelpContext)
4963 *pdwHelpContext = This->typelib_header.helpcontext;
4965 if(pBstrHelpFile) {
4966 if(This->typelib_header.helpfile == -1)
4967 *pBstrHelpFile = NULL;
4968 else {
4969 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
4971 *pBstrHelpFile = SysAllocString(string);
4972 if(!*pBstrHelpFile) {
4973 if(pBstrName) SysFreeString(*pBstrName);
4974 if(pBstrDocString) SysFreeString(*pBstrDocString);
4975 return E_OUTOFMEMORY;
4980 return S_OK;
4983 /******************************************************************************
4984 * ITypeLib2_IsName {OLEAUT32}
4986 static HRESULT WINAPI ITypeLib2_fnIsName(
4987 ITypeLib2 * iface,
4988 LPOLESTR szNameBuf,
4989 ULONG lHashVal,
4990 BOOL* pfName)
4992 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4994 char *encoded_name;
4995 int nameoffset;
4996 MSFT_NameIntro *nameintro;
4998 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
5000 ctl2_encode_name(This, szNameBuf, &encoded_name);
5001 nameoffset = ctl2_find_name(This, encoded_name);
5003 *pfName = 0;
5005 if (nameoffset == -1) return S_OK;
5007 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
5008 if (nameintro->hreftype == -1) return S_OK;
5010 *pfName = 1;
5012 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
5014 return S_OK;
5017 /******************************************************************************
5018 * ITypeLib2_FindName {OLEAUT32}
5020 static HRESULT WINAPI ITypeLib2_fnFindName(
5021 ITypeLib2 * iface,
5022 LPOLESTR szNameBuf,
5023 ULONG lHashVal,
5024 ITypeInfo** ppTInfo,
5025 MEMBERID* rgMemId,
5026 USHORT* pcFound)
5028 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5030 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
5032 return E_OUTOFMEMORY;
5035 /******************************************************************************
5036 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
5038 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
5039 ITypeLib2 * iface,
5040 TLIBATTR* pTLibAttr)
5042 TRACE("(%p,%p)\n", iface, pTLibAttr);
5044 HeapFree(GetProcessHeap(), 0, pTLibAttr);
5047 /******************************************************************************
5048 * ICreateTypeLib2_GetCustData {OLEAUT32}
5050 * Retrieves a custom data value stored on a type library.
5052 * RETURNS
5054 * Success: S_OK
5055 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5057 static HRESULT WINAPI ITypeLib2_fnGetCustData(
5058 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5059 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
5060 VARIANT* pVarVal) /* [O] The custom data. */
5062 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5064 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
5066 return E_OUTOFMEMORY;
5069 /******************************************************************************
5070 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
5072 * Retrieves some statistics about names in a type library, supposedly for
5073 * hash table optimization purposes.
5075 * RETURNS
5077 * Success: S_OK
5078 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5080 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
5081 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
5082 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
5083 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
5085 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5087 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
5089 return E_OUTOFMEMORY;
5092 /******************************************************************************
5093 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
5095 * Obtain locale-aware help string information.
5097 * RETURNS
5099 * Success: S_OK
5100 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
5102 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
5103 ITypeLib2 * iface,
5104 INT index,
5105 LCID lcid,
5106 BSTR* pbstrHelpString,
5107 DWORD* pdwHelpStringContext,
5108 BSTR* pbstrHelpStringDll)
5110 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5112 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
5114 return E_OUTOFMEMORY;
5117 /******************************************************************************
5118 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
5120 * Retrieve all of the custom data for a type library.
5122 * RETURNS
5124 * Success: S_OK
5125 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5127 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
5128 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5129 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
5131 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5133 FIXME("(%p,%p), stub!\n", This, pCustData);
5135 return E_OUTOFMEMORY;
5139 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
5141 static const ICreateTypeLib2Vtbl ctypelib2vt =
5144 ICreateTypeLib2_fnQueryInterface,
5145 ICreateTypeLib2_fnAddRef,
5146 ICreateTypeLib2_fnRelease,
5148 ICreateTypeLib2_fnCreateTypeInfo,
5149 ICreateTypeLib2_fnSetName,
5150 ICreateTypeLib2_fnSetVersion,
5151 ICreateTypeLib2_fnSetGuid,
5152 ICreateTypeLib2_fnSetDocString,
5153 ICreateTypeLib2_fnSetHelpFileName,
5154 ICreateTypeLib2_fnSetHelpContext,
5155 ICreateTypeLib2_fnSetLcid,
5156 ICreateTypeLib2_fnSetLibFlags,
5157 ICreateTypeLib2_fnSaveAllChanges,
5159 ICreateTypeLib2_fnDeleteTypeInfo,
5160 ICreateTypeLib2_fnSetCustData,
5161 ICreateTypeLib2_fnSetHelpStringContext,
5162 ICreateTypeLib2_fnSetHelpStringDll
5165 static const ITypeLib2Vtbl typelib2vt =
5168 ITypeLib2_fnQueryInterface,
5169 ITypeLib2_fnAddRef,
5170 ITypeLib2_fnRelease,
5172 ITypeLib2_fnGetTypeInfoCount,
5173 ITypeLib2_fnGetTypeInfo,
5174 ITypeLib2_fnGetTypeInfoType,
5175 ITypeLib2_fnGetTypeInfoOfGuid,
5176 ITypeLib2_fnGetLibAttr,
5177 ITypeLib2_fnGetTypeComp,
5178 ITypeLib2_fnGetDocumentation,
5179 ITypeLib2_fnIsName,
5180 ITypeLib2_fnFindName,
5181 ITypeLib2_fnReleaseTLibAttr,
5183 ITypeLib2_fnGetCustData,
5184 ITypeLib2_fnGetLibStatistics,
5185 ITypeLib2_fnGetDocumentation2,
5186 ITypeLib2_fnGetAllCustData,
5189 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
5191 ICreateTypeLib2Impl *pCreateTypeLib2Impl;
5192 int failed = 0;
5194 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
5196 pCreateTypeLib2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeLib2Impl));
5197 if (!pCreateTypeLib2Impl) return NULL;
5199 pCreateTypeLib2Impl->filename = HeapAlloc(GetProcessHeap(), 0, (strlenW(szFile) + 1) * sizeof(WCHAR));
5200 if (!pCreateTypeLib2Impl->filename) {
5201 HeapFree(GetProcessHeap(), 0, pCreateTypeLib2Impl);
5202 return NULL;
5204 strcpyW(pCreateTypeLib2Impl->filename, szFile);
5206 ctl2_init_header(pCreateTypeLib2Impl);
5207 ctl2_init_segdir(pCreateTypeLib2Impl);
5209 pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
5212 * The following two calls return an offset or -1 if out of memory. We
5213 * specifically need an offset of 0, however, so...
5215 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
5216 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
5218 pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
5219 pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
5221 memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
5222 memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
5224 pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
5225 pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
5226 pCreateTypeLib2Impl->ref = 1;
5228 if (failed) {
5229 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
5230 return 0;
5233 return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
5236 /******************************************************************************
5237 * CreateTypeLib2 [OLEAUT32.180]
5239 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5240 * library.
5242 * NOTES
5244 * See also CreateTypeLib.
5246 * RETURNS
5247 * Success: S_OK
5248 * Failure: Status
5250 HRESULT WINAPI CreateTypeLib2(
5251 SYSKIND syskind, /* [I] System type library is for */
5252 LPCOLESTR szFile, /* [I] Type library file name */
5253 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5255 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5257 if (!szFile) return E_INVALIDARG;
5258 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5259 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5262 /******************************************************************************
5263 * ClearCustData (OLEAUT32.171)
5265 * Clear a custom data types' data.
5267 * PARAMS
5268 * lpCust [I] The custom data type instance
5270 * RETURNS
5271 * Nothing.
5273 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5275 if (lpCust && lpCust->cCustData)
5277 if (lpCust->prgCustData)
5279 DWORD i;
5281 for (i = 0; i < lpCust->cCustData; i++)
5282 VariantClear(&lpCust->prgCustData[i].varValue);
5284 /* FIXME - Should be using a per-thread IMalloc */
5285 HeapFree(GetProcessHeap(), 0, lpCust->prgCustData);
5286 lpCust->prgCustData = NULL;
5288 lpCust->cCustData = 0;