netstat: Initial implementation.
[wine] / dlls / wshom.ocx / wshom_main.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "wshom_private.h"
20
21 #include "initguid.h"
22 #include "wshom.h"
23 #include "rpcproxy.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(wshom);
28
29 static HINSTANCE wshom_instance;
30
31 static ITypeLib *typelib;
32 static ITypeInfo *typeinfos[LAST_tid];
33
34 static REFIID tid_ids[] = {
35     &IID_NULL,
36     &IID_IWshShell3,
37     &IID_IWshCollection,
38     &IID_IWshShortcut
39 };
40
41 static HRESULT load_typelib(void)
42 {
43     HRESULT hres;
44     ITypeLib *tl;
45
46     hres = LoadRegTypeLib(&LIBID_IWshRuntimeLibrary, 1, 0, LOCALE_SYSTEM_DEFAULT, &tl);
47     if(FAILED(hres)) {
48         ERR("LoadRegTypeLib failed: %08x\n", hres);
49         return hres;
50     }
51
52     if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
53         ITypeLib_Release(tl);
54     return hres;
55 }
56
57 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
58 {
59     HRESULT hres;
60
61     if (!typelib)
62         hres = load_typelib();
63     if (!typelib)
64         return hres;
65
66     if(!typeinfos[tid]) {
67         ITypeInfo *ti;
68
69         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
70         if(FAILED(hres)) {
71             ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
72             return hres;
73         }
74
75         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
76             ITypeInfo_Release(ti);
77     }
78
79     *typeinfo = typeinfos[tid];
80     ITypeInfo_AddRef(*typeinfo);
81     return S_OK;
82 }
83
84 static
85 void release_typelib(void)
86 {
87     unsigned i;
88
89     if(!typelib)
90         return;
91
92     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
93         if(typeinfos[i])
94             ITypeInfo_Release(typeinfos[i]);
95
96     ITypeLib_Release(typelib);
97 }
98
99 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
100 {
101     *ppv = NULL;
102
103     if(IsEqualGUID(&IID_IUnknown, riid)) {
104         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
105         *ppv = iface;
106     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
107         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
108         *ppv = iface;
109     }
110
111     if(*ppv) {
112         IUnknown_AddRef((IUnknown*)*ppv);
113         return S_OK;
114     }
115
116     FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
117     return E_NOINTERFACE;
118 }
119
120 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
121 {
122     TRACE("(%p)\n", iface);
123     return 2;
124 }
125
126 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
127 {
128     TRACE("(%p)\n", iface);
129     return 1;
130 }
131
132 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
133 {
134     TRACE("(%p)->(%x)\n", iface, fLock);
135     return S_OK;
136 }
137
138 static const IClassFactoryVtbl WshShellFactoryVtbl = {
139     ClassFactory_QueryInterface,
140     ClassFactory_AddRef,
141     ClassFactory_Release,
142     WshShellFactory_CreateInstance,
143     ClassFactory_LockServer
144 };
145
146 static IClassFactory WshShellFactory = { &WshShellFactoryVtbl };
147
148 /******************************************************************
149  *              DllMain (wshom.ocx.@)
150  */
151 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
152 {
153     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
154
155     switch(fdwReason)
156     {
157     case DLL_WINE_PREATTACH:
158         return FALSE;  /* prefer native version */
159     case DLL_PROCESS_ATTACH:
160         wshom_instance = hInstDLL;
161         DisableThreadLibraryCalls(wshom_instance);
162         break;
163     case DLL_PROCESS_DETACH:
164         release_typelib();
165         break;
166     }
167
168     return TRUE;
169 }
170
171 /***********************************************************************
172  *              DllGetClassObject       (wshom.ocx.@)
173  */
174 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
175 {
176     if(IsEqualGUID(&CLSID_WshShell, rclsid)) {
177         TRACE("(CLSID_WshShell %s %p)\n", debugstr_guid(riid), ppv);
178         return IClassFactory_QueryInterface(&WshShellFactory, riid, ppv);
179     }
180
181     FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
182     return CLASS_E_CLASSNOTAVAILABLE;
183 }
184
185 /***********************************************************************
186  *          DllCanUnloadNow (wshom.ocx.@)
187  */
188 HRESULT WINAPI DllCanUnloadNow(void)
189 {
190     return S_FALSE;
191 }
192
193 /***********************************************************************
194  *          DllRegisterServer (wshom.ocx.@)
195  */
196 HRESULT WINAPI DllRegisterServer(void)
197 {
198     TRACE("()\n");
199     return __wine_register_resources(wshom_instance);
200 }
201
202 /***********************************************************************
203  *          DllUnregisterServer (wshom.ocx.@)
204  */
205 HRESULT WINAPI DllUnregisterServer(void)
206 {
207     TRACE("()\n");
208     return __wine_unregister_resources(wshom_instance);
209 }