explorer: add a navbar to explorer
[wine/gsoc_explorer.git] / dlls / winecrt0 / register.c
blob28ce3c571fbc216f9f1bc5481f947b87d1aeea8e
1 /*
2 * Support functions for Wine dll registrations
4 * Copyright (c) 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
21 #include "config.h"
22 #include <stdarg.h>
24 #define COBJMACROS
25 #define ATL_INITGUID
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "ole2.h"
30 #include "rpcproxy.h"
31 #include "atliface.h"
33 static const WCHAR ole32W[] = {'o','l','e','3','2','.','d','l','l',0};
34 static const WCHAR oleaut32W[] = {'o','l','e','a','u','t','3','2','.','d','l','l',0};
35 static const WCHAR regtypeW[] = {'W','I','N','E','_','R','E','G','I','S','T','R','Y',0};
36 static const WCHAR moduleW[] = {'M','O','D','U','L','E',0};
37 static const WCHAR clsidW[] = {'C','L','S','I','D','_','P','S','F','a','c','t','o','r','y','B','u','f','f','e','r',0};
38 static const WCHAR typelibW[] = {'T','Y','P','E','L','I','B',0};
40 struct reg_info
42 IRegistrar *registrar;
43 const CLSID *clsid;
44 BOOL do_register;
45 BOOL uninit;
46 HRESULT result;
49 static HMODULE ole32;
50 static HMODULE oleaut32;
51 static HRESULT (WINAPI *pCoInitialize)(LPVOID);
52 static void (WINAPI *pCoUninitialize)(void);
53 static HRESULT (WINAPI *pCoCreateInstance)(REFCLSID,LPUNKNOWN,DWORD,REFIID,LPVOID*);
54 static INT (WINAPI *pStringFromGUID2)(REFGUID,LPOLESTR,INT);
55 static HRESULT (WINAPI *pLoadTypeLibEx)(LPCOLESTR,REGKIND,ITypeLib**);
56 static HRESULT (WINAPI *pUnRegisterTypeLib)(REFGUID,WORD,WORD,LCID,SYSKIND);
58 static IRegistrar *create_registrar( HMODULE inst, struct reg_info *info )
60 if (!pStringFromGUID2)
62 if (!(ole32 = LoadLibraryW( ole32W )) ||
63 !(pCoInitialize = (void *)GetProcAddress( ole32, "CoInitialize" )) ||
64 !(pCoUninitialize = (void *)GetProcAddress( ole32, "CoUninitialize" )) ||
65 !(pCoCreateInstance = (void *)GetProcAddress( ole32, "CoCreateInstance" )) ||
66 !(pStringFromGUID2 = (void *)GetProcAddress( ole32, "StringFromGUID2" )))
68 info->result = E_NOINTERFACE;
69 return NULL;
72 info->uninit = SUCCEEDED( pCoInitialize( NULL ));
74 info->result = pCoCreateInstance( &CLSID_Registrar, NULL, CLSCTX_INPROC_SERVER,
75 &IID_IRegistrar, (void **)&info->registrar );
76 if (SUCCEEDED( info->result ))
78 WCHAR str[MAX_PATH];
80 GetModuleFileNameW( inst, str, MAX_PATH );
81 IRegistrar_AddReplacement( info->registrar, moduleW, str );
82 if (info->clsid)
84 pStringFromGUID2( info->clsid, str, MAX_PATH );
85 IRegistrar_AddReplacement( info->registrar, clsidW, str );
88 return info->registrar;
91 static BOOL CALLBACK register_resource( HMODULE module, LPCWSTR type, LPWSTR name, LONG_PTR arg )
93 struct reg_info *info = (struct reg_info *)arg;
94 WCHAR *buffer;
95 HRSRC rsrc = FindResourceW( module, name, type );
96 char *str = LoadResource( module, rsrc );
97 DWORD lenW, lenA = SizeofResource( module, rsrc );
99 if (!str) return FALSE;
100 if (!info->registrar && !create_registrar( module, info )) return FALSE;
101 lenW = MultiByteToWideChar( CP_UTF8, 0, str, lenA, NULL, 0 ) + 1;
102 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
104 info->result = E_OUTOFMEMORY;
105 return FALSE;
107 MultiByteToWideChar( CP_UTF8, 0, str, lenA, buffer, lenW );
108 buffer[lenW - 1] = 0;
110 if (info->do_register)
111 info->result = IRegistrar_StringRegister( info->registrar, buffer );
112 else
113 info->result = IRegistrar_StringUnregister( info->registrar, buffer );
115 HeapFree( GetProcessHeap(), 0, buffer );
116 return SUCCEEDED(info->result);
119 static HRESULT register_typelib( HMODULE module, BOOL do_register )
121 WCHAR name[MAX_PATH];
122 ITypeLib *typelib;
123 HRESULT ret;
125 if (!pUnRegisterTypeLib)
127 if (!(oleaut32 = LoadLibraryW( oleaut32W )) ||
128 !(pLoadTypeLibEx = (void *)GetProcAddress( oleaut32, "LoadTypeLibEx" )) ||
129 !(pUnRegisterTypeLib = (void *)GetProcAddress( oleaut32, "UnRegisterTypeLib" )))
130 return E_FAIL;
132 GetModuleFileNameW( module, name, MAX_PATH );
133 if (do_register)
135 ret = pLoadTypeLibEx( name, REGKIND_REGISTER, &typelib );
137 else
139 ret = pLoadTypeLibEx( name, REGKIND_NONE, &typelib );
140 if (SUCCEEDED( ret ))
142 TLIBATTR *attr;
143 ITypeLib_GetLibAttr( typelib, &attr );
144 ret = pUnRegisterTypeLib( &attr->guid, attr->wMajorVerNum, attr->wMinorVerNum,
145 attr->lcid, attr->syskind );
146 ITypeLib_ReleaseTLibAttr( typelib, attr );
149 if (SUCCEEDED( ret )) ITypeLib_Release( typelib );
150 return ret;
153 HRESULT __wine_register_resources( HMODULE module, const CLSID *clsid )
155 struct reg_info info;
157 info.registrar = NULL;
158 info.clsid = clsid;
159 info.do_register = TRUE;
160 info.uninit = FALSE;
161 info.result = S_OK;
162 EnumResourceNamesW( module, regtypeW, register_resource, (LONG_PTR)&info );
163 if (info.registrar) IRegistrar_Release( info.registrar );
164 if (SUCCEEDED(info.result) && FindResourceW( module, MAKEINTRESOURCEW(1), typelibW ))
165 info.result = register_typelib( module, TRUE );
166 if (info.uninit) pCoUninitialize();
167 return info.result;
170 HRESULT __wine_unregister_resources( HMODULE module, const CLSID *clsid )
172 struct reg_info info;
174 info.registrar = NULL;
175 info.clsid = clsid;
176 info.do_register = FALSE;
177 info.uninit = FALSE;
178 info.result = S_OK;
179 EnumResourceNamesW( module, regtypeW, register_resource, (LONG_PTR)&info );
180 if (info.registrar) IRegistrar_Release( info.registrar );
181 if (SUCCEEDED(info.result) && FindResourceW( module, MAKEINTRESOURCEW(1), typelibW ))
182 info.result = register_typelib( module, FALSE );
183 if (info.uninit) pCoUninitialize();
184 return info.result;