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