hlink: Convert dll registration to the IRegistrar mechanism.
[wine] / dlls / msimtf / main.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "advpub.h"
32 #include "ole2.h"
33 #include "dimm.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msimtf);
38
39 extern HRESULT ActiveIMMApp_Constructor(IUnknown *punkOuter, IUnknown **ppOut);
40
41 static HINSTANCE msimtf_instance;
42
43 /******************************************************************
44  *              DllMain (msimtf.@)
45  */
46 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
47 {
48     switch(fdwReason)
49     {
50     case DLL_WINE_PREATTACH:
51         return FALSE;  /* prefer native version */
52     case DLL_PROCESS_ATTACH:
53         msimtf_instance = hInstDLL;
54         DisableThreadLibraryCalls(hInstDLL);
55         break;
56     case DLL_PROCESS_DETACH:
57         break;
58     }
59     return TRUE;
60 }
61
62 typedef struct {
63     IClassFactory IClassFactory_iface;
64
65     HRESULT (*cf)(IUnknown*,IUnknown**);
66 } ClassFactory;
67
68 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
69 {
70     return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
71 }
72
73 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface,
74         REFIID riid, void **ppv)
75 {
76     *ppv = NULL;
77
78     if(IsEqualGUID(&IID_IUnknown, riid)) {
79         TRACE("(IID_IUnknown %p)\n", ppv);
80         *ppv = iface;
81     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
82         TRACE("IID_IClassFactory %p)\n", ppv);
83         *ppv = iface;
84     }
85
86     if(*ppv) {
87         IUnknown_AddRef((IUnknown*)*ppv);
88         return S_OK;
89     }
90
91     FIXME("(%s %p)\n", debugstr_guid(riid), ppv);
92     return E_NOINTERFACE;
93 }
94
95 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
96 {
97     return 2;
98 }
99
100 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
101 {
102     return 1;
103 }
104
105 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface,
106         IUnknown *pOuter, REFIID riid, void **ppv)
107 {
108     ClassFactory *This = impl_from_IClassFactory(iface);
109     HRESULT ret;
110     IUnknown *obj;
111     TRACE("(%p, %p, %s, %p)\n", iface, pOuter, debugstr_guid(riid), ppv);
112
113     ret = This->cf(pOuter, &obj);
114     if (FAILED(ret))
115         return ret;
116
117     ret = IUnknown_QueryInterface(obj,riid,ppv);
118     IUnknown_Release(obj);
119     return ret;
120 }
121
122 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
123 {
124     FIXME("(%d)\n", dolock);
125     return S_OK;
126 }
127
128 static const IClassFactoryVtbl ClassFactoryVtbl = {
129     ClassFactory_QueryInterface,
130     ClassFactory_AddRef,
131     ClassFactory_Release,
132     ClassFactory_CreateInstance,
133     ClassFactory_LockServer
134 };
135
136 /******************************************************************
137  *              DllGetClassObject (msimtf.@)
138  */
139 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
140 {
141     if(IsEqualGUID(&CLSID_CActiveIMM, rclsid)) {
142         static ClassFactory cf = {
143             { &ClassFactoryVtbl },
144             ActiveIMMApp_Constructor,
145         };
146
147         return IClassFactory_QueryInterface((IClassFactory*)&cf, riid, ppv);
148     }
149
150     FIXME("(%s %s %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
151     return CLASS_E_CLASSNOTAVAILABLE;
152 }
153
154 /******************************************************************
155  *              DllCanUnloadNow (msimtf.@)
156  */
157 HRESULT WINAPI DllCanUnloadNow(void)
158 {
159     return S_FALSE;
160 }
161
162 #define INF_SET_CLSID(clsid)                  \
163     do                                        \
164     {                                         \
165         static CHAR name[] = "CLSID_" #clsid; \
166                                               \
167         pse[i].pszName = name;                \
168         clsids[i++] = &CLSID_ ## clsid;       \
169     } while (0)
170
171 static HRESULT register_server(BOOL doregister)
172 {
173     HRESULT hres;
174     HMODULE hAdvpack;
175     HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
176     STRTABLEA strtable;
177     STRENTRYA pse[1];
178     static CLSID const *clsids[34];
179     unsigned int i = 0;
180
181     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
182
183     INF_SET_CLSID(CActiveIMM);
184
185     for(i = 0; i < sizeof(pse)/sizeof(pse[0]); i++) {
186         pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, 39);
187         sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
188                 clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
189                 clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
190                 clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
191     }
192
193     strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
194     strtable.pse = pse;
195
196     hAdvpack = LoadLibraryW(wszAdvpack);
197     pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
198
199     hres = pRegInstall(msimtf_instance, doregister ? "RegisterDll" : "UnregisterDll", &strtable);
200
201     for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
202         HeapFree(GetProcessHeap(), 0, pse[i].pszValue);
203
204     return hres;
205 }
206
207 #undef INF_SET_CLSID
208
209 /***********************************************************************
210  *          DllRegisterServer (msimtf.@)
211  */
212 HRESULT WINAPI DllRegisterServer(void)
213 {
214     return register_server(TRUE);
215 }
216
217 /***********************************************************************
218  *          DllUnregisterServer (msimtf.@)
219  */
220 HRESULT WINAPI DllUnregisterServer(void)
221 {
222     return register_server(FALSE);
223 }