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