将websdk改造成windows服务

This commit is contained in:
luoliangyi 2024-04-19 11:40:33 +08:00
parent c7f5172454
commit e5712a4eeb
4 changed files with 113 additions and 105 deletions

View File

@ -4,6 +4,7 @@ TEMPLATE = app
DEFINES += UNTITLED_LIBRARY
CONFIG += c++11
CONFIG += console
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings

View File

@ -1,11 +1,6 @@
#include "WSServer.h"
#include "WSUser.h"
#include "base/HGInfo.h"
#if defined(HG_CMP_MSC)
#include <shellapi.h>
extern ULONG taskbarMsgId;
extern NOTIFYICONDATAA nid;
#endif
namespace ver_2
{
@ -44,10 +39,6 @@ namespace ver_2
user->Open();
++m_currUserId;
m_vectorUser.push_back(user);
#if defined(HG_CMP_MSC)
sprintf(nid.szTip, "扫描服务程序(连接数 %u", m_vectorUser.size());
Shell_NotifyIconA(NIM_MODIFY, &nid);
#endif
}
void WSServer::Command(const WSCmdParam* param)

View File

@ -1,11 +1,6 @@
#include "WebServer.h"
#include "WebUser.h"
#include "base/HGInfo.h"
#if defined(HG_CMP_MSC)
#include <shellapi.h>
extern ULONG taskbarMsgId;
extern NOTIFYICONDATAA nid;
#endif
WebServer::WebServer(HGMsgPump msgPump)
{
@ -139,11 +134,6 @@ void WebServer::DisConnect(const DisConnectParam* param)
delete pUser;
pUser = NULL;
}
#if defined(HG_CMP_MSC)
sprintf(nid.szTip, "扫描服务程序(连接数 %u", m_vectorUser.size());
Shell_NotifyIconA(NIM_MODIFY, &nid);
#endif
}
#if defined(HG_CMP_MSC)

View File

