Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / oleaut32 / typelib2.c
blob78995de8a6dc4a90d788fccb9770c02bb9b1b470
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 ICreateTypeLib2 ICreateTypeLib2_iface;
167 ITypeLib2 ITypeLib2_iface;
168 LONG ref;
170 BSTR filename;
172 MSFT_Header typelib_header;
173 INT helpStringDll;
174 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
175 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
176 int typelib_segment_block_length[MSFT_SEG_MAX];
178 int typelib_guids; /* Number of defined typelib guids */
179 int typeinfo_guids; /* Number of defined typeinfo guids */
181 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
183 INT *typelib_namehash_segment;
184 INT *typelib_guidhash_segment;
186 struct tagICreateTypeInfo2Impl *typeinfos;
187 struct tagICreateTypeInfo2Impl *last_typeinfo;
188 } ICreateTypeLib2Impl;
190 static inline ICreateTypeLib2Impl *impl_from_ICreateTypeLib2(ICreateTypeLib2 *iface)
192 return CONTAINING_RECORD(iface, ICreateTypeLib2Impl, ICreateTypeLib2_iface);
195 static inline ICreateTypeLib2Impl *impl_from_ITypeLib2(ITypeLib2 *iface)
197 return CONTAINING_RECORD(iface, ICreateTypeLib2Impl, ITypeLib2_iface);
200 typedef struct tagICreateTypeInfo2Impl
202 ICreateTypeInfo2 ICreateTypeInfo2_iface;
203 ITypeInfo2 ITypeInfo2_iface;
205 LONG ref;
207 ICreateTypeLib2Impl *typelib;
208 MSFT_TypeInfoBase *typeinfo;
210 struct tagCyclicList *typedata; /* tail of cyclic list */
212 TYPEKIND typekind;
213 int datawidth;
215 struct tagICreateTypeInfo2Impl *next_typeinfo;
216 struct tagICreateTypeInfo2Impl *dual;
217 } ICreateTypeInfo2Impl;
219 static inline ICreateTypeInfo2Impl *impl_from_ICreateTypeInfo2(ICreateTypeInfo2 *iface)
221 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ICreateTypeInfo2_iface);
224 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
226 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ITypeInfo2_iface);
229 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
231 static CyclicList *alloc_cyclic_list_item(CyclicListElementType type)
233 CyclicList *ret = heap_alloc_zero(sizeof(CyclicList));
234 if (!ret)
235 return NULL;
236 ret->type = type;
237 return ret;
240 /*================== Internal functions ===================================*/
242 static inline UINT cti2_get_var_count(const MSFT_TypeInfoBase *typeinfo)
244 return typeinfo->cElement >> 16;
247 static inline UINT cti2_get_func_count(const MSFT_TypeInfoBase *typeinfo)
249 return typeinfo->cElement & 0xFFFF;
252 static inline INT ctl2_get_record_size(const CyclicList *iter)
254 return iter->u.data[0] & 0xFFFF;
257 static void ctl2_update_var_size(const ICreateTypeInfo2Impl *This, CyclicList *var, int size)
259 int old = ctl2_get_record_size(var), i;
261 if (old >= size) return;
263 /* initialize fields included in size but currently unused */
264 for (i = old/sizeof(int); i < (size/sizeof(int) - 1); i++)
266 /* HelpContext/HelpStringContext being 0 means it's not set */
267 var->u.data[i] = (i == 5 || i == 9) ? 0 : -1;
270 var->u.data[0] += size - old;
271 This->typedata->next->u.val += size - old;
274 /* NOTE: entry always assumed to be a function */
275 static inline INVOKEKIND ctl2_get_invokekind(const CyclicList *func)
277 /* INVOKEKIND uses bit flags up to 8 */
278 return (func->u.data[4] >> 3) & 0xF;
281 static inline SYSKIND ctl2_get_syskind(const ICreateTypeLib2Impl *This)
283 return This->typelib_header.varflags & 0xF;
286 /****************************************************************************
287 * ctl2_init_header
289 * Initializes the type library header of a new typelib.
291 static void ctl2_init_header(
292 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
294 This->typelib_header.magic1 = MSFT_SIGNATURE;
295 This->typelib_header.magic2 = 0x00010002;
296 This->typelib_header.posguid = -1;
297 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
298 This->typelib_header.varflags = 0x40;
299 This->typelib_header.version = 0;
300 This->typelib_header.flags = 0;
301 This->typelib_header.nrtypeinfos = 0;
302 This->typelib_header.helpstring = -1;
303 This->typelib_header.helpstringcontext = 0;
304 This->typelib_header.helpcontext = 0;
305 This->typelib_header.nametablecount = 0;
306 This->typelib_header.nametablechars = 0;
307 This->typelib_header.NameOffset = -1;
308 This->typelib_header.helpfile = -1;
309 This->typelib_header.CustomDataOffset = -1;
310 This->typelib_header.res44 = 0x20;
311 This->typelib_header.res48 = 0x80;
312 This->typelib_header.dispatchpos = -1;
313 This->typelib_header.nimpinfos = 0;
314 This->helpStringDll = -1;
317 /****************************************************************************
318 * ctl2_init_segdir
320 * Initializes the segment directory of a new typelib.
322 static void ctl2_init_segdir(
323 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
325 int i;
326 MSFT_pSeg *segdir;
328 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
330 for (i = 0; i < 15; i++) {
331 segdir[i].offset = -1;
332 segdir[i].length = 0;
333 segdir[i].res08 = -1;
334 segdir[i].res0c = 0x0f;
338 /****************************************************************************
339 * ctl2_hash_guid
341 * Generates a hash key from a GUID.
343 * RETURNS
345 * The hash key for the GUID.
347 static int ctl2_hash_guid(
348 REFGUID guid) /* [I] The guid to find. */
350 int hash;
351 int i;
353 hash = 0;
354 for (i = 0; i < 8; i ++) {
355 hash ^= ((const short *)guid)[i];
358 return hash & 0x1f;
361 /****************************************************************************
362 * ctl2_find_guid
364 * Locates a guid in a type library.
366 * RETURNS
368 * The offset into the GUID segment of the guid, or -1 if not found.
370 static int ctl2_find_guid(
371 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
372 int hash_key, /* [I] The hash key for the guid. */
373 REFGUID guid) /* [I] The guid to find. */
375 int offset;
376 MSFT_GuidEntry *guidentry;
378 offset = This->typelib_guidhash_segment[hash_key];
379 while (offset != -1) {
380 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
382 if (IsEqualGUID(guidentry, guid)) return offset;
384 offset = guidentry->next_hash;
387 return offset;
390 /****************************************************************************
391 * ctl2_find_name
393 * Locates a name in a type library.
395 * RETURNS
397 * The offset into the NAME segment of the name, or -1 if not found.
399 * NOTES
401 * The name must be encoded as with ctl2_encode_name().
403 static int ctl2_find_name(
404 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
405 const char *name) /* [I] The encoded name to find. */
407 int offset;
408 int *namestruct;
410 offset = This->typelib_namehash_segment[name[2] & 0x7f];
411 while (offset != -1) {
412 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
414 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
415 /* hash codes and lengths match, final test */
416 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
419 /* move to next item in hash bucket */
420 offset = namestruct[1];
423 return offset;
426 /****************************************************************************
427 * ctl2_encode_name
429 * Encodes a name string to a form suitable for storing into a type library
430 * or comparing to a name stored in a type library.
432 * RETURNS
434 * The length of the encoded name, including padding and length+hash fields.
436 * NOTES
438 * Will throw an exception if name or result are NULL. Is not multithread
439 * safe in the slightest.
441 static int ctl2_encode_name(
442 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
443 const WCHAR *name, /* [I] The name string to encode. */
444 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
446 int length;
447 static char converted_name[0x104];
448 int offset;
449 int value;
451 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
452 converted_name[0] = length & 0xff;
454 converted_name[length + 4] = 0;
456 converted_name[1] = 0x00;
458 value = LHashValOfNameSysA(ctl2_get_syskind(This), This->typelib_header.lcid, converted_name + 4);
460 converted_name[2] = value;
461 converted_name[3] = value >> 8;
463 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
465 *result = converted_name;
467 return (length + 7) & ~3;
470 /****************************************************************************
471 * ctl2_decode_name
473 * Converts string stored in typelib data to unicode.
475 static void ctl2_decode_name(
476 char *data, /* [I] String to be decoded */
477 WCHAR **string) /* [O] Decoded string */
479 int i, length;
480 static WCHAR converted_string[0x104];
482 length = data[0];
484 for(i=0; i<length; i++)
485 converted_string[i] = data[i+4];
486 converted_string[length] = '\0';
488 *string = converted_string;
491 /****************************************************************************
492 * ctl2_encode_string
494 * Encodes a string to a form suitable for storing into a type library or
495 * comparing to a string stored in a type library.
497 * RETURNS
499 * The length of the encoded string, including padding and length fields.
501 * NOTES
503 * Will throw an exception if string or result are NULL. Is not multithread
504 * safe in the slightest.
506 static int ctl2_encode_string(
507 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
508 const WCHAR *string, /* [I] The string to encode. */
509 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
511 int length;
512 static char converted_string[0x104];
513 int offset;
515 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
516 converted_string[0] = length & 0xff;
517 converted_string[1] = (length >> 8) & 0xff;
519 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
521 *result = converted_string;
523 return (length + 5) & ~3;
526 /****************************************************************************
527 * ctl2_decode_string
529 * Converts string stored in typelib data to unicode.
531 static void ctl2_decode_string(
532 unsigned char *data,/* [I] String to be decoded */
533 WCHAR **string) /* [O] Decoded string */
535 int i, length;
536 static WCHAR converted_string[0x104];
538 length = data[0] + (data[1]<<8);
539 if((length&0x3) == 1)
540 length >>= 2;
542 for(i=0; i<length; i++)
543 converted_string[i] = data[i+2];
544 converted_string[length] = '\0';
546 *string = converted_string;
549 /****************************************************************************
550 * ctl2_alloc_segment
552 * Allocates memory from a segment in a type library.
554 * RETURNS
556 * Success: The offset within the segment of the new data area.
557 * Failure: -1 (this is invariably an out of memory condition).
559 * BUGS
561 * Does not (yet) handle the case where the allocated segment memory needs to grow.
563 static int ctl2_alloc_segment(
564 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
565 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
566 int size, /* [I] The amount to allocate. */
567 int block_size) /* [I] Initial allocation block size, or 0 for default. */
569 int offset;
571 if(!This->typelib_segment_data[segment]) {
572 if (!block_size) block_size = 0x2000;
574 This->typelib_segment_block_length[segment] = block_size;
575 This->typelib_segment_data[segment] = heap_alloc(block_size);
576 if (!This->typelib_segment_data[segment]) return -1;
577 memset(This->typelib_segment_data[segment], 0x57, block_size);
580 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
581 unsigned char *block;
583 block_size = This->typelib_segment_block_length[segment];
584 block = heap_realloc(This->typelib_segment_data[segment], block_size << 1);
585 if (!block) return -1;
587 if (segment == MSFT_SEG_TYPEINFO) {
588 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
589 ICreateTypeInfo2Impl *typeinfo;
591 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
592 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
596 memset(block + block_size, 0x57, block_size);
597 This->typelib_segment_block_length[segment] = block_size << 1;
598 This->typelib_segment_data[segment] = block;
601 offset = This->typelib_segdir[segment].length;
602 This->typelib_segdir[segment].length += size;
604 return offset;
607 /****************************************************************************
608 * ctl2_alloc_typeinfo
610 * Allocates and initializes a typeinfo structure in a type library.
612 * RETURNS
614 * Success: The offset of the new typeinfo.
615 * Failure: -1 (this is invariably an out of memory condition).
617 static int ctl2_alloc_typeinfo(
618 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
619 int nameoffset) /* [I] The offset of the name for this typeinfo. */
621 int offset;
622 MSFT_TypeInfoBase *typeinfo;
624 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
625 if (offset == -1) return -1;
627 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
629 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
631 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
632 typeinfo->memoffset = -1; /* should be EOF if no elements */
633 typeinfo->res2 = 0;
634 typeinfo->res3 = 0;
635 typeinfo->res4 = 3;
636 typeinfo->res5 = 0;
637 typeinfo->cElement = 0;
638 typeinfo->res7 = 0;
639 typeinfo->res8 = 0;
640 typeinfo->res9 = 0;
641 typeinfo->resA = 0;
642 typeinfo->posguid = -1;
643 typeinfo->flags = 0;
644 typeinfo->NameOffset = nameoffset;
645 typeinfo->version = 0;
646 typeinfo->docstringoffs = -1;
647 typeinfo->helpstringcontext = 0;
648 typeinfo->helpcontext = 0;
649 typeinfo->oCustData = -1;
650 typeinfo->cbSizeVft = 0;
651 typeinfo->cImplTypes = 0;
652 typeinfo->size = 0;
653 typeinfo->datatype1 = -1;
654 typeinfo->datatype2 = 0;
655 typeinfo->res18 = 0;
656 typeinfo->res19 = -1;
658 return offset;
661 /****************************************************************************
662 * ctl2_alloc_guid
664 * Allocates and initializes a GUID structure in a type library. Also updates
665 * the GUID hash table as needed.
667 * RETURNS
669 * Success: The offset of the new GUID.
670 * Failure: -1 (this is invariably an out of memory condition).
672 static int ctl2_alloc_guid(
673 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
674 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
676 int offset;
677 MSFT_GuidEntry *guid_space;
678 int hash_key;
680 hash_key = ctl2_hash_guid(&guid->guid);
682 offset = ctl2_find_guid(This, hash_key, &guid->guid);
683 if (offset != -1) return offset;
685 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
686 if (offset == -1) return -1;
688 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
689 *guid_space = *guid;
691 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
692 This->typelib_guidhash_segment[hash_key] = offset;
694 return offset;
697 /****************************************************************************
698 * ctl2_alloc_name
700 * Allocates and initializes a name within a type library. Also updates the
701 * name hash table as needed.
703 * RETURNS
705 * Success: The offset within the segment of the new name.
706 * Failure: -1 (this is invariably an out of memory condition).
708 static int ctl2_alloc_name(
709 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
710 const WCHAR *name) /* [I] The name to store. */
712 int length;
713 int offset;
714 MSFT_NameIntro *name_space;
715 char *encoded_name;
717 length = ctl2_encode_name(This, name, &encoded_name);
719 offset = ctl2_find_name(This, encoded_name);
720 if (offset != -1) return offset;
722 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
723 if (offset == -1) return -1;
725 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
726 name_space->hreftype = -1;
727 name_space->next_hash = -1;
728 memcpy(&name_space->namelen, encoded_name, length);
730 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
731 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
733 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
735 This->typelib_header.nametablecount += 1;
736 This->typelib_header.nametablechars += *encoded_name;
738 return offset;
741 /****************************************************************************
742 * ctl2_alloc_string
744 * Allocates and initializes a string in a type library.
746 * RETURNS
748 * Success: The offset within the segment of the new string.
749 * Failure: -1 (this is invariably an out of memory condition).
751 static int ctl2_alloc_string(
752 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
753 const WCHAR *string) /* [I] The string to store. */
755 int length;
756 int offset;
757 unsigned char *string_space;
758 char *encoded_string;
760 length = ctl2_encode_string(This, string, &encoded_string);
762 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
763 offset += (((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
764 This->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
765 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
768 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
769 if (offset == -1) return -1;
771 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
772 memcpy(string_space, encoded_string, length);
774 return offset;
777 /****************************************************************************
778 * ctl2_alloc_importinfo
780 * Allocates and initializes an import information structure in a type library.
782 * RETURNS
784 * Success: The offset of the new importinfo.
785 * Failure: -1 (this is invariably an out of memory condition).
787 static int ctl2_alloc_importinfo(
788 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
789 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
791 int offset;
792 MSFT_ImpInfo *impinfo_space;
794 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
795 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
796 offset+=sizeof(MSFT_ImpInfo)) {
797 if(impinfo_space->oImpFile == impinfo->oImpFile
798 && impinfo_space->oGuid == impinfo->oGuid)
799 return offset;
801 impinfo_space += 1;
804 impinfo->flags |= This->typelib_header.nimpinfos++;
806 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
807 if (offset == -1) return -1;
809 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
810 *impinfo_space = *impinfo;
812 return offset;
815 /****************************************************************************
816 * ctl2_alloc_importfile
818 * Allocates and initializes an import file definition in a type library.
820 * RETURNS
822 * Success: The offset of the new importinfo.
823 * Failure: -1 (this is invariably an out of memory condition).
825 static int ctl2_alloc_importfile(
826 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
827 int guidoffset, /* [I] The offset to the GUID for the imported library. */
828 LCID lcid, /* [I] The LCID of imported library. */
829 int major_version, /* [I] The major version number of the imported library. */
830 int minor_version, /* [I] The minor version number of the imported library. */
831 const WCHAR *filename) /* [I] The filename of the imported library. */
833 int length;
834 int offset;
835 MSFT_ImpFile *importfile;
836 char *encoded_string;
838 length = ctl2_encode_string(This, filename, &encoded_string);
840 encoded_string[0] <<= 2;
841 encoded_string[0] |= 1;
843 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
844 offset += (((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
845 This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 5) & 0xfffc) + 0xc) {
846 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
849 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
850 if (offset == -1) return -1;
852 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
853 importfile->guid = guidoffset;
854 importfile->lcid = lcid;
855 importfile->version = major_version | (minor_version << 16);
856 memcpy(importfile->filename, encoded_string, length);
858 return offset;
861 /****************************************************************************
862 * ctl2_encode_variant
864 * Encodes a variant, inline if possible or in custom data segment
866 * RETURNS
868 * Success: S_OK
869 * Failure: Error code from winerror.h
871 static HRESULT ctl2_encode_variant(
872 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
873 int *encoded_value, /* [O] The encoded default value or data offset */
874 VARIANT *value, /* [I] Default value to be encoded */
875 VARTYPE arg_type) /* [I] Argument type */
877 VARIANT v;
878 HRESULT hres;
879 int mask = 0;
881 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
883 if(arg_type == VT_INT)
884 arg_type = VT_I4;
885 if(arg_type == VT_UINT)
886 arg_type = VT_UI4;
888 v = *value;
889 if(V_VT(value) != arg_type) {
890 hres = VariantChangeType(&v, value, 0, arg_type);
891 if(FAILED(hres))
892 return hres;
895 /* Check if default value can be stored in encoded_value */
896 switch(arg_type) {
897 case VT_I4:
898 case VT_UI4:
899 mask = 0x3ffffff;
900 if(V_UI4(&v)>0x3ffffff)
901 break;
902 /* fall through */
903 case VT_I1:
904 case VT_UI1:
905 case VT_BOOL:
906 if(!mask)
907 mask = 0xff;
908 /* fall through */
909 case VT_I2:
910 case VT_UI2:
911 if(!mask)
912 mask = 0xffff;
913 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
914 return S_OK;
917 switch(arg_type) {
918 case VT_I4:
919 case VT_R4:
920 case VT_UI4:
921 case VT_INT:
922 case VT_UINT:
923 case VT_HRESULT:
924 case VT_PTR: {
925 /* Construct the data to be allocated */
926 int data[2];
927 data[0] = arg_type + (V_UI4(&v)<<16);
928 data[1] = (V_UI4(&v)>>16) + 0x57570000;
930 /* Check if the data was already allocated */
931 /* Currently the structures doesn't allow to do it in a nice way */
932 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
933 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
934 return S_OK;
936 /* Allocate the data */
937 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
938 if(*encoded_value == -1)
939 return E_OUTOFMEMORY;
941 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
942 return S_OK;
944 case VT_BSTR: {
945 /* Construct the data */
946 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
947 char *data = heap_alloc(len);
949 if(!data)
950 return E_OUTOFMEMORY;
952 *((unsigned short*)data) = arg_type;
953 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
954 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
955 if(V_BSTR(&v)[i] <= 0x7f)
956 data[i+6] = V_BSTR(&v)[i];
957 else
958 data[i+6] = '?';
960 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
961 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
962 data[i] = 0x57;
964 /* Check if the data was already allocated */
965 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
966 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
967 heap_free(data);
968 return S_OK;
971 /* Allocate the data */
972 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
973 if(*encoded_value == -1) {
974 heap_free(data);
975 return E_OUTOFMEMORY;
978 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
979 heap_free(data);
980 return S_OK;
982 default:
983 FIXME("Argument type not yet handled\n");
984 return E_NOTIMPL;
988 static int ctl2_find_custdata(
989 ICreateTypeLib2Impl *This,
990 REFGUID guid,
991 int offset)
993 while (offset != -1) {
994 MSFT_CDGuid *cdentry =
995 (MSFT_CDGuid *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
996 MSFT_GuidEntry *guidentry =
997 (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][cdentry->GuidOffset];
999 if (IsEqualGUID(guidentry, guid))
1000 return offset;
1002 offset = cdentry->next;
1005 return -1;
1008 /****************************************************************************
1009 * ctl2_decode_variant
1011 * Decodes a variant
1013 * RETURNS
1015 * Success: S_OK
1016 * Failure: Error code from winerror.h
1018 static HRESULT ctl2_decode_variant(
1019 ICreateTypeLib2Impl *This, /* [I] The typelib that contains the variant */
1020 int data_offs, /* [I] Offset within the data array, or the encoded value itself */
1021 VARIANT *value) /* [O] Decoded value */
1023 unsigned char *encoded_data;
1024 VARTYPE type;
1026 if (data_offs & 0x80000000) {
1027 /* data_offs contains the encoded value */
1028 V_VT(value) = (data_offs & ~0x80000000) >> 26;
1029 V_UI4(value) = data_offs & ~0xFF000000;
1030 return S_OK;
1033 encoded_data = &This->typelib_segment_data[MSFT_SEG_CUSTDATA][data_offs];
1034 type = *encoded_data;
1036 switch(type) {
1037 case VT_I4:
1038 case VT_R4:
1039 case VT_UI4:
1040 case VT_INT:
1041 case VT_UINT:
1042 case VT_HRESULT:
1043 case VT_PTR: {
1044 V_VT(value) = type;
1045 V_UI4(value) = *(unsigned*)(encoded_data + 2);
1046 return S_OK;
1048 case VT_BSTR: {
1049 unsigned len, i;
1051 len = *(unsigned*)(encoded_data + 2);
1053 V_VT(value) = type;
1054 V_BSTR(value) = SysAllocStringByteLen(NULL, len * sizeof(OLECHAR));
1055 for (i = 0; i < len; ++i)
1056 V_BSTR(value)[i] = *(encoded_data + 6 + i);
1058 return S_OK;
1060 default:
1061 FIXME("Don't yet have decoder for this VARTYPE: %u\n", type);
1062 return E_NOTIMPL;
1066 /****************************************************************************
1067 * ctl2_set_custdata
1069 * Adds a custom data element to an object in a type library.
1071 * RETURNS
1073 * Success: S_OK.
1074 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
1076 static HRESULT ctl2_set_custdata(
1077 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
1078 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
1079 VARIANT *pVarVal, /* [I] The custom data itself. */
1080 int *offset) /* [I/O] The list of custom data to prepend to. */
1082 MSFT_GuidEntry guidentry;
1083 HRESULT status;
1084 int dataoffset;
1085 int guidoffset;
1086 int custoffset;
1087 int *custdata;
1088 BOOL new_segment = FALSE;
1090 switch(V_VT(pVarVal))
1092 case VT_I4:
1093 case VT_R4:
1094 case VT_UI4:
1095 case VT_INT:
1096 case VT_UINT:
1097 case VT_HRESULT:
1098 case VT_BSTR:
1099 /* empty */
1100 break;
1101 default:
1102 return DISP_E_BADVARTYPE;
1105 guidentry.guid = *guid;
1107 guidentry.hreftype = -1;
1108 guidentry.next_hash = -1;
1110 guidoffset = ctl2_alloc_guid(This, &guidentry);
1111 if (guidoffset == -1) return E_OUTOFMEMORY;
1113 status = ctl2_encode_variant(This, &dataoffset, pVarVal, V_VT(pVarVal));
1114 if (status)
1115 return status;
1117 custoffset = ctl2_find_custdata(This, guid, *offset);
1118 if (custoffset == -1) {
1119 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
1120 if (custoffset == -1)
1121 return E_OUTOFMEMORY;
1122 new_segment = TRUE;
1125 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1126 custdata[0] = guidoffset;
1127 custdata[1] = dataoffset;
1128 if (new_segment) {
1129 custdata[2] = *offset;
1130 *offset = custoffset;
1133 return S_OK;
1136 /****************************************************************************
1137 * ctl2_encode_typedesc
1139 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
1140 * segments as needed.
1142 * RETURNS
1144 * Success: 0.
1145 * Failure: -1.
1147 static int ctl2_encode_typedesc(
1148 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
1149 const TYPEDESC *tdesc, /* [I] The type description to encode. */
1150 int *encoded_tdesc, /* [O] The encoded type description. */
1151 int *width, /* [O] The width of the type, or NULL. */
1152 int *alignment, /* [O] The alignment of the type, or NULL. */
1153 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1155 int default_tdesc;
1156 int scratch;
1157 int typeoffset;
1158 int arrayoffset;
1159 int *typedata;
1160 int *arraydata;
1161 int target_type;
1162 int child_size;
1164 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
1165 if (!width) width = &scratch;
1166 if (!alignment) alignment = &scratch;
1167 if (!decoded_size) decoded_size = &scratch;
1169 *decoded_size = 0;
1171 switch (tdesc->vt) {
1172 case VT_UI1:
1173 case VT_I1:
1174 *encoded_tdesc = default_tdesc;
1175 *width = 1;
1176 *alignment = 1;
1177 break;
1179 case VT_INT:
1180 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
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_UINT:
1191 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
1192 if (ctl2_get_syskind(This) == SYS_WIN16) {
1193 *width = 2;
1194 *alignment = 2;
1195 } else {
1196 *width = 4;
1197 *alignment = 4;
1199 break;
1201 case VT_UI2:
1202 case VT_I2:
1203 case VT_BOOL:
1204 *encoded_tdesc = default_tdesc;
1205 *width = 2;
1206 *alignment = 2;
1207 break;
1209 case VT_I4:
1210 case VT_UI4:
1211 case VT_R4:
1212 case VT_ERROR:
1213 case VT_BSTR:
1214 case VT_HRESULT:
1215 *encoded_tdesc = default_tdesc;
1216 *width = 4;
1217 *alignment = 4;
1218 break;
1220 case VT_CY:
1221 *encoded_tdesc = default_tdesc;
1222 *width = 8;
1223 *alignment = 4; /* guess? */
1224 break;
1226 case VT_VOID:
1227 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1228 *width = 0;
1229 *alignment = 1;
1230 break;
1232 case VT_PTR:
1233 case VT_SAFEARRAY:
1234 /* FIXME: Make with the error checking. */
1235 FIXME("PTR or SAFEARRAY vartype, may not work correctly.\n");
1237 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1239 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1240 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1241 if (((typedata[0] & 0xffff) == tdesc->vt) && (typedata[1] == target_type)) break;
1244 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1245 int mix_field;
1247 if (target_type & 0x80000000) {
1248 mix_field = (target_type >> 16) & VT_TYPEMASK;
1249 } else {
1250 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1251 switch((typedata[0]>>16) & ~VT_ARRAY)
1253 case VT_UI1:
1254 case VT_I1:
1255 case VT_UI2:
1256 case VT_I2:
1257 case VT_I4:
1258 case VT_UI4:
1259 mix_field = typedata[0]>>16;
1260 break;
1261 default:
1262 mix_field = ((typedata[0] >> 16) == 0x7fff) ? 0x7fff : 0x7ffe;
1263 break;
1267 if (tdesc->vt == VT_PTR)
1268 mix_field |= VT_BYREF;
1269 else if (tdesc->vt == VT_SAFEARRAY)
1270 mix_field |= VT_ARRAY;
1272 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1273 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1275 typedata[0] = (mix_field << 16) | tdesc->vt;
1276 typedata[1] = target_type;
1279 *encoded_tdesc = typeoffset;
1281 *width = 4;
1282 *alignment = 4;
1283 *decoded_size = sizeof(TYPEDESC) + child_size;
1284 break;
1286 case VT_CARRAY:
1288 /* FIXME: Make with the error checking. */
1289 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1291 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1292 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1293 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1295 arraydata[0] = target_type;
1296 arraydata[1] = num_dims;
1297 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1298 arraydata += 2;
1300 for(dim = 0; dim < num_dims; dim++) {
1301 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1302 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1303 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1304 arraydata += 2;
1306 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1307 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1309 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1310 typedata[1] = arrayoffset;
1312 *encoded_tdesc = typeoffset;
1313 *width = *width * elements;
1314 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1316 break;
1318 case VT_USERDEFINED:
1320 const MSFT_TypeInfoBase *basetype;
1321 INT basevt = 0x7fff;
1323 TRACE("USERDEFINED.\n");
1324 if (tdesc->u.hreftype % sizeof(*basetype) == 0 && tdesc->u.hreftype < This->typelib_segdir[MSFT_SEG_TYPEINFO].length)
1326 basetype = (MSFT_TypeInfoBase*)&(This->typelib_segment_data[MSFT_SEG_TYPEINFO][tdesc->u.hreftype]);
1327 switch(basetype->typekind & 0xf)
1329 case TKIND_ENUM:
1330 basevt = VT_I4;
1331 break;
1332 default:
1333 FIXME("USERDEFINED basetype %d not handled\n", basetype->typekind & 0xf);
1334 break;
1337 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1338 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1339 if ((typedata[0] == ((basevt << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1342 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1343 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1344 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1346 typedata[0] = (basevt << 16) | VT_USERDEFINED;
1347 typedata[1] = tdesc->u.hreftype;
1350 *encoded_tdesc = typeoffset;
1351 *width = 0;
1352 *alignment = 1;
1353 break;
1356 default:
1357 FIXME("Unrecognized type %d.\n", tdesc->vt);
1358 *encoded_tdesc = default_tdesc;
1359 *width = 0;
1360 *alignment = 1;
1361 break;
1364 return 0;
1367 /****************************************************************************
1368 * ctl2_decode_typedesc
1370 * Decodes a type description from an ICreateTypeLib2Impl.
1372 * RETURNS
1374 * Success: S_OK.
1375 * Failure: HRESULT error code.
1377 static HRESULT ctl2_decode_typedesc(
1378 ICreateTypeLib2Impl *This, /* [I] The type library from which to decode the TYPEDESC. */
1379 int encoded_tdesc, /* [I] The encoded type description. */
1380 TYPEDESC *tdesc) /* [O] The decoded type description. */
1382 int *typedata, i;
1383 HRESULT hres;
1385 if (encoded_tdesc & 0x80000000) {
1386 tdesc->vt = encoded_tdesc & VT_TYPEMASK;
1387 tdesc->u.lptdesc = NULL;
1388 return S_OK;
1391 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][encoded_tdesc];
1393 tdesc->vt = typedata[0] & 0xFFFF;
1395 switch(tdesc->vt) {
1396 case VT_PTR:
1397 case VT_SAFEARRAY:
1398 tdesc->u.lptdesc = heap_alloc_zero(sizeof(TYPEDESC));
1399 if (!tdesc->u.lptdesc)
1400 return E_OUTOFMEMORY;
1402 hres = ctl2_decode_typedesc(This, typedata[1], tdesc->u.lptdesc);
1403 if (FAILED(hres)) {
1404 heap_free(tdesc->u.lptdesc);
1405 return hres;
1408 return S_OK;
1410 case VT_CARRAY: {
1411 int arrayoffset, *arraydata, num_dims;
1413 arrayoffset = typedata[1];
1414 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1415 num_dims = arraydata[1] & 0xFFFF;
1417 tdesc->u.lpadesc = heap_alloc_zero(sizeof(ARRAYDESC) + sizeof(SAFEARRAYBOUND) * (num_dims - 1));
1418 if (!tdesc->u.lpadesc)
1419 return E_OUTOFMEMORY;
1421 hres = ctl2_decode_typedesc(This, arraydata[0], &tdesc->u.lpadesc->tdescElem);
1422 if (FAILED(hres)) {
1423 heap_free(tdesc->u.lpadesc);
1424 return E_OUTOFMEMORY;
1427 for (i = 0; i < num_dims; ++i) {
1428 tdesc->u.lpadesc->rgbounds[i].cElements = arraydata[2 + i * 2];
1429 tdesc->u.lpadesc->rgbounds[i].lLbound = arraydata[3 + i * 2];
1432 return S_OK;
1434 case VT_USERDEFINED:
1435 tdesc->u.hreftype = typedata[1];
1436 return S_OK;
1437 default:
1438 FIXME("unable to decode typedesc (%08x): unknown VT: %d\n", encoded_tdesc, tdesc->vt);
1439 return E_NOTIMPL;
1443 /****************************************************************************
1444 * ctl2_find_nth_reference
1446 * Finds a reference by index into the linked list of reference records.
1448 * RETURNS
1450 * Success: Offset of the desired reference record.
1451 * Failure: -1.
1453 static int ctl2_find_nth_reference(
1454 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1455 int offset, /* [I] The starting offset of the reference list. */
1456 int index) /* [I] The index of the reference to find. */
1458 MSFT_RefRecord *ref;
1460 for (; index && (offset != -1); index--) {
1461 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1462 offset = ref->onext;
1465 return offset;
1468 /****************************************************************************
1469 * ctl2_find_typeinfo_from_offset
1471 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1473 * RETURNS
1475 * Success: S_OK.
1476 * Failure: TYPE_E_ELEMENTNOTFOUND.
1478 static HRESULT ctl2_find_typeinfo_from_offset(
1479 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1480 int offset, /* [I] The offset of the desired typeinfo. */
1481 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1483 void *typeinfodata;
1484 ICreateTypeInfo2Impl *typeinfo;
1486 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1488 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1489 if (typeinfo->typeinfo == typeinfodata) {
1490 *ppTinfo = (ITypeInfo *)&typeinfo->ITypeInfo2_iface;
1491 ITypeInfo_AddRef(*ppTinfo);
1492 return S_OK;
1496 ERR("Failed to find typeinfo, invariant varied.\n");
1498 return TYPE_E_ELEMENTNOTFOUND;
1501 /****************************************************************************
1502 * funcrecord_reallochdr
1504 * Ensure FuncRecord data block contains header of required size
1506 * PARAMS
1508 * typedata [IO] - reference to pointer to data block
1509 * need [I] - required size of block in bytes
1511 * RETURNS
1513 * Number of additionally allocated bytes
1515 static INT funcrecord_reallochdr(INT **typedata, int need)
1517 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1518 int hdr = (*typedata)[0] - tail;
1519 int i;
1521 if (hdr >= need)
1522 return 0;
1524 *typedata = heap_realloc(*typedata, need + tail);
1525 if (!*typedata)
1526 return -1;
1528 if (tail)
1529 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1530 (*typedata)[0] = need + tail;
1532 /* fill in default values */
1533 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1535 switch(i)
1537 case 2:
1538 (*typedata)[i] = 0;
1539 break;
1540 case 7:
1541 (*typedata)[i] = -1;
1542 break;
1543 case 8:
1544 (*typedata)[i] = -1;
1545 break;
1546 case 9:
1547 (*typedata)[i] = -1;
1548 break;
1549 case 10:
1550 (*typedata)[i] = -1;
1551 break;
1552 case 11:
1553 (*typedata)[i] = 0;
1554 break;
1555 case 12:
1556 (*typedata)[i] = -1;
1557 break;
1561 return need - hdr;
1564 /*================== ICreateTypeInfo2 Implementation ===================================*/
1566 /******************************************************************************
1567 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1570 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1571 ICreateTypeInfo2 * iface,
1572 REFIID riid,
1573 VOID **ppvObject)
1575 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1577 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1579 *ppvObject=NULL;
1580 if(IsEqualIID(riid, &IID_IUnknown) ||
1581 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1582 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1584 *ppvObject = This;
1585 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1586 IsEqualIID(riid, &IID_ITypeInfo2)) {
1587 *ppvObject = &This->ITypeInfo2_iface;
1590 if(*ppvObject)
1592 ICreateTypeInfo2_AddRef(iface);
1593 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1594 return S_OK;
1596 TRACE("-- Interface: E_NOINTERFACE\n");
1597 return E_NOINTERFACE;
1600 /******************************************************************************
1601 * ICreateTypeInfo2_AddRef {OLEAUT32}
1603 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1605 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1606 ULONG ref = InterlockedIncrement(&This->ref);
1608 TRACE("(%p)->ref was %u\n",This, ref - 1);
1610 if(ref==1 && This->typelib)
1611 ICreateTypeLib2_AddRef(&This->typelib->ICreateTypeLib2_iface);
1613 return ref;
1616 /******************************************************************************
1617 * ICreateTypeInfo2_Release {OLEAUT32}
1619 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1621 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1622 ULONG ref = InterlockedDecrement(&This->ref);
1624 TRACE("(%p)->(%u)\n",This, ref);
1626 if (!ref) {
1627 if (This->typelib) {
1628 ICreateTypeLib2_fnRelease(&This->typelib->ICreateTypeLib2_iface);
1629 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1630 /* This->typelib = NULL; */
1633 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1634 /* HeapFree(GetProcessHeap(),0,This); */
1635 return 0;
1638 return ref;
1642 /******************************************************************************
1643 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1645 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1647 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1649 MSFT_GuidEntry guidentry;
1650 int offset;
1652 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1654 guidentry.guid = *guid;
1655 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1656 guidentry.next_hash = -1;
1658 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1660 if (offset == -1) return E_OUTOFMEMORY;
1662 This->typeinfo->posguid = offset;
1664 if (IsEqualIID(guid, &IID_IDispatch)) {
1665 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1668 return S_OK;
1671 /******************************************************************************
1672 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1674 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1676 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1678 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1680 if(uTypeFlags & TYPEFLAG_FDUAL) {
1681 This->typeinfo->typekind |= 0x10;
1682 This->typeinfo->typekind &= ~0x0f;
1683 This->typeinfo->typekind |= TKIND_DISPATCH;
1685 if(!This->dual) {
1686 This->dual = heap_alloc(sizeof(ICreateTypeInfo2Impl));
1687 if(!This->dual)
1688 return E_OUTOFMEMORY;
1690 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1691 This->dual->ref = 0;
1692 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1693 TKIND_INTERFACE : TKIND_DISPATCH;
1694 This->dual->dual = This;
1697 /* Make sure dispatch is in typeinfos queue */
1698 if(This->typekind != TKIND_DISPATCH) {
1699 if(This->typelib->last_typeinfo == This)
1700 This->typelib->last_typeinfo = This->dual;
1702 if(This->typelib->typeinfos == This)
1703 This->typelib->typeinfos = This->dual;
1704 else {
1705 ICreateTypeInfo2Impl *iter;
1707 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1708 iter->next_typeinfo = This->dual;
1710 } else
1711 iface = &This->dual->ICreateTypeInfo2_iface;
1714 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1715 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1716 ITypeLib *stdole;
1717 ITypeInfo *dispatch;
1718 HREFTYPE hreftype;
1719 HRESULT hres;
1721 hres = LoadTypeLib(stdole2tlb, &stdole);
1722 if(FAILED(hres))
1723 return hres;
1725 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1726 ITypeLib_Release(stdole);
1727 if(FAILED(hres))
1728 return hres;
1730 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1731 ITypeInfo_Release(dispatch);
1732 if(FAILED(hres))
1733 return hres;
1736 This->typeinfo->flags = uTypeFlags;
1737 return S_OK;
1740 /******************************************************************************
1741 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1743 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1744 ICreateTypeInfo2* iface,
1745 LPOLESTR pStrDoc)
1747 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1749 int offset;
1751 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1752 if (!pStrDoc)
1753 return E_INVALIDARG;
1755 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1756 if (offset == -1) return E_OUTOFMEMORY;
1757 This->typeinfo->docstringoffs = offset;
1758 return S_OK;
1761 /******************************************************************************
1762 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1764 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1765 ICreateTypeInfo2* iface,
1766 DWORD dwHelpContext)
1768 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1770 TRACE("(%p,%d)\n", iface, dwHelpContext);
1772 This->typeinfo->helpcontext = dwHelpContext;
1774 return S_OK;
1777 /******************************************************************************
1778 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1780 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1781 ICreateTypeInfo2* iface,
1782 WORD wMajorVerNum,
1783 WORD wMinorVerNum)
1785 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1787 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1789 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1790 return S_OK;
1793 /******************************************************************************
1794 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1796 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1797 ICreateTypeInfo2* iface,
1798 ITypeInfo* pTInfo,
1799 HREFTYPE* phRefType)
1801 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1803 ITypeLib *container;
1804 UINT index;
1805 HRESULT res;
1807 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1809 if(!pTInfo || !phRefType)
1810 return E_INVALIDARG;
1813 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1814 * same internal structure as one of ours. It could be from another
1815 * implementation of ITypeInfo. So we need to do the following...
1817 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1818 if (FAILED(res)) {
1819 TRACE("failed to find containing typelib.\n");
1820 return res;
1823 if (container == (ITypeLib *)&This->typelib->ITypeLib2_iface) {
1824 /* Process locally defined TypeInfo */
1825 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1826 } else {
1827 BSTR name;
1828 TLIBATTR *tlibattr;
1829 TYPEATTR *typeattr;
1830 TYPEKIND typekind;
1831 MSFT_GuidEntry guid, *check_guid;
1832 MSFT_ImpInfo impinfo;
1833 int guid_offset, import_offset;
1834 HRESULT hres;
1836 /* Allocate container GUID */
1837 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1838 if(FAILED(hres)) {
1839 ITypeLib_Release(container);
1840 return hres;
1843 guid.guid = tlibattr->guid;
1844 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1845 guid.next_hash = -1;
1847 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1848 if(guid_offset == -1) {
1849 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1850 ITypeLib_Release(container);
1851 return E_OUTOFMEMORY;
1854 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1855 if(check_guid->hreftype == guid.hreftype)
1856 This->typelib->typelib_guids++;
1858 /* Get import file name */
1859 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1860 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1861 if(FAILED(hres)) {
1862 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1863 ITypeLib_Release(container);
1864 return hres;
1867 /* Import file */
1868 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1869 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1870 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1871 SysFreeString(name);
1873 if(import_offset == -1) {
1874 ITypeLib_Release(container);
1875 return E_OUTOFMEMORY;
1878 /* Allocate referenced guid */
1879 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1880 if(FAILED(hres)) {
1881 ITypeLib_Release(container);
1882 return hres;
1885 guid.guid = typeattr->guid;
1886 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1887 guid.next_hash = -1;
1888 typekind = typeattr->typekind;
1889 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1891 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1892 if(guid_offset == -1) {
1893 ITypeLib_Release(container);
1894 return E_OUTOFMEMORY;
1897 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1898 if(check_guid->hreftype == guid.hreftype)
1899 This->typelib->typeinfo_guids++;
1901 /* Allocate importinfo */
1902 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1903 impinfo.oImpFile = import_offset;
1904 impinfo.oGuid = guid_offset;
1905 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1907 if(IsEqualGUID(&guid.guid, &IID_IDispatch))
1908 This->typelib->typelib_header.dispatchpos = *phRefType;
1911 ITypeLib_Release(container);
1912 return S_OK;
1915 /******************************************************************************
1916 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1918 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1919 ICreateTypeInfo2* iface,
1920 UINT index,
1921 FUNCDESC* pFuncDesc)
1923 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1925 CyclicList *iter, *insert;
1926 int *typedata;
1927 int i, num_defaults = 0, num_retval = 0;
1928 int decoded_size;
1929 HRESULT hres;
1931 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1933 if(!pFuncDesc || pFuncDesc->oVft&3)
1934 return E_INVALIDARG;
1936 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1937 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1938 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1939 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1940 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1942 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1943 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1945 switch(This->typekind) {
1946 case TKIND_MODULE:
1947 if(pFuncDesc->funckind != FUNC_STATIC)
1948 return TYPE_E_BADMODULEKIND;
1949 break;
1950 case TKIND_DISPATCH:
1951 if(pFuncDesc->funckind != FUNC_DISPATCH)
1952 return TYPE_E_BADMODULEKIND;
1953 break;
1954 default:
1955 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1956 return TYPE_E_BADMODULEKIND;
1959 if(cti2_get_func_count(This->typeinfo) < index)
1960 return TYPE_E_ELEMENTNOTFOUND;
1962 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1963 !pFuncDesc->cParams)
1964 return TYPE_E_INCONSISTENTPROPFUNCS;
1966 /* get number of arguments with default values specified */
1967 for (i = 0; i < pFuncDesc->cParams; i++) {
1968 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1969 num_defaults++;
1970 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1971 num_retval++;
1974 if (!This->typedata) {
1975 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
1976 if(!This->typedata)
1977 return E_OUTOFMEMORY;
1979 This->typedata->next = This->typedata;
1981 if(This->dual)
1982 This->dual->typedata = This->typedata;
1985 /* allocate type data space for us */
1986 insert = alloc_cyclic_list_item(CyclicListFunc);
1987 if(!insert)
1988 return E_OUTOFMEMORY;
1989 insert->u.data = heap_alloc(FIELD_OFFSET(MSFT_FuncRecord, HelpContext) +
1990 sizeof(int)*(num_defaults?4:3)*pFuncDesc->cParams);
1991 if(!insert->u.data) {
1992 heap_free(insert);
1993 return E_OUTOFMEMORY;
1996 /* fill out the basic type information */
1997 typedata = insert->u.data;
1998 typedata[0] = FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
1999 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
2000 typedata[2] = pFuncDesc->wFuncFlags;
2001 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
2002 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
2003 if(num_defaults) typedata[4] |= 0x1000;
2004 if (num_retval) typedata[4] |= 0x4000;
2005 typedata[5] = pFuncDesc->cParams;
2007 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
2008 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
2009 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
2010 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
2012 /* add default values */
2013 if(num_defaults) {
2014 for (i = 0; i < pFuncDesc->cParams; i++)
2015 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
2016 hres = ctl2_encode_variant(This->typelib, typedata+6+i,
2017 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
2018 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
2020 if(FAILED(hres)) {
2021 heap_free(insert->u.data);
2022 heap_free(insert);
2023 return hres;
2025 } else
2026 typedata[6+i] = 0xffffffff;
2028 num_defaults = pFuncDesc->cParams;
2031 /* add arguments */
2032 for (i = 0; i < pFuncDesc->cParams; i++) {
2033 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
2034 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
2035 typedata[7+num_defaults+(i*3)] = -1;
2036 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
2037 typedata[3] += decoded_size << 16;
2040 /* update the index data */
2041 insert->indice = pFuncDesc->memid;
2042 insert->name = -1;
2044 /* insert type data to list */
2045 if(index == cti2_get_func_count(This->typeinfo)) {
2046 insert->next = This->typedata->next;
2047 This->typedata->next = insert;
2048 This->typedata = insert;
2050 if(This->dual)
2051 This->dual->typedata = This->typedata;
2052 } else {
2053 iter = This->typedata->next;
2054 for(i=0; i<index; i++)
2055 iter = iter->next;
2057 insert->next = iter->next;
2058 iter->next = insert;
2061 /* update type data size */
2062 This->typedata->next->u.val += FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
2064 /* Increment the number of function elements */
2065 This->typeinfo->cElement += 1;
2067 return S_OK;
2070 /******************************************************************************
2071 * ICreateTypeInfo2_AddImplType {OLEAUT32}
2073 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
2074 ICreateTypeInfo2* iface,
2075 UINT index,
2076 HREFTYPE hRefType)
2078 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2080 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
2082 if (This->typekind == TKIND_COCLASS) {
2083 int offset;
2084 MSFT_RefRecord *ref;
2086 if (index == 0) {
2087 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
2089 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2090 if (offset == -1) return E_OUTOFMEMORY;
2092 This->typeinfo->datatype1 = offset;
2093 } else {
2094 int lastoffset;
2096 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
2097 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
2099 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
2100 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
2102 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2103 if (offset == -1) return E_OUTOFMEMORY;
2105 ref->onext = offset;
2108 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2110 ref->reftype = hRefType;
2111 ref->flags = 0;
2112 ref->oCustData = -1;
2113 ref->onext = -1;
2114 This->typeinfo->cImplTypes++;
2115 } else if (This->typekind == TKIND_INTERFACE) {
2116 if (This->typeinfo->cImplTypes && index==1)
2117 return TYPE_E_BADMODULEKIND;
2119 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
2121 This->typeinfo->datatype1 = hRefType;
2122 This->typeinfo->cImplTypes = 1;
2123 } else if (This->typekind == TKIND_DISPATCH) {
2124 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
2126 /* FIXME: Check if referenced typeinfo is IDispatch */
2127 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2128 This->typeinfo->cImplTypes = 1;
2129 } else {
2130 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
2131 return E_OUTOFMEMORY;
2134 return S_OK;
2137 /******************************************************************************
2138 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
2140 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
2141 ICreateTypeInfo2* iface,
2142 UINT index,
2143 INT implTypeFlags)
2145 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2146 int offset;
2147 MSFT_RefRecord *ref;
2149 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
2151 if (This->typekind != TKIND_COCLASS) {
2152 return TYPE_E_BADMODULEKIND;
2155 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2156 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2158 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2159 ref->flags = implTypeFlags;
2161 return S_OK;
2164 /******************************************************************************
2165 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2167 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2168 ICreateTypeInfo2* iface,
2169 WORD cbAlignment)
2171 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2173 TRACE("(%p,%d)\n", iface, cbAlignment);
2175 if (!cbAlignment) return E_INVALIDARG;
2176 if (cbAlignment > 16) return E_INVALIDARG;
2178 This->typeinfo->typekind &= ~0xffc0;
2179 This->typeinfo->typekind |= cbAlignment << 6;
2181 /* FIXME: There's probably some way to simplify this. */
2182 switch (This->typekind) {
2183 case TKIND_ALIAS:
2184 default:
2185 break;
2187 case TKIND_ENUM:
2188 case TKIND_INTERFACE:
2189 case TKIND_DISPATCH:
2190 case TKIND_COCLASS:
2191 if (cbAlignment > 4) cbAlignment = 4;
2192 break;
2194 case TKIND_RECORD:
2195 case TKIND_MODULE:
2196 case TKIND_UNION:
2197 cbAlignment = 1;
2198 break;
2201 This->typeinfo->typekind |= cbAlignment << 11;
2203 return S_OK;
2206 /******************************************************************************
2207 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2209 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2210 ICreateTypeInfo2* iface,
2211 LPOLESTR pStrSchema)
2213 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2214 return E_OUTOFMEMORY;
2217 /******************************************************************************
2218 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2220 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2221 ICreateTypeInfo2* iface,
2222 UINT index,
2223 VARDESC* pVarDesc)
2225 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2227 HRESULT status = S_OK;
2228 CyclicList *insert;
2229 INT *typedata;
2230 int var_datawidth;
2231 int var_alignment;
2232 int var_type_size;
2233 int alignment;
2235 TRACE("(%p,%d,%p)\n", iface, index, pVarDesc);
2236 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2237 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2238 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2239 pVarDesc->wVarFlags, pVarDesc->varkind);
2241 if (cti2_get_var_count(This->typeinfo) != index)
2242 return TYPE_E_ELEMENTNOTFOUND;
2244 if (!This->typedata) {
2245 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
2246 if(!This->typedata)
2247 return E_OUTOFMEMORY;
2249 This->typedata->next = This->typedata;
2251 if(This->dual)
2252 This->dual->typedata = This->typedata;
2255 insert = alloc_cyclic_list_item(CyclicListVar);
2256 if(!insert)
2257 return E_OUTOFMEMORY;
2259 /* allocate whole structure, it's fixed size always */
2260 insert->u.data = heap_alloc(sizeof(MSFT_VarRecord));
2261 if(!insert->u.data) {
2262 heap_free(insert);
2263 return E_OUTOFMEMORY;
2266 insert->next = This->typedata->next;
2267 This->typedata->next = insert;
2268 This->typedata = insert;
2270 if(This->dual)
2271 This->dual->typedata = This->typedata;
2273 This->typedata->next->u.val += FIELD_OFFSET(MSFT_VarRecord, HelpContext);
2274 typedata = This->typedata->u.data;
2276 /* fill out the basic type information */
2278 /* no optional fields initially */
2279 typedata[0] = FIELD_OFFSET(MSFT_VarRecord, HelpContext) | (index << 16);
2280 typedata[2] = pVarDesc->wVarFlags;
2281 typedata[3] = (sizeof(VARDESC) << 16) | pVarDesc->varkind;
2283 /* update the index data */
2284 insert->indice = 0x40000000 + index;
2285 insert->name = -1;
2287 /* figure out type widths and whatnot */
2288 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2289 &typedata[1], &var_datawidth, &var_alignment,
2290 &var_type_size);
2292 if (pVarDesc->varkind != VAR_CONST)
2294 /* pad out starting position to data width */
2295 This->datawidth += var_alignment - 1;
2296 This->datawidth &= ~(var_alignment - 1);
2297 typedata[4] = This->datawidth;
2299 /* add the new variable to the total data width */
2300 This->datawidth += var_datawidth;
2301 if(This->dual)
2302 This->dual->datawidth = This->datawidth;
2304 /* add type description size to total required allocation */
2305 typedata[3] += var_type_size << 16;
2307 /* fix type alignment */
2308 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2309 if (alignment < var_alignment) {
2310 alignment = var_alignment;
2311 This->typeinfo->typekind &= ~0xf800;
2312 This->typeinfo->typekind |= alignment << 11;
2315 /* ??? */
2316 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2317 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2318 This->typeinfo->res2 <<= 1;
2321 /* ??? */
2322 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2323 This->typeinfo->res3 += 0x2c;
2325 /* pad data width to alignment */
2326 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2327 } else {
2328 VARIANT *value = pVarDesc->DUMMYUNIONNAME.lpvarValue;
2329 status = ctl2_encode_variant(This->typelib, typedata+4, value, V_VT(value));
2330 /* ??? native sets size 0x34 */
2331 typedata[3] += 0x10 << 16;
2334 /* increment the number of variable elements */
2335 This->typeinfo->cElement += 0x10000;
2337 return status;
2340 /******************************************************************************
2341 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2343 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2344 ICreateTypeInfo2* iface,
2345 UINT index,
2346 LPOLESTR* names,
2347 UINT cNames)
2349 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2350 CyclicList *iter, *iter2;
2351 int offset, len, i;
2352 unsigned char *namedata;
2354 TRACE("(%p %d %p %d)\n", This, index, names, cNames);
2356 if(!names)
2357 return E_INVALIDARG;
2359 if(index >= cti2_get_func_count(This->typeinfo) || cNames == 0)
2360 return TYPE_E_ELEMENTNOTFOUND;
2362 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2363 if (iter->type == CyclicListFunc)
2364 if (i++ >= index)
2365 break;
2367 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2368 if(cNames != iter->u.data[5] + (ctl2_get_invokekind(iter) & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2369 return TYPE_E_ELEMENTNOTFOUND;
2371 TRACE("function name %s\n", debugstr_w(names[0]));
2372 len = ctl2_encode_name(This->typelib, names[0], (char**)&namedata);
2373 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2375 int cmp = memcmp(namedata, This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len);
2376 if (iter2->name != -1 && cmp == 0) {
2377 if (iter2->type == CyclicListFunc) {
2378 INVOKEKIND inv1 = ctl2_get_invokekind(iter);
2379 INVOKEKIND inv2 = ctl2_get_invokekind(iter2);
2381 /* it's allowed to have PUT, PUTREF and GET methods with the same name */
2382 if ((inv1 != inv2) &&
2383 (inv1 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)) &&
2384 (inv2 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)))
2385 continue;
2388 return TYPE_E_AMBIGUOUSNAME;
2392 offset = ctl2_alloc_name(This->typelib, names[0]);
2393 if(offset == -1)
2394 return E_OUTOFMEMORY;
2396 iter->name = offset;
2398 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2399 if (*((INT*)namedata) == -1)
2400 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2402 len = ctl2_get_record_size(iter)/4 - iter->u.data[5]*3;
2404 for (i = 1; i < cNames; i++) {
2405 offset = ctl2_alloc_name(This->typelib, names[i]);
2406 iter->u.data[len + ((i-1)*3) + 1] = offset;
2409 return S_OK;
2412 /******************************************************************************
2413 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2415 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2416 ICreateTypeInfo2* iface,
2417 UINT index,
2418 LPOLESTR szName)
2420 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2421 CyclicList *iter;
2422 int offset, i;
2423 unsigned char *namedata;
2425 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(szName));
2427 if (cti2_get_var_count(This->typeinfo) <= index)
2428 return TYPE_E_ELEMENTNOTFOUND;
2430 offset = ctl2_alloc_name(This->typelib, szName);
2431 if (offset == -1) return E_OUTOFMEMORY;
2433 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2434 if (*((INT *)namedata) == -1) {
2435 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2436 namedata[9] |= 0x10;
2438 if (This->typekind == TKIND_ENUM) {
2439 namedata[9] |= 0x20;
2442 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2443 if (iter->type == CyclicListVar)
2444 if (i++ >= index)
2445 break;
2447 iter->name = offset;
2448 return S_OK;
2451 /******************************************************************************
2452 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2454 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2455 ICreateTypeInfo2* iface,
2456 TYPEDESC* pTDescAlias)
2458 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2460 int encoded_typedesc;
2461 int width;
2463 if (This->typekind != TKIND_ALIAS) {
2464 return TYPE_E_WRONGTYPEKIND;
2467 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2469 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2470 return E_OUTOFMEMORY;
2473 This->typeinfo->size = width;
2474 This->typeinfo->datatype1 = encoded_typedesc;
2476 return S_OK;
2479 /******************************************************************************
2480 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2482 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2483 ICreateTypeInfo2* iface,
2484 UINT index,
2485 LPOLESTR szDllName,
2486 LPOLESTR szProcName)
2488 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2489 return E_OUTOFMEMORY;
2492 /******************************************************************************
2493 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2495 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2496 ICreateTypeInfo2* iface,
2497 UINT index,
2498 LPOLESTR szDocString)
2500 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2501 return E_OUTOFMEMORY;
2504 /******************************************************************************
2505 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2507 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2508 ICreateTypeInfo2* iface,
2509 UINT index,
2510 LPOLESTR docstring)
2512 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2513 CyclicList *iter;
2515 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(docstring));
2517 if (!docstring) return E_INVALIDARG;
2519 if (cti2_get_var_count(This->typeinfo) <= index)
2520 return TYPE_E_ELEMENTNOTFOUND;
2522 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2523 if (iter->type == CyclicListVar)
2525 if (index-- == 0)
2527 int offset = ctl2_alloc_string(This->typelib, docstring);
2529 if (offset == -1) return E_OUTOFMEMORY;
2530 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, res9));
2531 iter->u.data[6] = offset;
2532 return S_OK;
2536 return TYPE_E_ELEMENTNOTFOUND;
2539 /******************************************************************************
2540 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2542 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2543 ICreateTypeInfo2* iface,
2544 UINT index,
2545 DWORD dwHelpContext)
2547 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2548 CyclicList *func;
2550 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2552 if(cti2_get_func_count(This->typeinfo) < index)
2553 return TYPE_E_ELEMENTNOTFOUND;
2555 if(cti2_get_func_count(This->typeinfo) == index && This->typedata->type == CyclicListFunc)
2556 func = This->typedata;
2557 else
2558 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2559 if (func->type == CyclicListFunc)
2560 if(index-- == 0)
2561 break;
2563 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2564 if(!func->u.data)
2565 return E_OUTOFMEMORY;
2567 func->u.data[6] = dwHelpContext;
2568 return S_OK;
2571 /******************************************************************************
2572 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2574 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2575 ICreateTypeInfo2* iface,
2576 UINT index,
2577 DWORD context)
2579 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2580 CyclicList *iter;
2582 TRACE("(%p,%d,%d)\n", This, index, context);
2584 if (cti2_get_var_count(This->typeinfo) <= index)
2585 return TYPE_E_ELEMENTNOTFOUND;
2587 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2588 if (iter->type == CyclicListVar)
2590 if (index-- == 0)
2592 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, HelpString));
2593 iter->u.data[5] = context;
2594 return S_OK;
2598 return TYPE_E_ELEMENTNOTFOUND;
2601 /******************************************************************************
2602 * ICreateTypeInfo2_SetMops {OLEAUT32}
2604 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2605 ICreateTypeInfo2* iface,
2606 UINT index,
2607 BSTR bstrMops)
2609 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2610 return E_OUTOFMEMORY;
2613 /******************************************************************************
2614 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2616 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2617 ICreateTypeInfo2* iface,
2618 IDLDESC* pIdlDesc)
2620 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2621 return E_OUTOFMEMORY;
2624 /******************************************************************************
2625 * ICreateTypeInfo2_LayOut {OLEAUT32}
2627 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2628 ICreateTypeInfo2* iface)
2630 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2631 CyclicList *iter, *iter2, *last = NULL, **typedata;
2632 HREFTYPE hreftype;
2633 HRESULT hres;
2634 unsigned user_vft = 0;
2635 int i;
2637 TRACE("(%p)\n", This);
2639 /* FIXME: LayOut should be run on all ImplTypes */
2640 if(This->typekind == TKIND_COCLASS || This->typekind == TKIND_ALIAS)
2641 return S_OK;
2643 /* Validate inheritance */
2644 This->typeinfo->datatype2 = 0;
2645 hreftype = This->typeinfo->datatype1;
2647 /* Process internally defined interfaces */
2648 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2649 MSFT_TypeInfoBase *header;
2651 if(hreftype&1)
2652 break;
2654 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2655 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2656 hreftype = header->datatype1;
2658 if(i == This->typelib->typelib_header.nrtypeinfos)
2659 return TYPE_E_CIRCULARTYPE;
2661 /* Process externally defined interfaces */
2662 if(hreftype != -1) {
2663 ITypeInfo *cur, *next;
2664 TYPEATTR *typeattr;
2666 hres = ICreateTypeInfo2_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2667 if(FAILED(hres))
2668 return hres;
2670 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2671 ITypeInfo_Release(next);
2672 if(FAILED(hres))
2673 return hres;
2676 while(1) {
2677 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2678 if(FAILED(hres)) {
2679 ITypeInfo_Release(cur);
2680 return hres;
2683 if(IsEqualGUID(&typeattr->guid, &IID_IDispatch))
2684 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2686 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2687 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2689 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2690 if(hres == TYPE_E_ELEMENTNOTFOUND)
2691 break;
2692 if(FAILED(hres)) {
2693 ITypeInfo_Release(cur);
2694 return hres;
2697 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2698 if(FAILED(hres)) {
2699 ITypeInfo_Release(cur);
2700 return hres;
2703 ITypeInfo_Release(cur);
2704 cur = next;
2706 ITypeInfo_Release(cur);
2709 /* Get cbSizeVft of inherited interface */
2710 /* Makes LayOut running recursively */
2711 if(This->typeinfo->datatype1 != -1) {
2712 ITypeInfo *cur, *inherited;
2713 TYPEATTR *typeattr;
2715 hres = ICreateTypeInfo2_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2716 if(FAILED(hres))
2717 return hres;
2719 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2720 ITypeInfo_Release(cur);
2721 if(FAILED(hres))
2722 return hres;
2724 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2725 if(FAILED(hres)) {
2726 ITypeInfo_Release(inherited);
2727 return hres;
2730 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2732 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2733 ITypeInfo_Release(inherited);
2734 } else
2735 This->typeinfo->cbSizeVft = 0;
2737 if(!This->typedata)
2738 return S_OK;
2740 typedata = heap_alloc(sizeof(CyclicList*)*cti2_get_func_count(This->typeinfo));
2741 if(!typedata)
2742 return E_OUTOFMEMORY;
2744 /* Assign IDs and VTBL entries */
2745 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2746 if (iter->type == CyclicListFunc)
2747 last = iter;
2749 if(last && last->u.data[3]&1)
2750 user_vft = last->u.data[3]&0xffff;
2752 i = 0;
2753 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2754 /* Assign MEMBERID if MEMBERID_NIL was specified */
2755 if(iter->indice == MEMBERID_NIL) {
2756 iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2758 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2759 if(iter == iter2) continue;
2760 if(iter2->indice == iter->indice) {
2761 iter->indice = 0x60000000 + This->typeinfo->cElement + (This->typeinfo->datatype2<<16);
2763 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2764 if(iter == iter2) continue;
2765 if(iter2->indice == iter->indice) {
2766 ++iter->indice;
2767 iter2 = This->typedata->next;
2771 break;
2776 if (iter->type != CyclicListFunc)
2777 continue;
2779 typedata[i] = iter;
2781 iter->u.data[0] = ctl2_get_record_size(iter) | (i<<16);
2783 if((iter->u.data[3]&1) != (user_vft&1)) {
2784 heap_free(typedata);
2785 return TYPE_E_INVALIDID;
2788 if(user_vft&1) {
2789 if(user_vft < (iter->u.data[3]&0xffff))
2790 user_vft = (iter->u.data[3]&0xffff);
2792 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2793 heap_free(typedata);
2794 return TYPE_E_INVALIDID;
2796 } else if(This->typekind != TKIND_MODULE) {
2797 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2798 This->typeinfo->cbSizeVft += 4;
2801 /* Construct a list of elements with the same memberid */
2802 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2803 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2804 if(iter->indice == iter2->indice) {
2805 int v1, v2;
2807 v1 = iter->u.data[4] >> 16;
2808 v2 = iter2->u.data[4] >> 16;
2810 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2811 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2812 break;
2816 i++;
2819 if(user_vft)
2820 This->typeinfo->cbSizeVft = user_vft+3;
2822 for(i=0; i< cti2_get_func_count(This->typeinfo); i++) {
2823 if(typedata[i]->u.data[4]>>16 > i) {
2824 INVOKEKIND inv = ctl2_get_invokekind(typedata[i]);
2826 i = typedata[i]->u.data[4] >> 16;
2828 while(i > typedata[i]->u.data[4]>>16) {
2829 INVOKEKIND invkind = ctl2_get_invokekind(typedata[i]);
2831 if(inv & invkind) {
2832 heap_free(typedata);
2833 return TYPE_E_DUPLICATEID;
2836 i = typedata[i]->u.data[4] >> 16;
2837 inv |= invkind;
2840 if(inv & INVOKE_FUNC) {
2841 heap_free(typedata);
2842 return TYPE_E_INCONSISTENTPROPFUNCS;
2847 heap_free(typedata);
2848 return S_OK;
2851 /******************************************************************************
2852 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2854 * Delete a function description from a type.
2856 * RETURNS
2858 * Success: S_OK.
2859 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2861 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2862 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2863 UINT index) /* [I] The index of the function to delete. */
2865 FIXME("(%p,%d), stub!\n", iface, index);
2866 return E_OUTOFMEMORY;
2869 /******************************************************************************
2870 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2872 * Delete a function description from a type.
2874 * RETURNS
2876 * Success: S_OK.
2877 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2879 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2880 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2881 MEMBERID memid, /* [I] The member id of the function to delete. */
2882 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2884 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2885 return E_OUTOFMEMORY;
2888 /******************************************************************************
2889 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2891 * Delete a variable description from a type.
2893 * RETURNS
2895 * Success: S_OK.
2896 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2897 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2899 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2900 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2901 UINT index) /* [I] The index of the variable description to delete. */
2903 FIXME("(%p,%d), stub!\n", iface, index);
2904 return E_OUTOFMEMORY;
2907 /******************************************************************************
2908 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2910 * Delete a variable description from a type.
2912 * RETURNS
2914 * Success: S_OK.
2915 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2916 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2918 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2919 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2920 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2922 FIXME("(%p,%d), stub!\n", iface, memid);
2923 return E_OUTOFMEMORY;
2926 /******************************************************************************
2927 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2929 * Delete an interface implementation from a type. (?)
2931 * RETURNS
2933 * Success: S_OK.
2934 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2936 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2937 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2938 UINT index) /* [I] The index of the interface to delete. */
2940 FIXME("(%p,%d), stub!\n", iface, index);
2941 return E_OUTOFMEMORY;
2944 /******************************************************************************
2945 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2947 * Set the custom data for a type.
2949 * RETURNS
2951 * Success: S_OK.
2952 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2954 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2955 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2956 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2957 VARIANT* pVarVal) /* [I] The custom data. */
2959 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2961 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2963 if (!pVarVal)
2964 return E_INVALIDARG;
2966 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2969 /******************************************************************************
2970 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2972 * Set the custom data for a function.
2974 * RETURNS
2976 * Success: S_OK.
2977 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2979 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2980 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2981 UINT index, /* [I] The index of the function for which to set the custom data. */
2982 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2983 VARIANT* pVarVal) /* [I] The custom data. */
2985 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2986 CyclicList *iter;
2988 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2990 if(index >= cti2_get_func_count(This->typeinfo))
2991 return TYPE_E_ELEMENTNOTFOUND;
2993 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2994 if (iter->type == CyclicListFunc)
2995 if (index-- == 0)
2996 break;
2998 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
2999 if(!iter->u.data)
3000 return E_OUTOFMEMORY;
3002 iter->u.data[4] |= 0x80;
3003 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
3006 /******************************************************************************
3007 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
3009 * Set the custom data for a function parameter.
3011 * RETURNS
3013 * Success: S_OK.
3014 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3016 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
3017 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3018 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
3019 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
3020 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3021 VARIANT* pVarVal) /* [I] The custom data. */
3023 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3024 return E_OUTOFMEMORY;
3027 /******************************************************************************
3028 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
3030 * Set the custom data for a variable.
3032 * RETURNS
3034 * Success: S_OK.
3035 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3037 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
3038 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3039 UINT index, /* [I] The index of the variable on which to set the custom data. */
3040 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3041 VARIANT* pVarVal) /* [I] The custom data. */
3043 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3044 return E_OUTOFMEMORY;
3047 /******************************************************************************
3048 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
3050 * Set the custom data for an implemented interface.
3052 * RETURNS
3054 * Success: S_OK.
3055 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3057 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
3058 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
3059 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
3060 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3061 VARIANT* pVarVal) /* [I] The custom data. */
3063 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3064 return E_OUTOFMEMORY;
3067 /******************************************************************************
3068 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
3070 * Set the help string context for the typeinfo.
3072 * RETURNS
3074 * Success: S_OK.
3075 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3077 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
3078 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3079 ULONG helpcontext) /* [I] The help string context. */
3081 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
3083 TRACE("(%p, %d)\n", iface, helpcontext);
3085 This->typelib->typelib_header.helpcontext = helpcontext;
3087 return S_OK;
3090 /******************************************************************************
3091 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
3093 * Set the help string context for a function.
3095 * RETURNS
3097 * Success: S_OK.
3098 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3100 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
3101 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3102 UINT index, /* [I] The index for the function on which to set the help string context. */
3103 ULONG dwHelpStringContext) /* [I] The help string context. */
3105 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3106 return E_OUTOFMEMORY;
3109 /******************************************************************************
3110 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
3112 * Set the help string context for a variable.
3114 * RETURNS
3116 * Success: S_OK.
3117 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3119 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
3120 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3121 UINT index, /* [I] The index of the variable on which to set the help string context. */
3122 ULONG dwHelpStringContext) /* [I] The help string context */
3124 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3125 return E_OUTOFMEMORY;
3128 /******************************************************************************
3129 * ICreateTypeInfo2_Invalidate {OLEAUT32}
3131 * Undocumented function. (!)
3133 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
3134 ICreateTypeInfo2* iface)
3136 FIXME("(%p), stub!\n", iface);
3137 return E_OUTOFMEMORY;
3140 /******************************************************************************
3141 * ICreateTypeInfo2_SetName {OLEAUT32}
3143 * Set the name for a typeinfo.
3145 * RETURNS
3147 * Success: S_OK.
3148 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
3150 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
3151 ICreateTypeInfo2* iface,
3152 LPOLESTR szName)
3154 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
3155 return E_OUTOFMEMORY;
3158 /*================== ITypeInfo2 Implementation ===================================*/
3160 /******************************************************************************
3161 * ITypeInfo2_QueryInterface {OLEAUT32}
3163 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
3165 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3167 return ICreateTypeInfo2_QueryInterface(&This->ICreateTypeInfo2_iface, riid, ppv);
3170 /******************************************************************************
3171 * ITypeInfo2_AddRef {OLEAUT32}
3173 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3175 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3177 return ICreateTypeInfo2_AddRef(&This->ICreateTypeInfo2_iface);
3180 /******************************************************************************
3181 * ITypeInfo2_Release {OLEAUT32}
3183 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3185 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3187 return ICreateTypeInfo2_Release(&This->ICreateTypeInfo2_iface);
3190 /******************************************************************************
3191 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3193 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3194 ITypeInfo2* iface,
3195 TYPEATTR** ppTypeAttr)
3197 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3198 HRESULT hres;
3200 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3202 if(!ppTypeAttr)
3203 return E_INVALIDARG;
3205 hres = ICreateTypeInfo2_LayOut(&This->ICreateTypeInfo2_iface);
3206 if(FAILED(hres))
3207 return hres;
3209 *ppTypeAttr = heap_alloc_zero(sizeof(TYPEATTR));
3210 if(!*ppTypeAttr)
3211 return E_OUTOFMEMORY;
3213 if(This->typeinfo->posguid != -1) {
3214 MSFT_GuidEntry *guid;
3216 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3217 (*ppTypeAttr)->guid = guid->guid;
3220 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3221 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3222 (*ppTypeAttr)->typekind = This->typekind;
3223 (*ppTypeAttr)->cFuncs = cti2_get_func_count(This->typeinfo);
3224 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3225 (*ppTypeAttr)->cFuncs += sizeof(IDispatchVtbl)/sizeof(void*);
3226 (*ppTypeAttr)->cVars = cti2_get_var_count(This->typeinfo);
3227 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3228 (*ppTypeAttr)->cbSizeVft = This->typekind == TKIND_DISPATCH ? sizeof(IDispatchVtbl) : This->typeinfo->cbSizeVft;
3229 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3230 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3231 (*ppTypeAttr)->wMajorVerNum = LOWORD(This->typeinfo->version);
3232 (*ppTypeAttr)->wMinorVerNum = HIWORD(This->typeinfo->version);
3234 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3235 FIXME("TKIND_ALIAS handling not implemented\n");
3237 return S_OK;
3240 /******************************************************************************
3241 * ITypeInfo2_GetTypeComp {OLEAUT32}
3243 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3244 ITypeInfo2* iface,
3245 ITypeComp** ppTComp)
3247 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3248 return E_OUTOFMEMORY;
3251 /******************************************************************************
3252 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3254 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3255 ITypeInfo2* iface,
3256 UINT index,
3257 FUNCDESC** ppFuncDesc)
3259 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3260 int i, *typedata, num_defaults = 0, hdr_len, tail, has_defaults;
3261 CyclicList *desc;
3262 HRESULT hres;
3264 TRACE("(%p,%d,%p), semi-stub\n", iface, index, ppFuncDesc);
3266 if (!ppFuncDesc)
3267 return E_INVALIDARG;
3269 if (index >= cti2_get_func_count(This->typeinfo))
3270 return TYPE_E_ELEMENTNOTFOUND;
3272 hres = ICreateTypeInfo2_LayOut(&This->ICreateTypeInfo2_iface);
3273 if (FAILED(hres))
3274 return hres;
3276 desc = This->typedata->next;
3277 for (i = index; i >= 0; ) {
3278 desc = desc->next;
3279 if (desc->type == CyclicListFunc)
3280 --i;
3283 typedata = desc->u.data;
3285 *ppFuncDesc = heap_alloc_zero(sizeof(FUNCDESC));
3286 if (!*ppFuncDesc)
3287 return E_OUTOFMEMORY;
3289 (*ppFuncDesc)->memid = desc->indice;
3290 (*ppFuncDesc)->lprgscode = NULL; /* FIXME: Unimplemented */
3291 (*ppFuncDesc)->funckind = typedata[4] & 0x7;
3292 (*ppFuncDesc)->invkind = (typedata[4] >> 3) & 0xF;
3293 (*ppFuncDesc)->callconv = (typedata[4] >> 8) & 0xF;
3294 (*ppFuncDesc)->cParams = typedata[5];
3295 (*ppFuncDesc)->cParamsOpt = 0; /* FIXME: Unimplemented*/
3296 (*ppFuncDesc)->oVft = typedata[3] & 0xFFFF;
3297 if ((*ppFuncDesc)->oVft)
3298 --(*ppFuncDesc)->oVft;
3299 (*ppFuncDesc)->cScodes = 0; /* FIXME: Unimplemented*/
3300 hres = ctl2_decode_typedesc(This->typelib, typedata[1],
3301 &(*ppFuncDesc)->elemdescFunc.tdesc);
3302 if (FAILED(hres)) {
3303 heap_free(*ppFuncDesc);
3304 return hres;
3306 (*ppFuncDesc)->wFuncFlags = typedata[2];
3308 has_defaults = typedata[4] & 0x1000;
3309 tail = typedata[5] * (has_defaults ? 16 : 12);
3310 hdr_len = (ctl2_get_record_size(desc) - tail) / sizeof(int);
3312 if ((*ppFuncDesc)->cParams > 0) {
3313 (*ppFuncDesc)->lprgelemdescParam = heap_alloc_zero((*ppFuncDesc)->cParams * sizeof(ELEMDESC));
3314 if (!(*ppFuncDesc)->lprgelemdescParam) {
3315 heap_free(*ppFuncDesc);
3316 return E_OUTOFMEMORY;
3318 if (has_defaults) {
3319 num_defaults = (*ppFuncDesc)->cParams;
3321 for (i = 0; i < num_defaults; ++i) {
3322 if (typedata[hdr_len + i] != 0xFFFFFFFF) {
3323 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags |= PARAMFLAG_FHASDEFAULT;
3325 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex = heap_alloc(sizeof(PARAMDESCEX));
3326 if (!(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex) {
3327 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3328 return E_OUTOFMEMORY;
3331 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->cBytes = sizeof(PARAMDESCEX);
3332 VariantInit(&(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3333 hres = ctl2_decode_variant(This->typelib, typedata[hdr_len + i],
3334 &(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3335 if (FAILED(hres)) {
3336 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3337 return hres;
3343 for (i = 0; i < (*ppFuncDesc)->cParams; ++i) {
3344 hres = ctl2_decode_typedesc(This->typelib, typedata[hdr_len + num_defaults + (i * 3)],
3345 &((*ppFuncDesc)->lprgelemdescParam + i)->tdesc);
3346 if (FAILED(hres)) {
3347 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3348 return hres;
3350 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags = typedata[hdr_len + num_defaults + (i * 3) + 2];
3354 return S_OK;
3357 /******************************************************************************
3358 * ITypeInfo2_GetVarDesc {OLEAUT32}
3360 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3361 ITypeInfo2* iface,
3362 UINT index,
3363 VARDESC** ppVarDesc)
3365 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3366 return E_OUTOFMEMORY;
3369 /******************************************************************************
3370 * ITypeInfo2_GetNames {OLEAUT32}
3372 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3373 ITypeInfo2* iface,
3374 MEMBERID memid,
3375 BSTR* rgBstrNames,
3376 UINT cMaxNames,
3377 UINT* pcNames)
3379 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3380 return E_OUTOFMEMORY;
3383 /******************************************************************************
3384 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3386 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3387 ITypeInfo2* iface,
3388 UINT index,
3389 HREFTYPE* pRefType)
3391 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3392 MSFT_RefRecord *ref;
3393 int offset;
3395 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3397 if(!pRefType)
3398 return E_INVALIDARG;
3400 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3401 if(index == -1) {
3402 *pRefType = -2;
3403 return S_OK;
3406 if(This->typekind == TKIND_DISPATCH)
3407 return ITypeInfo2_GetRefTypeOfImplType(&This->dual->ITypeInfo2_iface,
3408 index, pRefType);
3411 if(index>=This->typeinfo->cImplTypes)
3412 return TYPE_E_ELEMENTNOTFOUND;
3414 if(This->typekind == TKIND_INTERFACE) {
3415 *pRefType = This->typeinfo->datatype1 + 2;
3416 return S_OK;
3419 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3420 if(offset == -1)
3421 return TYPE_E_ELEMENTNOTFOUND;
3423 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3424 *pRefType = ref->reftype;
3425 return S_OK;
3428 /******************************************************************************
3429 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3431 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3432 ITypeInfo2* iface,
3433 UINT index,
3434 INT* pImplTypeFlags)
3436 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3437 int offset;
3438 MSFT_RefRecord *ref;
3440 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3442 if(!pImplTypeFlags)
3443 return E_INVALIDARG;
3445 if(index >= This->typeinfo->cImplTypes)
3446 return TYPE_E_ELEMENTNOTFOUND;
3448 if(This->typekind != TKIND_COCLASS) {
3449 *pImplTypeFlags = 0;
3450 return S_OK;
3453 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3454 if(offset == -1)
3455 return TYPE_E_ELEMENTNOTFOUND;
3457 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3458 *pImplTypeFlags = ref->flags;
3459 return S_OK;
3462 /******************************************************************************
3463 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3465 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3466 ITypeInfo2* iface,
3467 LPOLESTR* rgszNames,
3468 UINT cNames,
3469 MEMBERID* pMemId)
3471 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3472 return E_OUTOFMEMORY;
3475 /******************************************************************************
3476 * ITypeInfo2_Invoke {OLEAUT32}
3478 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3479 ITypeInfo2* iface,
3480 PVOID pvInstance,
3481 MEMBERID memid,
3482 WORD wFlags,
3483 DISPPARAMS* pDispParams,
3484 VARIANT* pVarResult,
3485 EXCEPINFO* pExcepInfo,
3486 UINT* puArgErr)
3488 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3489 return E_OUTOFMEMORY;
3492 /******************************************************************************
3493 * ITypeInfo2_GetDocumentation {OLEAUT32}
3495 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3496 ITypeInfo2* iface,
3497 MEMBERID memid,
3498 BSTR* pBstrName,
3499 BSTR* pBstrDocString,
3500 DWORD* pdwHelpContext,
3501 BSTR* pBstrHelpFile)
3503 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3504 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3505 INT nameoffset, docstringoffset, helpcontext;
3507 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3509 if (memid == -1)
3511 nameoffset = This->typeinfo->NameOffset;
3512 docstringoffset = This->typeinfo->docstringoffs;
3513 helpcontext = This->typeinfo->helpcontext;
3514 status = S_OK;
3515 } else {
3516 CyclicList *iter;
3517 if (This->typedata) {
3518 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3519 if (iter->indice == memid) {
3520 if (iter->type == CyclicListFunc) {
3521 const int *typedata = iter->u.data;
3522 int size = ctl2_get_record_size(iter) - typedata[5]*(typedata[4]&0x1000?16:12);
3524 nameoffset = iter->name;
3525 /* FIXME implement this once SetFuncDocString is implemented */
3526 docstringoffset = -1;
3527 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3529 status = S_OK;
3530 } else {
3531 FIXME("Not implemented for variable members\n");
3534 break;
3540 if (!status) {
3541 WCHAR *string;
3542 if (pBstrName) {
3543 if (nameoffset == -1)
3544 *pBstrName = NULL;
3545 else {
3546 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3547 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3548 ctl2_decode_name((char*)&name->namelen, &string);
3549 *pBstrName = SysAllocString(string);
3550 if(!*pBstrName)
3551 return E_OUTOFMEMORY;
3555 if (pBstrDocString) {
3556 if (docstringoffset == -1)
3557 *pBstrDocString = NULL;
3558 else {
3559 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3560 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3561 ctl2_decode_name((char*)&name->namelen, &string);
3562 *pBstrDocString = SysAllocString(string);
3563 if(!*pBstrDocString) {
3564 if (pBstrName) SysFreeString(*pBstrName);
3565 return E_OUTOFMEMORY;
3570 if (pdwHelpContext) {
3571 *pdwHelpContext = helpcontext;
3574 if (pBstrHelpFile) {
3575 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->ITypeLib2_iface,
3576 -1, NULL, NULL, NULL, pBstrHelpFile);
3577 if (status) {
3578 if (pBstrName) SysFreeString(*pBstrName);
3579 if (pBstrDocString) SysFreeString(*pBstrDocString);
3584 return status;
3587 /******************************************************************************
3588 * ITypeInfo2_GetDllEntry {OLEAUT32}
3590 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3591 ITypeInfo2* iface,
3592 MEMBERID memid,
3593 INVOKEKIND invKind,
3594 BSTR* pBstrDllName,
3595 BSTR* pBstrName,
3596 WORD* pwOrdinal)
3598 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3599 return E_OUTOFMEMORY;
3602 /******************************************************************************
3603 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3605 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3606 ITypeInfo2* iface,
3607 HREFTYPE hRefType,
3608 ITypeInfo** ppTInfo)
3610 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3612 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3614 if(!ppTInfo)
3615 return E_INVALIDARG;
3617 if(hRefType==-2 && This->dual) {
3618 *ppTInfo = (ITypeInfo *)&This->dual->ITypeInfo2_iface;
3619 ITypeInfo_AddRef(*ppTInfo);
3620 return S_OK;
3623 if(hRefType&1) {
3624 ITypeLib *tl;
3625 MSFT_ImpInfo *impinfo;
3626 MSFT_ImpFile *impfile;
3627 MSFT_GuidEntry *guid;
3628 WCHAR *filename;
3629 HRESULT hres;
3631 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3632 return E_FAIL;
3634 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3635 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3636 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3638 ctl2_decode_string((unsigned char*)impfile->filename, &filename);
3640 hres = LoadTypeLib(filename, &tl);
3641 if(FAILED(hres))
3642 return hres;
3644 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3646 ITypeLib_Release(tl);
3647 return hres;
3648 } else {
3649 ICreateTypeInfo2Impl *iter;
3650 int i = 0;
3652 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3653 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3654 *ppTInfo = (ITypeInfo *)&iter->ITypeInfo2_iface;
3656 ITypeInfo_AddRef(*ppTInfo);
3657 return S_OK;
3659 i++;
3663 return E_FAIL;
3666 /******************************************************************************
3667 * ITypeInfo2_AddressOfMember {OLEAUT32}
3669 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3670 ITypeInfo2* iface,
3671 MEMBERID memid,
3672 INVOKEKIND invKind,
3673 PVOID* ppv)
3675 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3676 return E_OUTOFMEMORY;
3679 /******************************************************************************
3680 * ITypeInfo2_CreateInstance {OLEAUT32}
3682 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3683 ITypeInfo2* iface,
3684 IUnknown* pUnkOuter,
3685 REFIID riid,
3686 PVOID* ppvObj)
3688 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3689 return E_OUTOFMEMORY;
3692 /******************************************************************************
3693 * ITypeInfo2_GetMops {OLEAUT32}
3695 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3696 ITypeInfo2* iface,
3697 MEMBERID memid,
3698 BSTR* pBstrMops)
3700 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3701 return E_OUTOFMEMORY;
3704 /******************************************************************************
3705 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3707 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3708 ITypeInfo2* iface,
3709 ITypeLib** ppTLib,
3710 UINT* pIndex)
3712 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3714 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3716 *ppTLib = (ITypeLib *)&This->typelib->ITypeLib2_iface;
3717 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3718 *pIndex = This->typeinfo->typekind >> 16;
3720 return S_OK;
3723 static void release_typedesc(TYPEDESC *tdesc)
3725 while (tdesc) {
3726 TYPEDESC *next;
3727 if (tdesc->vt == VT_USERDEFINED)
3728 next = NULL;
3729 else
3730 next = tdesc->u.lptdesc;
3731 heap_free(tdesc);
3732 tdesc = next;
3736 /******************************************************************************
3737 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3739 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3740 ITypeInfo2* iface,
3741 TYPEATTR* pTypeAttr)
3743 TRACE("(%p,%p)\n", iface, pTypeAttr);
3745 if (pTypeAttr->tdescAlias.vt != VT_USERDEFINED)
3746 release_typedesc(pTypeAttr->tdescAlias.u.lptdesc);
3748 heap_free(pTypeAttr);
3751 /******************************************************************************
3752 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3754 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3755 ITypeInfo2* iface,
3756 FUNCDESC* pFuncDesc)
3758 int i;
3760 TRACE("(%p,%p)\n", iface, pFuncDesc);
3762 heap_free(pFuncDesc->lprgscode);
3764 if (pFuncDesc->lprgelemdescParam) {
3765 for (i = 0; i < pFuncDesc->cParams; ++i) {
3766 if (pFuncDesc->lprgelemdescParam[i].tdesc.vt != VT_USERDEFINED)
3767 release_typedesc(pFuncDesc->lprgelemdescParam[i].tdesc.u.lptdesc);
3769 if (pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex)
3771 VariantClear(&pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3772 heap_free(pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex);
3775 heap_free(pFuncDesc->lprgelemdescParam);
3778 heap_free(pFuncDesc->elemdescFunc.u.paramdesc.pparamdescex);
3780 if (pFuncDesc->elemdescFunc.tdesc.vt != VT_USERDEFINED)
3781 release_typedesc(pFuncDesc->elemdescFunc.tdesc.u.lptdesc);
3783 heap_free(pFuncDesc);
3786 /******************************************************************************
3787 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3789 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3790 ITypeInfo2* iface,
3791 VARDESC* pVarDesc)
3793 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3796 /******************************************************************************
3797 * ITypeInfo2_GetTypeKind {OLEAUT32}
3799 * Get the TYPEKIND value for a TypeInfo.
3801 * RETURNS
3803 * Success: S_OK.
3804 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3806 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3807 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3808 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3810 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3811 return E_OUTOFMEMORY;
3814 /******************************************************************************
3815 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3817 * Get the Type Flags for a TypeInfo.
3819 * RETURNS
3821 * Success: S_OK.
3822 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3824 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3825 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3826 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3828 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3829 return E_OUTOFMEMORY;
3832 /******************************************************************************
3833 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3835 * Gets the index of a function given its member id.
3837 * RETURNS
3839 * Success: S_OK.
3840 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3842 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3843 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3844 MEMBERID memid, /* [I] The member id for the function. */
3845 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3846 UINT* pFuncIndex) /* [O] The index of the function. */
3848 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3849 return E_OUTOFMEMORY;
3852 /******************************************************************************
3853 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3855 * Gets the index of a variable given its member id.
3857 * RETURNS
3859 * Success: S_OK.
3860 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3862 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3863 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3864 MEMBERID memid, /* [I] The member id for the variable. */
3865 UINT* pVarIndex) /* [O] The index of the variable. */
3867 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3868 return E_OUTOFMEMORY;
3871 /******************************************************************************
3872 * ITypeInfo2_GetCustData {OLEAUT32}
3874 * Gets a custom data element from a TypeInfo.
3876 * RETURNS
3878 * Success: S_OK.
3879 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3881 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3882 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3883 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3884 VARIANT* pVarVal) /* [O] The custom data. */
3886 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3887 MSFT_CDGuid *cdentry;
3888 int offset;
3890 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
3892 if (!guid || !pVarVal)
3893 return E_INVALIDARG;
3895 VariantClear(pVarVal);
3897 offset = ctl2_find_custdata(This->typelib, guid, This->typeinfo->oCustData);
3898 if (offset == -1)
3899 return S_OK;
3901 cdentry = (MSFT_CDGuid *)&This->typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
3902 return ctl2_decode_variant(This->typelib, cdentry->DataOffset, pVarVal);
3905 /******************************************************************************
3906 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3908 * Gets a custom data element from a function.
3910 * RETURNS
3912 * Success: S_OK.
3913 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3915 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3916 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3917 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3918 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3919 VARIANT* pVarVal) /* [O] The custom data. */
3921 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3922 return E_OUTOFMEMORY;
3925 /******************************************************************************
3926 * ITypeInfo2_GetParamCustData {OLEAUT32}
3928 * Gets a custom data element from a parameter.
3930 * RETURNS
3932 * Success: S_OK.
3933 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3935 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3936 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3937 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3938 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3939 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3940 VARIANT* pVarVal) /* [O] The custom data. */
3942 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3943 return E_OUTOFMEMORY;
3946 /******************************************************************************
3947 * ITypeInfo2_GetVarCustData {OLEAUT32}
3949 * Gets a custom data element from a variable.
3951 * RETURNS
3953 * Success: S_OK.
3954 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3956 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3957 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3958 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3959 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3960 VARIANT* pVarVal) /* [O] The custom data. */
3962 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3963 return E_OUTOFMEMORY;
3966 /******************************************************************************
3967 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3969 * Gets a custom data element from an implemented type of a TypeInfo.
3971 * RETURNS
3973 * Success: S_OK.
3974 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3976 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3977 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3978 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3979 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3980 VARIANT* pVarVal) /* [O] The custom data. */
3982 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3983 return E_OUTOFMEMORY;
3986 /******************************************************************************
3987 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3989 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3991 * RETURNS
3993 * Success: S_OK.
3994 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3996 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3997 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
3998 MEMBERID memid, /* [I] The member id (why?). */
3999 LCID lcid, /* [I] The locale (why?). */
4000 BSTR* pbstrHelpString, /* [O] The help string. */
4001 DWORD* pdwHelpStringContext, /* [O] The help string context. */
4002 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
4004 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
4005 return E_OUTOFMEMORY;
4008 /******************************************************************************
4009 * ITypeInfo2_GetAllCustData {OLEAUT32}
4011 * Gets all of the custom data associated with a TypeInfo.
4013 * RETURNS
4015 * Success: S_OK.
4016 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4018 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
4019 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4020 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4022 FIXME("(%p,%p), stub!\n", iface, pCustData);
4023 return E_OUTOFMEMORY;
4026 /******************************************************************************
4027 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
4029 * Gets all of the custom data associated with a function.
4031 * RETURNS
4033 * Success: S_OK.
4034 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4036 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
4037 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4038 UINT index, /* [I] The index of the function 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_GetAllParamCustData {OLEAUT32}
4048 * Gets all of the custom data associated with a parameter.
4050 * RETURNS
4052 * Success: S_OK.
4053 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4055 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
4056 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4057 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
4058 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
4059 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4061 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
4062 return E_OUTOFMEMORY;
4065 /******************************************************************************
4066 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
4068 * Gets all of the custom data associated with a variable.
4070 * RETURNS
4072 * Success: S_OK.
4073 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4075 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
4076 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4077 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
4078 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4080 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4081 return E_OUTOFMEMORY;
4084 /******************************************************************************
4085 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
4087 * Gets all of the custom data associated with an implemented type.
4089 * RETURNS
4091 * Success: S_OK.
4092 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4094 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
4095 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4096 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
4097 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4099 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4100 return E_OUTOFMEMORY;
4104 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
4106 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
4109 ICreateTypeInfo2_fnQueryInterface,
4110 ICreateTypeInfo2_fnAddRef,
4111 ICreateTypeInfo2_fnRelease,
4113 ICreateTypeInfo2_fnSetGuid,
4114 ICreateTypeInfo2_fnSetTypeFlags,
4115 ICreateTypeInfo2_fnSetDocString,
4116 ICreateTypeInfo2_fnSetHelpContext,
4117 ICreateTypeInfo2_fnSetVersion,
4118 ICreateTypeInfo2_fnAddRefTypeInfo,
4119 ICreateTypeInfo2_fnAddFuncDesc,
4120 ICreateTypeInfo2_fnAddImplType,
4121 ICreateTypeInfo2_fnSetImplTypeFlags,
4122 ICreateTypeInfo2_fnSetAlignment,
4123 ICreateTypeInfo2_fnSetSchema,
4124 ICreateTypeInfo2_fnAddVarDesc,
4125 ICreateTypeInfo2_fnSetFuncAndParamNames,
4126 ICreateTypeInfo2_fnSetVarName,
4127 ICreateTypeInfo2_fnSetTypeDescAlias,
4128 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
4129 ICreateTypeInfo2_fnSetFuncDocString,
4130 ICreateTypeInfo2_fnSetVarDocString,
4131 ICreateTypeInfo2_fnSetFuncHelpContext,
4132 ICreateTypeInfo2_fnSetVarHelpContext,
4133 ICreateTypeInfo2_fnSetMops,
4134 ICreateTypeInfo2_fnSetTypeIdldesc,
4135 ICreateTypeInfo2_fnLayOut,
4137 ICreateTypeInfo2_fnDeleteFuncDesc,
4138 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
4139 ICreateTypeInfo2_fnDeleteVarDesc,
4140 ICreateTypeInfo2_fnDeleteVarDescByMemId,
4141 ICreateTypeInfo2_fnDeleteImplType,
4142 ICreateTypeInfo2_fnSetCustData,
4143 ICreateTypeInfo2_fnSetFuncCustData,
4144 ICreateTypeInfo2_fnSetParamCustData,
4145 ICreateTypeInfo2_fnSetVarCustData,
4146 ICreateTypeInfo2_fnSetImplTypeCustData,
4147 ICreateTypeInfo2_fnSetHelpStringContext,
4148 ICreateTypeInfo2_fnSetFuncHelpStringContext,
4149 ICreateTypeInfo2_fnSetVarHelpStringContext,
4150 ICreateTypeInfo2_fnInvalidate,
4151 ICreateTypeInfo2_fnSetName
4154 static const ITypeInfo2Vtbl typeinfo2vt =
4157 ITypeInfo2_fnQueryInterface,
4158 ITypeInfo2_fnAddRef,
4159 ITypeInfo2_fnRelease,
4161 ITypeInfo2_fnGetTypeAttr,
4162 ITypeInfo2_fnGetTypeComp,
4163 ITypeInfo2_fnGetFuncDesc,
4164 ITypeInfo2_fnGetVarDesc,
4165 ITypeInfo2_fnGetNames,
4166 ITypeInfo2_fnGetRefTypeOfImplType,
4167 ITypeInfo2_fnGetImplTypeFlags,
4168 ITypeInfo2_fnGetIDsOfNames,
4169 ITypeInfo2_fnInvoke,
4170 ITypeInfo2_fnGetDocumentation,
4171 ITypeInfo2_fnGetDllEntry,
4172 ITypeInfo2_fnGetRefTypeInfo,
4173 ITypeInfo2_fnAddressOfMember,
4174 ITypeInfo2_fnCreateInstance,
4175 ITypeInfo2_fnGetMops,
4176 ITypeInfo2_fnGetContainingTypeLib,
4177 ITypeInfo2_fnReleaseTypeAttr,
4178 ITypeInfo2_fnReleaseFuncDesc,
4179 ITypeInfo2_fnReleaseVarDesc,
4181 ITypeInfo2_fnGetTypeKind,
4182 ITypeInfo2_fnGetTypeFlags,
4183 ITypeInfo2_fnGetFuncIndexOfMemId,
4184 ITypeInfo2_fnGetVarIndexOfMemId,
4185 ITypeInfo2_fnGetCustData,
4186 ITypeInfo2_fnGetFuncCustData,
4187 ITypeInfo2_fnGetParamCustData,
4188 ITypeInfo2_fnGetVarCustData,
4189 ITypeInfo2_fnGetImplTypeCustData,
4190 ITypeInfo2_fnGetDocumentation2,
4191 ITypeInfo2_fnGetAllCustData,
4192 ITypeInfo2_fnGetAllFuncCustData,
4193 ITypeInfo2_fnGetAllParamCustData,
4194 ITypeInfo2_fnGetAllVarCustData,
4195 ITypeInfo2_fnGetAllImplTypeCustData,
4198 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
4200 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
4202 int nameoffset;
4203 int typeinfo_offset;
4204 MSFT_TypeInfoBase *typeinfo;
4206 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
4208 pCreateTypeInfo2Impl = heap_alloc_zero(sizeof(ICreateTypeInfo2Impl));
4209 if (!pCreateTypeInfo2Impl) return NULL;
4211 pCreateTypeInfo2Impl->ICreateTypeInfo2_iface.lpVtbl = &ctypeinfo2vt;
4212 pCreateTypeInfo2Impl->ITypeInfo2_iface.lpVtbl = &typeinfo2vt;
4213 pCreateTypeInfo2Impl->ref = 1;
4215 pCreateTypeInfo2Impl->typelib = typelib;
4216 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
4218 nameoffset = ctl2_alloc_name(typelib, szName);
4219 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
4220 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
4222 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
4223 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
4225 pCreateTypeInfo2Impl->typeinfo = typeinfo;
4227 pCreateTypeInfo2Impl->typekind = tkind;
4228 typeinfo->typekind |= tkind | 0x20;
4229 ICreateTypeInfo2_SetAlignment(&pCreateTypeInfo2Impl->ICreateTypeInfo2_iface, 4);
4231 switch (tkind) {
4232 case TKIND_ENUM:
4233 case TKIND_INTERFACE:
4234 case TKIND_DISPATCH:
4235 case TKIND_COCLASS:
4236 typeinfo->size = 4;
4237 break;
4239 case TKIND_RECORD:
4240 case TKIND_UNION:
4241 typeinfo->size = 0;
4242 break;
4244 case TKIND_MODULE:
4245 typeinfo->size = 2;
4246 break;
4248 case TKIND_ALIAS:
4249 typeinfo->size = -0x75;
4250 break;
4252 default:
4253 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
4254 typeinfo->size = 0xdeadbeef;
4255 break;
4258 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
4259 typelib->last_typeinfo = pCreateTypeInfo2Impl;
4260 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
4262 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
4264 return &pCreateTypeInfo2Impl->ICreateTypeInfo2_iface;
4268 /*================== ICreateTypeLib2 Implementation ===================================*/
4270 /******************************************************************************
4271 * ICreateTypeLib2_QueryInterface {OLEAUT32}
4273 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
4274 ICreateTypeLib2 * iface,
4275 REFIID riid,
4276 VOID **ppvObject)
4278 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4280 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4282 *ppvObject=NULL;
4283 if(IsEqualIID(riid, &IID_IUnknown) ||
4284 IsEqualIID(riid,&IID_ICreateTypeLib)||
4285 IsEqualIID(riid,&IID_ICreateTypeLib2))
4287 *ppvObject = This;
4288 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4289 IsEqualIID(riid, &IID_ITypeLib2)) {
4290 *ppvObject = &This->ITypeLib2_iface;
4293 if(*ppvObject)
4295 ICreateTypeLib2_AddRef(iface);
4296 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4297 return S_OK;
4299 TRACE("-- Interface: E_NOINTERFACE\n");
4300 return E_NOINTERFACE;
4303 /******************************************************************************
4304 * ICreateTypeLib2_AddRef {OLEAUT32}
4306 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4308 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4309 ULONG ref = InterlockedIncrement(&This->ref);
4311 TRACE("(%p)->(%u)\n", This, ref);
4313 return ref;
4316 /******************************************************************************
4317 * ICreateTypeLib2_Release {OLEAUT32}
4319 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4321 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4322 ULONG ref = InterlockedDecrement(&This->ref);
4324 TRACE("(%p)->(%u)\n", This, ref);
4326 if (!ref) {
4327 int i;
4329 for (i = 0; i < MSFT_SEG_MAX; i++) {
4330 heap_free(This->typelib_segment_data[i]);
4331 This->typelib_segment_data[i] = NULL;
4334 SysFreeString(This->filename);
4335 This->filename = NULL;
4337 while (This->typeinfos) {
4338 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4339 This->typeinfos = typeinfo->next_typeinfo;
4340 if(typeinfo->typedata) {
4341 CyclicList *iter, *rem;
4343 rem = typeinfo->typedata->next;
4344 typeinfo->typedata->next = NULL;
4345 iter = rem->next;
4346 heap_free(rem);
4348 while(iter) {
4349 rem = iter;
4350 iter = iter->next;
4351 heap_free(rem->u.data);
4352 heap_free(rem);
4356 heap_free(typeinfo->dual);
4357 heap_free(typeinfo);
4360 heap_free(This);
4363 return ref;
4367 /******************************************************************************
4368 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4370 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4371 ICreateTypeLib2 * iface,
4372 LPOLESTR szName,
4373 TYPEKIND tkind,
4374 ICreateTypeInfo **tinfo)
4376 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4377 char *name;
4379 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, tinfo);
4381 if (!szName || !tinfo) return E_INVALIDARG;
4383 ctl2_encode_name(This, szName, &name);
4384 if(ctl2_find_name(This, name) != -1)
4385 return TYPE_E_NAMECONFLICT;
4387 *tinfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4388 if (!*tinfo) return E_OUTOFMEMORY;
4390 return S_OK;
4393 /******************************************************************************
4394 * ICreateTypeLib2_SetName {OLEAUT32}
4396 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4397 ICreateTypeLib2 * iface,
4398 LPOLESTR szName)
4400 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4401 int offset;
4403 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4405 offset = ctl2_alloc_name(This, szName);
4406 if (offset == -1) return E_OUTOFMEMORY;
4407 This->typelib_header.NameOffset = offset;
4408 return S_OK;
4411 /******************************************************************************
4412 * ICreateTypeLib2_SetVersion {OLEAUT32}
4414 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4416 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4418 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4420 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4421 return S_OK;
4424 /******************************************************************************
4425 * ICreateTypeLib2_SetGuid {OLEAUT32}
4427 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4429 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4430 MSFT_GuidEntry guidentry;
4431 int offset;
4433 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4435 guidentry.guid = *guid;
4436 guidentry.hreftype = -2;
4437 guidentry.next_hash = -1;
4439 offset = ctl2_alloc_guid(This, &guidentry);
4441 if (offset == -1) return E_OUTOFMEMORY;
4443 This->typelib_header.posguid = offset;
4445 return S_OK;
4448 /******************************************************************************
4449 * ICreateTypeLib2_SetDocString {OLEAUT32}
4451 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4453 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4454 int offset;
4456 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4457 if (!szDoc)
4458 return E_INVALIDARG;
4460 offset = ctl2_alloc_string(This, szDoc);
4461 if (offset == -1) return E_OUTOFMEMORY;
4462 This->typelib_header.helpstring = offset;
4463 return S_OK;
4466 /******************************************************************************
4467 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4469 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4471 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4472 int offset;
4474 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4476 offset = ctl2_alloc_string(This, szHelpFileName);
4477 if (offset == -1) return E_OUTOFMEMORY;
4478 This->typelib_header.helpfile = offset;
4479 This->typelib_header.varflags |= 0x10;
4480 return S_OK;
4483 /******************************************************************************
4484 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4486 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4488 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4490 TRACE("(%p,%d)\n", iface, dwHelpContext);
4491 This->typelib_header.helpcontext = dwHelpContext;
4492 return S_OK;
4495 /******************************************************************************
4496 * ICreateTypeLib2_SetLcid {OLEAUT32}
4498 * Sets both the lcid and lcid2 members in the header to lcid.
4500 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4501 * is set to US English while the second one is set to 0.
4503 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4505 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4507 TRACE("(%p,%d)\n", iface, lcid);
4509 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4511 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4513 return S_OK;
4516 /******************************************************************************
4517 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4519 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4521 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4523 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4525 This->typelib_header.flags = uLibFlags;
4527 return S_OK;
4530 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4532 DWORD dwWritten;
4533 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4534 CloseHandle(hFile);
4535 return 0;
4537 return -1;
4540 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4542 DWORD dwWritten;
4543 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4544 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4545 CloseHandle(hFile);
4546 return 0;
4549 return -1;
4552 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4554 ICreateTypeInfo2Impl *typeinfo;
4555 HRESULT hres;
4557 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4558 typeinfo->typeinfo->memoffset = filesize;
4560 hres = ICreateTypeInfo2_fnLayOut(&typeinfo->ICreateTypeInfo2_iface);
4561 if(FAILED(hres))
4562 return hres;
4564 if (typeinfo->typedata)
4565 filesize += typeinfo->typedata->next->u.val
4566 + cti2_get_var_count(typeinfo->typeinfo) * 12
4567 + cti2_get_func_count(typeinfo->typeinfo) * 12 + 4;
4570 return S_OK;
4573 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4575 if (This->typelib_segdir[segment].length) {
4576 This->typelib_segdir[segment].offset = filepos;
4577 } else {
4578 This->typelib_segdir[segment].offset = -1;
4581 return This->typelib_segdir[segment].length;
4584 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4586 ICreateTypeInfo2Impl *typeinfo;
4588 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4589 CyclicList *iter;
4590 int offset = 0;
4592 if (!typeinfo->typedata) continue;
4594 iter = typeinfo->typedata->next;
4595 ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4596 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4597 ctl2_write_chunk(hFile, iter->u.data, ctl2_get_record_size(iter));
4599 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4600 ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4602 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4603 ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4605 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4606 ctl2_write_chunk(hFile, &offset, sizeof(int));
4607 offset += ctl2_get_record_size(iter);
4612 /******************************************************************************
4613 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4615 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4617 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4618 int retval;
4619 int filepos;
4620 HANDLE hFile;
4621 HRESULT hres;
4623 TRACE("(%p)\n", iface);
4625 retval = TYPE_E_IOERROR;
4627 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4628 if (hFile == INVALID_HANDLE_VALUE) return retval;
4630 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4631 filepos += This->typelib_header.nrtypeinfos * 4;
4633 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4634 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4635 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4636 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4637 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4638 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4639 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4640 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4641 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4642 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4643 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4644 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4645 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4647 hres = ctl2_finalize_typeinfos(This, filepos);
4648 if(FAILED(hres)) {
4649 CloseHandle(hFile);
4650 return hres;
4653 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4654 if (This->typelib_header.varflags & HELPDLLFLAG)
4655 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4656 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4657 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4658 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4659 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4660 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4661 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4662 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4663 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4664 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4665 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4666 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4667 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4668 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4669 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4670 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4672 ctl2_write_typeinfos(This, hFile);
4674 if (!CloseHandle(hFile)) return retval;
4676 return S_OK;
4680 /******************************************************************************
4681 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4683 * Deletes a named TypeInfo from a type library.
4685 * RETURNS
4687 * Success: S_OK
4688 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4690 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4691 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4692 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4694 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4695 return E_OUTOFMEMORY;
4698 /******************************************************************************
4699 * ICreateTypeLib2_SetCustData {OLEAUT32}
4701 * Sets custom data for a type library.
4703 * RETURNS
4705 * Success: S_OK
4706 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4708 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4709 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4710 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4711 VARIANT *pVarVal) /* [I] The custom data itself. */
4713 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4715 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4717 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4720 /******************************************************************************
4721 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4723 * Sets a context number for the library help string.
4725 * PARAMS
4726 * iface [I] The type library to set the help string context for.
4727 * dwContext [I] The help string context.
4729 * RETURNS
4730 * Success: S_OK
4731 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4733 static
4734 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4735 ULONG dwContext)
4737 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4739 TRACE("(%p,%d)\n", iface, dwContext);
4741 This->typelib_header.helpstringcontext = dwContext;
4742 return S_OK;
4745 /******************************************************************************
4746 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4748 * Set the DLL used to look up localized help strings.
4750 * PARAMS
4751 * iface [I] The type library to set the help DLL for.
4752 * szDllName [I] The name of the help DLL.
4754 * RETURNS
4755 * Success: S_OK
4756 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4758 static
4759 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4760 LPOLESTR szDllName)
4762 ICreateTypeLib2Impl *This = impl_from_ICreateTypeLib2(iface);
4763 int offset;
4765 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4766 if (!szDllName)
4767 return E_INVALIDARG;
4769 offset = ctl2_alloc_string(This, szDllName);
4770 if (offset == -1)
4771 return E_OUTOFMEMORY;
4772 This->typelib_header.varflags |= HELPDLLFLAG;
4773 This->helpStringDll = offset;
4774 return S_OK;
4777 /*================== ITypeLib2 Implementation ===================================*/
4779 /******************************************************************************
4780 * ITypeLib2_QueryInterface {OLEAUT32}
4782 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4784 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4786 return ICreateTypeLib2_QueryInterface(&This->ICreateTypeLib2_iface, riid, ppv);
4789 /******************************************************************************
4790 * ITypeLib2_AddRef {OLEAUT32}
4792 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4794 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4796 return ICreateTypeLib2_AddRef(&This->ICreateTypeLib2_iface);
4799 /******************************************************************************
4800 * ITypeLib2_Release {OLEAUT32}
4802 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4804 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4806 return ICreateTypeLib2_Release(&This->ICreateTypeLib2_iface);
4809 /******************************************************************************
4810 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4812 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4813 ITypeLib2 * iface)
4815 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4817 TRACE("(%p)\n", iface);
4819 return This->typelib_header.nrtypeinfos;
4822 /******************************************************************************
4823 * ITypeLib2_GetTypeInfo {OLEAUT32}
4825 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4826 ITypeLib2 * iface,
4827 UINT index,
4828 ITypeInfo** ppTInfo)
4830 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4832 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4834 if (!ppTInfo) return E_INVALIDARG;
4836 if (index >= This->typelib_header.nrtypeinfos) {
4837 return TYPE_E_ELEMENTNOTFOUND;
4840 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4843 /******************************************************************************
4844 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4846 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4847 ITypeLib2 * iface,
4848 UINT index,
4849 TYPEKIND* kind)
4851 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4853 TRACE("(%p,%d,%p)\n", iface, index, kind);
4855 if (!kind) return E_INVALIDARG;
4857 if (index >= This->typelib_header.nrtypeinfos) {
4858 return TYPE_E_ELEMENTNOTFOUND;
4861 *kind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 0xF;
4863 return S_OK;
4866 /******************************************************************************
4867 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4869 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4870 ITypeLib2 * iface,
4871 REFGUID guid,
4872 ITypeInfo** ppTinfo)
4874 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4876 int guidoffset;
4877 int typeinfo;
4879 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4881 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4882 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4884 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4885 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4887 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4890 /******************************************************************************
4891 * ITypeLib2_GetLibAttr {OLEAUT32}
4893 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4894 ITypeLib2 * iface,
4895 TLIBATTR** ppTLibAttr)
4897 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4899 TRACE("(%p,%p)\n", This, ppTLibAttr);
4901 if(!ppTLibAttr)
4902 return E_INVALIDARG;
4904 *ppTLibAttr = heap_alloc_zero(sizeof(TLIBATTR));
4905 if(!*ppTLibAttr)
4906 return E_OUTOFMEMORY;
4908 if(This->typelib_header.posguid != -1) {
4909 MSFT_GuidEntry *guid;
4911 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4912 (*ppTLibAttr)->guid = guid->guid;
4915 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4916 (*ppTLibAttr)->syskind = ctl2_get_syskind(This);
4917 (*ppTLibAttr)->wMajorVerNum = LOWORD(This->typelib_header.version);
4918 (*ppTLibAttr)->wMinorVerNum = HIWORD(This->typelib_header.version);
4919 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4920 return S_OK;
4923 /******************************************************************************
4924 * ITypeLib2_GetTypeComp {OLEAUT32}
4926 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4927 ITypeLib2 * iface,
4928 ITypeComp** ppTComp)
4930 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4932 FIXME("(%p,%p), stub!\n", This, ppTComp);
4934 return E_OUTOFMEMORY;
4937 /******************************************************************************
4938 * ITypeLib2_GetDocumentation {OLEAUT32}
4940 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4941 ITypeLib2 * iface,
4942 INT index,
4943 BSTR* pBstrName,
4944 BSTR* pBstrDocString,
4945 DWORD* pdwHelpContext,
4946 BSTR* pBstrHelpFile)
4948 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4949 WCHAR *string;
4951 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4953 if(index != -1) {
4954 ICreateTypeInfo2Impl *iter;
4956 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4957 index--;
4959 if(!iter)
4960 return TYPE_E_ELEMENTNOTFOUND;
4962 return ITypeInfo2_GetDocumentation(&iter->ITypeInfo2_iface,
4963 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4966 if(pBstrName) {
4967 if(This->typelib_header.NameOffset == -1)
4968 *pBstrName = NULL;
4969 else {
4970 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4971 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4973 ctl2_decode_name((char*)&name->namelen, &string);
4975 *pBstrName = SysAllocString(string);
4976 if(!*pBstrName)
4977 return E_OUTOFMEMORY;
4981 if(pBstrDocString) {
4982 if(This->typelib_header.helpstring == -1)
4983 *pBstrDocString = NULL;
4984 else {
4985 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4987 *pBstrDocString = SysAllocString(string);
4988 if(!*pBstrDocString) {
4989 if(pBstrName) SysFreeString(*pBstrName);
4990 return E_OUTOFMEMORY;
4995 if(pdwHelpContext)
4996 *pdwHelpContext = This->typelib_header.helpcontext;
4998 if(pBstrHelpFile) {
4999 if(This->typelib_header.helpfile == -1)
5000 *pBstrHelpFile = NULL;
5001 else {
5002 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
5004 *pBstrHelpFile = SysAllocString(string);
5005 if(!*pBstrHelpFile) {
5006 if(pBstrName) SysFreeString(*pBstrName);
5007 if(pBstrDocString) SysFreeString(*pBstrDocString);
5008 return E_OUTOFMEMORY;
5013 return S_OK;
5016 /******************************************************************************
5017 * ITypeLib2_IsName {OLEAUT32}
5019 static HRESULT WINAPI ITypeLib2_fnIsName(
5020 ITypeLib2 * iface,
5021 LPOLESTR szNameBuf,
5022 ULONG lHashVal,
5023 BOOL* pfName)
5025 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5027 char *encoded_name;
5028 int nameoffset;
5029 MSFT_NameIntro *nameintro;
5031 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
5033 ctl2_encode_name(This, szNameBuf, &encoded_name);
5034 nameoffset = ctl2_find_name(This, encoded_name);
5036 *pfName = 0;
5038 if (nameoffset == -1) return S_OK;
5040 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
5041 if (nameintro->hreftype == -1) return S_OK;
5043 *pfName = 1;
5045 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
5047 return S_OK;
5050 /******************************************************************************
5051 * ITypeLib2_FindName {OLEAUT32}
5053 static HRESULT WINAPI ITypeLib2_fnFindName(
5054 ITypeLib2 * iface,
5055 LPOLESTR szNameBuf,
5056 ULONG lHashVal,
5057 ITypeInfo** ppTInfo,
5058 MEMBERID* rgMemId,
5059 USHORT* pcFound)
5061 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5063 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
5065 return E_OUTOFMEMORY;
5068 /******************************************************************************
5069 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
5071 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
5072 ITypeLib2 * iface,
5073 TLIBATTR* attr)
5075 TRACE("(%p,%p)\n", iface, attr);
5076 heap_free(attr);
5079 /******************************************************************************
5080 * ICreateTypeLib2_GetCustData {OLEAUT32}
5082 * Retrieves a custom data value stored on a type library.
5084 * RETURNS
5086 * Success: S_OK
5087 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5089 static HRESULT WINAPI ITypeLib2_fnGetCustData(
5090 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5091 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
5092 VARIANT* pVarVal) /* [O] The custom data. */
5094 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5096 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
5098 return E_OUTOFMEMORY;
5101 /******************************************************************************
5102 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
5104 * Retrieves some statistics about names in a type library, supposedly for
5105 * hash table optimization purposes.
5107 * RETURNS
5109 * Success: S_OK
5110 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5112 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
5113 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
5114 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
5115 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
5117 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5119 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
5121 return E_OUTOFMEMORY;
5124 /******************************************************************************
5125 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
5127 * Obtain locale-aware help string information.
5129 * RETURNS
5131 * Success: S_OK
5132 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
5134 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
5135 ITypeLib2 * iface,
5136 INT index,
5137 LCID lcid,
5138 BSTR* pbstrHelpString,
5139 DWORD* pdwHelpStringContext,
5140 BSTR* pbstrHelpStringDll)
5142 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5144 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
5146 return E_OUTOFMEMORY;
5149 /******************************************************************************
5150 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
5152 * Retrieve all of the custom data for a type library.
5154 * RETURNS
5156 * Success: S_OK
5157 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5159 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
5160 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5161 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
5163 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5165 FIXME("(%p,%p), stub!\n", This, pCustData);
5167 return E_OUTOFMEMORY;
5171 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
5173 static const ICreateTypeLib2Vtbl ctypelib2vt =
5176 ICreateTypeLib2_fnQueryInterface,
5177 ICreateTypeLib2_fnAddRef,
5178 ICreateTypeLib2_fnRelease,
5180 ICreateTypeLib2_fnCreateTypeInfo,
5181 ICreateTypeLib2_fnSetName,
5182 ICreateTypeLib2_fnSetVersion,
5183 ICreateTypeLib2_fnSetGuid,
5184 ICreateTypeLib2_fnSetDocString,
5185 ICreateTypeLib2_fnSetHelpFileName,
5186 ICreateTypeLib2_fnSetHelpContext,
5187 ICreateTypeLib2_fnSetLcid,
5188 ICreateTypeLib2_fnSetLibFlags,
5189 ICreateTypeLib2_fnSaveAllChanges,
5191 ICreateTypeLib2_fnDeleteTypeInfo,
5192 ICreateTypeLib2_fnSetCustData,
5193 ICreateTypeLib2_fnSetHelpStringContext,
5194 ICreateTypeLib2_fnSetHelpStringDll
5197 static const ITypeLib2Vtbl typelib2vt =
5200 ITypeLib2_fnQueryInterface,
5201 ITypeLib2_fnAddRef,
5202 ITypeLib2_fnRelease,
5204 ITypeLib2_fnGetTypeInfoCount,
5205 ITypeLib2_fnGetTypeInfo,
5206 ITypeLib2_fnGetTypeInfoType,
5207 ITypeLib2_fnGetTypeInfoOfGuid,
5208 ITypeLib2_fnGetLibAttr,
5209 ITypeLib2_fnGetTypeComp,
5210 ITypeLib2_fnGetDocumentation,
5211 ITypeLib2_fnIsName,
5212 ITypeLib2_fnFindName,
5213 ITypeLib2_fnReleaseTLibAttr,
5215 ITypeLib2_fnGetCustData,
5216 ITypeLib2_fnGetLibStatistics,
5217 ITypeLib2_fnGetDocumentation2,
5218 ITypeLib2_fnGetAllCustData,
5221 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR filename)
5223 ICreateTypeLib2Impl *create_tlib2;
5224 int failed = 0;
5226 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(filename));
5228 create_tlib2 = heap_alloc_zero(sizeof(ICreateTypeLib2Impl));
5229 if (!create_tlib2) return NULL;
5231 create_tlib2->filename = SysAllocString(filename);
5232 if (!create_tlib2->filename) {
5233 heap_free(create_tlib2);
5234 return NULL;
5237 ctl2_init_header(create_tlib2);
5238 ctl2_init_segdir(create_tlib2);
5240 create_tlib2->typelib_header.varflags |= syskind;
5243 * The following two calls return an offset or -1 if out of memory. We
5244 * specifically need an offset of 0, however, so...
5246 if (ctl2_alloc_segment(create_tlib2, MSFT_SEG_GUIDHASH, 0x80, 0x80) == 0)
5248 create_tlib2->typelib_guidhash_segment = (int *)create_tlib2->typelib_segment_data[MSFT_SEG_GUIDHASH];
5249 memset(create_tlib2->typelib_guidhash_segment, 0xff, 0x80);
5251 else
5252 failed = 1;
5254 if (ctl2_alloc_segment(create_tlib2, MSFT_SEG_NAMEHASH, 0x200, 0x200) == 0)
5256 create_tlib2->typelib_namehash_segment = (int *)create_tlib2->typelib_segment_data[MSFT_SEG_NAMEHASH];
5257 memset(create_tlib2->typelib_namehash_segment, 0xff, 0x200);
5259 else
5260 failed = 1;
5262 create_tlib2->ICreateTypeLib2_iface.lpVtbl = &ctypelib2vt;
5263 create_tlib2->ITypeLib2_iface.lpVtbl = &typelib2vt;
5264 create_tlib2->ref = 1;
5266 if (failed) {
5267 ICreateTypeLib2_fnRelease(&create_tlib2->ICreateTypeLib2_iface);
5268 return NULL;
5271 return &create_tlib2->ICreateTypeLib2_iface;
5274 /******************************************************************************
5275 * CreateTypeLib2 [OLEAUT32.180]
5277 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5278 * library.
5280 * NOTES
5282 * See also CreateTypeLib.
5284 * RETURNS
5285 * Success: S_OK
5286 * Failure: Status
5288 HRESULT WINAPI CreateTypeLib2(
5289 SYSKIND syskind, /* [I] System type library is for */
5290 LPCOLESTR szFile, /* [I] Type library file name */
5291 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5293 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5295 if (!szFile) return E_INVALIDARG;
5296 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5297 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5300 /******************************************************************************
5301 * ClearCustData (OLEAUT32.171)
5303 * Clear a custom data types' data.
5305 * PARAMS
5306 * lpCust [I] The custom data type instance
5308 * RETURNS
5309 * Nothing.
5311 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5313 if (lpCust && lpCust->cCustData)
5315 if (lpCust->prgCustData)
5317 DWORD i;
5319 for (i = 0; i < lpCust->cCustData; i++)
5320 VariantClear(&lpCust->prgCustData[i].varValue);
5322 /* FIXME - Should be using a per-thread IMalloc */
5323 heap_free(lpCust->prgCustData);
5324 lpCust->prgCustData = NULL;
5326 lpCust->cCustData = 0;