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