Release 1.5.29.
[wine] / dlls / qmgr / qmgr_main.c
1 /*
2  * Main DLL interface to Queue Manager (BITS)
3  *
4  * Background Intelligent Transfer Service (BITS) interface.  Dll is named
5  * qmgr for backwards compatibility with early versions of BITS.
6  *
7  * Copyright 2007 Google (Roy Shea)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include <stdio.h>
25
26 #include "objbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "advpub.h"
30 #include "olectl.h"
31 #include "winsvc.h"
32
33 #include "bits.h"
34 #include "qmgr.h"
35 #include "initguid.h"
36
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
40
41 /* Handle to the base address of this DLL */
42 static HINSTANCE hInst;
43
44 /* Other GUIDs used by this module */
45 DEFINE_GUID(CLSID_BackgroundCopyQMgr, 0x69AD4AEE, 0x51BE, 0x439b, 0xA9,0x2C, 0x86,0xAE,0x49,0x0E,0x8B,0x30);
46
47 /* Entry point for DLL */
48 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
49 {
50     TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
51
52     switch (fdwReason)
53     {
54         case DLL_WINE_PREATTACH:
55             return FALSE;  /* prefer native version */
56         case DLL_PROCESS_ATTACH:
57             DisableThreadLibraryCalls(hinstDLL);
58             hInst = hinstDLL;
59             break;
60     }
61
62     return TRUE;
63 }
64
65 static HRESULT init_register_strtable(STRTABLEA *strtable)
66 {
67 #define CLSID_EXPANSION_ENTRY(id) { "CLSID_" #id, &CLSID_ ## id }
68     static const struct {
69         const char *name;
70         const CLSID *clsid;
71     } expns[] =  {
72         CLSID_EXPANSION_ENTRY(BackgroundCopyQMgr),
73         CLSID_EXPANSION_ENTRY(BackgroundCopyManager)
74     };
75 #undef CLSID_EXPANSION_ENTRY
76     static STRENTRYA pse[sizeof expns / sizeof expns[0]];
77     DWORD i;
78
79     strtable->cEntries = sizeof pse / sizeof pse[0];
80     strtable->pse = pse;
81     for (i = 0; i < strtable->cEntries; i++) {
82         static const char dummy_sample[] = "{12345678-1234-1234-1234-123456789012}";
83         const CLSID *clsid = expns[i].clsid;
84         pse[i].pszName = qmgr_strdup(expns[i].name);
85         pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, sizeof dummy_sample);
86         if (!pse[i].pszName || !pse[i].pszValue)
87             return E_OUTOFMEMORY;
88         sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
89                 clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
90                 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4],
91                 clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
92     }
93
94     return S_OK;
95 }
96
97 static void cleanup_register_strtable(STRTABLEA *strtable)
98 {
99     DWORD i;
100     for (i = 0; i < strtable->cEntries; i++) {
101         HeapFree(GetProcessHeap(), 0, strtable->pse[i].pszName);
102         HeapFree(GetProcessHeap(), 0, strtable->pse[i].pszValue);
103         if (!strtable->pse[i].pszName || !strtable->pse[i].pszValue)
104             return;
105     }
106 }
107
108 /* Use an INF file to register or unregister the DLL */
109 static HRESULT register_server(BOOL do_register)
110 {
111     HRESULT hr;
112     STRTABLEA strtable;
113     HMODULE hAdvpack;
114     HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
115     static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
116
117     TRACE("(%x)\n", do_register);
118
119     hAdvpack = LoadLibraryW(wszAdvpack);
120     pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
121
122     hr = init_register_strtable(&strtable);
123     if (SUCCEEDED(hr))
124         hr = pRegInstall(hInst, do_register ? "RegisterDll" : "UnregisterDll",
125                          &strtable);
126     cleanup_register_strtable(&strtable);
127
128     if (FAILED(hr))
129         ERR("RegInstall failed: %08x\n", hr);
130
131     return hr;
132 }
133
134 HRESULT WINAPI DllRegisterServer(void)
135 {
136     return register_server(TRUE);
137 }
138
139 HRESULT WINAPI DllUnregisterServer(void)
140 {
141     return register_server(FALSE);
142 }