Windows服务在字符集为unicode的情况下工作正常,但具有多字节的相同代码开始失败,出现1053错误 C++中的基本Windows服务实例代码,带有发布配置+ Unicode字符集,完全是在管理控制台中使用SC启动CMD,但是当我改变配置释放+多字节比SC启动CMD失败时,最常见的服务错误是:1053,服务没有及时响应启动或控制请求时尚

Windows服务在字符集为unicode的情况下工作正常,但具有多字节的相同代码开始失败,出现1053错误 C++中的基本Windows服务实例代码,带有发布配置+ Unicode字符集,完全是在管理控制台中使用SC启动CMD,但是当我改变配置释放+多字节比SC启动CMD失败时,最常见的服务错误是:1053,服务没有及时响应启动或控制请求时尚,unicode,windows-services,Unicode,Windows Services,我不知道这个unicode配置和多字节配置之间有什么关系。即使我已经交叉检查了每个函数的后缀,就像在unicode模式下一样,后缀是W,在多字节模式下,后缀是A #定义服务名称“USB设备监控服务” #定义睡眠时间(1000) void main() { 服务表条目服务表[1]; ServiceTable[0]。lpServiceName=服务\u名称; ServiceTable[0]。lpServiceProc=(LPSERVICE\u MAIN\u函数)ServiceMain; Start

我不知道这个unicode配置和多字节配置之间有什么关系。即使我已经交叉检查了每个函数的后缀,就像在unicode模式下一样,后缀是W,在多字节模式下,后缀是A


#定义服务名称“USB设备监控服务”
#定义睡眠时间(1000)
void main()
{
服务表条目服务表[1];
ServiceTable[0]。lpServiceName=服务\u名称;
ServiceTable[0]。lpServiceProc=(LPSERVICE\u MAIN\u函数)ServiceMain;
StartServiceCtrlDispatcher(服务表);
}
void ServiceMain()
{
开发广播设备接口通知过滤器;
Status.dwServiceType=SERVICE\u WIN32\u OWN\u进程;
Status.dwCurrentState=SERVICE\u START\u PENDING;
Status.dwControlsAccepted=服务接受停止服务接受关闭;
Status.dwWin32ExitCode=0;
Status.dwServiceSpecificExitCode=0;
Status.dwCheckPoint=0;
Status.dwWaitHint=0;
hStatus=RegisterServiceCtrlHandlerEx(服务名称,(LPHANDLER\u函数\u EX)ControlHandler,0);
if((服务状态句柄)0==hStatus)
{
//错误
}
设置服务状态(hs状态和状态);
Status.dwCurrentState=正在运行的服务;
设置服务状态(hs状态和状态);
零内存(&NotificationFilter,sizeof(NotificationFilter));
NotificationFilter.dbcc_size=sizeof(设备接口);
NotificationFilter.dbcc_devicetype=DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid=GUID\u设备接口\u USB\u设备;
//初始化
hDeviceNotify=注册设备设备化((句柄)hStatus和NotificationFilter,设备通知服务句柄);
if(NULL==hDeviceNotify)
{
//错误
}
while(服务运行==Status.dwCurrentState)
{
睡眠(睡眠时间);
}
}
DWORD控制处理程序(DWORD dwControl、DWORD dwEventType、,
LPVOID lpram,LPVOID lpContext)
{
开关(DWC控制)
{
案例服务控制关闭:
案例服务控制站:
未注册设备认证(HDevisionify);
Status.dwCurrentState=服务\u已停止;
设置服务状态(hs状态和状态);
闭柄(hPipe);
返回无错误;
案例服务\控制\设备事件:
if((DBT_DEVICEARRIVAL==dwEventType)|(DBT_devicemovecomplete==dwEventType))
{
尝试
{
DEV_BROADCAST_HDR*头=重新解释广播(lParam);
if(DBT_DEVTYP_DEVICEINTERFACE==头->dbch_devicetype)
{
//仅解析内置USB设备
}
}
捕获(常数std::nullptr\u t/*ex*/)
{
//“错误:处理WM_DEVICECHANGE失败
}
}
打破
违约:
//“错误:未知的dwControl:dwControl)
设置服务状态(hs状态和状态);
打破
}
返回无错误;
}
您的
ServiceMain()
ControlHandler()
函数被声明为错误,但您正在使用类型转换来阻止编译器抱怨。任何时候,当您不得不使用类型转换来使编译器安静下来时,请考虑您的代码试图做什么,因为它可能会做一些错误的事情

