2 * Generation of dll registration scripts
4 * Copyright 2010 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
40 static const char *format_uuid( const UUID
*uuid
)
42 static char buffer
[40];
43 sprintf( buffer
, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
44 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
,
45 uuid
->Data4
[0], uuid
->Data4
[1], uuid
->Data4
[2], uuid
->Data4
[3],
46 uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7] );
50 static const char *get_coclass_threading( const type_t
*class )
52 static const char * const models
[] =
55 "Apartment", /* THREADING_APARTMENT */
56 "Neutral", /* THREADING_NEUTRAL */
57 "Single", /* THREADING_SINGLE */
58 "Free", /* THREADING_FREE */
59 "Both", /* THREADING_BOTH */
61 return models
[get_attrv( class->attrs
, ATTR_THREADING
)];
64 static const type_t
*find_ps_factory( const statement_list_t
*stmts
)
66 const statement_t
*stmt
;
68 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
70 if (stmt
->type
== STMT_TYPE
)
72 const type_t
*type
= stmt
->u
.type
;
73 if (type_get_type(type
) == TYPE_COCLASS
&& !strcmp( type
->name
, "PSFactoryBuffer" ))
80 static int write_interface( const type_t
*iface
, const type_t
*ps_factory
)
82 const UUID
*uuid
= get_attrp( iface
->attrs
, ATTR_UUID
);
83 const UUID
*ps_uuid
= ps_factory
? get_attrp( ps_factory
->attrs
, ATTR_UUID
) : NULL
;
86 if (!is_object( iface
)) return 0;
87 if (!type_iface_get_inherit(iface
)) /* special case for IUnknown */
89 put_str( indent
, "ForceRemove '%s' = s '%s'\n", format_uuid( uuid
), iface
->name
);
92 if (is_local( iface
->attrs
)) return 0;
93 put_str( indent
, "ForceRemove '%s' = s '%s'\n", format_uuid( uuid
), iface
->name
);
94 put_str( indent
, "{\n" );
96 put_str( indent
, "NumMethods = s %u\n", count_methods( iface
));
97 put_str( indent
, "ProxyStubClsid32 = s '%s'\n",
98 ps_uuid
? format_uuid( ps_uuid
) : "%CLSID_PSFactoryBuffer%" );
100 put_str( indent
, "}\n" );
104 static int write_interfaces( const statement_list_t
*stmts
, const type_t
*ps_factory
)
106 const statement_t
*stmt
;
109 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
111 if (stmt
->type
== STMT_TYPE
&& type_get_type( stmt
->u
.type
) == TYPE_INTERFACE
)
112 count
+= write_interface( stmt
->u
.type
, ps_factory
);
117 static int write_coclass( const type_t
*class, const typelib_t
*typelib
)
119 const UUID
*uuid
= get_attrp( class->attrs
, ATTR_UUID
);
120 const char *descr
= get_attrp( class->attrs
, ATTR_HELPSTRING
);
121 const char *progid
= get_attrp( class->attrs
, ATTR_PROGID
);
122 const char *vi_progid
= get_attrp( class->attrs
, ATTR_VIPROGID
);
123 const char *threading
= get_coclass_threading( class );
124 unsigned int version
= get_attrv( class->attrs
, ATTR_VERSION
);
127 if (typelib
&& !threading
) return 0;
128 if (!descr
) descr
= class->name
;
130 put_str( indent
, "ForceRemove '%s' = s '%s'\n", format_uuid( uuid
), descr
);
131 put_str( indent
++, "{\n" );
132 if (threading
) put_str( indent
, "InprocServer32 = s '%%MODULE%%' { val ThreadingModel = s '%s' }\n",
134 if (progid
) put_str( indent
, "ProgId = s '%s'\n", progid
);
137 const UUID
*typelib_uuid
= get_attrp( typelib
->attrs
, ATTR_UUID
);
138 put_str( indent
, "TypeLib = s '%s'\n", format_uuid( typelib_uuid
));
139 if (!version
) version
= get_attrv( typelib
->attrs
, ATTR_VERSION
);
141 if (version
) put_str( indent
, "Version = s '%u.%u'\n", MAJORVERSION(version
), MINORVERSION(version
) );
142 if (vi_progid
) put_str( indent
, "VersionIndependentProgId = s '%s'\n", vi_progid
);
143 put_str( --indent
, "}\n" );
147 static void write_coclasses( const statement_list_t
*stmts
, const typelib_t
*typelib
)
149 const statement_t
*stmt
;
151 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
153 if (stmt
->type
== STMT_TYPE
)
155 const type_t
*type
= stmt
->u
.type
;
156 if (type_get_type(type
) == TYPE_COCLASS
) write_coclass( type
, typelib
);
158 else if (stmt
->type
== STMT_LIBRARY
)
160 const typelib_t
*lib
= stmt
->u
.lib
;
161 write_coclasses( lib
->stmts
, lib
);
166 static int write_progid( const type_t
*class )
168 const UUID
*uuid
= get_attrp( class->attrs
, ATTR_UUID
);
169 const char *descr
= get_attrp( class->attrs
, ATTR_HELPSTRING
);
170 const char *progid
= get_attrp( class->attrs
, ATTR_PROGID
);
171 const char *vi_progid
= get_attrp( class->attrs
, ATTR_VIPROGID
);
174 if (!descr
) descr
= class->name
;
178 put_str( indent
, "'%s' = s '%s'\n", progid
, descr
);
179 put_str( indent
++, "{\n" );
180 put_str( indent
, "CLSID = s '%s'\n", format_uuid( uuid
) );
181 put_str( --indent
, "}\n" );
185 put_str( indent
, "'%s' = s '%s'\n", vi_progid
, descr
);
186 put_str( indent
++, "{\n" );
187 put_str( indent
, "CLSID = s '%s'\n", format_uuid( uuid
) );
188 if (progid
&& strcmp( progid
, vi_progid
)) put_str( indent
, "CurVer = s '%s'\n", progid
);
189 put_str( --indent
, "}\n" );
194 static void write_progids( const statement_list_t
*stmts
)
196 const statement_t
*stmt
;
198 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
200 if (stmt
->type
== STMT_TYPE
)
202 const type_t
*type
= stmt
->u
.type
;
203 if (type_get_type(type
) == TYPE_COCLASS
) write_progid( type
);
205 else if (stmt
->type
== STMT_LIBRARY
)
207 write_progids( stmt
->u
.lib
->stmts
);
212 /* put a string into the resource file */
213 static inline void put_string( const char *str
)
217 unsigned char ch
= *str
++;
218 put_word( toupper(ch
) );
223 void write_regscript( const statement_list_t
*stmts
)
226 const type_t
*ps_factory
;
228 if (!do_regscript
) return;
229 if (do_everything
&& !need_proxy_file( stmts
)) return;
231 init_output_buffer();
233 put_str( indent
, "HKCR\n" );
234 put_str( indent
++, "{\n" );
236 put_str( indent
, "NoRemove Interface\n" );
237 put_str( indent
++, "{\n" );
238 ps_factory
= find_ps_factory( stmts
);
239 count
= write_interfaces( stmts
, ps_factory
);
240 put_str( --indent
, "}\n" );
242 put_str( indent
, "NoRemove CLSID\n" );
243 put_str( indent
++, "{\n" );
244 if (count
&& !ps_factory
)
246 put_str( indent
, "ForceRemove '%%CLSID_PSFactoryBuffer%%' = s 'PSFactoryBuffer'\n" );
247 put_str( indent
++, "{\n" );
248 put_str( indent
, "InprocServer32 = s '%%MODULE%%' { val ThreadingModel = s 'Both' }\n" );
249 put_str( --indent
, "}\n" );
251 write_coclasses( stmts
, NULL
);
252 put_str( --indent
, "}\n" );
254 write_progids( stmts
);
255 put_str( --indent
, "}\n" );
257 if (strendswith( regscript_name
, ".res" )) /* create a binary resource file */
259 unsigned char *data
= output_buffer
;
260 size_t data_size
= output_buffer_pos
;
261 size_t header_size
= 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);
263 header_size
+= (strlen(regscript_token
) + strlen("WINE_REGISTRY") + 2) * sizeof(unsigned short);
265 init_output_buffer();
267 put_dword( 0 ); /* ResSize */
268 put_dword( 32 ); /* HeaderSize */
269 put_word( 0xffff ); /* ResType */
271 put_word( 0xffff ); /* ResName */
273 put_dword( 0 ); /* DataVersion */
274 put_word( 0 ); /* Memory options */
275 put_word( 0 ); /* Language */
276 put_dword( 0 ); /* Version */
277 put_dword( 0 ); /* Characteristics */
279 put_dword( data_size
); /* ResSize */
280 put_dword( (header_size
+ 3) & ~3 ); /* HeaderSize */
281 put_string( "WINE_REGISTRY" ); /* ResType */
282 put_string( regscript_token
); /* ResName */
284 put_dword( 0 ); /* DataVersion */
285 put_word( 0 ); /* Memory options */
286 put_word( 0 ); /* Language */
287 put_dword( 0 ); /* Version */
288 put_dword( 0 ); /* Characteristics */
290 put_data( data
, data_size
);
293 flush_output_buffer( regscript_name
);
297 FILE *f
= fopen( regscript_name
, "w" );
298 if (!f
) error( "Could not open %s for output\n", regscript_name
);
299 if (fwrite( output_buffer
, output_buffer_pos
, 1, f
) != output_buffer_pos
)
300 error( "Failed to write to %s\n", regscript_name
);
302 error( "Failed to write to %s\n", regscript_name
);