msvcrt: Allow forwarding to stub functions when the arguments are known.
[wine] / dlls / wiaservc / wiaservc_main.c
1 /*
2  * Main DLL interface to WIA Device Manager
3  *
4  * Copyright 2009 Damjan Jovanovic
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 <stdio.h>
22
23 #include "objbase.h"
24 #include "winuser.h"
25 #include "winreg.h"
26 #include "advpub.h"
27 #include "olectl.h"
28 #include "rpcproxy.h"
29 #include "winsvc.h"
30
31 #include "wia_lh.h"
32 #include "initguid.h"
33
34 #include "wiaservc_private.h"
35
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(wia);
39
40 /* Handle to the base address of this DLL */
41 static HINSTANCE hInst;
42
43 /* Entry point for DLL */
44 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
45 {
46     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
47
48     switch (fdwReason)
49     {
50         case DLL_WINE_PREATTACH:
51             return FALSE;  /* prefer native version */
52         case DLL_PROCESS_ATTACH:
53             DisableThreadLibraryCalls(hinstDLL);
54             hInst = hinstDLL;
55             break;
56         case DLL_PROCESS_DETACH:
57             break;
58     }
59
60     return TRUE;
61 }
62
63 static HRESULT init_register_strtable(STRTABLEA *strtable)
64 {
65 #define CLSID_EXPANSION_ENTRY(id) { "CLSID_" #id, &CLSID_ ## id }
66     static const struct {
67         const char *name;
68         const CLSID *clsid;
69     } expns[] =  {
70         CLSID_EXPANSION_ENTRY(WiaDevMgr)
71     };
72 #undef CLSID_EXPANSION_ENTRY
73     static STRENTRYA pse[sizeof expns / sizeof expns[0]];
74     DWORD i;
75
76     strtable->cEntries = sizeof pse / sizeof pse[0];
77     strtable->pse = pse;
78     for (i = 0; i < strtable->cEntries; i++) {
79         static const char dummy_sample[] = "{12345678-1234-1234-1234-123456789012}";
80         const CLSID *clsid = expns[i].clsid;
81         pse[i].pszName = wiaservc_strdup(expns[i].name);
82         pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, sizeof dummy_sample);
83         if (!pse[i].pszName || !pse[i].pszValue)
84             return E_OUTOFMEMORY;
85         sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
86                 clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
87                 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4],
88                 clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
89     }
90
91     return S_OK;
92 }
93
94 static void cleanup_register_strtable(STRTABLEA *strtable)
95 {
96     DWORD i;
97     for (i = 0; i < strtable->cEntries; i++) {
98         HeapFree(GetProcessHeap(), 0, strtable->pse[i].pszName);
99         HeapFree(GetProcessHeap(), 0, strtable->pse[i].pszValue);
100         if (!strtable->pse[i].pszName || !strtable->pse[i].pszValue)
101             return;
102     }
103 }
104
105 static HRESULT register_service(BOOL do_register)
106 {
107     static const WCHAR name[] = { 's','t','i','s','v','c', 0 };
108     static const WCHAR path[] = { 's','v','c','h','o','s','t','.','e','x','e',
109                                   ' ','-','k',' ','i','m','g','s','v','c', 0 };
110     SC_HANDLE scm, service;
111
112     scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
113     if (!scm)
114         return SELFREG_E_CLASS;
115
116     if (do_register)
117         service = CreateServiceW(scm, name, name, SERVICE_ALL_ACCESS,
118                                  SERVICE_WIN32_OWN_PROCESS,
119                                  SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
120                                  path, NULL, NULL, NULL, NULL, NULL);
121     else
122         service = OpenServiceW(scm, name, DELETE);
123
124
125     CloseServiceHandle(scm);
126     if (service)
127     {
128         if (!do_register) DeleteService(service);
129         CloseServiceHandle(service);
130     }
131     return S_OK;
132 }
133
134 /* Use an INF file to register or unregister the DLL */
135 static HRESULT register_server(BOOL do_register)
136 {
137     HRESULT hr;
138     STRTABLEA strtable;
139     HMODULE hAdvpack;
140     HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
141     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
142
143     TRACE("(%x)\n", do_register);
144
145     hr = register_service(do_register);
146     if (FAILED(hr)) {
147         ERR("register_service failed: %d\n", GetLastError());
148         return hr;
149     }
150
151     hAdvpack = LoadLibraryW(wszAdvpack);
152     pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
153
154     hr = init_register_strtable(&strtable);
155     if (SUCCEEDED(hr))
156         hr = pRegInstall(hInst, do_register ? "RegisterDll" : "UnregisterDll",
157                          &strtable);
158     cleanup_register_strtable(&strtable);
159
160     if (FAILED(hr))
161         ERR("RegInstall failed: %08x\n", hr);
162
163     return hr;
164 }
165
166 HRESULT WINAPI DllRegisterServer(void)
167 {
168     HRESULT hr = __wine_register_resources( hInst, NULL );
169     if (FAILED(hr)) return hr;
170     return register_server(TRUE);
171 }
172
173 HRESULT WINAPI DllUnregisterServer(void)
174 {
175     HRESULT hr = __wine_unregister_resources( hInst, NULL );
176     if (FAILED(hr)) return hr;
177     return register_server(FALSE);
178 }