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