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