include/msvcrt: Define more CPU control word flags.
[wine] / dlls / winecrt0 / register.c
1 /*
2  * Support functions for Wine dll registrations
3  *
4  * Copyright (c) 2010 Alexandre Julliard
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 #include "config.h"
22 #include <stdarg.h>
23
24 #define COBJMACROS
25 #define ATL_INITGUID
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "objbase.h"
30 #include "rpcproxy.h"
31 #include "atliface.h"
32
33 static const WCHAR ole32W[] = {'o','l','e','3','2','.','d','l','l',0};
34 static const WCHAR regtypeW[] = {'W','I','N','E','_','R','E','G','I','S','T','R','Y',0};
35 static const WCHAR moduleW[] = {'M','O','D','U','L','E',0};
36 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};
37
38 struct reg_info
39 {
40     IRegistrar  *registrar;
41     const CLSID *clsid;
42     BOOL         do_register;
43     BOOL         uninit;
44     HRESULT      result;
45 };
46
47 static HMODULE ole32;
48 static HRESULT (WINAPI *pCoInitialize)(LPVOID);
49 static void (WINAPI *pCoUninitialize)(void);
50 static HRESULT (WINAPI *pCoCreateInstance)(REFCLSID,LPUNKNOWN,DWORD,REFIID,LPVOID*);
51 static INT (WINAPI *pStringFromGUID2)(REFGUID,LPOLESTR,INT);
52
53 static IRegistrar *create_registrar( HMODULE inst, struct reg_info *info )
54 {
55     if (!ole32)
56     {
57         if (!(ole32 = LoadLibraryW( ole32W )) ||
58             !(pCoInitialize = (void *)GetProcAddress( ole32, "CoInitialize" )) ||
59             !(pCoUninitialize = (void *)GetProcAddress( ole32, "CoUninitialize" )) ||
60             !(pCoCreateInstance = (void *)GetProcAddress( ole32, "CoCreateInstance" )) ||
61             !(pStringFromGUID2 = (void *)GetProcAddress( ole32, "StringFromGUID2" )))
62         {
63             info->result = E_NOINTERFACE;
64             return NULL;
65         }
66     }
67     info->uninit = SUCCEEDED( pCoInitialize( NULL ));
68
69     info->result = pCoCreateInstance( &CLSID_Registrar, NULL, CLSCTX_INPROC_SERVER,
70                                       &IID_IRegistrar, (void **)&info->registrar );
71     if (SUCCEEDED( info->result ))
72     {
73         WCHAR str[MAX_PATH];
74
75         GetModuleFileNameW( inst, str, MAX_PATH );
76         IRegistrar_AddReplacement( info->registrar, moduleW, str );
77         if (info->clsid)
78         {
79             pStringFromGUID2( info->clsid, str, MAX_PATH );
80             IRegistrar_AddReplacement( info->registrar, clsidW, str );
81         }
82     }
83     return info->registrar;
84 }
85
86 static BOOL CALLBACK register_resource( HMODULE module, LPCWSTR type, LPWSTR name, LONG_PTR arg )
87 {
88     struct reg_info *info = (struct reg_info *)arg;
89     WCHAR *buffer;
90     HRSRC rsrc = FindResourceW( module, name, type );
91     char *str = LoadResource( module, rsrc );
92     DWORD lenW, lenA = SizeofResource( module, rsrc );
93
94     if (!str) return FALSE;
95     if (!info->registrar && !create_registrar( module, info )) return FALSE;
96     lenW = MultiByteToWideChar( CP_UTF8, 0, str, lenA, NULL, 0 ) + 1;
97     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
98     {
99         info->result = E_OUTOFMEMORY;
100         return FALSE;
101     }
102     MultiByteToWideChar( CP_UTF8, 0, str, lenA, buffer, lenW );
103     buffer[lenW - 1] = 0;
104
105     if (info->do_register)
106         info->result = IRegistrar_StringRegister( info->registrar, buffer );
107     else
108         info->result = IRegistrar_StringUnregister( info->registrar, buffer );
109
110     HeapFree( GetProcessHeap(), 0, buffer );
111     return SUCCEEDED(info->result);
112 }
113
114 HRESULT __wine_register_resources( HMODULE module, const CLSID *clsid )
115 {
116     struct reg_info info;
117
118     info.registrar = NULL;
119     info.clsid = clsid;
120     info.do_register = TRUE;
121     info.uninit = FALSE;
122     info.result = S_OK;
123     EnumResourceNamesW( module, regtypeW, register_resource, (LONG_PTR)&info );
124     if (info.registrar) IRegistrar_Release( info.registrar );
125     if (info.uninit) pCoUninitialize();
126     return info.result;
127 }
128
129 HRESULT __wine_unregister_resources( HMODULE module, const CLSID *clsid )
130 {
131     struct reg_info info;
132
133     info.registrar = NULL;
134     info.clsid = clsid;
135     info.do_register = FALSE;
136     info.uninit = FALSE;
137     info.result = S_OK;
138     EnumResourceNamesW( module, regtypeW, register_resource, (LONG_PTR)&info );
139     if (info.registrar) IRegistrar_Release( info.registrar );
140     if (info.uninit) pCoUninitialize();
141     return info.result;
142 }