c++ - 使用 g++ 编译服务 DLL 以在 SvcHost.exe 中使用
问题描述
我的任务是使用 g++ 编译器创建一个由 SvcHost.exe 托管的 Windows 服务 DLL。到目前为止,我已经设置了服务,使用 ServiceMain 创建了一个 DLL,将“ServiceDLL”添加到 SYSTEM 注册表中,并将“(组名)”添加到 SOFTWARE 注册表中。
我使用了在Microsoft 找到的文档 - 编写 ServiceMain 函数,并跟踪了 MSDN 目录中的每个兔子洞。我没有做的一件事是下载 Visual Studio。
用其他服务做对比,看来我创建的一个服务是正确的,注册表也是正确的。我的代码应该是正确的......所以我唯一没有遵循说明的是编译方法。
因此我的问题是:“如何编译服务 DLL 以使用带有 g++ 的 SvcHost.exe 托管?”
免责声明:我知道微软建议不要使用 SvcHost.exe,但我不负责该项目或它的设计;我只是个苦工。
目前,我正在执行以下编译...
c:\> g++ -c svchostdemo.cpp
c:\> g++ -shared -o SvcHostDemo.dll svchostdemo.o
我做了以下事情来创建服务......
c:\> sc create SvcHostDemo binpath= "%SystemRoot%\System32\svchost.exe -k demo" type= share
已添加/修改以下键...
HKLM\SYSTEM\CurrentControlSet\services\SvcHostDemo\Parameters\ServiceDLL = %SystemRoot%\System32\SvcHostDemo.dll
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost\demo = SvcHostDemo
这是我启动服务时得到的...
C:\Windows>sc start SvcHostDemo
SERVICE_NAME: SvcHostDemo
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
PID : 6844
FLAGS :
这是我查询服务时得到的...
C:\Windows>sc query SvcHostDemo
SERVICE_NAME: SvcHostDemo
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 1 STOPPED
WIN32_EXIT_CODE : 193 (0xc1)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
DLL 代码...
#include <windows.h>
#define SVCNAME TEXT("SvcHostDemo")
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE gSvcStatusHandle;
HANDLE ghSvcStopEvent = NULL;
LPHANDLER_FUNCTION SvcCtrlHandler;
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv);
VOID ReportSvcStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint);
//
// Purpose:
// Entry point for the service
//
// Parameters:
// dwArgc - Number of arguments in the lpszArgv array
// lpszArgv - Array of strings. The first string is the name of
// the service and subsequent strings are passed by the process
// that called the StartService function to start the service.
//
// Return value:
// None.
//
VOID WINAPI ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv )
{
// Register the handler function for the service
gSvcStatusHandle = RegisterServiceCtrlHandler(
SVCNAME,
SvcCtrlHandler);
if( !gSvcStatusHandle )
{
MessageBox(NULL, "FAIL", "FAIL", MB_OK | MB_ICONQUESTION);
//SvcReportEvent(TEXT("RegisterServiceCtrlHandler"));
return;
}
MessageBox(NULL, "PASS", "PASS", MB_OK | MB_ICONQUESTION);
// These SERVICE_STATUS members remain as set here
gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
gSvcStatus.dwServiceSpecificExitCode = 0;
// Report initial status to the SCM
ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );
// Perform service-specific initialization and work.
SvcInit( dwArgc, lpszArgv );
}
//
// Purpose:
// The service code
//
// Parameters:
// dwArgc - Number of arguments in the lpszArgv array
// lpszArgv - Array of strings. The first string is the name of
// the service and subsequent strings are passed by the process
// that called the StartService function to start the service.
//
// Return value:
// None
//
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv)
{
// TO_DO: Declare and set any required variables.
// Be sure to periodically call ReportSvcStatus() with
// SERVICE_START_PENDING. If initialization fails, call
// ReportSvcStatus with SERVICE_STOPPED.
// Create an event. The control handler function, SvcCtrlHandler,
// signals this event when it receives the stop control code.
ghSvcStopEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual reset event
FALSE, // not signaled
NULL); // no name
if ( ghSvcStopEvent == NULL)
{
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
// Report running status when initialization is complete.
ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );
// TO_DO: Perform work until service stops.
while(1)
{
// Check whether to stop the service.
WaitForSingleObject(ghSvcStopEvent, INFINITE);
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
}
//
// Purpose:
// Sets the current service status and reports it to the SCM.
//
// Parameters:
// dwCurrentState - The current state (see SERVICE_STATUS)
// dwWin32ExitCode - The system error code
// dwWaitHint - Estimated time for pending operation,
// in milliseconds
//
// Return value:
// None
//
VOID ReportSvcStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
// Fill in the SERVICE_STATUS structure.
gSvcStatus.dwCurrentState = dwCurrentState;
gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
gSvcStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
gSvcStatus.dwControlsAccepted = 0;
else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ( (dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED) )
gSvcStatus.dwCheckPoint = 0;
else gSvcStatus.dwCheckPoint = dwCheckPoint++;
// Report the status of the service to the SCM.
SetServiceStatus( gSvcStatusHandle, &gSvcStatus );
}
// TO COMPILE:
// c:\> g++ -c svchostdemo.cpp
// c:\> g++ -shared -o SvcHostDemo.dll svchostdemo.o
解决方案
推荐阅读
- midi - 从 MIDI 轨道弹出消息
- html - 允许用户在 MySQL 表单提交中使用 enter 键换行
- ruby-on-rails - Rails 错误找不到没有 ID 的专辑
- scala - Apache Spark:如何保存数据框结果(带有连接的数据框),因此对数据框的操作不会改变结果?
- bootstrap-4 - 引导程序“行”导致 300 毫秒输入延迟
- android - 如何在 android 库中创建应用程序特定文件夹并保存也可以在 API 中共享的图像低于 29 和高于 29
- java - 用于 SRS 传感器的 Android Automotive CarSensorManager
- python - 如何在 Altair 图表上绘制闭合多边形
- python - 如何在 Synology DSM 上将 SQLite 升级到最新版本?
- python - 使用 sympy 获取返回标量的函数的 jacobian wrt 符号矩阵