msctf: Add base dll framework.
[wine] / dlls / msctf / msctf.c
1 /*
2  * MSCTF Server DLL
3  *
4  * Copyright 2008 Aric Stewart, CodeWeavers
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
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #define COBJMACROS
27
28 #include "wine/debug.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "shlguid.h"
34 #include "initguid.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
37
38 LONG MSCTF_refCount = 0;
39
40 HINSTANCE MSCTF_hinstance = 0;
41
42 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
43
44 static const struct {
45     REFCLSID clsid;
46     LPFNCONSTRUCTOR ctor;
47 } ClassesTable[] = {
48     {NULL, NULL}
49 };
50
51 typedef struct tagClassFactory
52 {
53     const IClassFactoryVtbl *vtbl;
54     LONG   ref;
55     LPFNCONSTRUCTOR ctor;
56 } ClassFactory;
57
58 static void ClassFactory_Destructor(ClassFactory *This)
59 {
60     TRACE("Destroying class factory %p\n", This);
61     HeapFree(GetProcessHeap(),0,This);
62     MSCTF_refCount--;
63 }
64
65 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
66 {
67     *ppvOut = NULL;
68     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
69         IClassFactory_AddRef(iface);
70         *ppvOut = iface;
71         return S_OK;
72     }
73
74     WARN("Unknown interface %s\n", debugstr_guid(riid));
75     return E_NOINTERFACE;
76 }
77
78 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
79 {
80     ClassFactory *This = (ClassFactory *)iface;
81     return InterlockedIncrement(&This->ref);
82 }
83
84 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
85 {
86     ClassFactory *This = (ClassFactory *)iface;
87     ULONG ret = InterlockedDecrement(&This->ref);
88
89     if (ret == 0)
90         ClassFactory_Destructor(This);
91     return ret;
92 }
93
94 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
95 {
96     ClassFactory *This = (ClassFactory *)iface;
97     HRESULT ret;
98     IUnknown *obj;
99
100     TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
101     ret = This->ctor(punkOuter, &obj);
102     if (FAILED(ret))
103         return ret;
104     ret = IUnknown_QueryInterface(obj, iid, ppvOut);
105     IUnknown_Release(obj);
106     return ret;
107 }
108
109 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
110 {
111     ClassFactory *This = (ClassFactory *)iface;
112
113     TRACE("(%p)->(%x)\n", This, fLock);
114
115     if(fLock)
116         InterlockedIncrement(&MSCTF_refCount);
117     else
118         InterlockedDecrement(&MSCTF_refCount);
119
120     return S_OK;
121 }
122
123 static const IClassFactoryVtbl ClassFactoryVtbl = {
124     /* IUnknown */
125     ClassFactory_QueryInterface,
126     ClassFactory_AddRef,
127     ClassFactory_Release,
128
129     /* IClassFactory*/
130     ClassFactory_CreateInstance,
131     ClassFactory_LockServer
132 };
133
134 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
135 {
136     ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
137     This->vtbl = &ClassFactoryVtbl;
138     This->ref = 1;
139     This->ctor = ctor;
140     *ppvOut = (LPVOID)This;
141     TRACE("Created class factory %p\n", This);
142     MSCTF_refCount++;
143     return S_OK;
144 }
145
146 /*************************************************************************
147  * MSCTF DllMain
148  */
149 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
150 {
151     TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
152     switch (fdwReason)
153     {
154         case DLL_WINE_PREATTACH:
155             return FALSE;   /* prefer native version */
156         case DLL_PROCESS_ATTACH:
157             DisableThreadLibraryCalls(hinst);
158             MSCTF_hinstance = hinst;
159             break;
160     }
161     return TRUE;
162 }
163
164 /*************************************************************************
165  *              DllCanUnloadNow (MSCTF.@)
166  */
167 HRESULT WINAPI DllCanUnloadNow(void)
168 {
169     return MSCTF_refCount ? S_FALSE : S_OK;
170 }
171
172 /***********************************************************************
173  *              DllGetClassObject (MSCTF.@)
174  */
175 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
176 {
177     int i;
178
179     *ppvOut = NULL;
180     if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
181         return E_NOINTERFACE;
182
183     for (i = 0; ClassesTable[i].clsid != NULL; i++)
184         if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
185             return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
186         }
187     FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
188     return CLASS_E_CLASSNOTAVAILABLE;
189 }