ntdll: Initialize a variable.
[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     InitializeCriticalSection(&pM->u.m_csTypeInfoHolder);
77     InitializeCriticalSection(&pM->m_csWindowCreate);
78     InitializeCriticalSection(&pM->m_csObjMap);
79
80     /* call mains */
81     i = 0;
82     if (pM->m_pObjMap != NULL  && size > ATLVer1Size)
83     {
84         while (pM->m_pObjMap[i].pclsid != NULL)
85         {
86             TRACE("Initializing object %i %p\n",i,p[i].pfnObjectMain);
87             if (p[i].pfnObjectMain)
88                 p[i].pfnObjectMain(TRUE);
89             i++;
90         }
91     }
92
93     return S_OK;
94 }
95
96 HRESULT WINAPI AtlModuleTerm(_ATL_MODULEA* pM)
97 {
98     _ATL_TERMFUNC_ELEM *iter = pM->m_pTermFuncs, *tmp;
99
100     TRACE("(%p)\n", pM);
101
102     while(iter) {
103         iter->pFunc(iter->dw);
104         tmp = iter;
105         iter = iter->pNext;
106         HeapFree(GetProcessHeap(), 0, tmp);
107     }
108
109     HeapFree(GetProcessHeap(), 0, pM);
110
111     return S_OK;
112 }
113
114 HRESULT WINAPI AtlModuleAddTermFunc(_ATL_MODULEW *pM, _ATL_TERMFUNC *pFunc, DWORD_PTR dw)
115 {
116     _ATL_TERMFUNC_ELEM *termfunc_elem;
117
118     TRACE("(%p %p %ld)\n", pM, pFunc, dw);
119
120     termfunc_elem = HeapAlloc(GetProcessHeap(), 0, sizeof(_ATL_TERMFUNC_ELEM));
121     termfunc_elem->pFunc = pFunc;
122     termfunc_elem->dw = dw;
123     termfunc_elem->pNext = pM->m_pTermFuncs;
124
125     pM->m_pTermFuncs = termfunc_elem;
126
127     return S_OK;
128 }
129
130 HRESULT WINAPI AtlModuleRegisterClassObjects(_ATL_MODULEA *pM, DWORD dwClsContext,
131                                              DWORD dwFlags)
132 {
133     HRESULT hRes = S_OK;
134     int i=0;
135
136     TRACE("(%p %li %li)\n",pM, dwClsContext, dwFlags);
137
138     if (pM == NULL)
139         return E_INVALIDARG;
140
141     while(pM->m_pObjMap[i].pclsid != NULL)
142     {
143         IUnknown* pUnknown;
144         _ATL_OBJMAP_ENTRYA *obj = &(pM->m_pObjMap[i]);
145         HRESULT rc;
146
147         TRACE("Registering object %i\n",i);
148         if (obj->pfnGetClassObject)
149         {
150             rc = obj->pfnGetClassObject(obj->pfnCreateInstance, &IID_IUnknown,
151                                    (LPVOID*)&pUnknown);
152             if (SUCCEEDED (rc) )
153             {
154                 CoRegisterClassObject(obj->pclsid, pUnknown, dwClsContext,
155                                       dwFlags, &obj->dwRegister);
156                 if (pUnknown)
157                     IUnknown_Release(pUnknown);
158             }
159         }
160         i++;
161     }
162
163    return hRes;
164 }
165
166 HRESULT WINAPI AtlModuleUnregisterServerEx(_ATL_MODULEA* pM, BOOL bUnRegTypeLib, const CLSID* pCLSID)
167 {
168     FIXME("(%p, %i, %p) stub\n", pM, bUnRegTypeLib, pCLSID);
169     return S_OK;
170 }
171
172 /***********************************************************************
173  *           AtlAxWinInit          [ATL.@]
174  * Initializes the control-hosting code: registering the AtlAxWin7 and AtlAxWinLic7 window
175  * classes and some messages.
176  *  
177  * RETURNS
178  *  TRUE or FALSE
179  */
180
181 BOOL WINAPI AtlAxWinInit(void)
182 {
183     FIXME("Try use native atl.dll if possible\n");
184     return FALSE;
185 }
186
187
188 IUnknown* WINAPI AtlComPtrAssign(IUnknown** pp, IUnknown *p)
189 {
190     TRACE("(%p %p)\n", pp, p);
191
192     if (p) IUnknown_AddRef(p);
193     if (*pp) IUnknown_Release(*pp);
194     *pp = p;
195     return p;
196 }
197
198
199 HRESULT WINAPI AtlInternalQueryInterface(LPVOID this, const _ATL_INTMAP_ENTRY* pEntries,  REFIID iid, LPVOID* ppvObject)
200 {
201     int i = 0;
202     HRESULT rc = E_NOINTERFACE;
203     TRACE("(%p, %p, %p, %p)\n",this, pEntries, iid, ppvObject);
204
205     if (IsEqualGUID(iid,&IID_IUnknown))
206     {
207         TRACE("Returning IUnknown\n");
208         *ppvObject = this;
209         IUnknown_AddRef((IUnknown*)this);
210         return S_OK;
211     }
212
213     while (pEntries[i].pFunc != 0)
214     {
215         TRACE("Trying entry %i (%p %li %p)\n",i,pEntries[i].piid,
216               pEntries[i].dw, pEntries[i].pFunc);
217
218         if (pEntries[i].piid && IsEqualGUID(iid,pEntries[i].piid))
219         {
220             TRACE("MATCH\n");
221             if (pEntries[i].pFunc == (_ATL_CREATORARGFUNC*)1)
222             {
223                 TRACE("Offset\n");
224                 *ppvObject = ((LPSTR)this+pEntries[i].dw);
225                 IUnknown_AddRef((IUnknown*)this);
226                 rc = S_OK;
227             }
228             else
229             {
230                 TRACE("Function\n");
231                 rc = pEntries[i].pFunc(this, iid, ppvObject,0);
232             }
233             break;
234         }
235         i++;
236     }
237     TRACE("Done returning (0x%lx)\n",rc);
238     return rc;
239 }
240
241 /***********************************************************************
242  *           AtlModuleRegisterServer         [ATL.@]
243  *
244  */
245 HRESULT WINAPI AtlModuleRegisterServer(_ATL_MODULEW* pM, BOOL bRegTypeLib, const CLSID* clsid) 
246 {
247     FIXME("%p %d %s\n", pM, bRegTypeLib, debugstr_guid(clsid));
248     return S_OK;
249 }
250
251 /***********************************************************************
252  *           AtlAdvise         [ATL.@]
253  */
254 HRESULT WINAPI AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID *iid, LPDWORD pdw)
255 {
256     FIXME("%p %p %p %p\n", pUnkCP, pUnk, iid, pdw);
257     return E_FAIL;
258 }
259
260 /***********************************************************************
261  *           AtlUnadvise         [ATL.@]
262  */
263 HRESULT WINAPI AtlUnadvise(IUnknown *pUnkCP, const IID *iid, DWORD dw)
264 {
265     FIXME("%p %p %ld\n", pUnkCP, iid, dw);
266     return S_OK;
267 }
268
269 /***********************************************************************
270  *           AtlFreeMarshalStream         [ATL.@]
271  */
272 HRESULT WINAPI AtlFreeMarshalStream(IStream *stm)
273 {
274     FIXME("%p\n", stm);
275     return S_OK;
276 }
277
278 /***********************************************************************
279  *           AtlMarshalPtrInProc         [ATL.@]
280  */
281 HRESULT WINAPI AtlMarshalPtrInProc(IUnknown *pUnk, const IID *iid, IStream **pstm)
282 {
283     FIXME("%p %p %p\n", pUnk, iid, pstm);
284     return E_FAIL;
285 }
286
287 /***********************************************************************
288  *           AtlUnmarshalPtr              [ATL.@]
289  */
290 HRESULT WINAPI AtlUnmarshalPtr(IStream *stm, const IID *iid, IUnknown **ppUnk)
291 {
292     FIXME("%p %p %p\n", stm, iid, ppUnk);
293     return E_FAIL;
294 }
295
296 /***********************************************************************
297  *           AtlModuleGetClassObject              [ATL.@]
298  */
299 HRESULT WINAPI AtlModuleGetClassObject(_ATL_MODULEW *pm, REFCLSID rclsid,
300                                        REFIID riid, LPVOID *ppv)
301 {
302     FIXME("%p %p %p %p\n", pm, rclsid, riid, ppv);
303     return E_FAIL;
304 }
305
306 /***********************************************************************
307  *           AtlModuleGetClassObject              [ATL.@]
308  */
309 HRESULT WINAPI AtlModuleRegisterTypeLib(_ATL_MODULEW *pm, LPCOLESTR lpszIndex)
310 {
311     FIXME("%p %s\n", pm, debugstr_w(lpszIndex));
312     return E_FAIL;
313 }
314
315 /***********************************************************************
316  *           AtlModuleRevokeClassObjects          [ATL.@]
317  */
318 HRESULT WINAPI AtlModuleRevokeClassObjects(_ATL_MODULEW *pm)
319 {
320     FIXME("%p\n", pm);
321     return E_FAIL;
322 }
323
324 /***********************************************************************
325  *           AtlModuleUnregisterServer           [ATL.@]
326  */
327 HRESULT WINAPI AtlModuleUnregisterServer(_ATL_MODULEW *pm, const CLSID *clsid)
328 {
329     FIXME("%p %s\n", pm, debugstr_guid(clsid));
330     return E_FAIL;
331 }
332
333 /***********************************************************************
334  *           AtlAxCreateControl           [ATL.@]
335  */
336 HRESULT WINAPI AtlAxCreateControl(LPCOLESTR lpszName, HWND hWnd,
337         IStream *pStream, IUnknown **ppUnkContainer)
338 {
339     FIXME("%s %p %p %p)\n", debugstr_w(lpszName), hWnd, pStream, ppUnkContainer);
340     return E_NOTIMPL;
341 }