另外,您要传递到的
SERVICE\u TABLE\u ENTRY[]
数组是不完整的-您不能像文档中所说的那样终止数组

因此,您声称在为Unicode编译时服务可以工作,但您所显示的代码实际上不会在Unicode下编译,因为
ServiceTable[0].lpServiceName
字段需要Unicode字符串,但所显示的代码指定的是ANSI字符串,这是一个错误

我建议您阅读Microsoft的文档,并密切关注其中给出的示例:

以下任务由以下人员执行:

相关话题

话虽如此,请尝试以下方式:

#define SERVICE_NAME    TEXT("USB Device Monitor Service")

HANDLE hStopEvent = NULL;
SERVICE_STATUS_HANDLE hStatus = NULL;

SERVICE_STATUS Status;

DWORD WINAPI ControlHandler(DWORD dwControl, DWORD dwEventType,
    LPVOID lParam, LPVOID lpContext)
{
    switch (dwControl)
    {
        case SERVICE_CONTROL_INTERROGATE:
            return NO_ERROR;

        case SERVICE_CONTROL_STOP:
        case SERVICE_CONTROL_SHUTDOWN:
            SetEvent(hStopEvent);
            return NO_ERROR;

        case SERVICE_CONTROL_DEVICEEVENT:
            if ((DBT_DEVICEARRIVAL == dwEventType) || (DBT_DEVICEREMOVECOMPLETE == dwEventType))
            {
                try
                {
                    DEV_BROADCAST_HDR* header = reinterpret_cast<DEV_BROADCAST_HDR*>(lParam);
                    if (DBT_DEVTYP_DEVICEINTERFACE == header->dbch_devicetype)
                    {
                        //parse intrested USB device only
                    }
                }
                catch (const std::nullptr_t /*ex*/)
                {
                    //"ERROR: Processing WM_DEVICECHANGE failed
                }
            }
            break;

        default:
            //"ERROR : Unknown dwControl: dwControl)
            break;
    }

    return NO_ERROR;
}

void WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    hStatus = RegisterServiceCtrlHandlerEx(SERVICE_NAME, &ControlHandler, 0);
    if (!hStatus)
    {
        // Error
        return;
    }

    ZeroMemory(&Status, sizeof(Status));
    Status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    Status.dwCurrentState = SERVICE_START_PENDING;
    SetServiceStatus(hStatus, &Status);

    // Initialization

    hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!hStopEvent)
    {
        // Error
        Status.dwCurrentState = SERVICE_STOPPED;
        Status.dwWin32ExitCode = GetLastError();
        SetServiceStatus(hStatus, &Status);
        return;
    }

    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
    ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;

    HDEVNOTIFY hDeviceNotify = RegisterDeviceNotification((HANDLE)hStatus, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
    if (!hDeviceNotify)
    {
        // Error
        Status.dwCurrentState = SERVICE_STOPPED;
        Status.dwWin32ExitCode = GetLastError();
        SetServiceStatus(hStatus, &Status);
        return;
    }

    Status.dwCurrentState = SERVICE_RUNNING;
    Status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
    SetServiceStatus(hStatus, &Status);

    WaitForSingleObject(hStopEvent, INFINITE);

    Status.dwCurrentState = SERVICE_STOP_PENDING;
    Status.dwControlsAccepted = 0;
    SetServiceStatus(hStatus, &Status);

    UnregisterDeviceNotification(hDeviceNotify);
    CloseHandle(hStopEvent);

    Status.dwCurrentState = SERVICE_STOPPED;
    SetServiceStatus(hStatus, &Status);
}

