31ff89050f7957db66d04a0f774af77eb773014a
[reactos.git] / reactos / dll / win32 / 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 "qmgr.h"
25
26 #include <stdio.h>
27
28 #include <winuser.h>
29 #include <winreg.h>
30 #include <advpub.h>
31 #include <olectl.h>
32 #include <initguid.h>
33
34 /* Handle to the base address of this DLL */
35 static HINSTANCE hInst;
36
37 /* Other GUIDs used by this module */
38 DEFINE_GUID(CLSID_BackgroundCopyQMgr, 0x69AD4AEE, 0x51BE, 0x439b, 0xA9,0x2C, 0x86,0xAE,0x49,0x0E,0x8B,0x30);
39
40 /* Entry point for DLL */
41 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
42 {
43 TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
44
45 switch (fdwReason)
46 {
47 case DLL_WINE_PREATTACH:
48 return FALSE; /* prefer native version */
49 case DLL_PROCESS_ATTACH:
50 DisableThreadLibraryCalls(hinstDLL);
51 hInst = hinstDLL;
52 break;
53 }
54
55 return TRUE;
56 }
57
58 static HRESULT init_register_strtable(STRTABLEA *strtable)
59 {
60 #define CLSID_EXPANSION_ENTRY(id) { "CLSID_" #id, &CLSID_ ## id }
61 static const struct {
62 const char *name;
63 const CLSID *clsid;
64 } expns[] = {
65 CLSID_EXPANSION_ENTRY(BackgroundCopyQMgr),
66 CLSID_EXPANSION_ENTRY(BackgroundCopyManager)
67 };
68 #undef CLSID_EXPANSION_ENTRY
69 static STRENTRYA pse[sizeof expns / sizeof expns[0]];
70 DWORD i;
71
72 strtable->cEntries = sizeof pse / sizeof pse[0];
73 strtable->pse = pse;
74 for (i = 0; i < strtable->cEntries; i++) {
75 static const char dummy_sample[] = "{12345678-1234-1234-1234-123456789012}";
76 const CLSID *clsid = expns[i].clsid;
77 pse[i].pszName = qmgr_strdup(expns[i].name);
78 pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, sizeof dummy_sample);
79 if (!pse[i].pszName || !pse[i].pszValue)
80 return E_OUTOFMEMORY;
81 sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
82 clsid->Data1, clsid->Data2, clsid->Data3, clsid->Data4[0],
83 clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], clsid->Data4[4],
84 clsid->Data4[5], clsid->Data4[6], clsid->Data4[7]);
85 }
86
87 return S_OK;
88 }
89
90 static void cleanup_register_strtable(STRTABLEA *strtable)
91 {
92 DWORD i;
93 for (i = 0; i < strtable->cEntries; i++) {
94 HeapFree(GetProcessHeap(), 0, strtable->pse[i].pszName);
95 HeapFree(GetProcessHeap(), 0, strtable->pse[i].pszValue);
96 if (!strtable->pse[i].pszName || !strtable->pse[i].pszValue)
97 return;
98 }
99 }
100
101 /* Use an INF file to register or unregister the DLL */
102 static HRESULT register_server(BOOL do_register)
103 {
104 HRESULT hr;
105 STRTABLEA strtable;
106 HMODULE hAdvpack;
107 HRESULT (WINAPI *pRegInstall)(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable);
108 static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
109
110 TRACE("(%x)\n", do_register);
111
112 hAdvpack = LoadLibraryW(wszAdvpack);
113 pRegInstall = (void *)GetProcAddress(hAdvpack, "RegInstall");
114
115 hr = init_register_strtable(&strtable);
116 if (SUCCEEDED(hr))
117 hr = pRegInstall(hInst, do_register ? "RegisterDll" : "UnregisterDll",
118 &strtable);
119 cleanup_register_strtable(&strtable);
120
121 if (FAILED(hr))
122 ERR("RegInstall failed: %08x\n", hr);
123
124 return hr;
125 }
126
127 HRESULT WINAPI DllRegisterServer(void)
128 {
129 return register_server(TRUE);
130 }
131
132 HRESULT WINAPI DllUnregisterServer(void)
133 {
134 return register_server(FALSE);
135 }