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