kernel32/tests: Fix broken usage of the ok() macro return value.
[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     } else {
1803         FIXME("AddImplType unsupported on typekind %d\n", This->typeinfo->typekind & 15);
1804         return E_OUTOFMEMORY;
1805     }
1806
1807     This->typeinfo->cImplTypes++;
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     if((This->typeinfo->typekind&0xf) == TKIND_COCLASS)
2258         return S_OK;
2259
2260     /* Validate inheritance */
2261     This->typeinfo->datatype2 = 0;
2262     hreftype = This->typeinfo->datatype1;
2263
2264     /* Process internally defined interfaces */
2265     for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2266         MSFT_TypeInfoBase *header;
2267
2268         if(hreftype&1)
2269             break;
2270
2271         header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2272         This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2273         hreftype = header->datatype1;
2274     }
2275     if(i == This->typelib->typelib_header.nrtypeinfos)
2276         return TYPE_E_CIRCULARTYPE;
2277
2278     /* Process externally defined interfaces */
2279     if(hreftype != -1) {
2280         ITypeInfo *cur, *next;
2281         TYPEATTR *typeattr;
2282
2283         hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2284         if(FAILED(hres))
2285             return hres;
2286
2287         hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2288         if(FAILED(hres))
2289             return hres;
2290
2291         ITypeInfo_Release(next);
2292
2293         while(1) {
2294             hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2295             if(FAILED(hres))
2296                 return hres;
2297
2298             This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2299             ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2300
2301             hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2302             if(hres == TYPE_E_ELEMENTNOTFOUND)
2303                 break;
2304             if(FAILED(hres))
2305                 return hres;
2306
2307             hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2308             if(FAILED(hres))
2309                 return hres;
2310
2311             ITypeInfo_Release(cur);
2312             cur = next;
2313         }
2314     }
2315
2316     This->typeinfo->cbSizeVft = (This->typeinfo->datatype2>>16) * 4;
2317     if(!This->typedata)
2318         return S_OK;
2319
2320     typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList*)*(This->typeinfo->cElement&0xffff));
2321     if(!typedata)
2322         return E_OUTOFMEMORY;
2323
2324     /* Assign IDs and VTBL entries */
2325     i = 0;
2326     for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2327         /* Assign MEMBERID if MEMBERID_NIL was specified */
2328         if(iter->indice == MEMBERID_NIL) {
2329             iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2330
2331             for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2332                 if(iter == iter2) continue;
2333                 if(iter2->indice == iter->indice) {
2334                     iter->indice = 0x5fffffff + This->typeinfo->cElement + i + (This->typeinfo->datatype2<<16);
2335
2336                     for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2337                         if(iter == iter2) continue;
2338                         if(iter2->indice == iter->indice)
2339                             return E_ACCESSDENIED;
2340                     }
2341
2342                     break;
2343                 }
2344             }
2345         }
2346
2347         typedata[i] = iter;
2348
2349         iter->u.data[0] = (iter->u.data[0]&0xffff) | (i<<16);
2350
2351         if((This->typeinfo->typekind&0xf) != TKIND_MODULE) {
2352             iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2353             This->typeinfo->cbSizeVft += 4;
2354         }
2355
2356         /* Construct a list of elements with the same memberid */
2357         iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2358         for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2359             if(iter->indice == iter2->indice) {
2360                 int v1, v2;
2361
2362                 v1 = iter->u.data[4] >> 16;
2363                 v2 = iter2->u.data[4] >> 16;
2364
2365                 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2366                 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2367                 break;
2368             }
2369         }
2370
2371         i++;
2372     }
2373
2374     for(i=0; i<(This->typeinfo->cElement&0xffff); i++) {
2375         if(typedata[i]->u.data[4]>>16 > i) {
2376             int inv;
2377
2378             inv = (typedata[i]->u.data[4]>>3) & 0xf;
2379             i = typedata[i]->u.data[4] >> 16;
2380
2381             while(i > typedata[i]->u.data[4]>>16) {
2382                 int invkind = (typedata[i]->u.data[4]>>3) & 0xf;
2383
2384                 if(inv & invkind) {
2385                     HeapFree(GetProcessHeap(), 0, typedata);
2386                     return TYPE_E_DUPLICATEID;
2387                 }
2388
2389                 i = typedata[i]->u.data[4] >> 16;
2390                 inv |= invkind;
2391             }
2392
2393             if(inv & INVOKE_FUNC) {
2394                 HeapFree(GetProcessHeap(), 0, typedata);
2395                 return TYPE_E_INCONSISTENTPROPFUNCS;
2396             }
2397         }
2398     }
2399
2400     HeapFree(GetProcessHeap(), 0, typedata);
2401     return S_OK;
2402 }
2403
2404 /******************************************************************************
2405  * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2406  *
2407  *  Delete a function description from a type.
2408  *
2409  * RETURNS
2410  *
2411  *  Success: S_OK.
2412  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2413  */
2414 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2415         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2416         UINT index)              /* [I] The index of the function to delete. */
2417 {
2418     FIXME("(%p,%d), stub!\n", iface, index);
2419     return E_OUTOFMEMORY;
2420 }
2421
2422 /******************************************************************************
2423  * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2424  *
2425  *  Delete a function description from a type.
2426  *
2427  * RETURNS
2428  *
2429  *  Success: S_OK.
2430  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2431  */
2432 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2433         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2434         MEMBERID memid,          /* [I] The member id of the function to delete. */
2435         INVOKEKIND invKind)      /* [I] The invocation type of the function to delete. (?) */
2436 {
2437     FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2438     return E_OUTOFMEMORY;
2439 }
2440
2441 /******************************************************************************
2442  * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2443  *
2444  *  Delete a variable description from a type.
2445  *
2446  * RETURNS
2447  *
2448  *  Success: S_OK.
2449  *  Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2450  *  TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2451  */
2452 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2453         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2454         UINT index)              /* [I] The index of the variable description to delete. */
2455 {
2456     FIXME("(%p,%d), stub!\n", iface, index);
2457     return E_OUTOFMEMORY;
2458 }
2459
2460 /******************************************************************************
2461  * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2462  *
2463  *  Delete a variable description from a type.
2464  *
2465  * RETURNS
2466  *
2467  *  Success: S_OK.
2468  *  Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2469  *  TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2470  */
2471 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2472         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2473         MEMBERID memid)          /* [I] The member id of the variable description to delete. */
2474 {
2475     FIXME("(%p,%d), stub!\n", iface, memid);
2476     return E_OUTOFMEMORY;
2477 }
2478
2479 /******************************************************************************
2480  * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2481  *
2482  *  Delete an interface implementation from a type. (?)
2483  *
2484  * RETURNS
2485  *
2486  *  Success: S_OK.
2487  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2488  */
2489 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2490         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2491         UINT index)              /* [I] The index of the interface to delete. */
2492 {
2493     FIXME("(%p,%d), stub!\n", iface, index);
2494     return E_OUTOFMEMORY;
2495 }
2496
2497 /******************************************************************************
2498  * ICreateTypeInfo2_SetCustData {OLEAUT32}
2499  *
2500  *  Set the custom data for a type.
2501  *
2502  * RETURNS
2503  *
2504  *  Success: S_OK.
2505  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2506  */
2507 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2508         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2509         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2510         VARIANT* pVarVal)        /* [I] The custom data. */
2511 {
2512     FIXME("(%p,%s,%p), stub!\n", iface, debugstr_guid(guid), pVarVal);
2513     return E_OUTOFMEMORY;
2514 }
2515
2516 /******************************************************************************
2517  * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2518  *
2519  *  Set the custom data for a function.
2520  *
2521  * RETURNS
2522  *
2523  *  Success: S_OK.
2524  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2525  */
2526 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2527         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2528         UINT index,              /* [I] The index of the function for which to set the custom data. */
2529         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2530         VARIANT* pVarVal)        /* [I] The custom data. */
2531 {
2532     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2533     return E_OUTOFMEMORY;
2534 }
2535
2536 /******************************************************************************
2537  * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
2538  *
2539  *  Set the custom data for a function parameter.
2540  *
2541  * RETURNS
2542  *
2543  *  Success: S_OK.
2544  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2545  */
2546 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
2547         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2548         UINT indexFunc,          /* [I] The index of the function on which the parameter resides. */
2549         UINT indexParam,         /* [I] The index of the parameter on which to set the custom data. */
2550         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2551         VARIANT* pVarVal)        /* [I] The custom data. */
2552 {
2553     FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
2554     return E_OUTOFMEMORY;
2555 }
2556
2557 /******************************************************************************
2558  * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
2559  *
2560  *  Set the custom data for a variable.
2561  *
2562  * RETURNS
2563  *
2564  *  Success: S_OK.
2565  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2566  */
2567 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
2568         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2569         UINT index,              /* [I] The index of the variable on which to set the custom data. */
2570         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2571         VARIANT* pVarVal)        /* [I] The custom data. */
2572 {
2573     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2574     return E_OUTOFMEMORY;
2575 }
2576
2577 /******************************************************************************
2578  * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
2579  *
2580  *  Set the custom data for an implemented interface.
2581  *
2582  * RETURNS
2583  *
2584  *  Success: S_OK.
2585  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2586  */
2587 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
2588         ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
2589         UINT index,              /* [I] The index of the implemented interface on which to set the custom data. */
2590         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2591         VARIANT* pVarVal)        /* [I] The custom data. */
2592 {
2593     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2594     return E_OUTOFMEMORY;
2595 }
2596
2597 /******************************************************************************
2598  * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
2599  *
2600  *  Set the help string context for the typeinfo.
2601  *
2602  * RETURNS
2603  *
2604  *  Success: S_OK.
2605  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2606  */
2607 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
2608         ICreateTypeInfo2* iface,   /* [I] The typeinfo on which to set the help string context. */
2609         ULONG dwHelpStringContext) /* [I] The help string context. */
2610 {
2611     FIXME("(%p,%d), stub!\n", iface, dwHelpStringContext);
2612     return E_OUTOFMEMORY;
2613 }
2614
2615 /******************************************************************************
2616  * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
2617  *
2618  *  Set the help string context for a function.
2619  *
2620  * RETURNS
2621  *
2622  *  Success: S_OK.
2623  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2624  */
2625 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
2626         ICreateTypeInfo2* iface,   /* [I] The typeinfo on which to set the help string context. */
2627         UINT index,                /* [I] The index for the function on which to set the help string context. */
2628         ULONG dwHelpStringContext) /* [I] The help string context. */
2629 {
2630     FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
2631     return E_OUTOFMEMORY;
2632 }
2633
2634 /******************************************************************************
2635  * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
2636  *
2637  *  Set the help string context for a variable.
2638  *
2639  * RETURNS
2640  *
2641  *  Success: S_OK.
2642  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2643  */
2644 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
2645         ICreateTypeInfo2* iface,   /* [I] The typeinfo on which to set the help string context. */
2646         UINT index,                /* [I] The index of the variable on which to set the help string context. */
2647         ULONG dwHelpStringContext) /* [I] The help string context */
2648 {
2649     FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
2650     return E_OUTOFMEMORY;
2651 }
2652
2653 /******************************************************************************
2654  * ICreateTypeInfo2_Invalidate {OLEAUT32}
2655  *
2656  *  Undocumented function. (!)
2657  */
2658 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
2659         ICreateTypeInfo2* iface)
2660 {
2661     FIXME("(%p), stub!\n", iface);
2662     return E_OUTOFMEMORY;
2663 }
2664
2665 /******************************************************************************
2666  * ICreateTypeInfo2_SetName {OLEAUT32}
2667  *
2668  *  Set the name for a typeinfo.
2669  *
2670  * RETURNS
2671  *
2672  *  Success: S_OK.
2673  *  Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
2674  */
2675 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
2676         ICreateTypeInfo2* iface,
2677         LPOLESTR szName)
2678 {
2679     FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
2680     return E_OUTOFMEMORY;
2681 }
2682
2683 /*================== ITypeInfo2 Implementation ===================================*/
2684
2685 /******************************************************************************
2686  * ITypeInfo2_QueryInterface {OLEAUT32}
2687  *
2688  *  See IUnknown_QueryInterface.
2689  */
2690 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
2691 {
2692     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2693
2694     return ICreateTypeInfo2_QueryInterface((ICreateTypeInfo2 *)This, riid, ppv);
2695 }
2696
2697 /******************************************************************************
2698  * ITypeInfo2_AddRef {OLEAUT32}
2699  *
2700  *  See IUnknown_AddRef.
2701  */
2702 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
2703 {
2704     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2705
2706     return ICreateTypeInfo2_AddRef((ICreateTypeInfo2 *)This);
2707 }
2708
2709 /******************************************************************************
2710  * ITypeInfo2_Release {OLEAUT32}
2711  *
2712  *  See IUnknown_Release.
2713  */
2714 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
2715 {
2716     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2717
2718     return ICreateTypeInfo2_Release((ICreateTypeInfo2 *)This);
2719 }
2720
2721 /******************************************************************************
2722  * ITypeInfo2_GetTypeAttr {OLEAUT32}
2723  *
2724  *  See ITypeInfo_GetTypeAttr.
2725  */
2726 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
2727         ITypeInfo2* iface,
2728         TYPEATTR** ppTypeAttr)
2729 {
2730     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2731     HRESULT hres;
2732
2733     TRACE("(%p,%p)\n", iface, ppTypeAttr);
2734
2735     if(!ppTypeAttr)
2736         return E_INVALIDARG;
2737
2738     hres = ICreateTypeInfo_LayOut((ICreateTypeInfo*)This);
2739     if(FAILED(hres))
2740         return hres;
2741
2742     *ppTypeAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TYPEATTR));
2743     if(!*ppTypeAttr)
2744         return E_OUTOFMEMORY;
2745
2746     if(This->typeinfo->posguid != -1) {
2747         MSFT_GuidEntry *guid;
2748
2749         guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
2750         (*ppTypeAttr)->guid = guid->guid;
2751     }
2752
2753     (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
2754     (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
2755     (*ppTypeAttr)->typekind = This->typeinfo->typekind&0xf;
2756     (*ppTypeAttr)->cFuncs = This->typeinfo->cElement&0xffff;
2757     (*ppTypeAttr)->cVars = This->typeinfo->cElement>>16;
2758     (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
2759     (*ppTypeAttr)->cbSizeVft = This->typeinfo->cbSizeVft;
2760     (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
2761     (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
2762     (*ppTypeAttr)->wMajorVerNum = This->typeinfo->version&0xffff;
2763     (*ppTypeAttr)->wMinorVerNum = This->typeinfo->version>>16;
2764
2765     if((*ppTypeAttr)->typekind == TKIND_ALIAS)
2766         FIXME("TKIND_ALIAS handling not implemented\n");
2767
2768     return S_OK;
2769 }
2770
2771 /******************************************************************************
2772  * ITypeInfo2_GetTypeComp {OLEAUT32}
2773  *
2774  *  See ITypeInfo_GetTypeComp.
2775  */
2776 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
2777         ITypeInfo2* iface,
2778         ITypeComp** ppTComp)
2779 {
2780     FIXME("(%p,%p), stub!\n", iface, ppTComp);
2781     return E_OUTOFMEMORY;
2782 }
2783
2784 /******************************************************************************
2785  * ITypeInfo2_GetFuncDesc {OLEAUT32}
2786  *
2787  *  See ITypeInfo_GetFuncDesc.
2788  */
2789 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
2790         ITypeInfo2* iface,
2791         UINT index,
2792         FUNCDESC** ppFuncDesc)
2793 {
2794     FIXME("(%p,%d,%p), stub!\n", iface, index, ppFuncDesc);
2795     return E_OUTOFMEMORY;
2796 }
2797
2798 /******************************************************************************
2799  * ITypeInfo2_GetVarDesc {OLEAUT32}
2800  *
2801  *  See ITypeInfo_GetVarDesc.
2802  */
2803 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
2804         ITypeInfo2* iface,
2805         UINT index,
2806         VARDESC** ppVarDesc)
2807 {
2808     FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
2809     return E_OUTOFMEMORY;
2810 }
2811
2812 /******************************************************************************
2813  * ITypeInfo2_GetNames {OLEAUT32}
2814  *
2815  *  See ITypeInfo_GetNames.
2816  */
2817 static HRESULT WINAPI ITypeInfo2_fnGetNames(
2818         ITypeInfo2* iface,
2819         MEMBERID memid,
2820         BSTR* rgBstrNames,
2821         UINT cMaxNames,
2822         UINT* pcNames)
2823 {
2824     FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
2825     return E_OUTOFMEMORY;
2826 }
2827
2828 /******************************************************************************
2829  * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
2830  *
2831  *  See ITypeInfo_GetRefTypeOfImplType.
2832  */
2833 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
2834         ITypeInfo2* iface,
2835         UINT index,
2836         HREFTYPE* pRefType)
2837 {
2838     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2839     MSFT_RefRecord *ref;
2840     int offset;
2841
2842     TRACE("(%p,%d,%p)\n", iface, index, pRefType);
2843
2844     if(!pRefType)
2845         return E_INVALIDARG;
2846
2847     if(index == -1) {
2848         FIXME("Dual interfaces not handled yet\n");
2849         return E_NOTIMPL;
2850     }
2851
2852     if(index >= This->typeinfo->cImplTypes)
2853         return TYPE_E_ELEMENTNOTFOUND;
2854
2855     if((This->typeinfo->typekind&0xf) == TKIND_INTERFACE) {
2856         *pRefType = This->typeinfo->datatype1 + 2;
2857         return S_OK;
2858     }
2859
2860     offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2861     if(offset == -1)
2862         return TYPE_E_ELEMENTNOTFOUND;
2863
2864     ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2865     *pRefType = ref->reftype;
2866     return S_OK;
2867 }
2868
2869 /******************************************************************************
2870  * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
2871  *
2872  *  See ITypeInfo_GetImplTypeFlags.
2873  */
2874 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
2875         ITypeInfo2* iface,
2876         UINT index,
2877         INT* pImplTypeFlags)
2878 {
2879     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2880     int offset;
2881     MSFT_RefRecord *ref;
2882
2883     TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
2884
2885     if(!pImplTypeFlags)
2886         return E_INVALIDARG;
2887
2888     if(index >= This->typeinfo->cImplTypes)
2889         return TYPE_E_ELEMENTNOTFOUND;
2890
2891     if((This->typeinfo->typekind&0xf) != TKIND_COCLASS) {
2892         *pImplTypeFlags = 0;
2893         return S_OK;
2894     }
2895
2896     offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2897     if(offset == -1)
2898         return TYPE_E_ELEMENTNOTFOUND;
2899
2900     ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2901     *pImplTypeFlags = ref->flags;
2902     return S_OK;
2903 }
2904
2905 /******************************************************************************
2906  * ITypeInfo2_GetIDsOfNames {OLEAUT32}
2907  *
2908  *  See ITypeInfo_GetIDsOfNames.
2909  */
2910 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
2911         ITypeInfo2* iface,
2912         LPOLESTR* rgszNames,
2913         UINT cNames,
2914         MEMBERID* pMemId)
2915 {
2916     FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
2917     return E_OUTOFMEMORY;
2918 }
2919
2920 /******************************************************************************
2921  * ITypeInfo2_Invoke {OLEAUT32}
2922  *
2923  *  See ITypeInfo_Invoke.
2924  */
2925 static HRESULT WINAPI ITypeInfo2_fnInvoke(
2926         ITypeInfo2* iface,
2927         PVOID pvInstance,
2928         MEMBERID memid,
2929         WORD wFlags,
2930         DISPPARAMS* pDispParams,
2931         VARIANT* pVarResult,
2932         EXCEPINFO* pExcepInfo,
2933         UINT* puArgErr)
2934 {
2935     FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2936     return E_OUTOFMEMORY;
2937 }
2938
2939 /******************************************************************************
2940  * ITypeInfo2_GetDocumentation {OLEAUT32}
2941  *
2942  *  See ITypeInfo_GetDocumentation.
2943  */
2944 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
2945         ITypeInfo2* iface,
2946         MEMBERID memid,
2947         BSTR* pBstrName,
2948         BSTR* pBstrDocString,
2949         DWORD* pdwHelpContext,
2950         BSTR* pBstrHelpFile)
2951 {
2952     FIXME("(%p,%d,%p,%p,%p,%p), stub!\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
2953     return E_OUTOFMEMORY;
2954 }
2955
2956 /******************************************************************************
2957  * ITypeInfo2_GetDllEntry {OLEAUT32}
2958  *
2959  *  See ITypeInfo_GetDllEntry.
2960  */
2961 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
2962         ITypeInfo2* iface,
2963         MEMBERID memid,
2964         INVOKEKIND invKind,
2965         BSTR* pBstrDllName,
2966         BSTR* pBstrName,
2967         WORD* pwOrdinal)
2968 {
2969     FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
2970     return E_OUTOFMEMORY;
2971 }
2972
2973 /******************************************************************************
2974  * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
2975  *
2976  *  See ITypeInfo_GetRefTypeInfo.
2977  */
2978 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
2979         ITypeInfo2* iface,
2980         HREFTYPE hRefType,
2981         ITypeInfo** ppTInfo)
2982 {
2983     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2984
2985     TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
2986
2987     if(!ppTInfo)
2988         return E_INVALIDARG;
2989
2990     if(hRefType&1) {
2991         ITypeLib *tl;
2992         MSFT_ImpInfo *impinfo;
2993         MSFT_ImpFile *impfile;
2994         MSFT_GuidEntry *guid;
2995         WCHAR *filename;
2996         HRESULT hres;
2997
2998         if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
2999             return E_FAIL;
3000
3001         impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3002         impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3003         guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3004
3005         ctl2_decode_string(impfile->filename, &filename);
3006
3007         hres = LoadTypeLib(filename, &tl);
3008         if(FAILED(hres))
3009             return hres;
3010
3011         hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3012
3013         ITypeLib_Release(tl);
3014         return hres;
3015     } else {
3016         ICreateTypeInfo2Impl *iter;
3017         int i = 0;
3018
3019         for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3020             if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3021                 *ppTInfo = (ITypeInfo*)&iter->lpVtblTypeInfo2;
3022
3023                 ITypeLib_AddRef(*ppTInfo);
3024                 return S_OK;
3025             }
3026             i++;
3027         }
3028     }
3029
3030     return E_FAIL;
3031 }
3032
3033 /******************************************************************************
3034  * ITypeInfo2_AddressOfMember {OLEAUT32}
3035  *
3036  *  See ITypeInfo_AddressOfMember.
3037  */
3038 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3039         ITypeInfo2* iface,
3040         MEMBERID memid,
3041         INVOKEKIND invKind,
3042         PVOID* ppv)
3043 {
3044     FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3045     return E_OUTOFMEMORY;
3046 }
3047
3048 /******************************************************************************
3049  * ITypeInfo2_CreateInstance {OLEAUT32}
3050  *
3051  *  See ITypeInfo_CreateInstance.
3052  */
3053 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3054         ITypeInfo2* iface,
3055         IUnknown* pUnkOuter,
3056         REFIID riid,
3057         PVOID* ppvObj)
3058 {
3059     FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3060     return E_OUTOFMEMORY;
3061 }
3062
3063 /******************************************************************************
3064  * ITypeInfo2_GetMops {OLEAUT32}
3065  *
3066  *  See ITypeInfo_GetMops.
3067  */
3068 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3069         ITypeInfo2* iface,
3070         MEMBERID memid,
3071         BSTR* pBstrMops)
3072 {
3073     FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3074     return E_OUTOFMEMORY;
3075 }
3076
3077 /******************************************************************************
3078  * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3079  *
3080  *  See ITypeInfo_GetContainingTypeLib.
3081  */
3082 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3083         ITypeInfo2* iface,
3084         ITypeLib** ppTLib,
3085         UINT* pIndex)
3086 {
3087     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3088
3089     TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3090     
3091     *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3092     This->typelib->ref++;
3093     *pIndex = This->typeinfo->typekind >> 16;
3094
3095     return S_OK;
3096 }
3097
3098 /******************************************************************************
3099  * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3100  *
3101  *  See ITypeInfo_ReleaseTypeAttr.
3102  */
3103 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3104         ITypeInfo2* iface,
3105         TYPEATTR* pTypeAttr)
3106 {
3107     TRACE("(%p,%p)\n", iface, pTypeAttr);
3108
3109     if(pTypeAttr)
3110         HeapFree(GetProcessHeap(), 0, pTypeAttr);
3111 }
3112
3113 /******************************************************************************
3114  * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3115  *
3116  *  See ITypeInfo_ReleaseFuncDesc.
3117  */
3118 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3119         ITypeInfo2* iface,
3120         FUNCDESC* pFuncDesc)
3121 {
3122     FIXME("(%p,%p), stub!\n", iface, pFuncDesc);
3123 }
3124
3125 /******************************************************************************
3126  * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3127  *
3128  *  See ITypeInfo_ReleaseVarDesc.
3129  */
3130 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3131         ITypeInfo2* iface,
3132         VARDESC* pVarDesc)
3133 {
3134     FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3135 }
3136
3137 /******************************************************************************
3138  * ITypeInfo2_GetTypeKind {OLEAUT32}
3139  *
3140  *  Get the TYPEKIND value for a TypeInfo.
3141  *
3142  * RETURNS
3143  *
3144  *  Success: S_OK.
3145  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3146  */
3147 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3148         ITypeInfo2* iface,   /* [I] The TypeInfo to obtain the typekind for. */
3149         TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3150 {
3151     FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3152     return E_OUTOFMEMORY;
3153 }
3154
3155 /******************************************************************************
3156  * ITypeInfo2_GetTypeFlags {OLEAUT32}
3157  *
3158  *  Get the Type Flags for a TypeInfo.
3159  *
3160  * RETURNS
3161  *
3162  *  Success: S_OK.
3163  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3164  */
3165 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3166         ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3167         ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3168 {
3169     FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3170     return E_OUTOFMEMORY;
3171 }
3172
3173 /******************************************************************************
3174  * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3175  *
3176  *  Gets the index of a function given its member id.
3177  *
3178  * RETURNS
3179  *
3180  *  Success: S_OK.
3181  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3182  */
3183 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3184         ITypeInfo2* iface,  /* [I] The TypeInfo in which to find the function. */
3185         MEMBERID memid,     /* [I] The member id for the function. */
3186         INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3187         UINT* pFuncIndex)   /* [O] The index of the function. */
3188 {
3189     FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3190     return E_OUTOFMEMORY;
3191 }
3192
3193 /******************************************************************************
3194  * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3195  *
3196  *  Gets the index of a variable given its member id.
3197  *
3198  * RETURNS
3199  *
3200  *  Success: S_OK.
3201  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3202  */
3203 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3204         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3205         MEMBERID memid,    /* [I] The member id for the variable. */
3206         UINT* pVarIndex)   /* [O] The index of the variable. */
3207 {
3208     FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3209     return E_OUTOFMEMORY;
3210 }
3211
3212 /******************************************************************************
3213  * ITypeInfo2_GetCustData {OLEAUT32}
3214  *
3215  *  Gets a custom data element from a TypeInfo.
3216  *
3217  * RETURNS
3218  *
3219  *  Success: S_OK.
3220  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3221  */
3222 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3223         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3224         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3225         VARIANT* pVarVal)  /* [O] The custom data. */
3226 {
3227     FIXME("(%p,%s,%p), stub!\n", iface, debugstr_guid(guid), pVarVal);
3228     return E_OUTOFMEMORY;
3229 }
3230
3231 /******************************************************************************
3232  * ITypeInfo2_GetFuncCustData {OLEAUT32}
3233  *
3234  *  Gets a custom data element from a function.
3235  *
3236  * RETURNS
3237  *
3238  *  Success: S_OK.
3239  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3240  */
3241 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3242         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3243         UINT index,        /* [I] The index of the function for which to retrieve the custom data. */
3244         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3245         VARIANT* pVarVal)  /* [O] The custom data. */
3246 {
3247     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3248     return E_OUTOFMEMORY;
3249 }
3250
3251 /******************************************************************************
3252  * ITypeInfo2_GetParamCustData {OLEAUT32}
3253  *
3254  *  Gets a custom data element from a parameter.
3255  *
3256  * RETURNS
3257  *
3258  *  Success: S_OK.
3259  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3260  */
3261 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3262         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3263         UINT indexFunc,    /* [I] The index of the function for which to retrieve the custom data. */
3264         UINT indexParam,   /* [I] The index of the parameter for which to retrieve the custom data. */
3265         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3266         VARIANT* pVarVal)  /* [O] The custom data. */
3267 {
3268     FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3269     return E_OUTOFMEMORY;
3270 }
3271
3272 /******************************************************************************
3273  * ITypeInfo2_GetVarCustData {OLEAUT32}
3274  *
3275  *  Gets a custom data element from a variable.
3276  *
3277  * RETURNS
3278  *
3279  *  Success: S_OK.
3280  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3281  */
3282 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3283         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3284         UINT index,        /* [I] The index of the variable for which to retrieve the custom data. */
3285         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3286         VARIANT* pVarVal)  /* [O] The custom data. */
3287 {
3288     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3289     return E_OUTOFMEMORY;
3290 }
3291
3292 /******************************************************************************
3293  * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3294  *
3295  *  Gets a custom data element from an implemented type of a TypeInfo.
3296  *
3297  * RETURNS
3298  *
3299  *  Success: S_OK.
3300  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3301  */
3302 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3303         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3304         UINT index,        /* [I] The index of the implemented type for which to retrieve the custom data. */
3305         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3306         VARIANT* pVarVal)  /* [O] The custom data. */
3307 {
3308     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3309     return E_OUTOFMEMORY;
3310 }
3311
3312 /******************************************************************************
3313  * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3314  *
3315  *  Gets some documentation from a TypeInfo in a locale-aware fashion.
3316  *
3317  * RETURNS
3318  *
3319  *  Success: S_OK.
3320  *  Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3321  */
3322 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3323         ITypeInfo2* iface,           /* [I] The TypeInfo to retrieve the documentation from. */
3324         MEMBERID memid,              /* [I] The member id (why?). */
3325         LCID lcid,                   /* [I] The locale (why?). */
3326         BSTR* pbstrHelpString,       /* [O] The help string. */
3327         DWORD* pdwHelpStringContext, /* [O] The help string context. */
3328         BSTR* pbstrHelpStringDll)    /* [O] The help file name. */
3329 {
3330     FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3331     return E_OUTOFMEMORY;
3332 }
3333
3334 /******************************************************************************
3335  * ITypeInfo2_GetAllCustData {OLEAUT32}
3336  *
3337  *  Gets all of the custom data associated with a TypeInfo.
3338  *
3339  * RETURNS
3340  *
3341  *  Success: S_OK.
3342  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3343  */
3344 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
3345         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3346         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3347 {
3348     FIXME("(%p,%p), stub!\n", iface, pCustData);
3349     return E_OUTOFMEMORY;
3350 }
3351
3352 /******************************************************************************
3353  * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
3354  *
3355  *  Gets all of the custom data associated with a function.
3356  *
3357  * RETURNS
3358  *
3359  *  Success: S_OK.
3360  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3361  */
3362 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
3363         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3364         UINT index,          /* [I] The index of the function for which to retrieve the custom data. */
3365         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3366 {
3367     FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3368     return E_OUTOFMEMORY;
3369 }
3370
3371 /******************************************************************************
3372  * ITypeInfo2_GetAllParamCustData {OLEAUT32}
3373  *
3374  *  Gets all of the custom data associated with a parameter.
3375  *
3376  * RETURNS
3377  *
3378  *  Success: S_OK.
3379  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3380  */
3381 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
3382         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3383         UINT indexFunc,      /* [I] The index of the function for which to retrieve the custom data. */
3384         UINT indexParam,     /* [I] The index of the parameter for which to retrieve the custom data. */
3385         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3386 {
3387     FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
3388     return E_OUTOFMEMORY;
3389 }
3390
3391 /******************************************************************************
3392  * ITypeInfo2_GetAllVarCustData {OLEAUT32}
3393  *
3394  *  Gets all of the custom data associated with a variable.
3395  *
3396  * RETURNS
3397  *
3398  *  Success: S_OK.
3399  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3400  */
3401 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
3402         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3403         UINT index,          /* [I] The index of the variable for which to retrieve the custom data. */
3404         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3405 {
3406     FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3407     return E_OUTOFMEMORY;
3408 }
3409
3410 /******************************************************************************
3411  * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
3412  *
3413  *  Gets all of the custom data associated with an implemented type.
3414  *
3415  * RETURNS
3416  *
3417  *  Success: S_OK.
3418  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3419  */
3420 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
3421         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3422         UINT index,          /* [I] The index of the implemented type for which to retrieve the custom data. */
3423         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3424 {
3425     FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3426     return E_OUTOFMEMORY;
3427 }
3428
3429
3430 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
3431
3432 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
3433 {
3434
3435     ICreateTypeInfo2_fnQueryInterface,
3436     ICreateTypeInfo2_fnAddRef,
3437     ICreateTypeInfo2_fnRelease,
3438
3439     ICreateTypeInfo2_fnSetGuid,
3440     ICreateTypeInfo2_fnSetTypeFlags,
3441     ICreateTypeInfo2_fnSetDocString,
3442     ICreateTypeInfo2_fnSetHelpContext,
3443     ICreateTypeInfo2_fnSetVersion,
3444     ICreateTypeInfo2_fnAddRefTypeInfo,
3445     ICreateTypeInfo2_fnAddFuncDesc,
3446     ICreateTypeInfo2_fnAddImplType,
3447     ICreateTypeInfo2_fnSetImplTypeFlags,
3448     ICreateTypeInfo2_fnSetAlignment,
3449     ICreateTypeInfo2_fnSetSchema,
3450     ICreateTypeInfo2_fnAddVarDesc,
3451     ICreateTypeInfo2_fnSetFuncAndParamNames,
3452     ICreateTypeInfo2_fnSetVarName,
3453     ICreateTypeInfo2_fnSetTypeDescAlias,
3454     ICreateTypeInfo2_fnDefineFuncAsDllEntry,
3455     ICreateTypeInfo2_fnSetFuncDocString,
3456     ICreateTypeInfo2_fnSetVarDocString,
3457     ICreateTypeInfo2_fnSetFuncHelpContext,
3458     ICreateTypeInfo2_fnSetVarHelpContext,
3459     ICreateTypeInfo2_fnSetMops,
3460     ICreateTypeInfo2_fnSetTypeIdldesc,
3461     ICreateTypeInfo2_fnLayOut,
3462
3463     ICreateTypeInfo2_fnDeleteFuncDesc,
3464     ICreateTypeInfo2_fnDeleteFuncDescByMemId,
3465     ICreateTypeInfo2_fnDeleteVarDesc,
3466     ICreateTypeInfo2_fnDeleteVarDescByMemId,
3467     ICreateTypeInfo2_fnDeleteImplType,
3468     ICreateTypeInfo2_fnSetCustData,
3469     ICreateTypeInfo2_fnSetFuncCustData,
3470     ICreateTypeInfo2_fnSetParamCustData,
3471     ICreateTypeInfo2_fnSetVarCustData,
3472     ICreateTypeInfo2_fnSetImplTypeCustData,
3473     ICreateTypeInfo2_fnSetHelpStringContext,
3474     ICreateTypeInfo2_fnSetFuncHelpStringContext,
3475     ICreateTypeInfo2_fnSetVarHelpStringContext,
3476     ICreateTypeInfo2_fnInvalidate,
3477     ICreateTypeInfo2_fnSetName
3478 };
3479
3480 static const ITypeInfo2Vtbl typeinfo2vt =
3481 {
3482
3483     ITypeInfo2_fnQueryInterface,
3484     ITypeInfo2_fnAddRef,
3485     ITypeInfo2_fnRelease,
3486
3487     ITypeInfo2_fnGetTypeAttr,
3488     ITypeInfo2_fnGetTypeComp,
3489     ITypeInfo2_fnGetFuncDesc,
3490     ITypeInfo2_fnGetVarDesc,
3491     ITypeInfo2_fnGetNames,
3492     ITypeInfo2_fnGetRefTypeOfImplType,
3493     ITypeInfo2_fnGetImplTypeFlags,
3494     ITypeInfo2_fnGetIDsOfNames,
3495     ITypeInfo2_fnInvoke,
3496     ITypeInfo2_fnGetDocumentation,
3497     ITypeInfo2_fnGetDllEntry,
3498     ITypeInfo2_fnGetRefTypeInfo,
3499     ITypeInfo2_fnAddressOfMember,
3500     ITypeInfo2_fnCreateInstance,
3501     ITypeInfo2_fnGetMops,
3502     ITypeInfo2_fnGetContainingTypeLib,
3503     ITypeInfo2_fnReleaseTypeAttr,
3504     ITypeInfo2_fnReleaseFuncDesc,
3505     ITypeInfo2_fnReleaseVarDesc,
3506
3507     ITypeInfo2_fnGetTypeKind,
3508     ITypeInfo2_fnGetTypeFlags,
3509     ITypeInfo2_fnGetFuncIndexOfMemId,
3510     ITypeInfo2_fnGetVarIndexOfMemId,
3511     ITypeInfo2_fnGetCustData,
3512     ITypeInfo2_fnGetFuncCustData,
3513     ITypeInfo2_fnGetParamCustData,
3514     ITypeInfo2_fnGetVarCustData,
3515     ITypeInfo2_fnGetImplTypeCustData,
3516     ITypeInfo2_fnGetDocumentation2,
3517     ITypeInfo2_fnGetAllCustData,
3518     ITypeInfo2_fnGetAllFuncCustData,
3519     ITypeInfo2_fnGetAllParamCustData,
3520     ITypeInfo2_fnGetAllVarCustData,
3521     ITypeInfo2_fnGetAllImplTypeCustData,
3522 };
3523
3524 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
3525 {
3526     ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
3527
3528     int nameoffset;
3529     int typeinfo_offset;
3530     MSFT_TypeInfoBase *typeinfo;
3531
3532     TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
3533
3534     pCreateTypeInfo2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeInfo2Impl));
3535     if (!pCreateTypeInfo2Impl) return NULL;
3536
3537     pCreateTypeInfo2Impl->lpVtbl = &ctypeinfo2vt;
3538     pCreateTypeInfo2Impl->lpVtblTypeInfo2 = &typeinfo2vt;
3539     pCreateTypeInfo2Impl->ref = 1;
3540
3541     pCreateTypeInfo2Impl->typelib = typelib;
3542     typelib->ref++;
3543
3544     nameoffset = ctl2_alloc_name(typelib, szName);
3545     typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
3546     typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
3547
3548     typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
3549     *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
3550
3551     pCreateTypeInfo2Impl->typeinfo = typeinfo;
3552
3553     typeinfo->typekind |= tkind | 0x20;
3554     ICreateTypeInfo2_SetAlignment((ICreateTypeInfo2 *)pCreateTypeInfo2Impl, 4);
3555
3556     switch (tkind) {
3557     case TKIND_ENUM:
3558     case TKIND_INTERFACE:
3559     case TKIND_DISPATCH:
3560     case TKIND_COCLASS:
3561         typeinfo->size = 4;
3562         break;
3563
3564     case TKIND_RECORD:
3565     case TKIND_UNION:
3566         typeinfo->size = 0;
3567         break;
3568
3569     case TKIND_MODULE:
3570         typeinfo->size = 2;
3571         break;
3572
3573     case TKIND_ALIAS:
3574         typeinfo->size = -0x75;
3575         break;
3576
3577     default:
3578         FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
3579         typeinfo->size = 0xdeadbeef;
3580         break;
3581     }
3582
3583     if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
3584     typelib->last_typeinfo = pCreateTypeInfo2Impl;
3585     if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
3586
3587     TRACE(" -- %p\n", pCreateTypeInfo2Impl);
3588
3589     return (ICreateTypeInfo2 *)pCreateTypeInfo2Impl;
3590 }
3591
3592
3593 /*================== ICreateTypeLib2 Implementation ===================================*/
3594
3595 /******************************************************************************
3596  * ICreateTypeLib2_QueryInterface {OLEAUT32}
3597  *
3598  *  See IUnknown_QueryInterface.
3599  */
3600 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
3601         ICreateTypeLib2 * iface,
3602         REFIID riid,
3603         VOID **ppvObject)
3604 {
3605     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3606
3607     TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
3608
3609     *ppvObject=NULL;
3610     if(IsEqualIID(riid, &IID_IUnknown) ||
3611        IsEqualIID(riid,&IID_ICreateTypeLib)||
3612        IsEqualIID(riid,&IID_ICreateTypeLib2))
3613     {
3614         *ppvObject = This;
3615     } else if (IsEqualIID(riid, &IID_ITypeLib) ||
3616                IsEqualIID(riid, &IID_ITypeLib2)) {
3617         *ppvObject = &This->lpVtblTypeLib2;
3618     }
3619
3620     if(*ppvObject)
3621     {
3622         ICreateTypeLib2_AddRef(iface);
3623         TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
3624         return S_OK;
3625     }
3626     TRACE("-- Interface: E_NOINTERFACE\n");
3627     return E_NOINTERFACE;
3628 }
3629
3630 /******************************************************************************
3631  * ICreateTypeLib2_AddRef {OLEAUT32}
3632  *
3633  *  See IUnknown_AddRef.
3634  */
3635 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
3636 {
3637     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3638     ULONG ref = InterlockedIncrement(&This->ref);
3639
3640     TRACE("(%p)->ref was %u\n",This, ref - 1);
3641
3642     return ref;
3643 }
3644
3645 /******************************************************************************
3646  * ICreateTypeLib2_Release {OLEAUT32}
3647  *
3648  *  See IUnknown_Release.
3649  */
3650 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
3651 {
3652     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3653     ULONG ref = InterlockedDecrement(&This->ref);
3654
3655     TRACE("(%p)->(%u)\n",This, ref);
3656
3657     if (!ref) {
3658         int i;
3659
3660         for (i = 0; i < MSFT_SEG_MAX; i++) {
3661             HeapFree(GetProcessHeap(), 0, This->typelib_segment_data[i]);
3662             This->typelib_segment_data[i] = NULL;
3663         }
3664
3665         HeapFree(GetProcessHeap(), 0, This->filename);
3666         This->filename = NULL;
3667
3668         while (This->typeinfos) {
3669             ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
3670             This->typeinfos = typeinfo->next_typeinfo;
3671             if(typeinfo->typedata) {
3672                 CyclicList *iter, *rem;
3673
3674                 rem = typeinfo->typedata->next;
3675                 typeinfo->typedata->next = NULL;
3676                 iter = rem->next;
3677                 HeapFree(GetProcessHeap(), 0, rem);
3678
3679                 while(iter) {
3680                     rem = iter;
3681                     iter = iter->next;
3682                     HeapFree(GetProcessHeap(), 0, rem->u.data);
3683                     HeapFree(GetProcessHeap(), 0, rem);
3684                 }
3685             }
3686             HeapFree(GetProcessHeap(), 0, typeinfo);
3687         }
3688
3689         HeapFree(GetProcessHeap(),0,This);
3690         return 0;
3691     }
3692
3693     return ref;
3694 }
3695
3696
3697 /******************************************************************************
3698  * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
3699  *
3700  *  See ICreateTypeLib_CreateTypeInfo.
3701  */
3702 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
3703         ICreateTypeLib2 * iface,
3704         LPOLESTR szName,
3705         TYPEKIND tkind,
3706         ICreateTypeInfo **ppCTInfo)
3707 {
3708     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3709     char *name;
3710
3711     TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, ppCTInfo);
3712
3713     ctl2_encode_name(This, szName, &name);
3714     if(ctl2_find_name(This, name) != -1)
3715         return TYPE_E_NAMECONFLICT;
3716
3717     *ppCTInfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
3718
3719     if (!*ppCTInfo) return E_OUTOFMEMORY;
3720
3721     return S_OK;
3722 }
3723
3724 /******************************************************************************
3725  * ICreateTypeLib2_SetName {OLEAUT32}
3726  *
3727  *  See ICreateTypeLib_SetName.
3728  */
3729 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
3730         ICreateTypeLib2 * iface,
3731         LPOLESTR szName)
3732 {
3733     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3734
3735     int offset;
3736
3737     TRACE("(%p,%s)\n", iface, debugstr_w(szName));
3738
3739     offset = ctl2_alloc_name(This, szName);
3740     if (offset == -1) return E_OUTOFMEMORY;
3741     This->typelib_header.NameOffset = offset;
3742     return S_OK;
3743 }
3744
3745 /******************************************************************************
3746  * ICreateTypeLib2_SetVersion {OLEAUT32}
3747  *
3748  *  See ICreateTypeLib_SetVersion.
3749  */
3750 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
3751 {
3752     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3753
3754     TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
3755
3756     This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
3757     return S_OK;
3758 }
3759
3760 /******************************************************************************
3761  * ICreateTypeLib2_SetGuid {OLEAUT32}
3762  *
3763  *  See ICreateTypeLib_SetGuid.
3764  */
3765 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
3766 {
3767     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3768
3769     MSFT_GuidEntry guidentry;
3770     int offset;
3771
3772     TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
3773
3774     guidentry.guid = *guid;
3775     guidentry.hreftype = -2;
3776     guidentry.next_hash = -1;
3777
3778     offset = ctl2_alloc_guid(This, &guidentry);
3779     
3780     if (offset == -1) return E_OUTOFMEMORY;
3781
3782     This->typelib_header.posguid = offset;
3783
3784     return S_OK;
3785 }
3786
3787 /******************************************************************************
3788  * ICreateTypeLib2_SetDocString {OLEAUT32}
3789  *
3790  *  See ICreateTypeLib_SetDocString.
3791  */
3792 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
3793 {
3794     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3795
3796     int offset;
3797
3798     TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
3799     if (!szDoc)
3800         return E_INVALIDARG;
3801
3802     offset = ctl2_alloc_string(This, szDoc);
3803     if (offset == -1) return E_OUTOFMEMORY;
3804     This->typelib_header.helpstring = offset;
3805     return S_OK;
3806 }
3807
3808 /******************************************************************************
3809  * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
3810  *
3811  *  See ICreateTypeLib_SetHelpFileName.
3812  */
3813 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
3814 {
3815     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3816
3817     int offset;
3818
3819     TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
3820
3821     offset = ctl2_alloc_string(This, szHelpFileName);
3822     if (offset == -1) return E_OUTOFMEMORY;
3823     This->typelib_header.helpfile = offset;
3824     This->typelib_header.varflags |= 0x10;
3825     return S_OK;
3826 }
3827
3828 /******************************************************************************
3829  * ICreateTypeLib2_SetHelpContext {OLEAUT32}
3830  *
3831  *  See ICreateTypeLib_SetHelpContext.
3832  */
3833 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
3834 {
3835     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3836
3837     TRACE("(%p,%d)\n", iface, dwHelpContext);
3838     This->typelib_header.helpcontext = dwHelpContext;
3839     return S_OK;
3840 }
3841
3842 /******************************************************************************
3843  * ICreateTypeLib2_SetLcid {OLEAUT32}
3844  *
3845  * Sets both the lcid and lcid2 members in the header to lcid.
3846  *
3847  * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
3848  * is set to US English while the second one is set to 0.
3849  */
3850 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
3851 {
3852     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3853
3854     TRACE("(%p,%d)\n", iface, lcid);
3855
3856     This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
3857
3858     if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
3859
3860     return S_OK;
3861 }
3862
3863 /******************************************************************************
3864  * ICreateTypeLib2_SetLibFlags {OLEAUT32}
3865  *
3866  *  See ICreateTypeLib_SetLibFlags.
3867  */
3868 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
3869 {
3870     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3871
3872     TRACE("(%p,0x%x)\n", iface, uLibFlags);
3873
3874     This->typelib_header.flags = uLibFlags;
3875
3876     return S_OK;
3877 }
3878
3879 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
3880 {
3881     DWORD dwWritten;
3882     if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
3883         CloseHandle(hFile);
3884         return 0;
3885     }
3886     return -1;
3887 }
3888
3889 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
3890 {
3891     DWORD dwWritten;
3892     if (!WriteFile(hFile, This->typelib_segment_data[segment],
3893                    This->typelib_segdir[segment].length, &dwWritten, 0)) {
3894         CloseHandle(hFile);
3895         return 0;
3896     }
3897
3898     return -1;
3899 }
3900
3901 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
3902 {
3903     ICreateTypeInfo2Impl *typeinfo;
3904     HRESULT hres;
3905
3906     for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
3907         typeinfo->typeinfo->memoffset = filesize;
3908
3909         hres = ICreateTypeInfo2_fnLayOut((ICreateTypeInfo2 *)typeinfo);
3910         if(FAILED(hres))
3911             return hres;
3912
3913         if (typeinfo->typedata)
3914             filesize += typeinfo->typedata->next->u.val
3915                 + ((typeinfo->typeinfo->cElement >> 16) * 12)
3916                 + ((typeinfo->typeinfo->cElement & 0xffff) * 12) + 4;
3917     }
3918
3919     return S_OK;
3920 }
3921
3922 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
3923 {
3924     if (This->typelib_segdir[segment].length) {
3925         This->typelib_segdir[segment].offset = filepos;
3926     } else {
3927         This->typelib_segdir[segment].offset = -1;
3928     }
3929
3930     return This->typelib_segdir[segment].length;
3931 }
3932
3933 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
3934 {
3935     ICreateTypeInfo2Impl *typeinfo;
3936
3937     for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
3938         CyclicList *iter;
3939         int offset = 0;
3940
3941         if (!typeinfo->typedata) continue;
3942
3943         iter = typeinfo->typedata->next;
3944         ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
3945         for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
3946             ctl2_write_chunk(hFile, iter->u.data, iter->u.data[0] & 0xffff);
3947
3948         for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
3949             ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
3950
3951         for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
3952             ctl2_write_chunk(hFile, &iter->name, sizeof(int));
3953
3954         for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
3955             ctl2_write_chunk(hFile, &offset, sizeof(int));
3956             offset += iter->u.data[0] & 0xffff;
3957         }
3958     }
3959 }
3960
3961 /******************************************************************************
3962  * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
3963  *
3964  *  See ICreateTypeLib_SaveAllChanges.
3965  */
3966 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
3967 {
3968     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3969
3970     int retval;
3971     int filepos;
3972     HANDLE hFile;
3973     HRESULT hres;
3974
3975     TRACE("(%p)\n", iface);
3976
3977     retval = TYPE_E_IOERROR;
3978
3979     hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
3980     if (hFile == INVALID_HANDLE_VALUE) return retval;
3981
3982     filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
3983     filepos += This->typelib_header.nrtypeinfos * 4;
3984
3985     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
3986     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
3987     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
3988     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
3989     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
3990     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
3991     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
3992     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
3993     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
3994     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
3995     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
3996     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
3997     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
3998
3999     hres = ctl2_finalize_typeinfos(This, filepos);
4000     if(FAILED(hres)) {
4001         CloseHandle(hFile);
4002         return hres;
4003     }
4004
4005     if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4006     if (This->typelib_header.varflags & HELPDLLFLAG)
4007         if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4008     if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4009     if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4010     if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO    )) return retval;
4011     if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH    )) return retval;
4012     if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID        )) return retval;
4013     if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES  )) return retval;
4014     if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO  )) return retval;
4015     if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4016     if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH    )) return retval;
4017     if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME        )) return retval;
4018     if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING      )) return retval;
4019     if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC    )) return retval;
4020     if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC   )) return retval;
4021     if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA    )) return retval;
4022     if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4023
4024     ctl2_write_typeinfos(This, hFile);
4025
4026     if (!CloseHandle(hFile)) return retval;
4027
4028     return S_OK;
4029 }
4030
4031
4032 /******************************************************************************
4033  * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4034  *
4035  *  Deletes a named TypeInfo from a type library.
4036  *
4037  * RETURNS
4038  *
4039  *  Success: S_OK
4040  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4041  */
4042 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4043         ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4044         LPOLESTR szName)         /* [I] The name of the typeinfo to delete. */
4045 {
4046     FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4047     return E_OUTOFMEMORY;
4048 }
4049
4050 /******************************************************************************
4051  * ICreateTypeLib2_SetCustData {OLEAUT32}
4052  *
4053  *  Sets custom data for a type library.
4054  *
4055  * RETURNS
4056  *
4057  *  Success: S_OK
4058  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4059  */
4060 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4061         ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4062         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
4063         VARIANT *pVarVal)        /* [I] The custom data itself. */
4064 {
4065     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4066
4067     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4068
4069     return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4070 }
4071
4072 /******************************************************************************
4073  * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4074  *
4075  *  Sets a context number for the library help string.
4076  *
4077  * PARAMS
4078  *  iface     [I] The type library to set the help string context for.
4079  *  dwContext [I] The help string context.
4080  *
4081  * RETURNS
4082  *  Success: S_OK
4083  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4084  */
4085 static
4086 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4087                                                       ULONG dwContext)
4088 {
4089     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4090
4091     TRACE("(%p,%d)\n", iface, dwContext);
4092
4093     This->typelib_header.helpstringcontext = dwContext;
4094     return S_OK;
4095 }
4096
4097 /******************************************************************************
4098  * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4099  *
4100  *  Set the DLL used to look up localized help strings.
4101  *
4102  * PARAMS
4103  *  iface     [I] The type library to set the help DLL for.
4104  *  szDllName [I] The name of the help DLL.
4105  *
4106  * RETURNS
4107  *  Success: S_OK
4108  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4109  */
4110 static
4111 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4112                                                   LPOLESTR szDllName)
4113 {
4114     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4115     int offset;
4116
4117     TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4118     if (!szDllName)
4119         return E_INVALIDARG;
4120
4121     offset = ctl2_alloc_string(This, szDllName);
4122     if (offset == -1)
4123         return E_OUTOFMEMORY;
4124     This->typelib_header.varflags |= HELPDLLFLAG;
4125     This->helpStringDll = offset;
4126     return S_OK;
4127 }
4128
4129 /*================== ITypeLib2 Implementation ===================================*/
4130
4131 /******************************************************************************
4132  * ITypeLib2_QueryInterface {OLEAUT32}
4133  *
4134  *  See IUnknown_QueryInterface.
4135  */
4136 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4137 {
4138     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4139
4140     return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4141 }
4142
4143 /******************************************************************************
4144  * ITypeLib2_AddRef {OLEAUT32}
4145  *
4146  *  See IUnknown_AddRef.
4147  */
4148 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4149 {
4150     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4151
4152     return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4153 }
4154
4155 /******************************************************************************
4156  * ITypeLib2_Release {OLEAUT32}
4157  *
4158  *  See IUnknown_Release.
4159  */
4160 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4161 {
4162     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4163
4164     return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4165 }
4166
4167 /******************************************************************************
4168  * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4169  *
4170  *  See ITypeLib_GetTypeInfoCount.
4171  */
4172 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4173         ITypeLib2 * iface)
4174 {
4175     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4176
4177     TRACE("(%p)\n", iface);
4178
4179     return This->typelib_header.nrtypeinfos;
4180 }
4181
4182 /******************************************************************************
4183  * ITypeLib2_GetTypeInfo {OLEAUT32}
4184  *
4185  *  See ITypeLib_GetTypeInfo.
4186  */
4187 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4188         ITypeLib2 * iface,
4189         UINT index,
4190         ITypeInfo** ppTInfo)
4191 {
4192     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4193
4194     TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4195
4196     if (index >= This->typelib_header.nrtypeinfos) {
4197         return TYPE_E_ELEMENTNOTFOUND;
4198     }
4199
4200     return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4201 }
4202
4203 /******************************************************************************
4204  * ITypeLib2_GetTypeInfoType {OLEAUT32}
4205  *
4206  *  See ITypeLib_GetTypeInfoType.
4207  */
4208 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4209         ITypeLib2 * iface,
4210         UINT index,
4211         TYPEKIND* pTKind)
4212 {
4213     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4214
4215     TRACE("(%p,%d,%p)\n", iface, index, pTKind);
4216
4217     if (index >= This->typelib_header.nrtypeinfos) {
4218         return TYPE_E_ELEMENTNOTFOUND;
4219     }
4220
4221     *pTKind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 15;
4222
4223     return S_OK;
4224 }
4225
4226 /******************************************************************************
4227  * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4228  *
4229  *  See ITypeLib_GetTypeInfoOfGuid.
4230  */
4231 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4232         ITypeLib2 * iface,
4233         REFGUID guid,
4234         ITypeInfo** ppTinfo)
4235 {
4236     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4237
4238     int guidoffset;
4239     int typeinfo;
4240
4241     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4242
4243     guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4244     if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4245
4246     typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4247     if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4248
4249     return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4250 }
4251
4252 /******************************************************************************
4253  * ITypeLib2_GetLibAttr {OLEAUT32}
4254  *
4255  *  See ITypeLib_GetLibAttr.
4256  */
4257 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4258         ITypeLib2 * iface,
4259         TLIBATTR** ppTLibAttr)
4260 {
4261     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4262
4263     FIXME("(%p,%p), stub!\n", This, ppTLibAttr);
4264
4265     return E_OUTOFMEMORY;
4266 }
4267
4268 /******************************************************************************
4269  * ITypeLib2_GetTypeComp {OLEAUT32}
4270  *
4271  *  See ITypeLib_GetTypeComp.
4272  */
4273 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4274         ITypeLib2 * iface,
4275         ITypeComp** ppTComp)
4276 {
4277     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4278
4279     FIXME("(%p,%p), stub!\n", This, ppTComp);
4280
4281     return E_OUTOFMEMORY;
4282 }
4283
4284 /******************************************************************************
4285  * ITypeLib2_GetDocumentation {OLEAUT32}
4286  *
4287  *  See ITypeLib_GetDocumentation.
4288  */
4289 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4290         ITypeLib2 * iface,
4291         INT index,
4292         BSTR* pBstrName,
4293         BSTR* pBstrDocString,
4294         DWORD* pdwHelpContext,
4295         BSTR* pBstrHelpFile)
4296 {
4297     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4298
4299     FIXME("(%p,%d,%p,%p,%p,%p), stub!\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4300
4301     return E_OUTOFMEMORY;
4302 }
4303
4304 /******************************************************************************
4305  * ITypeLib2_IsName {OLEAUT32}
4306  *
4307  *  See ITypeLib_IsName.
4308  */
4309 static HRESULT WINAPI ITypeLib2_fnIsName(
4310         ITypeLib2 * iface,
4311         LPOLESTR szNameBuf,
4312         ULONG lHashVal,
4313         BOOL* pfName)
4314 {
4315     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4316
4317     char *encoded_name;
4318     int nameoffset;
4319     MSFT_NameIntro *nameintro;
4320
4321     TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
4322
4323     ctl2_encode_name(This, szNameBuf, &encoded_name);
4324     nameoffset = ctl2_find_name(This, encoded_name);
4325
4326     *pfName = 0;
4327
4328     if (nameoffset == -1) return S_OK;
4329
4330     nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
4331     if (nameintro->hreftype == -1) return S_OK;
4332
4333     *pfName = 1;
4334
4335     FIXME("Should be decoding our copy of the name over szNameBuf.\n");
4336
4337     return S_OK;
4338 }
4339
4340 /******************************************************************************
4341  * ITypeLib2_FindName {OLEAUT32}
4342  *
4343  *  See ITypeLib_FindName.
4344  */
4345 static HRESULT WINAPI ITypeLib2_fnFindName(
4346         ITypeLib2 * iface,
4347         LPOLESTR szNameBuf,
4348         ULONG lHashVal,
4349         ITypeInfo** ppTInfo,
4350         MEMBERID* rgMemId,
4351         USHORT* pcFound)
4352 {
4353     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4354
4355     FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
4356
4357     return E_OUTOFMEMORY;
4358 }
4359
4360 /******************************************************************************
4361  * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
4362  *
4363  *  See ITypeLib_ReleaseTLibAttr.
4364  */
4365 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
4366         ITypeLib2 * iface,
4367         TLIBATTR* pTLibAttr)
4368 {
4369     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4370
4371     FIXME("(%p,%p), stub!\n", This, pTLibAttr);
4372 }
4373
4374 /******************************************************************************
4375  * ICreateTypeLib2_GetCustData {OLEAUT32}
4376  *
4377  *  Retrieves a custom data value stored on a type library.
4378  *
4379  * RETURNS
4380  *
4381  *  Success: S_OK
4382  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4383  */
4384 static HRESULT WINAPI ITypeLib2_fnGetCustData(
4385         ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
4386         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
4387         VARIANT* pVarVal)  /* [O] The custom data. */
4388 {
4389     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4390
4391     FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
4392
4393     return E_OUTOFMEMORY;
4394 }
4395
4396 /******************************************************************************
4397  * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
4398  *
4399  *  Retrieves some statistics about names in a type library, supposedly for
4400  *  hash table optimization purposes.
4401  *
4402  * RETURNS
4403  *
4404  *  Success: S_OK
4405  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4406  */
4407 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
4408         ITypeLib2 * iface,      /* [I] The type library to get statistics about. */
4409         ULONG* pcUniqueNames,   /* [O] The number of unique names in the type library. */
4410         ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
4411 {
4412     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4413
4414     FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
4415
4416     return E_OUTOFMEMORY;
4417 }
4418
4419 /******************************************************************************
4420  * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
4421  *
4422  *  Obtain locale-aware help string information.
4423  *
4424  * RETURNS
4425  *
4426  *  Success: S_OK
4427  *  Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
4428  */
4429 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
4430         ITypeLib2 * iface,
4431         INT index,
4432         LCID lcid,
4433         BSTR* pbstrHelpString,
4434         DWORD* pdwHelpStringContext,
4435         BSTR* pbstrHelpStringDll)
4436 {
4437     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4438
4439     FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
4440
4441     return E_OUTOFMEMORY;
4442 }
4443
4444 /******************************************************************************
4445  * ICreateTypeLib2_GetAllCustData {OLEAUT32}
4446  *
4447  *  Retrieve all of the custom data for a type library.
4448  *
4449  * RETURNS
4450  *
4451  *  Success: S_OK
4452  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4453  */
4454 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
4455         ITypeLib2 * iface,   /* [I] The type library in which to find the custom data. */
4456         CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
4457 {
4458     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4459
4460     FIXME("(%p,%p), stub!\n", This, pCustData);
4461
4462     return E_OUTOFMEMORY;
4463 }
4464
4465
4466 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
4467
4468 static const ICreateTypeLib2Vtbl ctypelib2vt =
4469 {
4470
4471     ICreateTypeLib2_fnQueryInterface,
4472     ICreateTypeLib2_fnAddRef,
4473     ICreateTypeLib2_fnRelease,
4474
4475     ICreateTypeLib2_fnCreateTypeInfo,
4476     ICreateTypeLib2_fnSetName,
4477     ICreateTypeLib2_fnSetVersion,
4478     ICreateTypeLib2_fnSetGuid,
4479     ICreateTypeLib2_fnSetDocString,
4480     ICreateTypeLib2_fnSetHelpFileName,
4481     ICreateTypeLib2_fnSetHelpContext,
4482     ICreateTypeLib2_fnSetLcid,
4483     ICreateTypeLib2_fnSetLibFlags,
4484     ICreateTypeLib2_fnSaveAllChanges,
4485
4486     ICreateTypeLib2_fnDeleteTypeInfo,
4487     ICreateTypeLib2_fnSetCustData,
4488     ICreateTypeLib2_fnSetHelpStringContext,
4489     ICreateTypeLib2_fnSetHelpStringDll
4490 };
4491
4492 static const ITypeLib2Vtbl typelib2vt =
4493 {
4494
4495     ITypeLib2_fnQueryInterface,
4496     ITypeLib2_fnAddRef,
4497     ITypeLib2_fnRelease,
4498
4499     ITypeLib2_fnGetTypeInfoCount,
4500     ITypeLib2_fnGetTypeInfo,
4501     ITypeLib2_fnGetTypeInfoType,
4502     ITypeLib2_fnGetTypeInfoOfGuid,
4503     ITypeLib2_fnGetLibAttr,
4504     ITypeLib2_fnGetTypeComp,
4505     ITypeLib2_fnGetDocumentation,
4506     ITypeLib2_fnIsName,
4507     ITypeLib2_fnFindName,
4508     ITypeLib2_fnReleaseTLibAttr,
4509
4510     ITypeLib2_fnGetCustData,
4511     ITypeLib2_fnGetLibStatistics,
4512     ITypeLib2_fnGetDocumentation2,
4513     ITypeLib2_fnGetAllCustData,
4514 };
4515
4516 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
4517 {
4518     ICreateTypeLib2Impl *pCreateTypeLib2Impl;
4519     int failed = 0;
4520
4521     TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
4522
4523     pCreateTypeLib2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeLib2Impl));
4524     if (!pCreateTypeLib2Impl) return NULL;
4525
4526     pCreateTypeLib2Impl->filename = HeapAlloc(GetProcessHeap(), 0, (strlenW(szFile) + 1) * sizeof(WCHAR));
4527     if (!pCreateTypeLib2Impl->filename) {
4528         HeapFree(GetProcessHeap(), 0, pCreateTypeLib2Impl);
4529         return NULL;
4530     }
4531     strcpyW(pCreateTypeLib2Impl->filename, szFile);
4532
4533     ctl2_init_header(pCreateTypeLib2Impl);
4534     ctl2_init_segdir(pCreateTypeLib2Impl);
4535
4536     pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
4537
4538     /*
4539      * The following two calls return an offset or -1 if out of memory. We
4540      * specifically need an offset of 0, however, so...
4541      */
4542     if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
4543     if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
4544
4545     pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
4546     pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
4547
4548     memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
4549     memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
4550
4551     pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
4552     pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
4553     pCreateTypeLib2Impl->ref = 1;
4554
4555     if (failed) {
4556         ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
4557         return 0;
4558     }
4559
4560     return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
4561 }
4562
4563 /******************************************************************************
4564  * CreateTypeLib2 [OLEAUT32.180]
4565  *
4566  *  Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
4567  *  library.
4568  *
4569  * NOTES
4570  *
4571  *  See also CreateTypeLib.
4572  *
4573  * RETURNS
4574  *    Success: S_OK
4575  *    Failure: Status
4576  */
4577 HRESULT WINAPI CreateTypeLib2(
4578         SYSKIND syskind,           /* [I] System type library is for */
4579         LPCOLESTR szFile,          /* [I] Type library file name */
4580         ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
4581 {
4582     TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
4583
4584     if (!szFile) return E_INVALIDARG;
4585     *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
4586     return (*ppctlib)? S_OK: E_OUTOFMEMORY;
4587 }
4588
4589 /******************************************************************************
4590  * ClearCustData (OLEAUT32.171)
4591  *
4592  * Clear a custom data types' data.
4593  *
4594  * PARAMS
4595  *  lpCust [I] The custom data type instance
4596  *
4597  * RETURNS
4598  *  Nothing.
4599  */
4600 void WINAPI ClearCustData(LPCUSTDATA lpCust)
4601 {
4602     if (lpCust && lpCust->cCustData)
4603     {
4604         if (lpCust->prgCustData)
4605         {
4606             DWORD i;
4607
4608             for (i = 0; i < lpCust->cCustData; i++)
4609                 VariantClear(&lpCust->prgCustData[i].varValue);
4610
4611             /* FIXME - Should be using a per-thread IMalloc */
4612             HeapFree(GetProcessHeap(), 0, lpCust->prgCustData);
4613             lpCust->prgCustData = NULL;
4614         }
4615         lpCust->cCustData = 0;
4616     }
4617 }