@ -13,6 +13,9 @@
#include "MsgPumpCallback.h"
#include "curl/curl.h"
HGMsgPump msgPumpV1 = NULL;
HGMsgPump msgPumpV2 = NULL;
static void HGAPI CrashFunc(HGPointer crashAddr, HGPointer param)
{
HGChar logPath[256];
@ -69,102 +72,83 @@ static void HGAPI ThreadFuncV2(HGThread thread, HGPointer param)
}
}
#if defined(HG_CMP_MSC)
#include <shellapi.h>
#include "resource.h"
#define WM_TRAY (WM_USER + 100)
ULONG taskbarMsgId = 0;
NOTIFYICONDATAA nid;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (WM_DESTROY == uMsg)
{
Shell_NotifyIconA(NIM_DELETE, &nid);
PostQuitMessage(0);
return 0;
}
else if (uMsg == taskbarMsgId)
{
//系统Explorer崩溃重启时重新加载托盘
Shell_NotifyIconA(NIM_ADD, &nid);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
#endif
#if defined(HG_CMP_MSC)
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE iPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
#if defined(OEM_HANWANG)
#define SERVICE_NAME TEXT("HWWebService")
#elif defined(OEM_LISICHENG)
#define SERVICE_NAME TEXT("LSCWebService")
#elif defined(OEM_CANGTIAN)
#define SERVICE_NAME TEXT("CTSWebService")
#elif defined(OEM_ZHONGJING)
#define SERVICE_NAME TEXT("ZJWebService")
#elif defined(OEM_ZIGUANG)
#define SERVICE_NAME TEXT("ZGWebService")
#elif defined(OEM_NEUTRAL)
#define SERVICE_NAME TEXT("NEUWebService")
#elif defined(OEM_DELI)
#define SERVICE_NAME TEXT("DLWebService")
#else
int main()
#define SERVICE_NAME TEXT("HGWebService")
#endif
SERVICE_STATUS_HANDLE ServiceStatusHandle = NULL;
SERVICE_STATUS ServiceStatus = {0};
void WINAPI ServiceCtrlHandler(DWORD dwControl)
{
HGBase_RegisterCrashFunc(CrashFunc, NULL);
switch (dwControl)
{
case SERVICE_CONTROL_STOP: // 暂停控制
ServiceStatus.dwCurrentState = SERVICE_STOPPED;// 状态设置为停止
HGBase_ExitMsgPump(msgPumpV1);
HGBase_ExitMsgPump(msgPumpV2);
break;
case SERVICE_CONTROL_SHUTDOWN:// 关机控制
ServiceStatus.dwCurrentState = SERVICE_STOPPED;// 状态设置为停止
HGBase_ExitMsgPump(msgPumpV1);
HGBase_ExitMsgPump(msgPumpV2);
break;
default:
break;
}
#if defined(HG_CMP_MSC)
HANDLE hMutex = CreateMutexW(NULL, FALSE, L"{1A67EFC9-B41C-4CE6-B95A-A16958E7010F}");
assert(NULL != hMutex);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBoxA(NULL, "扫描服务已打开", "提示", 0);
CloseHandle(hMutex);
return -1;
}
// 设置服务状态
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
}
WSADATA ws = { 0 };
int ret = WSAStartup(MAKEWORD(2, 2), &ws);
assert(0 == ret);
#endif
void WINAPI ServiceMain(DWORD dwNumServicesArgs, LPTSTR* lpServiceArgVectors)
{
ServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
if (NULL == ServiceStatusHandle)
{
return;
}
curl_global_init(CURL_GLOBAL_ALL);
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING; // 服务正在启动
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP; // 接受关机与暂停两种控制
ServiceStatus.dwWin32ExitCode = 0; // 下面四个默认都为0
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
WSADATA ws = { 0 };
int ret = WSAStartup(MAKEWORD(2, 2), &ws);
assert(0 == ret);
curl_global_init(CURL_GLOBAL_ALL);
HGMsgPump msgPumpV1 = NULL;
HGBase_CreateMsgPump(&msgPumpV1);
HGThread threadV1 = NULL;
HGBase_OpenThread(ThreadFuncV1, msgPumpV1, &threadV1);
HGMsgPump msgPumpV2 = NULL;
HGBase_CreateMsgPump(&msgPumpV2);
HGThread threadV2 = NULL;
HGBase_OpenThread(ThreadFuncV2, msgPumpV2, &threadV2);
#if defined(HG_CMP_MSC)
WNDCLASSA wc = { 0 };
wc.style = NULL;
wc.hIcon = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "HGWebServie";
wc.hCursor = NULL;
RegisterClassA(&wc);
HWND hWnd = CreateWindowExA(WS_EX_TOOLWINDOW, "HGWebServie", "HGWebServie", WS_POPUP, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
taskbarMsgId = RegisterWindowMessageA("TaskbarCreated");
nid.cbSize = sizeof(NOTIFYICONDATAA);
nid.hWnd = hWnd;
nid.uID = IDI_ICON_HGWEBSERVICE;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_INFO;
nid.uCallbackMessage = WM_TRAY;
nid.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_HGWEBSERVICE));
lstrcpyA(nid.szTip, "扫描服务程序(连接数 0");
Shell_NotifyIconA(NIM_ADD, &nid);
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#endif
ServiceStatus.dwCurrentState = SERVICE_RUNNING; // 成功运行则把状态设置为运行状态
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
HGBase_CloseThread(threadV2);
threadV2 = NULL;
@ -175,11 +159,53 @@ int main()
HGBase_DestroyMsgPump(msgPumpV1);
msgPumpV1 = NULL;
curl_global_cleanup();
#if defined(HG_CMP_MSC)
WSACleanup();
CloseHandle(hMutex);
#endif
return 0;
curl_global_cleanup();
WSACleanup();
}
int main()
{
HGBase_RegisterCrashFunc(CrashFunc, NULL);
TCHAR serviceName[128];
lstrcpy(serviceName, SERVICE_NAME);
SERVICE_TABLE_ENTRY tableEntry[2];
tableEntry[0].lpServiceName = serviceName; // 服务程序名称
tableEntry[0].lpServiceProc = ServiceMain; // 服务程序入口
tableEntry[1].lpServiceName = NULL;
tableEntry[1].lpServiceProc = NULL;
StartServiceCtrlDispatcher(tableEntry);
return 0;
}
#else
int main()
{
HGBase_RegisterCrashFunc(CrashFunc, NULL);
curl_global_init(CURL_GLOBAL_ALL);
HGBase_CreateMsgPump(&msgPumpV1);
HGThread threadV1 = NULL;
HGBase_OpenThread(ThreadFuncV1, msgPumpV1, &threadV1);
HGBase_CreateMsgPump(&msgPumpV2);
HGThread threadV2 = NULL;
HGBase_OpenThread(ThreadFuncV2, msgPumpV2, &threadV2);
HGBase_CloseThread(threadV2);
threadV2 = NULL;
HGBase_DestroyMsgPump(msgPumpV2);
msgPumpV2 = NULL;
HGBase_CloseThread(threadV1);
threadV1 = NULL;
HGBase_DestroyMsgPump(msgPumpV1);
msgPumpV1 = NULL;
curl_global_cleanup();
return 0;
}
#endif