push 378fe7a60681a28e8b22f62dcfe122d585b92570
[wine/hacks.git] / dlls / msctf / msctf.c
blob254444bacb76756f390c8115fd715e0de9d13115
1 /*
2 * MSCTF Server DLL
4 * Copyright 2008 Aric Stewart, CodeWeavers
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"
23 #include <stdarg.h>
24 #include <stdio.h>
26 #define COBJMACROS
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"
38 #include "msctf_internal.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
42 static LONG MSCTF_refCount;
44 static HINSTANCE MSCTF_hinstance;
46 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
48 static const struct {
49 REFCLSID clsid;
50 LPFNCONSTRUCTOR ctor;
51 } ClassesTable[] = {
52 {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
53 {NULL, NULL}
56 typedef struct tagClassFactory
58 const IClassFactoryVtbl *vtbl;
59 LONG ref;
60 LPFNCONSTRUCTOR ctor;
61 } ClassFactory;
63 static void ClassFactory_Destructor(ClassFactory *This)
65 TRACE("Destroying class factory %p\n", This);
66 HeapFree(GetProcessHeap(),0,This);
67 MSCTF_refCount--;
70 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
72 *ppvOut = NULL;
73 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
74 IClassFactory_AddRef(iface);
75 *ppvOut = iface;
76 return S_OK;
79 WARN("Unknown interface %s\n", debugstr_guid(riid));
80 return E_NOINTERFACE;
83 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
85 ClassFactory *This = (ClassFactory *)iface;
86 return InterlockedIncrement(&This->ref);
89 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
91 ClassFactory *This = (ClassFactory *)iface;
92 ULONG ret = InterlockedDecrement(&This->ref);
94 if (ret == 0)
95 ClassFactory_Destructor(This);
96 return ret;
99 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
101 ClassFactory *This = (ClassFactory *)iface;
102 HRESULT ret;
103 IUnknown *obj;
105 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
106 ret = This->ctor(punkOuter, &obj);
107 if (FAILED(ret))
108 return ret;
109 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
110 IUnknown_Release(obj);
111 return ret;
114 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
116 ClassFactory *This = (ClassFactory *)iface;
118 TRACE("(%p)->(%x)\n", This, fLock);
120 if(fLock)
121 InterlockedIncrement(&MSCTF_refCount);
122 else
123 InterlockedDecrement(&MSCTF_refCount);
125 return S_OK;
128 static const IClassFactoryVtbl ClassFactoryVtbl = {
129 /* IUnknown */
130 ClassFactory_QueryInterface,
131 ClassFactory_AddRef,
132 ClassFactory_Release,
134 /* IClassFactory*/
135 ClassFactory_CreateInstance,
136 ClassFactory_LockServer
139 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
141 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
142 This->vtbl = &ClassFactoryVtbl;
143 This->ref = 1;
144 This->ctor = ctor;
145 *ppvOut = (LPVOID)This;
146 TRACE("Created class factory %p\n", This);
147 MSCTF_refCount++;
148 return S_OK;
151 /*************************************************************************
152 * MSCTF DllMain
154 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
156 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
157 switch (fdwReason)
159 case DLL_WINE_PREATTACH:
160 return FALSE; /* prefer native version */
161 case DLL_PROCESS_ATTACH:
162 DisableThreadLibraryCalls(hinst);
163 MSCTF_hinstance = hinst;
164 break;
166 return TRUE;
169 /*************************************************************************
170 * DllCanUnloadNow (MSCTF.@)
172 HRESULT WINAPI DllCanUnloadNow(void)
174 return MSCTF_refCount ? S_FALSE : S_OK;
177 /***********************************************************************
178 * DllGetClassObject (MSCTF.@)
180 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
182 int i;
184 *ppvOut = NULL;
185 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
186 return E_NOINTERFACE;
188 for (i = 0; ClassesTable[i].clsid != NULL; i++)
189 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
190 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
192 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
193 return CLASS_E_CLASSNOTAVAILABLE;