xref: /reactos/sdk/lib/3rdparty/libwine/register.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * Support functions for Wine dll registrations
3*c2c66affSColin Finck  *
4*c2c66affSColin Finck  * Copyright (c) 2010 Alexandre Julliard
5*c2c66affSColin Finck  *
6*c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7*c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8*c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9*c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10*c2c66affSColin Finck  *
11*c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12*c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*c2c66affSColin Finck  * Lesser General Public License for more details.
15*c2c66affSColin Finck  *
16*c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17*c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18*c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19*c2c66affSColin Finck  */
20*c2c66affSColin Finck 
21*c2c66affSColin Finck #include "config.h"
22*c2c66affSColin Finck #include <stdarg.h>
23*c2c66affSColin Finck 
24*c2c66affSColin Finck #define COBJMACROS
25*c2c66affSColin Finck #define ATL_INITGUID
26*c2c66affSColin Finck #include "windef.h"
27*c2c66affSColin Finck #include "winbase.h"
28*c2c66affSColin Finck #include "winnls.h"
29*c2c66affSColin Finck #include "ole2.h"
30*c2c66affSColin Finck #include "rpcproxy.h"
31*c2c66affSColin Finck #include "atliface.h"
32*c2c66affSColin Finck 
33*c2c66affSColin Finck static const WCHAR atl100W[] = {'a','t','l','1','0','0','.','d','l','l',0};
34*c2c66affSColin Finck static const WCHAR regtypeW[] = {'W','I','N','E','_','R','E','G','I','S','T','R','Y',0};
35*c2c66affSColin Finck static const WCHAR moduleW[] = {'M','O','D','U','L','E',0};
36*c2c66affSColin Finck 
37*c2c66affSColin Finck struct reg_info
38*c2c66affSColin Finck {
39*c2c66affSColin Finck     IRegistrar  *registrar;
40*c2c66affSColin Finck     BOOL         do_register;
41*c2c66affSColin Finck     HRESULT      result;
42*c2c66affSColin Finck };
43*c2c66affSColin Finck 
44*c2c66affSColin Finck static HMODULE atl100;
45*c2c66affSColin Finck static HRESULT (WINAPI *pAtlCreateRegistrar)(IRegistrar**);
46*c2c66affSColin Finck 
create_registrar(HMODULE inst,struct reg_info * info)47*c2c66affSColin Finck static IRegistrar *create_registrar( HMODULE inst, struct reg_info *info )
48*c2c66affSColin Finck {
49*c2c66affSColin Finck     if (!pAtlCreateRegistrar)
50*c2c66affSColin Finck     {
51*c2c66affSColin Finck         if (!(atl100 = LoadLibraryW( atl100W )) ||
52*c2c66affSColin Finck             !(pAtlCreateRegistrar = (void *)GetProcAddress( atl100, "AtlCreateRegistrar" )))
53*c2c66affSColin Finck         {
54*c2c66affSColin Finck             info->result = E_NOINTERFACE;
55*c2c66affSColin Finck             return NULL;
56*c2c66affSColin Finck         }
57*c2c66affSColin Finck     }
58*c2c66affSColin Finck 
59*c2c66affSColin Finck     info->result = pAtlCreateRegistrar( &info->registrar );
60*c2c66affSColin Finck     if (SUCCEEDED( info->result ))
61*c2c66affSColin Finck     {
62*c2c66affSColin Finck         WCHAR str[MAX_PATH];
63*c2c66affSColin Finck 
64*c2c66affSColin Finck         GetModuleFileNameW( inst, str, MAX_PATH );
65*c2c66affSColin Finck         IRegistrar_AddReplacement( info->registrar, moduleW, str );
66*c2c66affSColin Finck     }
67*c2c66affSColin Finck     return info->registrar;
68*c2c66affSColin Finck }
69*c2c66affSColin Finck 
register_resource(HMODULE module,LPCWSTR type,LPWSTR name,LONG_PTR arg)70*c2c66affSColin Finck static BOOL CALLBACK register_resource( HMODULE module, LPCWSTR type, LPWSTR name, LONG_PTR arg )
71*c2c66affSColin Finck {
72*c2c66affSColin Finck     struct reg_info *info = (struct reg_info *)arg;
73*c2c66affSColin Finck     WCHAR *buffer;
74*c2c66affSColin Finck     HRSRC rsrc = FindResourceW( module, name, type );
75*c2c66affSColin Finck     char *str = LoadResource( module, rsrc );
76*c2c66affSColin Finck     DWORD lenW, lenA = SizeofResource( module, rsrc );
77*c2c66affSColin Finck 
78*c2c66affSColin Finck     if (!str) return FALSE;
79*c2c66affSColin Finck     if (!info->registrar && !create_registrar( module, info )) return FALSE;
80*c2c66affSColin Finck     lenW = MultiByteToWideChar( CP_UTF8, 0, str, lenA, NULL, 0 ) + 1;
81*c2c66affSColin Finck     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
82*c2c66affSColin Finck     {
83*c2c66affSColin Finck         info->result = E_OUTOFMEMORY;
84*c2c66affSColin Finck         return FALSE;
85*c2c66affSColin Finck     }
86*c2c66affSColin Finck     MultiByteToWideChar( CP_UTF8, 0, str, lenA, buffer, lenW );
87*c2c66affSColin Finck     buffer[lenW - 1] = 0;
88*c2c66affSColin Finck 
89*c2c66affSColin Finck     if (info->do_register)
90*c2c66affSColin Finck         info->result = IRegistrar_StringRegister( info->registrar, buffer );
91*c2c66affSColin Finck     else
92*c2c66affSColin Finck         info->result = IRegistrar_StringUnregister( info->registrar, buffer );
93*c2c66affSColin Finck 
94*c2c66affSColin Finck     HeapFree( GetProcessHeap(), 0, buffer );
95*c2c66affSColin Finck     return SUCCEEDED(info->result);
96*c2c66affSColin Finck }
97*c2c66affSColin Finck 
__wine_register_resources(HMODULE module)98*c2c66affSColin Finck HRESULT __wine_register_resources( HMODULE module )
99*c2c66affSColin Finck {
100*c2c66affSColin Finck     struct reg_info info;
101*c2c66affSColin Finck 
102*c2c66affSColin Finck     info.registrar = NULL;
103*c2c66affSColin Finck     info.do_register = TRUE;
104*c2c66affSColin Finck     info.result = S_OK;
105*c2c66affSColin Finck     EnumResourceNamesW( module, regtypeW, register_resource, (LONG_PTR)&info );
106*c2c66affSColin Finck     if (info.registrar) IRegistrar_Release( info.registrar );
107*c2c66affSColin Finck     return info.result;
108*c2c66affSColin Finck }
109*c2c66affSColin Finck 
__wine_unregister_resources(HMODULE module)110*c2c66affSColin Finck HRESULT __wine_unregister_resources( HMODULE module )
111*c2c66affSColin Finck {
112*c2c66affSColin Finck     struct reg_info info;
113*c2c66affSColin Finck 
114*c2c66affSColin Finck     info.registrar = NULL;
115*c2c66affSColin Finck     info.do_register = FALSE;
116*c2c66affSColin Finck     info.result = S_OK;
117*c2c66affSColin Finck     EnumResourceNamesW( module, regtypeW, register_resource, (LONG_PTR)&info );
118*c2c66affSColin Finck     if (info.registrar) IRegistrar_Release( info.registrar );
119*c2c66affSColin Finck     return info.result;
120*c2c66affSColin Finck }
121