[PATCH] bits: Stub version of qmgr with svchost and proxy (5/12)
Roy Shea
roy at cs.hmc.edu
Thu Nov 15 10:22:24 CST 2007
This adds svchost, proxying, and a stub version of the qmgr.dll process. The
functional code in this patch is boilerplate required to run a DLL as a
service via svchost.
---
dlls/qmgr/Makefile.in | 6 ++-
dlls/qmgr/factory.c | 85 +++++++++++++++++++++++
dlls/qmgr/qmgr.c | 8 ++-
dlls/qmgr/qmgr.inf | 32 +++++++++
dlls/qmgr/qmgr.spec | 1 +
dlls/qmgr/qmgr_main.c | 98 +++++++++++++++++++++++++--
dlls/qmgr/qmgr_private.h | 14 ++++-
dlls/qmgr/qmgr_service.c | 156 +++++++++++++++++++++++++++++++++++++++++++
dlls/qmgr/rsrc.rc | 21 ++++++
dlls/qmgrprxy/Makefile.in | 25 +++++++
dlls/qmgrprxy/qmgrprxy.idl | 20 ++++++
dlls/qmgrprxy/qmgrprxy.spec | 5 ++
12 files changed, 461 insertions(+), 10 deletions(-)
create mode 100644 dlls/qmgr/factory.c
create mode 100644 dlls/qmgr/qmgr.inf
create mode 100644 dlls/qmgr/qmgr_service.c
create mode 100644 dlls/qmgr/rsrc.rc
create mode 100644 dlls/qmgrprxy/Makefile.in
create mode 100644 dlls/qmgrprxy/qmgrprxy.idl
create mode 100644 dlls/qmgrprxy/qmgrprxy.spec
diff --git a/dlls/qmgr/Makefile.in b/dlls/qmgr/Makefile.in
index 6975e0f..1886a0d 100644
--- a/dlls/qmgr/Makefile.in
+++ b/dlls/qmgr/Makefile.in
@@ -3,13 +3,17 @@ TOPOBJDIR = ../..
SRCDIR = @srcdir@
VPATH = @srcdir@
MODULE = qmgr.dll
-IMPORTS = kernel32
+IMPORTS = kernel32 advapi32 ole32 user32
EXTRALIBS =
C_SRCS = \
+ factory.c \
qmgr_main.c \
+ qmgr_service.c \
qmgr.c
+RC_SRCS = rsrc.rc
+
IDL_I_SRCS = qmgr_local.idl
@MAKE_DLL_RULES@
diff --git a/dlls/qmgr/factory.c b/dlls/qmgr/factory.c
new file mode 100644
index 0000000..daaedce
--- /dev/null
+++ b/dlls/qmgr/factory.c
@@ -0,0 +1,85 @@
+/*
+ * Class factory interface for Queue Manager (BITS)
+ *
+ * Copyright (C) 2007 Google (Roy Shea)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "objbase.h"
+
+#include "qmgr_private.h"
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(qmgr_svchost);
+
+static ULONG WINAPI BITS_IClassFactory_AddRef(LPCLASSFACTORY iface)
+{
+ FIXME("Not implemented\n");
+ return E_NOTIMPL;
+}
+
+static HRESULT WINAPI BITS_IClassFactory_QueryInterface(
+ LPCLASSFACTORY iface,
+ REFIID riid,
+ LPVOID *ppvObj)
+{
+ FIXME("Not implemented\n");
+ return E_NOTIMPL;
+}
+
+static ULONG WINAPI BITS_IClassFactory_Release(LPCLASSFACTORY iface)
+{
+ FIXME("Not implemented\n");
+ return E_NOTIMPL;
+}
+
+static HRESULT WINAPI BITS_IClassFactory_CreateInstance(
+ LPCLASSFACTORY iface,
+ LPUNKNOWN pUnkOuter,
+ REFIID riid,
+ LPVOID *ppvObj)
+{
+ FIXME("Not implemented\n");
+ return E_NOTIMPL;
+}
+
+static HRESULT WINAPI BITS_IClassFactory_LockServer(
+ LPCLASSFACTORY iface,
+ BOOL fLock)
+{
+ FIXME("Not implemented\n");
+ return E_NOTIMPL;
+}
+
+static const IClassFactoryVtbl BITS_IClassFactory_Vtbl =
+{
+ BITS_IClassFactory_QueryInterface,
+ BITS_IClassFactory_AddRef,
+ BITS_IClassFactory_Release,
+ BITS_IClassFactory_CreateInstance,
+ BITS_IClassFactory_LockServer
+};
+
+ClassFactoryImpl BITS_ClassFactory =
+{
+ &BITS_IClassFactory_Vtbl,
+ 0
+};
+
diff --git a/dlls/qmgr/qmgr.c b/dlls/qmgr/qmgr.c
index 65106bc..3247dc0 100644
--- a/dlls/qmgr/qmgr.c
+++ b/dlls/qmgr/qmgr.c
@@ -93,7 +93,7 @@ static HRESULT WINAPI BITS_IBackgroundCopyManager_GetErrorDescription(
}
-const IBackgroundCopyManagerVtbl BITS_IBackgroundCopyManager_Vtbl =
+static const IBackgroundCopyManagerVtbl BITS_IBackgroundCopyManager_Vtbl =
{
BITS_IBackgroundCopyManager_QueryInterface,
BITS_IBackgroundCopyManager_AddRef,
@@ -104,3 +104,9 @@ const IBackgroundCopyManagerVtbl BITS_IBackgroundCopyManager_Vtbl =
BITS_IBackgroundCopyManager_GetErrorDescription
};
+BackgroundCopyManagerImpl BITS_BackgroundCopyManager =
+{
+ &BITS_IBackgroundCopyManager_Vtbl,
+ 0
+};
+
diff --git a/dlls/qmgr/qmgr.inf b/dlls/qmgr/qmgr.inf
new file mode 100644
index 0000000..4b9fb56
--- /dev/null
+++ b/dlls/qmgr/qmgr.inf
@@ -0,0 +1,32 @@
+[version]
+Signature="$CHICAGO$"
+
+
+[Strings]
+Services="System\CurrentControlSet\Services"
+
+
+[RegisterDll]
+AddReg = Qmgr.Reg
+
+
+[UnregisterDll]
+DelReg = Qmgr.Reg
+
+
+[Qmgr.Reg]
+
+HKCR,"AppID\BITS","AppID",,"%CLSID_BackgroundCopyQMgr%"
+HKCR,"AppID\%CLSID_BackgroundCopyQMgr%","LocalService",,"BITS"
+HKCR,"CLSID\%CLSID_BackgroundCopyManager%","AppID",,"%CLSID_BackgroundCopyQMgr%"
+
+HKLM,"Software\Microsoft\Windows NT\CurrentVersion\SvcHost","netsvcs",0x00010000,"BITS"
+
+HKLM,"%Services%\BITS","Type",0x00010001,00000110
+HKLM,"%Services%\BITS","Start",0x00010001,00000003
+HKLM,"%Services%\BITS","ErrorControl",0x00010001,00000001
+HKLM,"%Services%\BITS","Description",,"BITS Service"
+HKLM,"%Services%\BITS","DispalyName",,"BITS"
+HKLM,"%Services%\BITS","ImagePath",0x00020000,"svchost.exe -k netsvcs"
+HKLM,"%Services%\BITS\Parameters","ServiceDll",0x00020000,"qmgr.dll"
+
diff --git a/dlls/qmgr/qmgr.spec b/dlls/qmgr/qmgr.spec
index c2eb3c0..13979d0 100644
--- a/dlls/qmgr/qmgr.spec
+++ b/dlls/qmgr/qmgr.spec
@@ -2,4 +2,5 @@
@ stdcall -private DllGetClassObject(ptr ptr ptr)
@ stub DllRegisterServer
@ stub DllUnregisterServer
+@ stdcall -private ServiceMain(long ptr)
diff --git a/dlls/qmgr/qmgr_main.c b/dlls/qmgr/qmgr_main.c
index 89ba17c..a6fee51 100644
--- a/dlls/qmgr/qmgr_main.c
+++ b/dlls/qmgr/qmgr_main.c
@@ -21,45 +21,129 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
-#include <stdarg.h>
+#include <stdio.h>
-#include "windef.h"
-#include "winbase.h"
#include "objbase.h"
+#include "winuser.h"
+#include "winreg.h"
+#include "advpub.h"
+#include "bits.h"
+#include "initguid.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
+/* Number of references to this DLL */
LONG dll_ref = 0;
-BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
+/* Handle to the base address of this DLL */
+static HINSTANCE hInst;
+
+/* Other GUIDs used by this module */
+DEFINE_GUID(CLSID_BackgroundCopyQMgr, 0x69AD4AEE, 0x51BE, 0x439b, 0xA9,0x2C, 0x86,0xAE,0x49,0x0E,0x8B,0x30);
+
+/* Entry point for DLL */
+BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
- TRACE("(%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
+ TRACE("(%p, %d, %p)\n", hInstDLL, fdwReason, lpvReserved);
switch (fdwReason)
{
case DLL_WINE_PREATTACH:
return FALSE; /* prefer native version */
case DLL_PROCESS_ATTACH:
- DisableThreadLibraryCalls(hinstDLL);
+ DisableThreadLibraryCalls(hInstDLL);
+ hInst = hInstDLL;
break;
case DLL_PROCESS_DETACH:
break;
}
-
return TRUE;
}
+/* Retreive class object from DLL */
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
{
FIXME("Not implemented\n");
return E_NOTIMPL;
}
+/* Check to see if the DLL can be unloaded */
HRESULT WINAPI DllCanUnloadNow(void)
{
return dll_ref != 0 ? S_FALSE : S_OK;
}
+/* Helper macro used to package GUID strings to expand the INF string section */
+#define INF_SET_ID(id) \
+ do \
+ { \
+ static CHAR name[] = #id; \
+ \
+ pse[i].pszName = name; \
+ clsids[i++] = &id; \
+ } while (0)
+
+#define INF_SET_CLSID(clsid) INF_SET_ID(CLSID_ ## clsid)
+
+/* Use an INF file to register or unregister the DLL */
+static HRESULT register_server(BOOL do_register)
+{
+ HRESULT hres;
+ HMODULE hAdvpack;
+ typeof(RegInstallA) *pRegInstall;
+ STRTABLEA strtable;
+ STRENTRYA pse[2];
+ static CLSID const *clsids[2];
+ int i = 0;
+
+ static const WCHAR wszAdvpack[] = {'a','d','v','p','a','c','k','.','d','l','l',0};
+
+ TRACE("(%x)\n", do_register);
+
+ INF_SET_CLSID(BackgroundCopyQMgr);
+ INF_SET_CLSID(BackgroundCopyManager);
+
+ strtable.cEntries = sizeof(pse)/sizeof(pse[0]);
+ strtable.pse = pse;
+
+ for(i=0; i < strtable.cEntries; i++) {
+ pse[i].pszValue = HeapAlloc(GetProcessHeap(), 0, 39);
+ sprintf(pse[i].pszValue, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
+ clsids[i]->Data1, clsids[i]->Data2, clsids[i]->Data3, clsids[i]->Data4[0],
+ clsids[i]->Data4[1], clsids[i]->Data4[2], clsids[i]->Data4[3], clsids[i]->Data4[4],
+ clsids[i]->Data4[5], clsids[i]->Data4[6], clsids[i]->Data4[7]);
+ }
+
+ hAdvpack = LoadLibraryW(wszAdvpack);
+ pRegInstall = (typeof(RegInstallA)*)GetProcAddress(hAdvpack, "RegInstall");
+
+ hres = pRegInstall(hInst, do_register ? "RegisterDll" : "UnregisterDll", &strtable);
+
+ for(i=0; i < sizeof(pse)/sizeof(pse[0]); i++)
+ HeapFree(GetProcessHeap(), 0, pse[i].pszValue);
+
+ if(FAILED(hres)) {
+ ERR("RegInstall failed: %08x\n", hres);
+ }
+
+ return hres;
+}
+
+#undef INF_SET_CLSID
+#undef INF_SET_ID
+
+/* Register Queue Manager (BITS) server */
+HRESULT WINAPI DllRegisterServer(void)
+{
+ return register_server(TRUE);
+}
+
+/* Unregister Queue Manager (BITS) server */
+HRESULT WINAPI DllUnregisterServer(void)
+{
+ return register_server(FALSE);
+}
+
diff --git a/dlls/qmgr/qmgr_private.h b/dlls/qmgr/qmgr_private.h
index d4ccce3..b57a041 100644
--- a/dlls/qmgr/qmgr_private.h
+++ b/dlls/qmgr/qmgr_private.h
@@ -35,15 +35,27 @@
/* Count number of references to this DLL */
extern LONG dll_ref;
+/* Factory vtbl and related data */
+typedef struct
+{
+ const IClassFactoryVtbl *lpVtbl;
+ LONG ref;
+} ClassFactoryImpl;
+
/* Background copy manager vtbl and related data */
typedef struct
{
- const IBackgroundCopyManager *bitsVtbl;
+ const IBackgroundCopyManagerVtbl *bitsVtbl;
LONG ref;
} BackgroundCopyManagerImpl;
/* Single instance of the background copy manager used to create new instances */
+extern ClassFactoryImpl BITS_ClassFactory;
extern BackgroundCopyManagerImpl BITS_BackgroundCopyManager;
+/*
+extern HRESULT CountConstructor(IUnknown *pUnkOuter, LPVOID *ppObj);
+*/
+
#endif /* __QMGR_PRIVATE_H__ */
diff --git a/dlls/qmgr/qmgr_service.c b/dlls/qmgr/qmgr_service.c
new file mode 100644
index 0000000..ae9e44c
--- /dev/null
+++ b/dlls/qmgr/qmgr_service.c
@@ -0,0 +1,156 @@
+/*
+ * ServiceMain function for qmgr running within svchost
+ *
+ * Copyright 2007 (C) Google (Roy Shea)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+#include "objbase.h"
+#include "winsvc.h"
+
+#include "qmgr_private.h"
+#include "wine/debug.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
+
+static WCHAR qmgr_nameW[] = {'B','I','T','S',0};
+static SERVICE_STATUS_HANDLE status_handle;
+static SERVICE_STATUS status;
+static HANDLE stop_event = NULL;
+
+/* Helper function to update service status */
+static VOID UpdateStatus(DWORD dwCurrentState,
+ DWORD dwWin32ExitCode,
+ DWORD dwWaitHint)
+{
+ status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+ status.dwCurrentState = dwCurrentState;
+ if (dwCurrentState == SERVICE_START_PENDING) {
+ status.dwControlsAccepted = 0;
+ }
+ else
+ {
+ status.dwControlsAccepted = SERVICE_ACCEPT_STOP |
+ SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;
+ }
+ status.dwWin32ExitCode = 0;
+ status.dwServiceSpecificExitCode = 0;
+ status.dwCheckPoint = 0;
+ status.dwWaitHint = dwWaitHint;
+
+ if (!SetServiceStatus(status_handle, &status))
+ {
+ ERR("Failed to set service status\n");
+ SetEvent(stop_event);
+ return;
+ }
+
+ return;
+}
+
+/* Handle incoming ControlService signals */
+static DWORD WINAPI ServiceHandler(DWORD ctrl, DWORD event_type,
+ LPVOID event_data, LPVOID context)
+{
+ switch (ctrl)
+ {
+ case SERVICE_CONTROL_STOP:
+ case SERVICE_CONTROL_SHUTDOWN:
+ TRACE("shutting down service\n");
+ UpdateStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
+ SetEvent(stop_event);
+ break;
+ default:
+ FIXME("ignoring handle service ctrl %x\n", ctrl);
+ UpdateStatus(status.dwCurrentState, NO_ERROR, 0);
+ break;
+ }
+
+ return NO_ERROR;
+}
+
+/* Main thread of the service */
+static BOOL StartCount(void)
+{
+ HRESULT hr;
+ DWORD dwReg;
+
+ TRACE("\n");
+
+ hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+ if (!SUCCEEDED(hr))
+ {
+ return FALSE;
+ }
+
+ hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
+ RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IMPERSONATE,
+ NULL, EOAC_NONE, NULL);
+ if (!SUCCEEDED(hr))
+ {
+ return FALSE;
+ }
+
+ hr = CoRegisterClassObject(&CLSID_BackgroundCopyManager,
+ (IUnknown*)&BITS_ClassFactory, CLSCTX_LOCAL_SERVER,
+ REGCLS_MULTIPLEUSE, &dwReg);
+ if (!SUCCEEDED(hr))
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Service entry point */
+VOID WINAPI ServiceMain( DWORD dwArgc, LPWSTR *lpszArgv )
+{
+ TRACE("\n");
+
+ stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
+ if (stop_event == NULL)
+ {
+ ERR("Failed to create stop_event\n");
+ return;
+ }
+
+ status_handle = RegisterServiceCtrlHandlerExW(
+ qmgr_nameW, ServiceHandler, NULL);
+ if (!status_handle)
+ {
+ ERR("Failed to register handler: %u\n", GetLastError());
+ return;
+ }
+
+ UpdateStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
+ if (!StartCount())
+ {
+ ERR("Failed starting service thread\n");
+ UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
+ return;
+ }
+
+ UpdateStatus(SERVICE_RUNNING, NO_ERROR, 0);
+
+ WaitForSingleObject(stop_event, INFINITE);
+ UpdateStatus(SERVICE_STOPPED, NO_ERROR, 0);
+ TRACE("Service Stoped\n");
+}
+
diff --git a/dlls/qmgr/rsrc.rc b/dlls/qmgr/rsrc.rc
new file mode 100644
index 0000000..bf493b7
--- /dev/null
+++ b/dlls/qmgr/rsrc.rc
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2007 Google (Roy Shea)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/* @makedep: qmgr.inf */
+REGINST REGINST qmgr.inf
+
diff --git a/dlls/qmgrprxy/Makefile.in b/dlls/qmgrprxy/Makefile.in
new file mode 100644
index 0000000..eb1155e
--- /dev/null
+++ b/dlls/qmgrprxy/Makefile.in
@@ -0,0 +1,25 @@
+TOPSRCDIR = @top_srcdir@
+TOPOBJDIR = ../..
+SRCDIR = @srcdir@
+VPATH = @srcdir@
+MODULE = qmgrprxy.dll
+IMPORTLIB = libqmgrprxy.$(IMPLIBEXT)
+IMPORTS = ole32 user32 advapi32 kernel32 rpcrt4
+EXTRALIBS = -luuid
+EXTRADEFS = -DREGISTER_PROXY_DLL
+
+C_SRCS =
+
+IDL_I_SRCS = qmgrprxy.idl
+IDL_P_SRCS = qmgrprxy.idl
+
+EXTRA_OBJS = dlldata.o
+
+ at MAKE_DLL_RULES@
+
+dlldata.o: dlldata.c
+
+dlldata.c: $(WINEBUILD)
+ $(WIDL) $(IDLFLAGS) --dlldata-only $(IDL_P_SRCS:.idl=)
+
+ at DEPENDENCIES@ # everything below this line is overwritten by make depend
diff --git a/dlls/qmgrprxy/qmgrprxy.idl b/dlls/qmgrprxy/qmgrprxy.idl
new file mode 100644
index 0000000..7522fb3
--- /dev/null
+++ b/dlls/qmgrprxy/qmgrprxy.idl
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2007 Google (Roy Shea)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "bits.idl"
+
diff --git a/dlls/qmgrprxy/qmgrprxy.spec b/dlls/qmgrprxy/qmgrprxy.spec
new file mode 100644
index 0000000..5cdde31
--- /dev/null
+++ b/dlls/qmgrprxy/qmgrprxy.spec
@@ -0,0 +1,5 @@
+@ stdcall -private DllCanUnloadNow()
+@ stdcall -private DllGetClassObject(ptr ptr ptr)
+@ stdcall -private DllRegisterServer()
+@ stdcall -private DllUnregisterServer()
+@ stdcall GetProxyDllInfo(ptr ptr)
--
1.5.3.1
More information about the wine-patches
mailing list