int main()
{
    SERVICE_TABLE_ENTRY ServiceTable[2];
    ServiceTable[0].lpServiceName = SERVICE_NAME;
    ServiceTable[0].lpServiceProc = &ServiceMain;
    ServiceTable[1].lpServiceName = NULL;
    ServiceTable[1].lpServiceProc = NULL;

    StartServiceCtrlDispatcher(ServiceTable);

    return 0;
}
#定义服务名称文本(“USB设备监控服务”)
HANDLE hStopEvent=NULL;
服务状态句柄hStatus=NULL;
服务状态;
DWORD WINAPI控制处理程序(DWORD dwControl、DWORD dwEventType、,
LPVOID lpram,LPVOID lpContext)
{
开关(DWC控制)
{
案件服务(控制)(调查):
返回无错误;
案例服务控制站:
案例服务控制关闭:
SetEvent(hStopEvent);
返回无错误;
案例服务\控制\设备事件:
if((DBT_DEVICEARRIVAL==dwEventType)|(DBT_devicemovecomplete==dwEventType))
{
尝试
{
DEV_BROADCAST_HDR*头=重新解释广播(lParam);
if(DBT_DEVTYP_DEVICEINTERFACE==头->dbch_devicetype)
{
//仅解析内置USB设备
}
}
捕获(常数std::nullptr\u t/*ex*/)
{
//“错误:处理WM_DEVICECHANGE失败
}
}
打破
违约:
//“错误:未知的dwControl:dwControl)
打破
}
返回无错误;
}
void WINAPI ServiceMain(DWORD dwArgc,LPTSTR*lpszArgv)
{
hStatus=RegisterServiceCtrlHandlerEx(服务名称和控制处理器,0);
如果(!hStatus)
{
//错误
返回;
}
Z
#define SERVICE_NAME    TEXT("USB Device Monitor Service")

HANDLE hStopEvent = NULL;
SERVICE_STATUS_HANDLE hStatus = NULL;

SERVICE_STATUS Status;

DWORD WINAPI ControlHandler(DWORD dwControl, DWORD dwEventType,
    LPVOID lParam, LPVOID lpContext)
{
    switch (dwControl)
    {
        case SERVICE_CONTROL_INTERROGATE:
            return NO_ERROR;

        case SERVICE_CONTROL_STOP:
        case SERVICE_CONTROL_SHUTDOWN:
            SetEvent(hStopEvent);
            return NO_ERROR;

        case SERVICE_CONTROL_DEVICEEVENT:
            if ((DBT_DEVICEARRIVAL == dwEventType) || (DBT_DEVICEREMOVECOMPLETE == dwEventType))
            {
                try
                {
                    DEV_BROADCAST_HDR* header = reinterpret_cast<DEV_BROADCAST_HDR*>(lParam);
                    if (DBT_DEVTYP_DEVICEINTERFACE == header->dbch_devicetype)
                    {
                        //parse intrested USB device only
                    }
                }
                catch (const std::nullptr_t /*ex*/)
                {
                    //"ERROR: Processing WM_DEVICECHANGE failed
                }
            }
            break;

        default:
            //"ERROR : Unknown dwControl: dwControl)
            break;
    }

    return NO_ERROR;
}

void WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    hStatus = RegisterServiceCtrlHandlerEx(SERVICE_NAME, &ControlHandler, 0);
    if (!hStatus)
    {
        // Error
        return;
    }

    ZeroMemory(&Status, sizeof(Status));
    Status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    Status.dwCurrentState = SERVICE_START_PENDING;
    SetServiceStatus(hStatus, &Status);

    // Initialization

    hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!hStopEvent)
    {
        // Error
        Status.dwCurrentState = SERVICE_STOPPED;
        Status.dwWin32ExitCode = GetLastError();
        SetServiceStatus(hStatus, &Status);
        return;
    }

    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
    ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
    NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;

    HDEVNOTIFY hDeviceNotify = RegisterDeviceNotification((HANDLE)hStatus, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
    if (!hDeviceNotify)
    {
        // Error
        Status.dwCurrentState = SERVICE_STOPPED;
        Status.dwWin32ExitCode = GetLastError();
        SetServiceStatus(hStatus, &Status);
        return;
    }

    Status.dwCurrentState = SERVICE_RUNNING;
    Status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
    SetServiceStatus(hStatus, &Status);

    WaitForSingleObject(hStopEvent, INFINITE);

    Status.dwCurrentState = SERVICE_STOP_PENDING;
    Status.dwControlsAccepted = 0;
    SetServiceStatus(hStatus, &Status);

    UnregisterDeviceNotification(hDeviceNotify);
    CloseHandle(hStopEvent);

    Status.dwCurrentState = SERVICE_STOPPED;
    SetServiceStatus(hStatus, &Status);
}

int main()
{
    SERVICE_TABLE_ENTRY ServiceTable[2];
    ServiceTable[0].lpServiceName = SERVICE_NAME;
    ServiceTable[0].lpServiceProc = &ServiceMain;
    ServiceTable[1].lpServiceName = NULL;
    ServiceTable[1].lpServiceProc = NULL;

    StartServiceCtrlDispatcher(ServiceTable);

    return 0;
}