Remove redundant check.
[wine] / dlls / atl / atl_main.c
1 /*
2  * Implementation of Active Template Library (atl.dll)
3  *
4  * Copyright 2004 Aric Stewart for 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winuser.h"
30 #include "wine/debug.h"
31 #include "objbase.h"
32 #include "objidl.h"
33 #include "ole2.h"
34 #include "atlbase.h"
35 #include "atliface.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(atl);
38
39 HINSTANCE hInst;
40
41 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
42 {
43     TRACE("(0x%p, %ld, %p)\n",hinstDLL,fdwReason,lpvReserved);
44
45     if (fdwReason == DLL_PROCESS_ATTACH) {
46         DisableThreadLibraryCalls(hinstDLL);
47         hInst = hinstDLL;
48     }
49     return TRUE;
50 }
51
52 #define ATLVer1Size 100
53
54 HRESULT WINAPI AtlModuleInit(_ATL_MODULEA* pM, _ATL_OBJMAP_ENTRYA* p, HINSTANCE h)
55 {
56     INT i;
57     UINT size;
58
59     FIXME("SEMI-STUB (%p %p %p)\n",pM,p,h);
60
61     size = pM->cbSize;
62     if  (size != sizeof(_ATL_MODULEA) && size != ATLVer1Size)
63     {
64         FIXME("Unknown structure version (size %i)\n",size);
65         return E_INVALIDARG;
66     }
67     
68     memset(pM,0,pM->cbSize);
69     pM->cbSize = size;
70     pM->m_hInst = h;
71     pM->m_hInstResource = h;
72     pM->m_hInstTypeLib = h;
73     pM->m_pObjMap = p;
74     pM->m_hHeap = GetProcessHeap();
75
76     /* call mains */
77     i = 0;
78     if (pM->m_pObjMap != NULL  && size > ATLVer1Size)
79     {
80         while (pM->m_pObjMap[i].pclsid != NULL)
81         {
82             TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
83             if (p[i].pfnObjectMain)
84                 p[i].pfnObjectMain(TRUE);
85             i++;
86         }
87     }
88
89     return S_OK;
90 }
91
92 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEA* pM)
93 {
94     HeapFree(GetProcessHeap(), 0, pM);
95     return S_OK;
96 }
97
98 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEA *pM, DWORD dwClsContext,
99                                              DWORD dwFlags)
100 {
101     HRESULT hRes = S_OK;
102     int i=0;
103
104     TRACE("(%p %li %li)\n",pM, dwClsContext, dwFlags);
105
106     if (pM == NULL)
107         return E_INVALIDARG;
108
109     while(pM->m_pObjMap[i].pclsid != NULL)
110     {
111         IUnknown* pUnknown;
112         _ATL_OBJMAP_ENTRYA *obj = &(pM->m_pObjMap[i]);
113         HRESULT rc;
114
115         TRACE("Registering object %i\n",i);
116         if (obj->pfnGetClassObject)
117         {
118             rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
119                                    (LPVOID*)&pUnknown);
120             if (SUCCEEDED (rc) )
121             {
122                 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
123                                       dwFlags, &obj->dwRegister);
124                 if (pUnknown)
125                     IUnknown_Release(pUnknown);
126             }
127         }
128         i++;
129     }
130
131    return hRes;
132 }
133
134 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEA* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
135 {
136     FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
137     return S_OK;
138 }
139
140 HRESULT WINAPI AtlInternalQueryInterface(LPVOID this, const _ATL_INTMAP_ENTRY* pEntries,  REFIID iid, LPVOID* ppvObject)
141 {
142     int i = 0;
143     HRESULT rc = E_NOINTERFACE;
144     TRACE("(%p, %p, %p, %p)\n",this, pEntries, iid, ppvObject);
145
146     if (IsEqualGUID(iid,&IID_IUnknown))
147     {
148         TRACE("Returning IUnknown\n");
149         *ppvObject = this;
150         IUnknown_AddRef((IUnknown*)this);
151         return S_OK;
152     }
153
154     while (pEntries[i].pFunc != 0)
155     {
156         TRACE("Trying entry %i (%p %li %p)\n",i,pEntries[i].piid,
157               pEntries[i].dw, pEntries[i].pFunc);
158
159         if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
160         {
161             TRACE("MATCH\n");
162             if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
163             {
164                 TRACE("Offset\n");
165                 *ppvObject = ((LPSTR)this+pEntries[i].dw);
166                 IUnknown_AddRef((IUnknown*)this);
167                 rc = S_OK;
168             }
169             else
170             {
171                 TRACE("Function\n");
172                 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
173             }
174             break;
175         }
176         i++;
177     }
178     TRACE("Done returning (0x%lx)\n",rc);
179     return rc;
180 }
181
182 /***********************************************************************
183  *           AtlModuleRegisterServer         [ATL.@]
184  *
185  */
186 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid) 
187 {
188     FIXME("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
189     return S_OK;
190 }
191
192 /***********************************************************************
193  *           AtlAdvise         [ATL.@]
194  */
195 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
196 {
197     FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
198     return E_FAIL;
199 }
200
201 /***********************************************************************
202  *           AtlUnadvise         [ATL.@]
203  */
204 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
205 {
206     FIXME("%p %p %ld\n", pUnkCP, iid, dw);
207     return S_OK;
208 }
209
210 /***********************************************************************
211  *           AtlFreeMarshalStream         [ATL.@]
212  */
213 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
214 {
215     FIXME("%p\n", stm);
216     return S_OK;
217 }
218
219 /***********************************************************************
220  *           AtlMarshalPtrInProc         [ATL.@]
221  */
222 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
223 {
224     FIXME("%p %p %p\n", pUnk, iid, pstm);
225     return E_FAIL;
226 }
227
228 /***********************************************************************
229  *           AtlUnmarshalPtr              [ATL.@]
230  */
231 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
232 {
233     FIXME("%p %p %p\n", stm, iid, ppUnk);
234     return E_FAIL;
235 }
236
237 /***********************************************************************
238  *           AtlModuleGetClassObject              [ATL.@]
239  */
240 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
241                                        REFIID riid, LPVOID *ppv)
242 {
243     FIXME("%p %p %p %p\n", pm, rclsid, riid, ppv);
244     return E_FAIL;
245 }
246
247 /***********************************************************************
248  *           AtlModuleGetClassObject              [ATL.@]
249  */
250 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
251 {
252     FIXME("%p %s\n", pm, debugstr_w(lpszIndex));
253     return E_FAIL;
254 }
255
256 /***********************************************************************
257  *           AtlModuleRevokeClassObjects          [ATL.@]
258  */
259 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
260 {
261     FIXME("%p\n", pm);
262     return E_FAIL;
263 }
264
265 /***********************************************************************
266  *           AtlModuleUnregisterServer           [ATL.@]
267  */
268 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
269 {
270     FIXME("%p %s\n", pm, debugstr_guid(clsid));
271     return E_FAIL;
272 }