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