oleaut32: Don't rewrite name's hreftype.
[wine/multimedia.git] / dlls / oleaut32 / typelib2.c
blobaa14d22579876bd2080f48a13fff08fc3ae6fc5f
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 enum tagCyclicListElementType {
122 CyclicListFunc,
123 CyclicListVar
125 typedef struct tagCyclicList {
126 struct tagCyclicList *next;
127 int indice;
128 int name;
129 enum tagCyclicListElementType type;
131 union {
132 int val;
133 int *data;
135 } CyclicList;
137 enum MSFT_segment_index {
138 MSFT_SEG_TYPEINFO = 0, /* type information */
139 MSFT_SEG_IMPORTINFO, /* import information */
140 MSFT_SEG_IMPORTFILES, /* import filenames */
141 MSFT_SEG_REFERENCES, /* references (?) */
142 MSFT_SEG_GUIDHASH, /* hash table for guids? */
143 MSFT_SEG_GUID, /* guid storage */
144 MSFT_SEG_NAMEHASH, /* hash table for names */
145 MSFT_SEG_NAME, /* name storage */
146 MSFT_SEG_STRING, /* string storage */
147 MSFT_SEG_TYPEDESC, /* type descriptions */
148 MSFT_SEG_ARRAYDESC, /* array descriptions */
149 MSFT_SEG_CUSTDATA, /* custom data */
150 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
151 MSFT_SEG_UNKNOWN, /* ??? */
152 MSFT_SEG_UNKNOWN2, /* ??? */
153 MSFT_SEG_MAX /* total number of segments */
156 typedef struct tagMSFT_ImpFile {
157 int guid;
158 LCID lcid;
159 int version;
160 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
161 } MSFT_ImpFile;
163 typedef struct tagICreateTypeLib2Impl
165 const ICreateTypeLib2Vtbl *lpVtbl;
166 const ITypeLib2Vtbl *lpVtblTypeLib2;
168 LONG ref;
170 WCHAR *filename;
172 MSFT_Header typelib_header;
173 INT helpStringDll;
174 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
175 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_ITypeLib2( ITypeLib2 *iface )
192 return (ICreateTypeLib2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeLib2Impl, lpVtblTypeLib2));
195 typedef struct tagICreateTypeInfo2Impl
197 const ICreateTypeInfo2Vtbl *lpVtbl;
198 const ITypeInfo2Vtbl *lpVtblTypeInfo2;
200 LONG ref;
202 ICreateTypeLib2Impl *typelib;
203 MSFT_TypeInfoBase *typeinfo;
205 struct tagCyclicList *typedata; /* tail of cyclic list */
207 TYPEKIND typekind;
208 int datawidth;
210 struct tagICreateTypeInfo2Impl *next_typeinfo;
211 struct tagICreateTypeInfo2Impl *dual;
212 } ICreateTypeInfo2Impl;
214 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
216 return (ICreateTypeInfo2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeInfo2Impl, lpVtblTypeInfo2));
219 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
222 /*================== Internal functions ===================================*/
224 /****************************************************************************
225 * ctl2_init_header
227 * Initializes the type library header of a new typelib.
229 static void ctl2_init_header(
230 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
232 This->typelib_header.magic1 = 0x5446534d;
233 This->typelib_header.magic2 = 0x00010002;
234 This->typelib_header.posguid = -1;
235 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
236 This->typelib_header.varflags = 0x40;
237 This->typelib_header.version = 0;
238 This->typelib_header.flags = 0;
239 This->typelib_header.nrtypeinfos = 0;
240 This->typelib_header.helpstring = -1;
241 This->typelib_header.helpstringcontext = 0;
242 This->typelib_header.helpcontext = 0;
243 This->typelib_header.nametablecount = 0;
244 This->typelib_header.nametablechars = 0;
245 This->typelib_header.NameOffset = -1;
246 This->typelib_header.helpfile = -1;
247 This->typelib_header.CustomDataOffset = -1;
248 This->typelib_header.res44 = 0x20;
249 This->typelib_header.res48 = 0x80;
250 This->typelib_header.dispatchpos = -1;
251 This->typelib_header.nimpinfos = 0;
252 This->helpStringDll = -1;
255 /****************************************************************************
256 * ctl2_init_segdir
258 * Initializes the segment directory of a new typelib.
260 static void ctl2_init_segdir(
261 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
263 int i;
264 MSFT_pSeg *segdir;
266 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
268 for (i = 0; i < 15; i++) {
269 segdir[i].offset = -1;
270 segdir[i].length = 0;
271 segdir[i].res08 = -1;
272 segdir[i].res0c = 0x0f;
276 /****************************************************************************
277 * ctl2_hash_guid
279 * Generates a hash key from a GUID.
281 * RETURNS
283 * The hash key for the GUID.
285 static int ctl2_hash_guid(
286 REFGUID guid) /* [I] The guid to find. */
288 int hash;
289 int i;
291 hash = 0;
292 for (i = 0; i < 8; i ++) {
293 hash ^= ((const short *)guid)[i];
296 return hash & 0x1f;
299 /****************************************************************************
300 * ctl2_find_guid
302 * Locates a guid in a type library.
304 * RETURNS
306 * The offset into the GUID segment of the guid, or -1 if not found.
308 static int ctl2_find_guid(
309 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
310 int hash_key, /* [I] The hash key for the guid. */
311 REFGUID guid) /* [I] The guid to find. */
313 int offset;
314 MSFT_GuidEntry *guidentry;
316 offset = This->typelib_guidhash_segment[hash_key];
317 while (offset != -1) {
318 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
320 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
322 offset = guidentry->next_hash;
325 return offset;
328 /****************************************************************************
329 * ctl2_find_name
331 * Locates a name in a type library.
333 * RETURNS
335 * The offset into the NAME segment of the name, or -1 if not found.
337 * NOTES
339 * The name must be encoded as with ctl2_encode_name().
341 static int ctl2_find_name(
342 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
343 const char *name) /* [I] The encoded name to find. */
345 int offset;
346 int *namestruct;
348 offset = This->typelib_namehash_segment[name[2] & 0x7f];
349 while (offset != -1) {
350 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
352 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
353 /* hash codes and lengths match, final test */
354 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
357 /* move to next item in hash bucket */
358 offset = namestruct[1];
361 return offset;
364 /****************************************************************************
365 * ctl2_encode_name
367 * Encodes a name string to a form suitable for storing into a type library
368 * or comparing to a name stored in a type library.
370 * RETURNS
372 * The length of the encoded name, including padding and length+hash fields.
374 * NOTES
376 * Will throw an exception if name or result are NULL. Is not multithread
377 * safe in the slightest.
379 static int ctl2_encode_name(
380 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
381 const WCHAR *name, /* [I] The name string to encode. */
382 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
384 int length;
385 static char converted_name[0x104];
386 int offset;
387 int value;
389 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
390 converted_name[0] = length & 0xff;
392 converted_name[length + 4] = 0;
394 converted_name[1] = 0x00;
396 value = LHashValOfNameSysA(This->typelib_header.varflags & 0x0f, This->typelib_header.lcid, converted_name + 4);
398 converted_name[2] = value;
399 converted_name[3] = value >> 8;
401 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
403 *result = converted_name;
405 return (length + 7) & ~3;
408 /****************************************************************************
409 * ctl2_decode_name
411 * Converts string stored in typelib data to unicode.
413 static void ctl2_decode_name(
414 char *data, /* [I] String to be decoded */
415 WCHAR **string) /* [O] Decoded string */
417 int i, length;
418 static WCHAR converted_string[0x104];
420 length = data[0];
422 for(i=0; i<length; i++)
423 converted_string[i] = data[i+4];
424 converted_string[length] = '\0';
426 *string = converted_string;
429 /****************************************************************************
430 * ctl2_encode_string
432 * Encodes a string to a form suitable for storing into a type library or
433 * comparing to a string stored in a type library.
435 * RETURNS
437 * The length of the encoded string, including padding and length fields.
439 * NOTES
441 * Will throw an exception if string or result are NULL. Is not multithread
442 * safe in the slightest.
444 static int ctl2_encode_string(
445 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
446 const WCHAR *string, /* [I] The string to encode. */
447 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
449 int length;
450 static char converted_string[0x104];
451 int offset;
453 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
454 converted_string[0] = length & 0xff;
455 converted_string[1] = (length >> 8) & 0xff;
457 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
459 *result = converted_string;
461 return (length + 5) & ~3;
464 /****************************************************************************
465 * ctl2_decode_string
467 * Converts string stored in typelib data to unicode.
469 static void ctl2_decode_string(
470 char *data, /* [I] String to be decoded */
471 WCHAR **string) /* [O] Decoded string */
473 int i, length;
474 static WCHAR converted_string[0x104];
476 length = data[0] + (data[1]<<8);
477 if((length&0x3) == 1)
478 length >>= 2;
480 for(i=0; i<length; i++)
481 converted_string[i] = data[i+2];
482 converted_string[length] = '\0';
484 *string = converted_string;
487 /****************************************************************************
488 * ctl2_alloc_segment
490 * Allocates memory from a segment in a type library.
492 * RETURNS
494 * Success: The offset within the segment of the new data area.
495 * Failure: -1 (this is invariably an out of memory condition).
497 * BUGS
499 * Does not (yet) handle the case where the allocated segment memory needs to grow.
501 static int ctl2_alloc_segment(
502 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
503 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
504 int size, /* [I] The amount to allocate. */
505 int block_size) /* [I] Initial allocation block size, or 0 for default. */
507 int offset;
509 if(!This->typelib_segment_data[segment]) {
510 if (!block_size) block_size = 0x2000;
512 This->typelib_segment_block_length[segment] = block_size;
513 This->typelib_segment_data[segment] = HeapAlloc(GetProcessHeap(), 0, block_size);
514 if (!This->typelib_segment_data[segment]) return -1;
515 memset(This->typelib_segment_data[segment], 0x57, block_size);
518 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
519 char *block;
521 block_size = This->typelib_segment_block_length[segment];
522 block = HeapReAlloc(GetProcessHeap(), 0, This->typelib_segment_data[segment], block_size << 1);
523 if (!block) return -1;
525 if (segment == MSFT_SEG_TYPEINFO) {
526 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
527 ICreateTypeInfo2Impl *typeinfo;
529 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
530 typeinfo->typeinfo = (void *)&block[((char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
534 memset(block + block_size, 0x57, block_size);
535 This->typelib_segment_block_length[segment] = block_size << 1;
536 This->typelib_segment_data[segment] = block;
539 offset = This->typelib_segdir[segment].length;
540 This->typelib_segdir[segment].length += size;
542 return offset;
545 /****************************************************************************
546 * ctl2_alloc_typeinfo
548 * Allocates and initializes a typeinfo structure in a type library.
550 * RETURNS
552 * Success: The offset of the new typeinfo.
553 * Failure: -1 (this is invariably an out of memory condition).
555 static int ctl2_alloc_typeinfo(
556 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
557 int nameoffset) /* [I] The offset of the name for this typeinfo. */
559 int offset;
560 MSFT_TypeInfoBase *typeinfo;
562 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
563 if (offset == -1) return -1;
565 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
567 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
569 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
570 typeinfo->memoffset = -1; /* should be EOF if no elements */
571 typeinfo->res2 = 0;
572 typeinfo->res3 = 0;
573 typeinfo->res4 = 3;
574 typeinfo->res5 = 0;
575 typeinfo->cElement = 0;
576 typeinfo->res7 = 0;
577 typeinfo->res8 = 0;
578 typeinfo->res9 = 0;
579 typeinfo->resA = 0;
580 typeinfo->posguid = -1;
581 typeinfo->flags = 0;
582 typeinfo->NameOffset = nameoffset;
583 typeinfo->version = 0;
584 typeinfo->docstringoffs = -1;
585 typeinfo->helpstringcontext = 0;
586 typeinfo->helpcontext = 0;
587 typeinfo->oCustData = -1;
588 typeinfo->cbSizeVft = 0;
589 typeinfo->cImplTypes = 0;
590 typeinfo->size = 0;
591 typeinfo->datatype1 = -1;
592 typeinfo->datatype2 = 0;
593 typeinfo->res18 = 0;
594 typeinfo->res19 = -1;
596 return offset;
599 /****************************************************************************
600 * ctl2_alloc_guid
602 * Allocates and initializes a GUID structure in a type library. Also updates
603 * the GUID hash table as needed.
605 * RETURNS
607 * Success: The offset of the new GUID.
608 * Failure: -1 (this is invariably an out of memory condition).
610 static int ctl2_alloc_guid(
611 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
612 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
614 int offset;
615 MSFT_GuidEntry *guid_space;
616 int hash_key;
618 hash_key = ctl2_hash_guid(&guid->guid);
620 offset = ctl2_find_guid(This, hash_key, &guid->guid);
621 if (offset != -1) return offset;
623 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
624 if (offset == -1) return -1;
626 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
627 *guid_space = *guid;
629 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
630 This->typelib_guidhash_segment[hash_key] = offset;
632 return offset;
635 /****************************************************************************
636 * ctl2_alloc_name
638 * Allocates and initializes a name within a type library. Also updates the
639 * name hash table as needed.
641 * RETURNS
643 * Success: The offset within the segment of the new name.
644 * Failure: -1 (this is invariably an out of memory condition).
646 static int ctl2_alloc_name(
647 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
648 const WCHAR *name) /* [I] The name to store. */
650 int length;
651 int offset;
652 MSFT_NameIntro *name_space;
653 char *encoded_name;
655 length = ctl2_encode_name(This, name, &encoded_name);
657 offset = ctl2_find_name(This, encoded_name);
658 if (offset != -1) return offset;
660 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
661 if (offset == -1) return -1;
663 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
664 name_space->hreftype = -1;
665 name_space->next_hash = -1;
666 memcpy(&name_space->namelen, encoded_name, length);
668 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
669 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
671 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
673 This->typelib_header.nametablecount += 1;
674 This->typelib_header.nametablechars += *encoded_name;
676 return offset;
679 /****************************************************************************
680 * ctl2_alloc_string
682 * Allocates and initializes a string in a type library.
684 * RETURNS
686 * Success: The offset within the segment of the new string.
687 * Failure: -1 (this is invariably an out of memory condition).
689 static int ctl2_alloc_string(
690 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
691 const WCHAR *string) /* [I] The string to store. */
693 int length;
694 int offset;
695 char *string_space;
696 char *encoded_string;
698 length = ctl2_encode_string(This, string, &encoded_string);
700 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
701 offset += ((((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) & 0xff)
702 | (This->typelib_segment_data[MSFT_SEG_STRING][offset + 0] & 0xff)) + 5) & ~3) {
703 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
706 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
707 if (offset == -1) return -1;
709 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
710 memcpy(string_space, encoded_string, length);
712 return offset;
715 /****************************************************************************
716 * ctl2_alloc_importinfo
718 * Allocates and initializes an import information structure in a type library.
720 * RETURNS
722 * Success: The offset of the new importinfo.
723 * Failure: -1 (this is invariably an out of memory condition).
725 static int ctl2_alloc_importinfo(
726 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
727 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
729 int offset;
730 MSFT_ImpInfo *impinfo_space;
732 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
733 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
734 offset+=sizeof(MSFT_ImpInfo)) {
735 if(impinfo_space->oImpFile == impinfo->oImpFile
736 && impinfo_space->oGuid == impinfo->oGuid)
737 return offset;
739 impinfo_space += 1;
742 impinfo->flags |= This->typelib_header.nimpinfos++;
744 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
745 if (offset == -1) return -1;
747 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
748 *impinfo_space = *impinfo;
750 return offset;
753 /****************************************************************************
754 * ctl2_alloc_importfile
756 * Allocates and initializes an import file definition in a type library.
758 * RETURNS
760 * Success: The offset of the new importinfo.
761 * Failure: -1 (this is invariably an out of memory condition).
763 static int ctl2_alloc_importfile(
764 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
765 int guidoffset, /* [I] The offset to the GUID for the imported library. */
766 LCID lcid, /* [I] The LCID of imported library. */
767 int major_version, /* [I] The major version number of the imported library. */
768 int minor_version, /* [I] The minor version number of the imported library. */
769 const WCHAR *filename) /* [I] The filename of the imported library. */
771 int length;
772 int offset;
773 MSFT_ImpFile *importfile;
774 char *encoded_string;
776 length = ctl2_encode_string(This, filename, &encoded_string);
778 encoded_string[0] <<= 2;
779 encoded_string[0] |= 1;
781 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
782 offset += ((((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) & 0xff00)
783 | (This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc] & 0xff)) >> 2) + 5) & 0xfffc) + 0xc) {
784 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
787 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
788 if (offset == -1) return -1;
790 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
791 importfile->guid = guidoffset;
792 importfile->lcid = lcid;
793 importfile->version = major_version | (minor_version << 16);
794 memcpy(importfile->filename, encoded_string, length);
796 return offset;
799 /****************************************************************************
800 * ctl2_alloc_custdata
802 * Allocates and initializes a "custom data" value in a type library.
804 * RETURNS
806 * Success: The offset of the new custdata.
807 * Failure:
809 * -1: Out of memory.
810 * -2: Unable to encode VARIANT data (typically a bug).
812 static int ctl2_alloc_custdata(
813 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the value. */
814 VARIANT *pVarVal) /* [I] The value to encode. */
816 int offset;
818 TRACE("(%p,%p(%d))\n",This,pVarVal,V_VT(pVarVal));
820 switch (V_VT(pVarVal)) {
821 case VT_UI4:
822 case VT_I4:
823 case VT_R4:
824 case VT_INT:
825 case VT_UINT:
826 case VT_HRESULT:
827 offset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
828 if (offset == -1) return offset;
830 *((unsigned short *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = V_VT(pVarVal);
831 *((DWORD *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2]) = V_UI4(pVarVal);
832 break;
834 case VT_BSTR: {
835 /* Construct the data */
836 UINT cp = CP_ACP;
837 int stringlen = SysStringLen(V_BSTR(pVarVal));
838 int len = 0;
839 if (stringlen > 0) {
840 GetLocaleInfoA(This->typelib_header.lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
841 (LPSTR)&cp, sizeof(cp));
842 len = WideCharToMultiByte(cp, 0, V_BSTR(pVarVal), SysStringLen(V_BSTR(pVarVal)), NULL, 0, NULL, NULL);
843 if (!len)
844 return -1;
847 offset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, (6 + len + 3) & ~0x3, 0);
848 if (offset == -1) return offset;
850 *((unsigned short *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = V_VT(pVarVal);
851 *((DWORD *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2]) = (DWORD)len;
852 if (stringlen > 0) {
853 WideCharToMultiByte(cp, 0, V_BSTR(pVarVal), SysStringLen(V_BSTR(pVarVal)),
854 &This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], len, NULL, NULL);
857 break;
859 default:
860 FIXME("Unknown variable encoding vt %d.\n", V_VT(pVarVal));
861 return -2;
864 return offset;
867 /****************************************************************************
868 * ctl2_set_custdata
870 * Adds a custom data element to an object in a type library.
872 * RETURNS
874 * Success: S_OK.
875 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
877 static HRESULT ctl2_set_custdata(
878 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
879 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
880 VARIANT *pVarVal, /* [I] The custom data itself. */
881 int *offset) /* [I/O] The list of custom data to prepend to. */
883 MSFT_GuidEntry guidentry;
884 int dataoffset;
885 int guidoffset;
886 int custoffset;
887 int *custdata;
889 guidentry.guid = *guid;
891 guidentry.hreftype = -1;
892 guidentry.next_hash = -1;
894 guidoffset = ctl2_alloc_guid(This, &guidentry);
895 if (guidoffset == -1) return E_OUTOFMEMORY;
896 dataoffset = ctl2_alloc_custdata(This, pVarVal);
897 if (dataoffset == -1) return E_OUTOFMEMORY;
898 if (dataoffset == -2) return DISP_E_BADVARTYPE;
900 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
901 if (custoffset == -1) return E_OUTOFMEMORY;
903 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
904 custdata[0] = guidoffset;
905 custdata[1] = dataoffset;
906 custdata[2] = *offset;
907 *offset = custoffset;
909 return S_OK;
912 /****************************************************************************
913 * ctl2_encode_typedesc
915 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
916 * segments as needed.
918 * RETURNS
920 * Success: 0.
921 * Failure: -1.
923 static int ctl2_encode_typedesc(
924 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
925 const TYPEDESC *tdesc, /* [I] The type description to encode. */
926 int *encoded_tdesc, /* [O] The encoded type description. */
927 int *width, /* [O] The width of the type, or NULL. */
928 int *alignment, /* [O] The alignment of the type, or NULL. */
929 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
931 int default_tdesc;
932 int scratch;
933 int typeoffset;
934 int arrayoffset;
935 int *typedata;
936 int *arraydata;
937 int target_type;
938 int child_size;
940 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
941 if (!width) width = &scratch;
942 if (!alignment) alignment = &scratch;
943 if (!decoded_size) decoded_size = &scratch;
945 *decoded_size = 0;
947 switch (tdesc->vt) {
948 case VT_UI1:
949 case VT_I1:
950 *encoded_tdesc = default_tdesc;
951 *width = 1;
952 *alignment = 1;
953 break;
955 case VT_INT:
956 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
957 if ((This->typelib_header.varflags & 0x0f) == SYS_WIN16) {
958 *width = 2;
959 *alignment = 2;
960 } else {
961 *width = 4;
962 *alignment = 4;
964 break;
966 case VT_UINT:
967 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
968 if ((This->typelib_header.varflags & 0x0f) == SYS_WIN16) {
969 *width = 2;
970 *alignment = 2;
971 } else {
972 *width = 4;
973 *alignment = 4;
975 break;
977 case VT_UI2:
978 case VT_I2:
979 case VT_BOOL:
980 *encoded_tdesc = default_tdesc;
981 *width = 2;
982 *alignment = 2;
983 break;
985 case VT_I4:
986 case VT_UI4:
987 case VT_R4:
988 case VT_ERROR:
989 case VT_BSTR:
990 case VT_HRESULT:
991 *encoded_tdesc = default_tdesc;
992 *width = 4;
993 *alignment = 4;
994 break;
996 case VT_CY:
997 *encoded_tdesc = default_tdesc;
998 *width = 8;
999 *alignment = 4; /* guess? */
1000 break;
1002 case VT_VOID:
1003 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1004 *width = 0;
1005 *alignment = 1;
1006 break;
1008 case VT_PTR:
1009 /* FIXME: Make with the error checking. */
1010 FIXME("PTR vartype, may not work correctly.\n");
1012 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1014 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1015 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1016 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1019 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1020 int mix_field;
1022 if (target_type & 0x80000000) {
1023 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1024 } else {
1025 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1026 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1029 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1030 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1032 typedata[0] = (mix_field << 16) | VT_PTR;
1033 typedata[1] = target_type;
1036 *encoded_tdesc = typeoffset;
1038 *width = 4;
1039 *alignment = 4;
1040 *decoded_size = sizeof(TYPEDESC) + child_size;
1041 break;
1043 case VT_SAFEARRAY:
1044 /* FIXME: Make with the error checking. */
1045 FIXME("SAFEARRAY vartype, may not work correctly.\n");
1047 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1049 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1050 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1051 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
1054 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1055 int mix_field;
1057 if (target_type & 0x80000000) {
1058 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
1059 } else {
1060 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1061 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1064 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1065 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1067 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
1068 typedata[1] = target_type;
1071 *encoded_tdesc = typeoffset;
1073 *width = 4;
1074 *alignment = 4;
1075 *decoded_size = sizeof(TYPEDESC) + child_size;
1076 break;
1078 case VT_CARRAY:
1080 /* FIXME: Make with the error checking. */
1081 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1083 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1084 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1085 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1087 arraydata[0] = target_type;
1088 arraydata[1] = num_dims;
1089 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1090 arraydata += 2;
1092 for(dim = 0; dim < num_dims; dim++) {
1093 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1094 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1095 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1096 arraydata += 2;
1098 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1099 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1101 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1102 typedata[1] = arrayoffset;
1104 *encoded_tdesc = typeoffset;
1105 *width = *width * elements;
1106 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1108 break;
1110 case VT_USERDEFINED:
1111 TRACE("USERDEFINED.\n");
1112 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1113 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1114 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1117 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1118 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1119 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1121 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1122 typedata[1] = tdesc->u.hreftype;
1125 *encoded_tdesc = typeoffset;
1126 *width = 0;
1127 *alignment = 1;
1128 break;
1130 default:
1131 FIXME("Unrecognized type %d.\n", tdesc->vt);
1132 *encoded_tdesc = default_tdesc;
1133 *width = 0;
1134 *alignment = 1;
1135 break;
1138 return 0;
1141 /****************************************************************************
1142 * ctl2_find_nth_reference
1144 * Finds a reference by index into the linked list of reference records.
1146 * RETURNS
1148 * Success: Offset of the desired reference record.
1149 * Failure: -1.
1151 static int ctl2_find_nth_reference(
1152 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1153 int offset, /* [I] The starting offset of the reference list. */
1154 int index) /* [I] The index of the reference to find. */
1156 MSFT_RefRecord *ref;
1158 for (; index && (offset != -1); index--) {
1159 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1160 offset = ref->onext;
1163 return offset;
1166 /****************************************************************************
1167 * ctl2_find_typeinfo_from_offset
1169 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1171 * RETURNS
1173 * Success: S_OK.
1174 * Failure: TYPE_E_ELEMENTNOTFOUND.
1176 static HRESULT ctl2_find_typeinfo_from_offset(
1177 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1178 int offset, /* [I] The offset of the desired typeinfo. */
1179 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1181 void *typeinfodata;
1182 ICreateTypeInfo2Impl *typeinfo;
1184 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1186 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1187 if (typeinfo->typeinfo == typeinfodata) {
1188 *ppTinfo = (ITypeInfo *)&typeinfo->lpVtblTypeInfo2;
1189 ITypeInfo2_AddRef(*ppTinfo);
1190 return S_OK;
1194 ERR("Failed to find typeinfo, invariant varied.\n");
1196 return TYPE_E_ELEMENTNOTFOUND;
1199 /****************************************************************************
1200 * ctl2_add_default_value
1202 * Adds default value of an argument
1204 * RETURNS
1206 * Success: S_OK
1207 * Failure: Error code from winerror.h
1209 static HRESULT ctl2_add_default_value(
1210 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
1211 int *encoded_value, /* [O] The encoded default value or data offset */
1212 VARIANT *value, /* [I] Default value to be encoded */
1213 VARTYPE arg_type) /* [I] Argument type */
1215 VARIANT v;
1216 HRESULT hres;
1217 int mask = 0;
1219 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
1221 if(arg_type == VT_INT)
1222 arg_type = VT_I4;
1223 if(arg_type == VT_UINT)
1224 arg_type = VT_UI4;
1226 v = *value;
1227 if(V_VT(value) != arg_type) {
1228 hres = VariantChangeType(&v, value, 0, arg_type);
1229 if(FAILED(hres))
1230 return hres;
1233 /* Check if default value can be stored in encoded_value */
1234 switch(arg_type) {
1235 case VT_I4:
1236 case VT_UI4:
1237 mask = 0x3ffffff;
1238 if(V_UI4(&v)>0x3ffffff)
1239 break;
1240 case VT_I1:
1241 case VT_UI1:
1242 case VT_BOOL:
1243 if(!mask)
1244 mask = 0xff;
1245 case VT_I2:
1246 case VT_UI2:
1247 if(!mask)
1248 mask = 0xffff;
1249 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
1250 return S_OK;
1253 switch(arg_type) {
1254 case VT_I4:
1255 case VT_R4:
1256 case VT_UI4:
1257 case VT_INT:
1258 case VT_UINT:
1259 case VT_HRESULT:
1260 case VT_PTR: {
1261 /* Construct the data to be allocated */
1262 int data[2];
1263 data[0] = arg_type + (V_UI4(&v)<<16);
1264 data[1] = (V_UI4(&v)>>16) + 0x57570000;
1266 /* Check if the data was already allocated */
1267 /* Currently the structures doesn't allow to do it in a nice way */
1268 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
1269 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
1270 return S_OK;
1272 /* Allocate the data */
1273 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
1274 if(*encoded_value == -1)
1275 return E_OUTOFMEMORY;
1277 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
1278 return S_OK;
1280 case VT_BSTR: {
1281 /* Construct the data */
1282 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
1283 char *data = HeapAlloc(GetProcessHeap(), 0, len);
1285 if(!data)
1286 return E_OUTOFMEMORY;
1288 *((unsigned short*)data) = arg_type;
1289 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
1290 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
1291 if(V_BSTR(&v)[i] <= 0x7f)
1292 data[i+6] = V_BSTR(&v)[i];
1293 else
1294 data[i+6] = '?';
1296 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
1297 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
1298 data[i] = 0x57;
1300 /* Check if the data was already allocated */
1301 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
1302 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
1303 HeapFree(GetProcessHeap(), 0, data);
1304 return S_OK;
1307 /* Allocate the data */
1308 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
1309 if(*encoded_value == -1) {
1310 HeapFree(GetProcessHeap(), 0, data);
1311 return E_OUTOFMEMORY;
1314 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
1315 HeapFree(GetProcessHeap(), 0, data);
1316 return S_OK;
1318 default:
1319 FIXME("Argument type not yet handled\n");
1320 return E_NOTIMPL;
1324 /****************************************************************************
1325 * funcrecord_reallochdr
1327 * Ensure FuncRecord data block contains header of required size
1329 * PARAMS
1331 * typedata [IO] - reference to pointer to data block
1332 * need [I] - required size of block in bytes
1334 * RETURNS
1336 * Number of additionally allocated bytes
1338 static INT funcrecord_reallochdr(INT **typedata, int need)
1340 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1341 int hdr = (*typedata)[0] - tail;
1342 int i;
1344 if (hdr >= need)
1345 return 0;
1347 *typedata = HeapReAlloc(GetProcessHeap(), 0, *typedata, need + tail);
1348 if (!*typedata)
1349 return -1;
1351 if (tail)
1352 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1353 (*typedata)[0] = need + tail;
1355 /* fill in default values */
1356 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1358 switch(i)
1360 case 2:
1361 (*typedata)[i] = 0;
1362 break;
1363 case 7:
1364 (*typedata)[i] = -1;
1365 break;
1366 case 8:
1367 (*typedata)[i] = -1;
1368 break;
1369 case 9:
1370 (*typedata)[i] = -1;
1371 break;
1372 case 10:
1373 (*typedata)[i] = -1;
1374 break;
1375 case 11:
1376 (*typedata)[i] = 0;
1377 break;
1378 case 12:
1379 (*typedata)[i] = -1;
1380 break;
1384 return need - hdr;
1387 /*================== ICreateTypeInfo2 Implementation ===================================*/
1389 /******************************************************************************
1390 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1392 * See IUnknown_QueryInterface.
1394 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1395 ICreateTypeInfo2 * iface,
1396 REFIID riid,
1397 VOID **ppvObject)
1399 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1401 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1403 *ppvObject=NULL;
1404 if(IsEqualIID(riid, &IID_IUnknown) ||
1405 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1406 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1408 *ppvObject = This;
1409 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1410 IsEqualIID(riid, &IID_ITypeInfo2)) {
1411 *ppvObject = &This->lpVtblTypeInfo2;
1414 if(*ppvObject)
1416 ICreateTypeInfo2_AddRef(iface);
1417 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1418 return S_OK;
1420 TRACE("-- Interface: E_NOINTERFACE\n");
1421 return E_NOINTERFACE;
1424 /******************************************************************************
1425 * ICreateTypeInfo2_AddRef {OLEAUT32}
1427 * See IUnknown_AddRef.
1429 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1431 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1432 ULONG ref = InterlockedIncrement(&This->ref);
1434 TRACE("(%p)->ref was %u\n",This, ref - 1);
1436 if(ref==1 && This->typelib)
1437 ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This->typelib);
1439 return ref;
1442 /******************************************************************************
1443 * ICreateTypeInfo2_Release {OLEAUT32}
1445 * See IUnknown_Release.
1447 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1449 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1450 ULONG ref = InterlockedDecrement(&This->ref);
1452 TRACE("(%p)->(%u)\n",This, ref);
1454 if (!ref) {
1455 if (This->typelib) {
1456 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)This->typelib);
1457 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1458 /* This->typelib = NULL; */
1461 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1462 /* HeapFree(GetProcessHeap(),0,This); */
1463 return 0;
1466 return ref;
1470 /******************************************************************************
1471 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1473 * See ICreateTypeInfo_SetGuid.
1475 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1477 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1479 MSFT_GuidEntry guidentry;
1480 int offset;
1482 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1484 guidentry.guid = *guid;
1485 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1486 guidentry.next_hash = -1;
1488 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1490 if (offset == -1) return E_OUTOFMEMORY;
1492 This->typeinfo->posguid = offset;
1494 if (IsEqualIID(guid, &IID_IDispatch)) {
1495 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1498 return S_OK;
1501 /******************************************************************************
1502 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1504 * See ICreateTypeInfo_SetTypeFlags.
1506 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1508 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1510 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1512 if(uTypeFlags & TYPEFLAG_FDUAL) {
1513 This->typeinfo->typekind |= 0x10;
1514 This->typeinfo->typekind &= ~0x0f;
1515 This->typeinfo->typekind |= TKIND_DISPATCH;
1517 if(!This->dual) {
1518 This->dual = HeapAlloc(GetProcessHeap(), 0, sizeof(ICreateTypeInfo2Impl));
1519 if(!This->dual)
1520 return E_OUTOFMEMORY;
1522 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1523 This->dual->ref = 0;
1524 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1525 TKIND_INTERFACE : TKIND_DISPATCH;
1526 This->dual->dual = This;
1529 /* Make sure dispatch is in typeinfos queue */
1530 if(This->typekind != TKIND_DISPATCH) {
1531 if(This->typelib->last_typeinfo == This)
1532 This->typelib->last_typeinfo = This->dual;
1534 if(This->typelib->typeinfos == This)
1535 This->typelib->typeinfos = This->dual;
1536 else {
1537 ICreateTypeInfo2Impl *iter;
1539 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1540 iter->next_typeinfo = This->dual;
1542 } else
1543 iface = (ICreateTypeInfo2*)&This->dual->lpVtbl;
1546 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1547 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1548 ITypeLib *stdole;
1549 ITypeInfo *dispatch;
1550 HREFTYPE hreftype;
1551 HRESULT hres;
1553 hres = LoadTypeLib(stdole2tlb, &stdole);
1554 if(FAILED(hres))
1555 return hres;
1557 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1558 ITypeLib_Release(stdole);
1559 if(FAILED(hres))
1560 return hres;
1562 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1563 ITypeInfo_Release(dispatch);
1564 if(FAILED(hres))
1565 return hres;
1568 This->typeinfo->flags = uTypeFlags;
1569 return S_OK;
1572 /******************************************************************************
1573 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1575 * See ICreateTypeInfo_SetDocString.
1577 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1578 ICreateTypeInfo2* iface,
1579 LPOLESTR pStrDoc)
1581 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1583 int offset;
1585 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1586 if (!pStrDoc)
1587 return E_INVALIDARG;
1589 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1590 if (offset == -1) return E_OUTOFMEMORY;
1591 This->typeinfo->docstringoffs = offset;
1592 return S_OK;
1595 /******************************************************************************
1596 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1598 * See ICreateTypeInfo_SetHelpContext.
1600 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1601 ICreateTypeInfo2* iface,
1602 DWORD dwHelpContext)
1604 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1606 TRACE("(%p,%d)\n", iface, dwHelpContext);
1608 This->typeinfo->helpcontext = dwHelpContext;
1610 return S_OK;
1613 /******************************************************************************
1614 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1616 * See ICreateTypeInfo_SetVersion.
1618 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1619 ICreateTypeInfo2* iface,
1620 WORD wMajorVerNum,
1621 WORD wMinorVerNum)
1623 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1625 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1627 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1628 return S_OK;
1631 /******************************************************************************
1632 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1634 * See ICreateTypeInfo_AddRefTypeInfo.
1636 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1637 ICreateTypeInfo2* iface,
1638 ITypeInfo* pTInfo,
1639 HREFTYPE* phRefType)
1641 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1643 ITypeLib *container;
1644 UINT index;
1645 HRESULT res;
1647 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1649 if(!pTInfo || !phRefType)
1650 return E_INVALIDARG;
1653 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1654 * same internal structure as one of ours. It could be from another
1655 * implementation of ITypeInfo. So we need to do the following...
1657 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1658 if (FAILED(res)) {
1659 TRACE("failed to find containing typelib.\n");
1660 return res;
1663 if (container == (ITypeLib *)&This->typelib->lpVtblTypeLib2) {
1664 /* Process locally defined TypeInfo */
1665 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1666 } else {
1667 BSTR name;
1668 TLIBATTR *tlibattr;
1669 TYPEATTR *typeattr;
1670 TYPEKIND typekind;
1671 MSFT_GuidEntry guid, *check_guid;
1672 MSFT_ImpInfo impinfo;
1673 int guid_offset, import_offset;
1674 HRESULT hres;
1676 /* Allocate container GUID */
1677 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1678 if(FAILED(hres)) {
1679 ITypeLib_Release(container);
1680 return hres;
1683 guid.guid = tlibattr->guid;
1684 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1685 guid.next_hash = -1;
1687 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1688 if(guid_offset == -1) {
1689 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1690 ITypeLib_Release(container);
1691 return E_OUTOFMEMORY;
1694 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1695 if(check_guid->hreftype == guid.hreftype)
1696 This->typelib->typelib_guids++;
1698 /* Get import file name */
1699 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1700 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1701 if(FAILED(hres)) {
1702 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1703 ITypeLib_Release(container);
1704 return hres;
1707 /* Import file */
1708 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1709 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1710 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1711 SysFreeString(name);
1713 if(import_offset == -1) {
1714 ITypeLib_Release(container);
1715 return E_OUTOFMEMORY;
1718 /* Allocate referenced guid */
1719 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1720 if(FAILED(hres)) {
1721 ITypeLib_Release(container);
1722 return hres;
1725 guid.guid = typeattr->guid;
1726 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1727 guid.next_hash = -1;
1728 typekind = typeattr->typekind;
1729 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1731 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1732 if(guid_offset == -1) {
1733 ITypeLib_Release(container);
1734 return E_OUTOFMEMORY;
1737 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1738 if(check_guid->hreftype == guid.hreftype)
1739 This->typelib->typeinfo_guids++;
1741 /* Allocate importinfo */
1742 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1743 impinfo.oImpFile = import_offset;
1744 impinfo.oGuid = guid_offset;
1745 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1747 if(!memcmp(&guid.guid, &IID_IDispatch, sizeof(GUID)))
1748 This->typelib->typelib_header.dispatchpos = *phRefType;
1751 ITypeLib_Release(container);
1752 return S_OK;
1755 /******************************************************************************
1756 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1758 * See ICreateTypeInfo_AddFuncDesc.
1760 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1761 ICreateTypeInfo2* iface,
1762 UINT index,
1763 FUNCDESC* pFuncDesc)
1765 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1767 CyclicList *iter, *insert;
1768 int *typedata;
1769 int i, num_defaults = 0, num_retval = 0;
1770 int decoded_size;
1771 HRESULT hres;
1773 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1775 if(!pFuncDesc || pFuncDesc->oVft&3)
1776 return E_INVALIDARG;
1778 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1779 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1780 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1781 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1782 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1784 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1785 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1787 switch(This->typekind) {
1788 case TKIND_MODULE:
1789 if(pFuncDesc->funckind != FUNC_STATIC)
1790 return TYPE_E_BADMODULEKIND;
1791 break;
1792 case TKIND_DISPATCH:
1793 if(pFuncDesc->funckind != FUNC_DISPATCH)
1794 return TYPE_E_BADMODULEKIND;
1795 break;
1796 default:
1797 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1798 return TYPE_E_BADMODULEKIND;
1801 if(This->typeinfo->cElement<index)
1802 return TYPE_E_ELEMENTNOTFOUND;
1804 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1805 !pFuncDesc->cParams)
1806 return TYPE_E_INCONSISTENTPROPFUNCS;
1808 /* get number of arguments with default values specified */
1809 for (i = 0; i < pFuncDesc->cParams; i++) {
1810 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1811 num_defaults++;
1812 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1813 num_retval++;
1816 if (!This->typedata) {
1817 This->typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
1818 if(!This->typedata)
1819 return E_OUTOFMEMORY;
1821 This->typedata->next = This->typedata;
1822 This->typedata->u.val = 0;
1824 if(This->dual)
1825 This->dual->typedata = This->typedata;
1828 /* allocate type data space for us */
1829 insert = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
1830 if(!insert)
1831 return E_OUTOFMEMORY;
1832 insert->u.data = HeapAlloc(GetProcessHeap(), 0, sizeof(int[6])+sizeof(int[(num_defaults?4:3)])*pFuncDesc->cParams);
1833 if(!insert->u.data) {
1834 HeapFree(GetProcessHeap(), 0, insert);
1835 return E_OUTOFMEMORY;
1838 /* fill out the basic type information */
1839 typedata = insert->u.data;
1840 typedata[0] = 0x18 + pFuncDesc->cParams*(num_defaults?16:12);
1841 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
1842 typedata[2] = pFuncDesc->wFuncFlags;
1843 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
1844 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
1845 if(num_defaults) typedata[4] |= 0x1000;
1846 if (num_retval) typedata[4] |= 0x4000;
1847 typedata[5] = pFuncDesc->cParams;
1849 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1850 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1851 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
1852 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
1854 /* add default values */
1855 if(num_defaults) {
1856 for (i = 0; i < pFuncDesc->cParams; i++)
1857 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
1858 hres = ctl2_add_default_value(This->typelib, typedata+6+i,
1859 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
1860 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
1862 if(FAILED(hres)) {
1863 HeapFree(GetProcessHeap(), 0, insert->u.data);
1864 HeapFree(GetProcessHeap(), 0, insert);
1865 return hres;
1867 } else
1868 typedata[6+i] = 0xffffffff;
1870 num_defaults = pFuncDesc->cParams;
1873 /* add arguments */
1874 for (i = 0; i < pFuncDesc->cParams; i++) {
1875 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
1876 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
1877 typedata[7+num_defaults+(i*3)] = -1;
1878 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
1879 typedata[3] += decoded_size << 16;
1882 /* update the index data */
1883 insert->indice = pFuncDesc->memid;
1884 insert->name = -1;
1885 insert->type = CyclicListFunc;
1887 /* insert type data to list */
1888 if(index == This->typeinfo->cElement) {
1889 insert->next = This->typedata->next;
1890 This->typedata->next = insert;
1891 This->typedata = insert;
1893 if(This->dual)
1894 This->dual->typedata = This->typedata;
1895 } else {
1896 iter = This->typedata->next;
1897 for(i=0; i<index; i++)
1898 iter = iter->next;
1900 insert->next = iter->next;
1901 iter->next = insert;
1904 /* update type data size */
1905 This->typedata->next->u.val += 0x18 + pFuncDesc->cParams*(num_defaults?16:12);
1907 /* Increment the number of function elements */
1908 This->typeinfo->cElement += 1;
1910 return S_OK;
1913 /******************************************************************************
1914 * ICreateTypeInfo2_AddImplType {OLEAUT32}
1916 * See ICreateTypeInfo_AddImplType.
1918 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
1919 ICreateTypeInfo2* iface,
1920 UINT index,
1921 HREFTYPE hRefType)
1923 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1925 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
1927 if (This->typekind == TKIND_COCLASS) {
1928 int offset;
1929 MSFT_RefRecord *ref;
1931 if (index == 0) {
1932 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
1934 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
1935 if (offset == -1) return E_OUTOFMEMORY;
1937 This->typeinfo->datatype1 = offset;
1938 } else {
1939 int lastoffset;
1941 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
1942 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
1944 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
1945 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
1947 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
1948 if (offset == -1) return E_OUTOFMEMORY;
1950 ref->onext = offset;
1953 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1955 ref->reftype = hRefType;
1956 ref->flags = 0;
1957 ref->oCustData = -1;
1958 ref->onext = -1;
1959 This->typeinfo->cImplTypes++;
1960 } else if (This->typekind == TKIND_INTERFACE) {
1961 if (This->typeinfo->cImplTypes && index==1)
1962 return TYPE_E_BADMODULEKIND;
1964 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
1966 This->typeinfo->datatype1 = hRefType;
1967 This->typeinfo->cImplTypes = 1;
1968 } else if (This->typekind == TKIND_DISPATCH) {
1969 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
1971 /* FIXME: Check if referenced typeinfo is IDispatch */
1972 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
1973 This->typeinfo->cImplTypes = 1;
1974 } else {
1975 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
1976 return E_OUTOFMEMORY;
1979 return S_OK;
1982 /******************************************************************************
1983 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
1985 * See ICreateTypeInfo_SetImplTypeFlags.
1987 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
1988 ICreateTypeInfo2* iface,
1989 UINT index,
1990 INT implTypeFlags)
1992 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1993 int offset;
1994 MSFT_RefRecord *ref;
1996 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
1998 if (This->typekind != TKIND_COCLASS) {
1999 return TYPE_E_BADMODULEKIND;
2002 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2003 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2005 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2006 ref->flags = implTypeFlags;
2008 return S_OK;
2011 /******************************************************************************
2012 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2014 * See ICreateTypeInfo_SetAlignment.
2016 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2017 ICreateTypeInfo2* iface,
2018 WORD cbAlignment)
2020 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2022 TRACE("(%p,%d)\n", iface, cbAlignment);
2024 if (!cbAlignment) return E_INVALIDARG;
2025 if (cbAlignment > 16) return E_INVALIDARG;
2027 This->typeinfo->typekind &= ~0xffc0;
2028 This->typeinfo->typekind |= cbAlignment << 6;
2030 /* FIXME: There's probably some way to simplify this. */
2031 switch (This->typekind) {
2032 case TKIND_ALIAS:
2033 default:
2034 break;
2036 case TKIND_ENUM:
2037 case TKIND_INTERFACE:
2038 case TKIND_DISPATCH:
2039 case TKIND_COCLASS:
2040 if (cbAlignment > 4) cbAlignment = 4;
2041 break;
2043 case TKIND_RECORD:
2044 case TKIND_MODULE:
2045 case TKIND_UNION:
2046 cbAlignment = 1;
2047 break;
2050 This->typeinfo->typekind |= cbAlignment << 11;
2052 return S_OK;
2055 /******************************************************************************
2056 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2058 * See ICreateTypeInfo_SetSchema.
2060 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2061 ICreateTypeInfo2* iface,
2062 LPOLESTR pStrSchema)
2064 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2065 return E_OUTOFMEMORY;
2068 /******************************************************************************
2069 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2071 * See ICreateTypeInfo_AddVarDesc.
2073 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2074 ICreateTypeInfo2* iface,
2075 UINT index,
2076 VARDESC* pVarDesc)
2078 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2080 CyclicList *insert;
2081 INT *typedata;
2082 int var_datawidth;
2083 int var_alignment;
2084 int var_type_size;
2085 int alignment;
2087 TRACE("(%p,%d,%p), stub!\n", iface, index, pVarDesc);
2088 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2089 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2090 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2091 pVarDesc->wVarFlags, pVarDesc->varkind);
2093 if ((This->typeinfo->cElement >> 16) != index) {
2094 TRACE("Out-of-order element.\n");
2095 return TYPE_E_ELEMENTNOTFOUND;
2098 if (!This->typedata) {
2099 This->typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
2100 if(!This->typedata)
2101 return E_OUTOFMEMORY;
2103 This->typedata->next = This->typedata;
2104 This->typedata->u.val = 0;
2106 if(This->dual)
2107 This->dual->typedata = This->typedata;
2110 /* allocate type data space for us */
2111 insert = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
2112 if(!insert)
2113 return E_OUTOFMEMORY;
2114 insert->u.data = HeapAlloc(GetProcessHeap(), 0, sizeof(int[5]));
2115 if(!insert->u.data) {
2116 HeapFree(GetProcessHeap(), 0, insert);
2117 return E_OUTOFMEMORY;
2120 insert->next = This->typedata->next;
2121 This->typedata->next = insert;
2122 This->typedata = insert;
2124 if(This->dual)
2125 This->dual->typedata = This->typedata;
2127 This->typedata->next->u.val += 0x14;
2128 typedata = This->typedata->u.data;
2130 /* fill out the basic type information */
2131 typedata[0] = 0x14 | (index << 16);
2132 typedata[2] = pVarDesc->wVarFlags;
2133 typedata[3] = (sizeof(VARDESC) << 16) | 0;
2135 /* update the index data */
2136 insert->indice = 0x40000000 + index;
2137 insert->name = -1;
2138 insert->type = CyclicListVar;
2140 /* figure out type widths and whatnot */
2141 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2142 &typedata[1], &var_datawidth, &var_alignment,
2143 &var_type_size);
2145 /* pad out starting position to data width */
2146 This->datawidth += var_alignment - 1;
2147 This->datawidth &= ~(var_alignment - 1);
2148 typedata[4] = This->datawidth;
2150 /* add the new variable to the total data width */
2151 This->datawidth += var_datawidth;
2152 if(This->dual)
2153 This->dual->datawidth = This->datawidth;
2155 /* add type description size to total required allocation */
2156 typedata[3] += var_type_size << 16;
2158 /* fix type alignment */
2159 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2160 if (alignment < var_alignment) {
2161 alignment = var_alignment;
2162 This->typeinfo->typekind &= ~0xf800;
2163 This->typeinfo->typekind |= alignment << 11;
2166 /* ??? */
2167 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2168 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2169 This->typeinfo->res2 <<= 1;
2172 /* ??? */
2173 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2174 This->typeinfo->res3 += 0x2c;
2176 /* increment the number of variable elements */
2177 This->typeinfo->cElement += 0x10000;
2179 /* pad data width to alignment */
2180 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2182 return S_OK;
2185 /******************************************************************************
2186 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2188 * See ICreateTypeInfo_SetFuncAndParamNames.
2190 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2191 ICreateTypeInfo2* iface,
2192 UINT index,
2193 LPOLESTR* rgszNames,
2194 UINT cNames)
2196 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2197 CyclicList *iter = NULL, *iter2;
2198 int offset, len, i=0;
2199 char *namedata;
2201 TRACE("(%p %d %p %d)\n", iface, index, rgszNames, cNames);
2203 if(!rgszNames)
2204 return E_INVALIDARG;
2206 if(index >= (This->typeinfo->cElement&0xFFFF) || !cNames)
2207 return TYPE_E_ELEMENTNOTFOUND;
2209 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2210 if (iter->type == CyclicListFunc)
2211 if (i++ >= index)
2212 break;
2214 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2215 if(cNames != iter->u.data[5] + ((iter->u.data[4]>>3)&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2216 return TYPE_E_ELEMENTNOTFOUND;
2218 len = ctl2_encode_name(This->typelib, rgszNames[0], &namedata);
2219 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2220 if(iter2->name!=-1 && !memcmp(namedata,
2221 This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len)) {
2222 /* getters/setters can have a same name */
2223 if (iter2->type == CyclicListFunc) {
2224 INT inv1 = iter2->u.data[4] >> 3;
2225 INT inv2 = iter->u.data[4] >> 3;
2226 if (((inv1&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) && (inv2&INVOKE_PROPERTYGET)) ||
2227 ((inv2&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) && (inv1&INVOKE_PROPERTYGET)))
2228 continue;
2231 return TYPE_E_AMBIGUOUSNAME;
2235 offset = ctl2_alloc_name(This->typelib, rgszNames[0]);
2236 if(offset == -1)
2237 return E_OUTOFMEMORY;
2239 iter->name = offset;
2241 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2242 if (*((INT*)namedata) == -1)
2243 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2245 len = iter->u.data[0]/4 - iter->u.data[5]*3;
2247 for (i = 1; i < cNames; i++) {
2248 offset = ctl2_alloc_name(This->typelib, rgszNames[i]);
2249 iter->u.data[len + ((i-1)*3) + 1] = offset;
2252 return S_OK;
2255 /******************************************************************************
2256 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2258 * See ICreateTypeInfo_SetVarName.
2260 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2261 ICreateTypeInfo2* iface,
2262 UINT index,
2263 LPOLESTR szName)
2265 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2266 CyclicList *iter;
2267 int offset, i;
2268 char *namedata;
2270 TRACE("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szName));
2272 if ((This->typeinfo->cElement >> 16) <= index) {
2273 TRACE("Out-of-order element.\n");
2274 return TYPE_E_ELEMENTNOTFOUND;
2277 offset = ctl2_alloc_name(This->typelib, szName);
2278 if (offset == -1) return E_OUTOFMEMORY;
2280 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2281 if (*((INT *)namedata) == -1) {
2282 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2283 namedata[9] |= 0x10;
2285 if (This->typekind == TKIND_ENUM) {
2286 namedata[9] |= 0x20;
2289 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2290 if (iter->type == CyclicListVar)
2291 if (i++ >= index)
2292 break;
2294 iter->name = offset;
2295 return S_OK;
2298 /******************************************************************************
2299 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2301 * See ICreateTypeInfo_SetTypeDescAlias.
2303 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2304 ICreateTypeInfo2* iface,
2305 TYPEDESC* pTDescAlias)
2307 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2309 int encoded_typedesc;
2310 int width;
2312 if (This->typekind != TKIND_ALIAS) {
2313 return TYPE_E_WRONGTYPEKIND;
2316 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2318 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2319 return E_OUTOFMEMORY;
2322 This->typeinfo->size = width;
2323 This->typeinfo->datatype1 = encoded_typedesc;
2325 return S_OK;
2328 /******************************************************************************
2329 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2331 * See ICreateTypeInfo_DefineFuncAsDllEntry.
2333 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2334 ICreateTypeInfo2* iface,
2335 UINT index,
2336 LPOLESTR szDllName,
2337 LPOLESTR szProcName)
2339 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2340 return E_OUTOFMEMORY;
2343 /******************************************************************************
2344 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2346 * See ICreateTypeInfo_SetFuncDocString.
2348 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2349 ICreateTypeInfo2* iface,
2350 UINT index,
2351 LPOLESTR szDocString)
2353 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2354 return E_OUTOFMEMORY;
2357 /******************************************************************************
2358 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2360 * See ICreateTypeInfo_SetVarDocString.
2362 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2363 ICreateTypeInfo2* iface,
2364 UINT index,
2365 LPOLESTR szDocString)
2367 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2369 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2371 ctl2_alloc_string(This->typelib, szDocString);
2373 return E_OUTOFMEMORY;
2376 /******************************************************************************
2377 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2379 * See ICreateTypeInfo_SetFuncHelpContext.
2381 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2382 ICreateTypeInfo2* iface,
2383 UINT index,
2384 DWORD dwHelpContext)
2386 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2387 CyclicList *func;
2389 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2391 if(This->typeinfo->cElement<index)
2392 return TYPE_E_ELEMENTNOTFOUND;
2394 if(This->typeinfo->cElement == index && This->typedata->type == CyclicListFunc)
2395 func = This->typedata;
2396 else
2397 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2398 if (func->type == CyclicListFunc)
2399 if(index-- == 0)
2400 break;
2402 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2403 if(!func->u.data)
2404 return E_OUTOFMEMORY;
2406 func->u.data[6] = dwHelpContext;
2407 return S_OK;
2410 /******************************************************************************
2411 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2413 * See ICreateTypeInfo_SetVarHelpContext.
2415 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2416 ICreateTypeInfo2* iface,
2417 UINT index,
2418 DWORD dwHelpContext)
2420 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpContext);
2421 return E_OUTOFMEMORY;
2424 /******************************************************************************
2425 * ICreateTypeInfo2_SetMops {OLEAUT32}
2427 * See ICreateTypeInfo_SetMops.
2429 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2430 ICreateTypeInfo2* iface,
2431 UINT index,
2432 BSTR bstrMops)
2434 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2435 return E_OUTOFMEMORY;
2438 /******************************************************************************
2439 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2441 * See ICreateTypeInfo_SetTypeIdldesc.
2443 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2444 ICreateTypeInfo2* iface,
2445 IDLDESC* pIdlDesc)
2447 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2448 return E_OUTOFMEMORY;
2451 /******************************************************************************
2452 * ICreateTypeInfo2_LayOut {OLEAUT32}
2454 * See ICreateTypeInfo_LayOut.
2456 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2457 ICreateTypeInfo2* iface)
2459 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2460 CyclicList *iter, *iter2, *last = NULL, **typedata;
2461 HREFTYPE hreftype;
2462 HRESULT hres;
2463 unsigned user_vft = 0;
2464 int i;
2466 TRACE("(%p)\n", iface);
2468 /* FIXME: LayOut should be run on all ImplTypes */
2469 if(This->typekind == TKIND_COCLASS)
2470 return S_OK;
2472 /* Validate inheritance */
2473 This->typeinfo->datatype2 = 0;
2474 hreftype = This->typeinfo->datatype1;
2476 /* Process internally defined interfaces */
2477 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2478 MSFT_TypeInfoBase *header;
2480 if(hreftype&1)
2481 break;
2483 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2484 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2485 hreftype = header->datatype1;
2487 if(i == This->typelib->typelib_header.nrtypeinfos)
2488 return TYPE_E_CIRCULARTYPE;
2490 /* Process externally defined interfaces */
2491 if(hreftype != -1) {
2492 ITypeInfo *cur, *next;
2493 TYPEATTR *typeattr;
2495 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2496 if(FAILED(hres))
2497 return hres;
2499 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2500 ITypeInfo_Release(next);
2501 if(FAILED(hres))
2502 return hres;
2505 while(1) {
2506 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2507 if(FAILED(hres)) {
2508 ITypeInfo_Release(cur);
2509 return hres;
2512 if(!memcmp(&typeattr->guid, &IID_IDispatch, sizeof(IDispatch)))
2513 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2515 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2516 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2518 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2519 if(hres == TYPE_E_ELEMENTNOTFOUND)
2520 break;
2521 if(FAILED(hres)) {
2522 ITypeInfo_Release(cur);
2523 return hres;
2526 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2527 if(FAILED(hres)) {
2528 ITypeInfo_Release(cur);
2529 return hres;
2532 ITypeInfo_Release(cur);
2533 cur = next;
2535 ITypeInfo_Release(cur);
2538 /* Get cbSizeVft of inherited interface */
2539 /* Makes LayOut running recursively */
2540 if(This->typeinfo->datatype1 != -1) {
2541 ITypeInfo *cur, *inherited;
2542 TYPEATTR *typeattr;
2544 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2545 if(FAILED(hres))
2546 return hres;
2548 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2549 ITypeInfo_Release(cur);
2550 if(FAILED(hres))
2551 return hres;
2553 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2554 if(FAILED(hres)) {
2555 ITypeInfo_Release(inherited);
2556 return hres;
2559 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2561 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2562 ITypeInfo_Release(inherited);
2563 } else
2564 This->typeinfo->cbSizeVft = 0;
2566 if(!This->typedata)
2567 return S_OK;
2569 typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList*)*(This->typeinfo->cElement&0xffff));
2570 if(!typedata)
2571 return E_OUTOFMEMORY;
2573 /* Assign IDs and VTBL entries */
2574 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2575 if (iter->type == CyclicListFunc)
2576 last = iter;
2578 if(last && last->u.data[3]&1)
2579 user_vft = last->u.data[3]&0xffff;
2581 i = 0;
2582 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2583 /* Assign MEMBERID if MEMBERID_NIL was specified */
2584 if(iter->indice == MEMBERID_NIL) {
2585 iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2587 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2588 if(iter == iter2) continue;
2589 if(iter2->indice == iter->indice) {
2590 iter->indice = 0x5fffffff + This->typeinfo->cElement + i + (This->typeinfo->datatype2<<16);
2592 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2593 if(iter == iter2) continue;
2594 if(iter2->indice == iter->indice) {
2595 HeapFree(GetProcessHeap(), 0, typedata);
2596 return E_ACCESSDENIED;
2600 break;
2605 if (iter->type != CyclicListFunc)
2606 continue;
2608 typedata[i] = iter;
2610 iter->u.data[0] = (iter->u.data[0]&0xffff) | (i<<16);
2612 if((iter->u.data[3]&1) != (user_vft&1)) {
2613 HeapFree(GetProcessHeap(), 0, typedata);
2614 return TYPE_E_INVALIDID;
2617 if(user_vft&1) {
2618 if(user_vft < (iter->u.data[3]&0xffff))
2619 user_vft = (iter->u.data[3]&0xffff);
2621 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2622 HeapFree(GetProcessHeap(), 0, typedata);
2623 return TYPE_E_INVALIDID;
2625 } else if(This->typekind != TKIND_MODULE) {
2626 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2627 This->typeinfo->cbSizeVft += 4;
2630 /* Construct a list of elements with the same memberid */
2631 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2632 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2633 if(iter->indice == iter2->indice) {
2634 int v1, v2;
2636 v1 = iter->u.data[4] >> 16;
2637 v2 = iter2->u.data[4] >> 16;
2639 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2640 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2641 break;
2645 i++;
2648 if(user_vft)
2649 This->typeinfo->cbSizeVft = user_vft+3;
2651 for(i=0; i<(This->typeinfo->cElement&0xffff); i++) {
2652 if(typedata[i]->u.data[4]>>16 > i) {
2653 int inv;
2655 inv = (typedata[i]->u.data[4]>>3) & 0xf;
2656 i = typedata[i]->u.data[4] >> 16;
2658 while(i > typedata[i]->u.data[4]>>16) {
2659 int invkind = (typedata[i]->u.data[4]>>3) & 0xf;
2661 if(inv & invkind) {
2662 HeapFree(GetProcessHeap(), 0, typedata);
2663 return TYPE_E_DUPLICATEID;
2666 i = typedata[i]->u.data[4] >> 16;
2667 inv |= invkind;
2670 if(inv & INVOKE_FUNC) {
2671 HeapFree(GetProcessHeap(), 0, typedata);
2672 return TYPE_E_INCONSISTENTPROPFUNCS;
2677 HeapFree(GetProcessHeap(), 0, typedata);
2678 return S_OK;
2681 /******************************************************************************
2682 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2684 * Delete a function description from a type.
2686 * RETURNS
2688 * Success: S_OK.
2689 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2691 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2692 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2693 UINT index) /* [I] The index of the function to delete. */
2695 FIXME("(%p,%d), stub!\n", iface, index);
2696 return E_OUTOFMEMORY;
2699 /******************************************************************************
2700 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2702 * Delete a function description from a type.
2704 * RETURNS
2706 * Success: S_OK.
2707 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2709 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2710 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2711 MEMBERID memid, /* [I] The member id of the function to delete. */
2712 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2714 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2715 return E_OUTOFMEMORY;
2718 /******************************************************************************
2719 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2721 * Delete a variable description from a type.
2723 * RETURNS
2725 * Success: S_OK.
2726 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2727 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2729 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2730 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2731 UINT index) /* [I] The index of the variable description to delete. */
2733 FIXME("(%p,%d), stub!\n", iface, index);
2734 return E_OUTOFMEMORY;
2737 /******************************************************************************
2738 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2740 * Delete a variable description from a type.
2742 * RETURNS
2744 * Success: S_OK.
2745 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2746 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2748 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2749 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2750 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2752 FIXME("(%p,%d), stub!\n", iface, memid);
2753 return E_OUTOFMEMORY;
2756 /******************************************************************************
2757 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2759 * Delete an interface implementation from a type. (?)
2761 * RETURNS
2763 * Success: S_OK.
2764 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2766 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2767 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2768 UINT index) /* [I] The index of the interface to delete. */
2770 FIXME("(%p,%d), stub!\n", iface, index);
2771 return E_OUTOFMEMORY;
2774 /******************************************************************************
2775 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2777 * Set the custom data for a type.
2779 * RETURNS
2781 * Success: S_OK.
2782 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2784 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2785 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2786 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2787 VARIANT* pVarVal) /* [I] The custom data. */
2789 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2791 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2793 if (!pVarVal)
2794 return E_INVALIDARG;
2796 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2799 /******************************************************************************
2800 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2802 * Set the custom data for a function.
2804 * RETURNS
2806 * Success: S_OK.
2807 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2809 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2810 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2811 UINT index, /* [I] The index of the function for which to set the custom data. */
2812 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2813 VARIANT* pVarVal) /* [I] The custom data. */
2815 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2816 CyclicList *iter;
2818 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2820 if(index >= (This->typeinfo->cElement&0xFFFF))
2821 return TYPE_E_ELEMENTNOTFOUND;
2823 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2824 if (iter->type == CyclicListFunc)
2825 if (index-- == 0)
2826 break;
2828 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
2829 if(!iter->u.data)
2830 return E_OUTOFMEMORY;
2832 iter->u.data[4] |= 0x80;
2833 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
2836 /******************************************************************************
2837 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
2839 * Set the custom data for a function parameter.
2841 * RETURNS
2843 * Success: S_OK.
2844 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2846 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
2847 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2848 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
2849 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
2850 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2851 VARIANT* pVarVal) /* [I] The custom data. */
2853 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
2854 return E_OUTOFMEMORY;
2857 /******************************************************************************
2858 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
2860 * Set the custom data for a variable.
2862 * RETURNS
2864 * Success: S_OK.
2865 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2867 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
2868 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2869 UINT index, /* [I] The index of the variable on which to set the custom data. */
2870 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2871 VARIANT* pVarVal) /* [I] The custom data. */
2873 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2874 return E_OUTOFMEMORY;
2877 /******************************************************************************
2878 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
2880 * Set the custom data for an implemented interface.
2882 * RETURNS
2884 * Success: S_OK.
2885 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2887 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
2888 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
2889 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
2890 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2891 VARIANT* pVarVal) /* [I] The custom data. */
2893 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2894 return E_OUTOFMEMORY;
2897 /******************************************************************************
2898 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
2900 * Set the help string context for the typeinfo.
2902 * RETURNS
2904 * Success: S_OK.
2905 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2907 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
2908 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
2909 ULONG dwHelpStringContext) /* [I] The help string context. */
2911 FIXME("(%p,%d), stub!\n", iface, dwHelpStringContext);
2912 return E_OUTOFMEMORY;
2915 /******************************************************************************
2916 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
2918 * Set the help string context for a function.
2920 * RETURNS
2922 * Success: S_OK.
2923 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2925 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
2926 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
2927 UINT index, /* [I] The index for the function on which to set the help string context. */
2928 ULONG dwHelpStringContext) /* [I] The help string context. */
2930 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
2931 return E_OUTOFMEMORY;
2934 /******************************************************************************
2935 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
2937 * Set the help string context for a variable.
2939 * RETURNS
2941 * Success: S_OK.
2942 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2944 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
2945 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
2946 UINT index, /* [I] The index of the variable on which to set the help string context. */
2947 ULONG dwHelpStringContext) /* [I] The help string context */
2949 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
2950 return E_OUTOFMEMORY;
2953 /******************************************************************************
2954 * ICreateTypeInfo2_Invalidate {OLEAUT32}
2956 * Undocumented function. (!)
2958 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
2959 ICreateTypeInfo2* iface)
2961 FIXME("(%p), stub!\n", iface);
2962 return E_OUTOFMEMORY;
2965 /******************************************************************************
2966 * ICreateTypeInfo2_SetName {OLEAUT32}
2968 * Set the name for a typeinfo.
2970 * RETURNS
2972 * Success: S_OK.
2973 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
2975 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
2976 ICreateTypeInfo2* iface,
2977 LPOLESTR szName)
2979 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
2980 return E_OUTOFMEMORY;
2983 /*================== ITypeInfo2 Implementation ===================================*/
2985 /******************************************************************************
2986 * ITypeInfo2_QueryInterface {OLEAUT32}
2988 * See IUnknown_QueryInterface.
2990 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
2992 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2994 return ICreateTypeInfo2_QueryInterface((ICreateTypeInfo2 *)This, riid, ppv);
2997 /******************************************************************************
2998 * ITypeInfo2_AddRef {OLEAUT32}
3000 * See IUnknown_AddRef.
3002 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3004 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3006 return ICreateTypeInfo2_AddRef((ICreateTypeInfo2 *)This);
3009 /******************************************************************************
3010 * ITypeInfo2_Release {OLEAUT32}
3012 * See IUnknown_Release.
3014 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3016 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3018 return ICreateTypeInfo2_Release((ICreateTypeInfo2 *)This);
3021 /******************************************************************************
3022 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3024 * See ITypeInfo_GetTypeAttr.
3026 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3027 ITypeInfo2* iface,
3028 TYPEATTR** ppTypeAttr)
3030 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3031 HRESULT hres;
3033 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3035 if(!ppTypeAttr)
3036 return E_INVALIDARG;
3038 hres = ICreateTypeInfo_LayOut((ICreateTypeInfo*)This);
3039 if(FAILED(hres))
3040 return hres;
3042 *ppTypeAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TYPEATTR));
3043 if(!*ppTypeAttr)
3044 return E_OUTOFMEMORY;
3046 if(This->typeinfo->posguid != -1) {
3047 MSFT_GuidEntry *guid;
3049 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3050 (*ppTypeAttr)->guid = guid->guid;
3053 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3054 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3055 (*ppTypeAttr)->typekind = This->typekind;
3056 (*ppTypeAttr)->cFuncs = This->typeinfo->cElement&0xffff;
3057 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3058 (*ppTypeAttr)->cFuncs += 7;
3059 (*ppTypeAttr)->cVars = This->typeinfo->cElement>>16;
3060 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3061 (*ppTypeAttr)->cbSizeVft = This->typekind==TKIND_DISPATCH ? 7 * sizeof(void*) : This->typeinfo->cbSizeVft;
3062 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3063 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3064 (*ppTypeAttr)->wMajorVerNum = This->typeinfo->version&0xffff;
3065 (*ppTypeAttr)->wMinorVerNum = This->typeinfo->version>>16;
3067 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3068 FIXME("TKIND_ALIAS handling not implemented\n");
3070 return S_OK;
3073 /******************************************************************************
3074 * ITypeInfo2_GetTypeComp {OLEAUT32}
3076 * See ITypeInfo_GetTypeComp.
3078 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3079 ITypeInfo2* iface,
3080 ITypeComp** ppTComp)
3082 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3083 return E_OUTOFMEMORY;
3086 /******************************************************************************
3087 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3089 * See ITypeInfo_GetFuncDesc.
3091 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3092 ITypeInfo2* iface,
3093 UINT index,
3094 FUNCDESC** ppFuncDesc)
3096 FIXME("(%p,%d,%p), stub!\n", iface, index, ppFuncDesc);
3097 return E_OUTOFMEMORY;
3100 /******************************************************************************
3101 * ITypeInfo2_GetVarDesc {OLEAUT32}
3103 * See ITypeInfo_GetVarDesc.
3105 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3106 ITypeInfo2* iface,
3107 UINT index,
3108 VARDESC** ppVarDesc)
3110 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3111 return E_OUTOFMEMORY;
3114 /******************************************************************************
3115 * ITypeInfo2_GetNames {OLEAUT32}
3117 * See ITypeInfo_GetNames.
3119 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3120 ITypeInfo2* iface,
3121 MEMBERID memid,
3122 BSTR* rgBstrNames,
3123 UINT cMaxNames,
3124 UINT* pcNames)
3126 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3127 return E_OUTOFMEMORY;
3130 /******************************************************************************
3131 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3133 * See ITypeInfo_GetRefTypeOfImplType.
3135 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3136 ITypeInfo2* iface,
3137 UINT index,
3138 HREFTYPE* pRefType)
3140 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3141 MSFT_RefRecord *ref;
3142 int offset;
3144 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3146 if(!pRefType)
3147 return E_INVALIDARG;
3149 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3150 if(index == -1) {
3151 *pRefType = -2;
3152 return S_OK;
3155 if(This->typekind == TKIND_DISPATCH)
3156 return ITypeInfo2_GetRefTypeOfImplType((ITypeInfo2*)&This->dual->lpVtblTypeInfo2,
3157 index, pRefType);
3160 if(index>=This->typeinfo->cImplTypes)
3161 return TYPE_E_ELEMENTNOTFOUND;
3163 if(This->typekind == TKIND_INTERFACE) {
3164 *pRefType = This->typeinfo->datatype1 + 2;
3165 return S_OK;
3168 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3169 if(offset == -1)
3170 return TYPE_E_ELEMENTNOTFOUND;
3172 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3173 *pRefType = ref->reftype;
3174 return S_OK;
3177 /******************************************************************************
3178 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3180 * See ITypeInfo_GetImplTypeFlags.
3182 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3183 ITypeInfo2* iface,
3184 UINT index,
3185 INT* pImplTypeFlags)
3187 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3188 int offset;
3189 MSFT_RefRecord *ref;
3191 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3193 if(!pImplTypeFlags)
3194 return E_INVALIDARG;
3196 if(index >= This->typeinfo->cImplTypes)
3197 return TYPE_E_ELEMENTNOTFOUND;
3199 if(This->typekind != TKIND_COCLASS) {
3200 *pImplTypeFlags = 0;
3201 return S_OK;
3204 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3205 if(offset == -1)
3206 return TYPE_E_ELEMENTNOTFOUND;
3208 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3209 *pImplTypeFlags = ref->flags;
3210 return S_OK;
3213 /******************************************************************************
3214 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3216 * See ITypeInfo_GetIDsOfNames.
3218 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3219 ITypeInfo2* iface,
3220 LPOLESTR* rgszNames,
3221 UINT cNames,
3222 MEMBERID* pMemId)
3224 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3225 return E_OUTOFMEMORY;
3228 /******************************************************************************
3229 * ITypeInfo2_Invoke {OLEAUT32}
3231 * See ITypeInfo_Invoke.
3233 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3234 ITypeInfo2* iface,
3235 PVOID pvInstance,
3236 MEMBERID memid,
3237 WORD wFlags,
3238 DISPPARAMS* pDispParams,
3239 VARIANT* pVarResult,
3240 EXCEPINFO* pExcepInfo,
3241 UINT* puArgErr)
3243 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3244 return E_OUTOFMEMORY;
3247 /******************************************************************************
3248 * ITypeInfo2_GetDocumentation {OLEAUT32}
3250 * See ITypeInfo_GetDocumentation.
3252 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3253 ITypeInfo2* iface,
3254 MEMBERID memid,
3255 BSTR* pBstrName,
3256 BSTR* pBstrDocString,
3257 DWORD* pdwHelpContext,
3258 BSTR* pBstrHelpFile)
3260 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3261 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3262 INT nameoffset, docstringoffset, helpcontext;
3264 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3266 if (memid == -1)
3268 nameoffset = This->typeinfo->NameOffset;
3269 docstringoffset = This->typeinfo->docstringoffs;
3270 helpcontext = This->typeinfo->helpcontext;
3271 status = S_OK;
3272 } else {
3273 CyclicList *iter;
3274 if (This->typedata) {
3275 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3276 if (iter->indice == memid) {
3277 if (iter->type == CyclicListFunc) {
3278 const int *typedata = iter->u.data;
3279 int size = typedata[0] - typedata[5]*(typedata[4]&0x1000?16:12);
3281 nameoffset = iter->name;
3282 /* FIXME implement this once SetFuncDocString is implemented */
3283 docstringoffset = -1;
3284 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3286 status = S_OK;
3287 } else {
3288 FIXME("Not implemented for variable members\n");
3291 break;
3297 if (!status) {
3298 WCHAR *string;
3299 if (pBstrName) {
3300 if (nameoffset == -1)
3301 *pBstrName = NULL;
3302 else {
3303 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3304 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3305 ctl2_decode_name((char*)&name->namelen, &string);
3306 *pBstrName = SysAllocString(string);
3307 if(!*pBstrName)
3308 return E_OUTOFMEMORY;
3312 if (pBstrDocString) {
3313 if (docstringoffset == -1)
3314 *pBstrDocString = NULL;
3315 else {
3316 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3317 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3318 ctl2_decode_name((char*)&name->namelen, &string);
3319 *pBstrDocString = SysAllocString(string);
3320 if(!*pBstrDocString) {
3321 if (pBstrName) SysFreeString(*pBstrName);
3322 return E_OUTOFMEMORY;
3327 if (pdwHelpContext) {
3328 *pdwHelpContext = helpcontext;
3331 if (pBstrHelpFile) {
3332 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->lpVtblTypeLib2,
3333 -1, NULL, NULL, NULL, pBstrHelpFile);
3334 if (status) {
3335 if (pBstrName) SysFreeString(*pBstrName);
3336 if (pBstrDocString) SysFreeString(*pBstrDocString);
3341 return status;
3344 /******************************************************************************
3345 * ITypeInfo2_GetDllEntry {OLEAUT32}
3347 * See ITypeInfo_GetDllEntry.
3349 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3350 ITypeInfo2* iface,
3351 MEMBERID memid,
3352 INVOKEKIND invKind,
3353 BSTR* pBstrDllName,
3354 BSTR* pBstrName,
3355 WORD* pwOrdinal)
3357 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3358 return E_OUTOFMEMORY;
3361 /******************************************************************************
3362 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3364 * See ITypeInfo_GetRefTypeInfo.
3366 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3367 ITypeInfo2* iface,
3368 HREFTYPE hRefType,
3369 ITypeInfo** ppTInfo)
3371 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3373 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3375 if(!ppTInfo)
3376 return E_INVALIDARG;
3378 if(hRefType==-2 && This->dual) {
3379 *ppTInfo = (ITypeInfo*)&This->dual->lpVtblTypeInfo2;
3380 ITypeInfo_AddRef(*ppTInfo);
3381 return S_OK;
3384 if(hRefType&1) {
3385 ITypeLib *tl;
3386 MSFT_ImpInfo *impinfo;
3387 MSFT_ImpFile *impfile;
3388 MSFT_GuidEntry *guid;
3389 WCHAR *filename;
3390 HRESULT hres;
3392 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3393 return E_FAIL;
3395 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3396 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3397 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3399 ctl2_decode_string(impfile->filename, &filename);
3401 hres = LoadTypeLib(filename, &tl);
3402 if(FAILED(hres))
3403 return hres;
3405 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3407 ITypeLib_Release(tl);
3408 return hres;
3409 } else {
3410 ICreateTypeInfo2Impl *iter;
3411 int i = 0;
3413 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3414 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3415 *ppTInfo = (ITypeInfo*)&iter->lpVtblTypeInfo2;
3417 ITypeLib_AddRef(*ppTInfo);
3418 return S_OK;
3420 i++;
3424 return E_FAIL;
3427 /******************************************************************************
3428 * ITypeInfo2_AddressOfMember {OLEAUT32}
3430 * See ITypeInfo_AddressOfMember.
3432 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3433 ITypeInfo2* iface,
3434 MEMBERID memid,
3435 INVOKEKIND invKind,
3436 PVOID* ppv)
3438 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3439 return E_OUTOFMEMORY;
3442 /******************************************************************************
3443 * ITypeInfo2_CreateInstance {OLEAUT32}
3445 * See ITypeInfo_CreateInstance.
3447 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3448 ITypeInfo2* iface,
3449 IUnknown* pUnkOuter,
3450 REFIID riid,
3451 PVOID* ppvObj)
3453 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3454 return E_OUTOFMEMORY;
3457 /******************************************************************************
3458 * ITypeInfo2_GetMops {OLEAUT32}
3460 * See ITypeInfo_GetMops.
3462 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3463 ITypeInfo2* iface,
3464 MEMBERID memid,
3465 BSTR* pBstrMops)
3467 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3468 return E_OUTOFMEMORY;
3471 /******************************************************************************
3472 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3474 * See ITypeInfo_GetContainingTypeLib.
3476 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3477 ITypeInfo2* iface,
3478 ITypeLib** ppTLib,
3479 UINT* pIndex)
3481 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3483 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3485 *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3486 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3487 *pIndex = This->typeinfo->typekind >> 16;
3489 return S_OK;
3492 /******************************************************************************
3493 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3495 * See ITypeInfo_ReleaseTypeAttr.
3497 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3498 ITypeInfo2* iface,
3499 TYPEATTR* pTypeAttr)
3501 TRACE("(%p,%p)\n", iface, pTypeAttr);
3503 HeapFree(GetProcessHeap(), 0, pTypeAttr);
3506 /******************************************************************************
3507 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3509 * See ITypeInfo_ReleaseFuncDesc.
3511 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3512 ITypeInfo2* iface,
3513 FUNCDESC* pFuncDesc)
3515 FIXME("(%p,%p), stub!\n", iface, pFuncDesc);
3518 /******************************************************************************
3519 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3521 * See ITypeInfo_ReleaseVarDesc.
3523 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3524 ITypeInfo2* iface,
3525 VARDESC* pVarDesc)
3527 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3530 /******************************************************************************
3531 * ITypeInfo2_GetTypeKind {OLEAUT32}
3533 * Get the TYPEKIND value for a TypeInfo.
3535 * RETURNS
3537 * Success: S_OK.
3538 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3540 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3541 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3542 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3544 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3545 return E_OUTOFMEMORY;
3548 /******************************************************************************
3549 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3551 * Get the Type Flags for a TypeInfo.
3553 * RETURNS
3555 * Success: S_OK.
3556 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3558 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3559 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3560 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3562 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3563 return E_OUTOFMEMORY;
3566 /******************************************************************************
3567 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3569 * Gets the index of a function given its member id.
3571 * RETURNS
3573 * Success: S_OK.
3574 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3576 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3577 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3578 MEMBERID memid, /* [I] The member id for the function. */
3579 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3580 UINT* pFuncIndex) /* [O] The index of the function. */
3582 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3583 return E_OUTOFMEMORY;
3586 /******************************************************************************
3587 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3589 * Gets the index of a variable given its member id.
3591 * RETURNS
3593 * Success: S_OK.
3594 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3596 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3597 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3598 MEMBERID memid, /* [I] The member id for the variable. */
3599 UINT* pVarIndex) /* [O] The index of the variable. */
3601 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3602 return E_OUTOFMEMORY;
3605 /******************************************************************************
3606 * ITypeInfo2_GetCustData {OLEAUT32}
3608 * Gets a custom data element from a TypeInfo.
3610 * RETURNS
3612 * Success: S_OK.
3613 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3615 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3616 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3617 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3618 VARIANT* pVarVal) /* [O] The custom data. */
3620 FIXME("(%p,%s,%p), stub!\n", iface, debugstr_guid(guid), pVarVal);
3621 return E_OUTOFMEMORY;
3624 /******************************************************************************
3625 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3627 * Gets a custom data element from a function.
3629 * RETURNS
3631 * Success: S_OK.
3632 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3634 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3635 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3636 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3637 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3638 VARIANT* pVarVal) /* [O] The custom data. */
3640 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3641 return E_OUTOFMEMORY;
3644 /******************************************************************************
3645 * ITypeInfo2_GetParamCustData {OLEAUT32}
3647 * Gets a custom data element from a parameter.
3649 * RETURNS
3651 * Success: S_OK.
3652 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3654 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3655 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3656 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3657 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3658 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3659 VARIANT* pVarVal) /* [O] The custom data. */
3661 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3662 return E_OUTOFMEMORY;
3665 /******************************************************************************
3666 * ITypeInfo2_GetVarCustData {OLEAUT32}
3668 * Gets a custom data element from a variable.
3670 * RETURNS
3672 * Success: S_OK.
3673 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3675 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3676 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3677 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3678 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3679 VARIANT* pVarVal) /* [O] The custom data. */
3681 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3682 return E_OUTOFMEMORY;
3685 /******************************************************************************
3686 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3688 * Gets a custom data element from an implemented type of a TypeInfo.
3690 * RETURNS
3692 * Success: S_OK.
3693 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3695 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3696 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3697 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3698 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3699 VARIANT* pVarVal) /* [O] The custom data. */
3701 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3702 return E_OUTOFMEMORY;
3705 /******************************************************************************
3706 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3708 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3710 * RETURNS
3712 * Success: S_OK.
3713 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3715 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3716 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
3717 MEMBERID memid, /* [I] The member id (why?). */
3718 LCID lcid, /* [I] The locale (why?). */
3719 BSTR* pbstrHelpString, /* [O] The help string. */
3720 DWORD* pdwHelpStringContext, /* [O] The help string context. */
3721 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
3723 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3724 return E_OUTOFMEMORY;
3727 /******************************************************************************
3728 * ITypeInfo2_GetAllCustData {OLEAUT32}
3730 * Gets all of the custom data associated with a TypeInfo.
3732 * RETURNS
3734 * Success: S_OK.
3735 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3737 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
3738 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3739 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3741 FIXME("(%p,%p), stub!\n", iface, pCustData);
3742 return E_OUTOFMEMORY;
3745 /******************************************************************************
3746 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
3748 * Gets all of the custom data associated with a function.
3750 * RETURNS
3752 * Success: S_OK.
3753 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3755 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
3756 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3757 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3758 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3760 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3761 return E_OUTOFMEMORY;
3764 /******************************************************************************
3765 * ITypeInfo2_GetAllParamCustData {OLEAUT32}
3767 * Gets all of the custom data associated with a parameter.
3769 * RETURNS
3771 * Success: S_OK.
3772 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3774 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
3775 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3776 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3777 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3778 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3780 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
3781 return E_OUTOFMEMORY;
3784 /******************************************************************************
3785 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
3787 * Gets all of the custom data associated with a variable.
3789 * RETURNS
3791 * Success: S_OK.
3792 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3794 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
3795 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3796 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3797 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3799 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3800 return E_OUTOFMEMORY;
3803 /******************************************************************************
3804 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
3806 * Gets all of the custom data associated with an implemented type.
3808 * RETURNS
3810 * Success: S_OK.
3811 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3813 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
3814 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3815 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3816 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3818 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3819 return E_OUTOFMEMORY;
3823 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
3825 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
3828 ICreateTypeInfo2_fnQueryInterface,
3829 ICreateTypeInfo2_fnAddRef,
3830 ICreateTypeInfo2_fnRelease,
3832 ICreateTypeInfo2_fnSetGuid,
3833 ICreateTypeInfo2_fnSetTypeFlags,
3834 ICreateTypeInfo2_fnSetDocString,
3835 ICreateTypeInfo2_fnSetHelpContext,
3836 ICreateTypeInfo2_fnSetVersion,
3837 ICreateTypeInfo2_fnAddRefTypeInfo,
3838 ICreateTypeInfo2_fnAddFuncDesc,
3839 ICreateTypeInfo2_fnAddImplType,
3840 ICreateTypeInfo2_fnSetImplTypeFlags,
3841 ICreateTypeInfo2_fnSetAlignment,
3842 ICreateTypeInfo2_fnSetSchema,
3843 ICreateTypeInfo2_fnAddVarDesc,
3844 ICreateTypeInfo2_fnSetFuncAndParamNames,
3845 ICreateTypeInfo2_fnSetVarName,
3846 ICreateTypeInfo2_fnSetTypeDescAlias,
3847 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
3848 ICreateTypeInfo2_fnSetFuncDocString,
3849 ICreateTypeInfo2_fnSetVarDocString,
3850 ICreateTypeInfo2_fnSetFuncHelpContext,
3851 ICreateTypeInfo2_fnSetVarHelpContext,
3852 ICreateTypeInfo2_fnSetMops,
3853 ICreateTypeInfo2_fnSetTypeIdldesc,
3854 ICreateTypeInfo2_fnLayOut,
3856 ICreateTypeInfo2_fnDeleteFuncDesc,
3857 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
3858 ICreateTypeInfo2_fnDeleteVarDesc,
3859 ICreateTypeInfo2_fnDeleteVarDescByMemId,
3860 ICreateTypeInfo2_fnDeleteImplType,
3861 ICreateTypeInfo2_fnSetCustData,
3862 ICreateTypeInfo2_fnSetFuncCustData,
3863 ICreateTypeInfo2_fnSetParamCustData,
3864 ICreateTypeInfo2_fnSetVarCustData,
3865 ICreateTypeInfo2_fnSetImplTypeCustData,
3866 ICreateTypeInfo2_fnSetHelpStringContext,
3867 ICreateTypeInfo2_fnSetFuncHelpStringContext,
3868 ICreateTypeInfo2_fnSetVarHelpStringContext,
3869 ICreateTypeInfo2_fnInvalidate,
3870 ICreateTypeInfo2_fnSetName
3873 static const ITypeInfo2Vtbl typeinfo2vt =
3876 ITypeInfo2_fnQueryInterface,
3877 ITypeInfo2_fnAddRef,
3878 ITypeInfo2_fnRelease,
3880 ITypeInfo2_fnGetTypeAttr,
3881 ITypeInfo2_fnGetTypeComp,
3882 ITypeInfo2_fnGetFuncDesc,
3883 ITypeInfo2_fnGetVarDesc,
3884 ITypeInfo2_fnGetNames,
3885 ITypeInfo2_fnGetRefTypeOfImplType,
3886 ITypeInfo2_fnGetImplTypeFlags,
3887 ITypeInfo2_fnGetIDsOfNames,
3888 ITypeInfo2_fnInvoke,
3889 ITypeInfo2_fnGetDocumentation,
3890 ITypeInfo2_fnGetDllEntry,
3891 ITypeInfo2_fnGetRefTypeInfo,
3892 ITypeInfo2_fnAddressOfMember,
3893 ITypeInfo2_fnCreateInstance,
3894 ITypeInfo2_fnGetMops,
3895 ITypeInfo2_fnGetContainingTypeLib,
3896 ITypeInfo2_fnReleaseTypeAttr,
3897 ITypeInfo2_fnReleaseFuncDesc,
3898 ITypeInfo2_fnReleaseVarDesc,
3900 ITypeInfo2_fnGetTypeKind,
3901 ITypeInfo2_fnGetTypeFlags,
3902 ITypeInfo2_fnGetFuncIndexOfMemId,
3903 ITypeInfo2_fnGetVarIndexOfMemId,
3904 ITypeInfo2_fnGetCustData,
3905 ITypeInfo2_fnGetFuncCustData,
3906 ITypeInfo2_fnGetParamCustData,
3907 ITypeInfo2_fnGetVarCustData,
3908 ITypeInfo2_fnGetImplTypeCustData,
3909 ITypeInfo2_fnGetDocumentation2,
3910 ITypeInfo2_fnGetAllCustData,
3911 ITypeInfo2_fnGetAllFuncCustData,
3912 ITypeInfo2_fnGetAllParamCustData,
3913 ITypeInfo2_fnGetAllVarCustData,
3914 ITypeInfo2_fnGetAllImplTypeCustData,
3917 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
3919 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
3921 int nameoffset;
3922 int typeinfo_offset;
3923 MSFT_TypeInfoBase *typeinfo;
3925 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
3927 pCreateTypeInfo2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeInfo2Impl));
3928 if (!pCreateTypeInfo2Impl) return NULL;
3930 pCreateTypeInfo2Impl->lpVtbl = &ctypeinfo2vt;
3931 pCreateTypeInfo2Impl->lpVtblTypeInfo2 = &typeinfo2vt;
3932 pCreateTypeInfo2Impl->ref = 1;
3934 pCreateTypeInfo2Impl->typelib = typelib;
3935 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
3937 nameoffset = ctl2_alloc_name(typelib, szName);
3938 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
3939 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
3941 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
3942 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
3944 pCreateTypeInfo2Impl->typeinfo = typeinfo;
3946 pCreateTypeInfo2Impl->typekind = tkind;
3947 typeinfo->typekind |= tkind | 0x20;
3948 ICreateTypeInfo2_SetAlignment((ICreateTypeInfo2 *)pCreateTypeInfo2Impl, 4);
3950 switch (tkind) {
3951 case TKIND_ENUM:
3952 case TKIND_INTERFACE:
3953 case TKIND_DISPATCH:
3954 case TKIND_COCLASS:
3955 typeinfo->size = 4;
3956 break;
3958 case TKIND_RECORD:
3959 case TKIND_UNION:
3960 typeinfo->size = 0;
3961 break;
3963 case TKIND_MODULE:
3964 typeinfo->size = 2;
3965 break;
3967 case TKIND_ALIAS:
3968 typeinfo->size = -0x75;
3969 break;
3971 default:
3972 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
3973 typeinfo->size = 0xdeadbeef;
3974 break;
3977 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
3978 typelib->last_typeinfo = pCreateTypeInfo2Impl;
3979 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
3981 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
3983 return (ICreateTypeInfo2 *)pCreateTypeInfo2Impl;
3987 /*================== ICreateTypeLib2 Implementation ===================================*/
3989 /******************************************************************************
3990 * ICreateTypeLib2_QueryInterface {OLEAUT32}
3992 * See IUnknown_QueryInterface.
3994 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
3995 ICreateTypeLib2 * iface,
3996 REFIID riid,
3997 VOID **ppvObject)
3999 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4001 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4003 *ppvObject=NULL;
4004 if(IsEqualIID(riid, &IID_IUnknown) ||
4005 IsEqualIID(riid,&IID_ICreateTypeLib)||
4006 IsEqualIID(riid,&IID_ICreateTypeLib2))
4008 *ppvObject = This;
4009 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4010 IsEqualIID(riid, &IID_ITypeLib2)) {
4011 *ppvObject = &This->lpVtblTypeLib2;
4014 if(*ppvObject)
4016 ICreateTypeLib2_AddRef(iface);
4017 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4018 return S_OK;
4020 TRACE("-- Interface: E_NOINTERFACE\n");
4021 return E_NOINTERFACE;
4024 /******************************************************************************
4025 * ICreateTypeLib2_AddRef {OLEAUT32}
4027 * See IUnknown_AddRef.
4029 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4031 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4032 ULONG ref = InterlockedIncrement(&This->ref);
4034 TRACE("(%p)->ref was %u\n",This, ref - 1);
4036 return ref;
4039 /******************************************************************************
4040 * ICreateTypeLib2_Release {OLEAUT32}
4042 * See IUnknown_Release.
4044 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4046 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4047 ULONG ref = InterlockedDecrement(&This->ref);
4049 TRACE("(%p)->(%u)\n",This, ref);
4051 if (!ref) {
4052 int i;
4054 for (i = 0; i < MSFT_SEG_MAX; i++) {
4055 HeapFree(GetProcessHeap(), 0, This->typelib_segment_data[i]);
4056 This->typelib_segment_data[i] = NULL;
4059 HeapFree(GetProcessHeap(), 0, This->filename);
4060 This->filename = NULL;
4062 while (This->typeinfos) {
4063 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4064 This->typeinfos = typeinfo->next_typeinfo;
4065 if(typeinfo->typedata) {
4066 CyclicList *iter, *rem;
4068 rem = typeinfo->typedata->next;
4069 typeinfo->typedata->next = NULL;
4070 iter = rem->next;
4071 HeapFree(GetProcessHeap(), 0, rem);
4073 while(iter) {
4074 rem = iter;
4075 iter = iter->next;
4076 HeapFree(GetProcessHeap(), 0, rem->u.data);
4077 HeapFree(GetProcessHeap(), 0, rem);
4081 HeapFree(GetProcessHeap(), 0, typeinfo->dual);
4082 HeapFree(GetProcessHeap(), 0, typeinfo);
4085 HeapFree(GetProcessHeap(),0,This);
4086 return 0;
4089 return ref;
4093 /******************************************************************************
4094 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4096 * See ICreateTypeLib_CreateTypeInfo.
4098 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4099 ICreateTypeLib2 * iface,
4100 LPOLESTR szName,
4101 TYPEKIND tkind,
4102 ICreateTypeInfo **ppCTInfo)
4104 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4105 char *name;
4107 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, ppCTInfo);
4109 ctl2_encode_name(This, szName, &name);
4110 if(ctl2_find_name(This, name) != -1)
4111 return TYPE_E_NAMECONFLICT;
4113 *ppCTInfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4115 if (!*ppCTInfo) return E_OUTOFMEMORY;
4117 return S_OK;
4120 /******************************************************************************
4121 * ICreateTypeLib2_SetName {OLEAUT32}
4123 * See ICreateTypeLib_SetName.
4125 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4126 ICreateTypeLib2 * iface,
4127 LPOLESTR szName)
4129 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4131 int offset;
4133 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4135 offset = ctl2_alloc_name(This, szName);
4136 if (offset == -1) return E_OUTOFMEMORY;
4137 This->typelib_header.NameOffset = offset;
4138 return S_OK;
4141 /******************************************************************************
4142 * ICreateTypeLib2_SetVersion {OLEAUT32}
4144 * See ICreateTypeLib_SetVersion.
4146 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4148 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4150 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4152 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4153 return S_OK;
4156 /******************************************************************************
4157 * ICreateTypeLib2_SetGuid {OLEAUT32}
4159 * See ICreateTypeLib_SetGuid.
4161 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4163 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4165 MSFT_GuidEntry guidentry;
4166 int offset;
4168 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4170 guidentry.guid = *guid;
4171 guidentry.hreftype = -2;
4172 guidentry.next_hash = -1;
4174 offset = ctl2_alloc_guid(This, &guidentry);
4176 if (offset == -1) return E_OUTOFMEMORY;
4178 This->typelib_header.posguid = offset;
4180 return S_OK;
4183 /******************************************************************************
4184 * ICreateTypeLib2_SetDocString {OLEAUT32}
4186 * See ICreateTypeLib_SetDocString.
4188 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4190 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4192 int offset;
4194 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4195 if (!szDoc)
4196 return E_INVALIDARG;
4198 offset = ctl2_alloc_string(This, szDoc);
4199 if (offset == -1) return E_OUTOFMEMORY;
4200 This->typelib_header.helpstring = offset;
4201 return S_OK;
4204 /******************************************************************************
4205 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4207 * See ICreateTypeLib_SetHelpFileName.
4209 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4211 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4213 int offset;
4215 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4217 offset = ctl2_alloc_string(This, szHelpFileName);
4218 if (offset == -1) return E_OUTOFMEMORY;
4219 This->typelib_header.helpfile = offset;
4220 This->typelib_header.varflags |= 0x10;
4221 return S_OK;
4224 /******************************************************************************
4225 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4227 * See ICreateTypeLib_SetHelpContext.
4229 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4231 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4233 TRACE("(%p,%d)\n", iface, dwHelpContext);
4234 This->typelib_header.helpcontext = dwHelpContext;
4235 return S_OK;
4238 /******************************************************************************
4239 * ICreateTypeLib2_SetLcid {OLEAUT32}
4241 * Sets both the lcid and lcid2 members in the header to lcid.
4243 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4244 * is set to US English while the second one is set to 0.
4246 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4248 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4250 TRACE("(%p,%d)\n", iface, lcid);
4252 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4254 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4256 return S_OK;
4259 /******************************************************************************
4260 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4262 * See ICreateTypeLib_SetLibFlags.
4264 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4266 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4268 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4270 This->typelib_header.flags = uLibFlags;
4272 return S_OK;
4275 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4277 DWORD dwWritten;
4278 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4279 CloseHandle(hFile);
4280 return 0;
4282 return -1;
4285 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4287 DWORD dwWritten;
4288 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4289 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4290 CloseHandle(hFile);
4291 return 0;
4294 return -1;
4297 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4299 ICreateTypeInfo2Impl *typeinfo;
4300 HRESULT hres;
4302 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4303 typeinfo->typeinfo->memoffset = filesize;
4305 hres = ICreateTypeInfo2_fnLayOut((ICreateTypeInfo2 *)typeinfo);
4306 if(FAILED(hres))
4307 return hres;
4309 if (typeinfo->typedata)
4310 filesize += typeinfo->typedata->next->u.val
4311 + ((typeinfo->typeinfo->cElement >> 16) * 12)
4312 + ((typeinfo->typeinfo->cElement & 0xffff) * 12) + 4;
4315 return S_OK;
4318 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4320 if (This->typelib_segdir[segment].length) {
4321 This->typelib_segdir[segment].offset = filepos;
4322 } else {
4323 This->typelib_segdir[segment].offset = -1;
4326 return This->typelib_segdir[segment].length;
4329 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4331 ICreateTypeInfo2Impl *typeinfo;
4333 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4334 CyclicList *iter;
4335 int offset = 0;
4337 if (!typeinfo->typedata) continue;
4339 iter = typeinfo->typedata->next;
4340 ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4341 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4342 ctl2_write_chunk(hFile, iter->u.data, iter->u.data[0] & 0xffff);
4344 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4345 ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4347 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4348 ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4350 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4351 ctl2_write_chunk(hFile, &offset, sizeof(int));
4352 offset += iter->u.data[0] & 0xffff;
4357 /******************************************************************************
4358 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4360 * See ICreateTypeLib_SaveAllChanges.
4362 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4364 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4366 int retval;
4367 int filepos;
4368 HANDLE hFile;
4369 HRESULT hres;
4371 TRACE("(%p)\n", iface);
4373 retval = TYPE_E_IOERROR;
4375 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4376 if (hFile == INVALID_HANDLE_VALUE) return retval;
4378 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4379 filepos += This->typelib_header.nrtypeinfos * 4;
4381 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4382 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4383 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4384 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4385 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4386 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4387 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4388 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4389 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4390 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4391 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4392 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4393 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4395 hres = ctl2_finalize_typeinfos(This, filepos);
4396 if(FAILED(hres)) {
4397 CloseHandle(hFile);
4398 return hres;
4401 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4402 if (This->typelib_header.varflags & HELPDLLFLAG)
4403 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4404 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4405 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4406 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4407 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4408 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4409 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4410 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4411 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4412 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4413 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4414 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4415 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4416 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4417 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4418 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4420 ctl2_write_typeinfos(This, hFile);
4422 if (!CloseHandle(hFile)) return retval;
4424 return S_OK;
4428 /******************************************************************************
4429 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4431 * Deletes a named TypeInfo from a type library.
4433 * RETURNS
4435 * Success: S_OK
4436 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4438 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4439 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4440 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4442 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4443 return E_OUTOFMEMORY;
4446 /******************************************************************************
4447 * ICreateTypeLib2_SetCustData {OLEAUT32}
4449 * Sets custom data for a type library.
4451 * RETURNS
4453 * Success: S_OK
4454 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4456 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4457 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4458 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4459 VARIANT *pVarVal) /* [I] The custom data itself. */
4461 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4463 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4465 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4468 /******************************************************************************
4469 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4471 * Sets a context number for the library help string.
4473 * PARAMS
4474 * iface [I] The type library to set the help string context for.
4475 * dwContext [I] The help string context.
4477 * RETURNS
4478 * Success: S_OK
4479 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4481 static
4482 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4483 ULONG dwContext)
4485 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4487 TRACE("(%p,%d)\n", iface, dwContext);
4489 This->typelib_header.helpstringcontext = dwContext;
4490 return S_OK;
4493 /******************************************************************************
4494 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4496 * Set the DLL used to look up localized help strings.
4498 * PARAMS
4499 * iface [I] The type library to set the help DLL for.
4500 * szDllName [I] The name of the help DLL.
4502 * RETURNS
4503 * Success: S_OK
4504 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4506 static
4507 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4508 LPOLESTR szDllName)
4510 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4511 int offset;
4513 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4514 if (!szDllName)
4515 return E_INVALIDARG;
4517 offset = ctl2_alloc_string(This, szDllName);
4518 if (offset == -1)
4519 return E_OUTOFMEMORY;
4520 This->typelib_header.varflags |= HELPDLLFLAG;
4521 This->helpStringDll = offset;
4522 return S_OK;
4525 /*================== ITypeLib2 Implementation ===================================*/
4527 /******************************************************************************
4528 * ITypeLib2_QueryInterface {OLEAUT32}
4530 * See IUnknown_QueryInterface.
4532 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4534 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4536 return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4539 /******************************************************************************
4540 * ITypeLib2_AddRef {OLEAUT32}
4542 * See IUnknown_AddRef.
4544 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4546 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4548 return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4551 /******************************************************************************
4552 * ITypeLib2_Release {OLEAUT32}
4554 * See IUnknown_Release.
4556 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4558 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4560 return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4563 /******************************************************************************
4564 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4566 * See ITypeLib_GetTypeInfoCount.
4568 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4569 ITypeLib2 * iface)
4571 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4573 TRACE("(%p)\n", iface);
4575 return This->typelib_header.nrtypeinfos;
4578 /******************************************************************************
4579 * ITypeLib2_GetTypeInfo {OLEAUT32}
4581 * See ITypeLib_GetTypeInfo.
4583 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4584 ITypeLib2 * iface,
4585 UINT index,
4586 ITypeInfo** ppTInfo)
4588 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4590 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4592 if (index >= This->typelib_header.nrtypeinfos) {
4593 return TYPE_E_ELEMENTNOTFOUND;
4596 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4599 /******************************************************************************
4600 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4602 * See ITypeLib_GetTypeInfoType.
4604 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4605 ITypeLib2 * iface,
4606 UINT index,
4607 TYPEKIND* pTKind)
4609 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4611 TRACE("(%p,%d,%p)\n", iface, index, pTKind);
4613 if (index >= This->typelib_header.nrtypeinfos) {
4614 return TYPE_E_ELEMENTNOTFOUND;
4617 *pTKind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 15;
4619 return S_OK;
4622 /******************************************************************************
4623 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4625 * See ITypeLib_GetTypeInfoOfGuid.
4627 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4628 ITypeLib2 * iface,
4629 REFGUID guid,
4630 ITypeInfo** ppTinfo)
4632 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4634 int guidoffset;
4635 int typeinfo;
4637 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4639 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4640 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4642 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4643 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4645 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4648 /******************************************************************************
4649 * ITypeLib2_GetLibAttr {OLEAUT32}
4651 * See ITypeLib_GetLibAttr.
4653 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4654 ITypeLib2 * iface,
4655 TLIBATTR** ppTLibAttr)
4657 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4659 TRACE("(%p,%p)\n", This, ppTLibAttr);
4661 if(!ppTLibAttr)
4662 return E_INVALIDARG;
4664 *ppTLibAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TLIBATTR));
4665 if(!*ppTLibAttr)
4666 return E_OUTOFMEMORY;
4668 if(This->typelib_header.posguid != -1) {
4669 MSFT_GuidEntry *guid;
4671 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4672 (*ppTLibAttr)->guid = guid->guid;
4675 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4676 (*ppTLibAttr)->syskind = This->typelib_header.varflags&0x3;
4677 (*ppTLibAttr)->wMajorVerNum = This->typelib_header.version&0xffff;
4678 (*ppTLibAttr)->wMinorVerNum = This->typelib_header.version>>16;
4679 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4680 return S_OK;
4683 /******************************************************************************
4684 * ITypeLib2_GetTypeComp {OLEAUT32}
4686 * See ITypeLib_GetTypeComp.
4688 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4689 ITypeLib2 * iface,
4690 ITypeComp** ppTComp)
4692 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4694 FIXME("(%p,%p), stub!\n", This, ppTComp);
4696 return E_OUTOFMEMORY;
4699 /******************************************************************************
4700 * ITypeLib2_GetDocumentation {OLEAUT32}
4702 * See ITypeLib_GetDocumentation.
4704 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4705 ITypeLib2 * iface,
4706 INT index,
4707 BSTR* pBstrName,
4708 BSTR* pBstrDocString,
4709 DWORD* pdwHelpContext,
4710 BSTR* pBstrHelpFile)
4712 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4713 WCHAR *string;
4715 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4717 if(index != -1) {
4718 ICreateTypeInfo2Impl *iter;
4720 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4721 index--;
4723 if(!iter)
4724 return TYPE_E_ELEMENTNOTFOUND;
4726 return ITypeInfo_GetDocumentation((ITypeInfo*)&iter->lpVtblTypeInfo2,
4727 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4730 if(pBstrName) {
4731 if(This->typelib_header.NameOffset == -1)
4732 *pBstrName = NULL;
4733 else {
4734 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4735 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4737 ctl2_decode_name((char*)&name->namelen, &string);
4739 *pBstrName = SysAllocString(string);
4740 if(!*pBstrName)
4741 return E_OUTOFMEMORY;
4745 if(pBstrDocString) {
4746 if(This->typelib_header.helpstring == -1)
4747 *pBstrDocString = NULL;
4748 else {
4749 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4751 *pBstrDocString = SysAllocString(string);
4752 if(!*pBstrDocString) {
4753 if(pBstrName) SysFreeString(*pBstrName);
4754 return E_OUTOFMEMORY;
4759 if(pdwHelpContext)
4760 *pdwHelpContext = This->typelib_header.helpcontext;
4762 if(pBstrHelpFile) {
4763 if(This->typelib_header.helpfile == -1)
4764 *pBstrHelpFile = NULL;
4765 else {
4766 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
4768 *pBstrHelpFile = SysAllocString(string);
4769 if(!*pBstrHelpFile) {
4770 if(pBstrName) SysFreeString(*pBstrName);
4771 if(pBstrDocString) SysFreeString(*pBstrDocString);
4772 return E_OUTOFMEMORY;
4777 return S_OK;
4780 /******************************************************************************
4781 * ITypeLib2_IsName {OLEAUT32}
4783 * See ITypeLib_IsName.
4785 static HRESULT WINAPI ITypeLib2_fnIsName(
4786 ITypeLib2 * iface,
4787 LPOLESTR szNameBuf,
4788 ULONG lHashVal,
4789 BOOL* pfName)
4791 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4793 char *encoded_name;
4794 int nameoffset;
4795 MSFT_NameIntro *nameintro;
4797 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
4799 ctl2_encode_name(This, szNameBuf, &encoded_name);
4800 nameoffset = ctl2_find_name(This, encoded_name);
4802 *pfName = 0;
4804 if (nameoffset == -1) return S_OK;
4806 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
4807 if (nameintro->hreftype == -1) return S_OK;
4809 *pfName = 1;
4811 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
4813 return S_OK;
4816 /******************************************************************************
4817 * ITypeLib2_FindName {OLEAUT32}
4819 * See ITypeLib_FindName.
4821 static HRESULT WINAPI ITypeLib2_fnFindName(
4822 ITypeLib2 * iface,
4823 LPOLESTR szNameBuf,
4824 ULONG lHashVal,
4825 ITypeInfo** ppTInfo,
4826 MEMBERID* rgMemId,
4827 USHORT* pcFound)
4829 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4831 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
4833 return E_OUTOFMEMORY;
4836 /******************************************************************************
4837 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
4839 * See ITypeLib_ReleaseTLibAttr.
4841 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
4842 ITypeLib2 * iface,
4843 TLIBATTR* pTLibAttr)
4845 TRACE("(%p,%p)\n", iface, pTLibAttr);
4847 HeapFree(GetProcessHeap(), 0, pTLibAttr);
4850 /******************************************************************************
4851 * ICreateTypeLib2_GetCustData {OLEAUT32}
4853 * Retrieves a custom data value stored on a type library.
4855 * RETURNS
4857 * Success: S_OK
4858 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4860 static HRESULT WINAPI ITypeLib2_fnGetCustData(
4861 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
4862 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
4863 VARIANT* pVarVal) /* [O] The custom data. */
4865 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4867 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
4869 return E_OUTOFMEMORY;
4872 /******************************************************************************
4873 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
4875 * Retrieves some statistics about names in a type library, supposedly for
4876 * hash table optimization purposes.
4878 * RETURNS
4880 * Success: S_OK
4881 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4883 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
4884 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
4885 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
4886 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
4888 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4890 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
4892 return E_OUTOFMEMORY;
4895 /******************************************************************************
4896 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
4898 * Obtain locale-aware help string information.
4900 * RETURNS
4902 * Success: S_OK
4903 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
4905 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
4906 ITypeLib2 * iface,
4907 INT index,
4908 LCID lcid,
4909 BSTR* pbstrHelpString,
4910 DWORD* pdwHelpStringContext,
4911 BSTR* pbstrHelpStringDll)
4913 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4915 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
4917 return E_OUTOFMEMORY;
4920 /******************************************************************************
4921 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
4923 * Retrieve all of the custom data for a type library.
4925 * RETURNS
4927 * Success: S_OK
4928 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4930 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
4931 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
4932 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
4934 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4936 FIXME("(%p,%p), stub!\n", This, pCustData);
4938 return E_OUTOFMEMORY;
4942 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
4944 static const ICreateTypeLib2Vtbl ctypelib2vt =
4947 ICreateTypeLib2_fnQueryInterface,
4948 ICreateTypeLib2_fnAddRef,
4949 ICreateTypeLib2_fnRelease,
4951 ICreateTypeLib2_fnCreateTypeInfo,
4952 ICreateTypeLib2_fnSetName,
4953 ICreateTypeLib2_fnSetVersion,
4954 ICreateTypeLib2_fnSetGuid,
4955 ICreateTypeLib2_fnSetDocString,
4956 ICreateTypeLib2_fnSetHelpFileName,
4957 ICreateTypeLib2_fnSetHelpContext,
4958 ICreateTypeLib2_fnSetLcid,
4959 ICreateTypeLib2_fnSetLibFlags,
4960 ICreateTypeLib2_fnSaveAllChanges,
4962 ICreateTypeLib2_fnDeleteTypeInfo,
4963 ICreateTypeLib2_fnSetCustData,
4964 ICreateTypeLib2_fnSetHelpStringContext,
4965 ICreateTypeLib2_fnSetHelpStringDll
4968 static const ITypeLib2Vtbl typelib2vt =
4971 ITypeLib2_fnQueryInterface,
4972 ITypeLib2_fnAddRef,
4973 ITypeLib2_fnRelease,
4975 ITypeLib2_fnGetTypeInfoCount,
4976 ITypeLib2_fnGetTypeInfo,
4977 ITypeLib2_fnGetTypeInfoType,
4978 ITypeLib2_fnGetTypeInfoOfGuid,
4979 ITypeLib2_fnGetLibAttr,
4980 ITypeLib2_fnGetTypeComp,
4981 ITypeLib2_fnGetDocumentation,
4982 ITypeLib2_fnIsName,
4983 ITypeLib2_fnFindName,
4984 ITypeLib2_fnReleaseTLibAttr,
4986 ITypeLib2_fnGetCustData,
4987 ITypeLib2_fnGetLibStatistics,
4988 ITypeLib2_fnGetDocumentation2,
4989 ITypeLib2_fnGetAllCustData,
4992 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
4994 ICreateTypeLib2Impl *pCreateTypeLib2Impl;
4995 int failed = 0;
4997 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
4999 pCreateTypeLib2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeLib2Impl));
5000 if (!pCreateTypeLib2Impl) return NULL;
5002 pCreateTypeLib2Impl->filename = HeapAlloc(GetProcessHeap(), 0, (strlenW(szFile) + 1) * sizeof(WCHAR));
5003 if (!pCreateTypeLib2Impl->filename) {
5004 HeapFree(GetProcessHeap(), 0, pCreateTypeLib2Impl);
5005 return NULL;
5007 strcpyW(pCreateTypeLib2Impl->filename, szFile);
5009 ctl2_init_header(pCreateTypeLib2Impl);
5010 ctl2_init_segdir(pCreateTypeLib2Impl);
5012 pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
5015 * The following two calls return an offset or -1 if out of memory. We
5016 * specifically need an offset of 0, however, so...
5018 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
5019 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
5021 pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
5022 pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
5024 memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
5025 memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
5027 pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
5028 pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
5029 pCreateTypeLib2Impl->ref = 1;
5031 if (failed) {
5032 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
5033 return 0;
5036 return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
5039 /******************************************************************************
5040 * CreateTypeLib2 [OLEAUT32.180]
5042 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5043 * library.
5045 * NOTES
5047 * See also CreateTypeLib.
5049 * RETURNS
5050 * Success: S_OK
5051 * Failure: Status
5053 HRESULT WINAPI CreateTypeLib2(
5054 SYSKIND syskind, /* [I] System type library is for */
5055 LPCOLESTR szFile, /* [I] Type library file name */
5056 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5058 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5060 if (!szFile) return E_INVALIDARG;
5061 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5062 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5065 /******************************************************************************
5066 * ClearCustData (OLEAUT32.171)
5068 * Clear a custom data types' data.
5070 * PARAMS
5071 * lpCust [I] The custom data type instance
5073 * RETURNS
5074 * Nothing.
5076 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5078 if (lpCust && lpCust->cCustData)
5080 if (lpCust->prgCustData)
5082 DWORD i;
5084 for (i = 0; i < lpCust->cCustData; i++)
5085 VariantClear(&lpCust->prgCustData[i].varValue);
5087 /* FIXME - Should be using a per-thread IMalloc */
5088 HeapFree(GetProcessHeap(), 0, lpCust->prgCustData);
5089 lpCust->prgCustData = NULL;
5091 lpCust->cCustData = 0;