我现在的服务什么也做不了,在什么地方加上实现服务功能的代码段呢?

MFCClass 2004-01-07 12:24:14
same
...全文
41 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanw0212 2004-01-09
  • 打赏
  • 举报
回复


// 接受service的控制命令
VOID WINAPI ServiceCtrlHandler(DWORD dwControl)
{
switch(dwControl)
{
case SERVICE_CONTROL_STOP:
ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;

m_processAlarm.release();
m_filemappingprocee.release();
handler->release();
m_sockImpl.Del();

ulRet = CopyRightSafe_Logout(ulDogHandle);
if ( ulRet != 0 )
{
MessageBox(NULL,"加密狗异常退出!","服务提示",0 | 0x00200000L);
AddLogtoQ("加密狗异常退出");
}
delete []comkey;
comkey = NULL;
delete []inikey;
inikey = NULL;
delete []basickey;
basickey = NULL;

AddLogtoQ("成功关闭故障报警服务");
m_WriteLog.release();
g_isRunning = false;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;

break;

default:
break;
} //end of switch()

// Send current status.
if ( !SetServiceStatus(ServiceStatusHandle, &ServiceStatus) )
{
}
return;
}
yanw0212 2004-01-09
  • 打赏
  • 举报
回复
// Second function to implement
VOID WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
{
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS ;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING; //server处于正在启动状态
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP /*| SERVICE_ACCEPT_PAUSE_CONTINUE*/ ,
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;

// 在ServiceMain()中应该立即调用RegisterServiceCtrlHandler()注册一个Handler去处理控
ServiceStatusHandle = RegisterServiceCtrlHandler( "ExceptionService", ServiceCtrlHandler );

// 判断注册信息
if ( ServiceStatusHandle == (SERVICE_STATUS_HANDLE) 0 ) //(SERVICE_STATUS_HANDLE) 0 :类型转换
{
if (!SetServiceStatus(ServiceStatusHandle, &ServiceStatus))
{
sprintf(g_Msg,"IHISSERVICE_ANFANG SetServiceStatus error %ld\n", GetLastError() );
WriteInLogFile( g_Msg );
}
return;
}
else
{
if( !m_WriteLog.create() ) // 创建写日志的守护线程
{
MessageBox(NULL,"创建日至线程失败,服务不能运行","安防服务提示",0 | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
return;
}

Sleep(2000);

ulDogHandle = 0;
ulRet = CopyRightSafe_Login(ulDogHandle);
if ( ulRet == 0 )
{
ulRet = CopyRightSafe_GetLicenseEnable(ulDogHandle, LicenseNumber, MaxLicenseNumber, AddLicenseNumber, MaxAddLicenseNumber);
if ( ulRet == 1 )
{
ulRet = CopyRightSafe_DownLicenseNumber(ulDogHandle);
if ( ulRet == 0 )
{
}
else
{
MessageBox(NULL,"读取加密狗工作次数失败,服务不能运行","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("读取加密狗工作次数失败,服务不能运行");
return;
}

ZeroMemory(inikey, 8);
ulRet = CopyRightSafe_GetServiceINIKey(ulDogHandle, inikey);
if ( ulRet == 0 )
{
}
else
{
MessageBox(NULL,"读取加密狗密钥1失败,服务不能运行","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("读取加密狗密钥1失败,服务不能运行");
return;
}

ZeroMemory(basickey, 8);
ulRet = CopyRightSafe_GetBasicKey(ulDogHandle, basickey);
if ( ulRet == 0 )
{

}
else
{
MessageBox(NULL,"读取加密狗密钥2失败,服务不能运行","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("读取加密狗密钥2失败,服务不能运行");

return;
}

ZeroMemory(comkey, 8);
ulRet = CopyRightSafe_GetInitTempKey(ulDogHandle, comkey);
if ( ulRet == 0 )
{

}
else
{
MessageBox(NULL,"读取加密狗密钥3失败,服务不能运行","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("读取加密狗密钥2失败,服务不能运行");

return;
}
}
else
{
MessageBox(NULL,"读取加密狗失败,软件不能运行!","故障服务提示",MB_OK | 0x00200000L );
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("读取加密狗失败,软件不能运行");
return;
}
}
else
{
MessageBox(NULL,"没有找到加密狗,软件不能运行!","ExceptionService提示信息",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("没有找到加密狗,软件不能运行");
return;
}
}

GetINIData();
if (servername.IsEmpty() || dbname.IsEmpty() || administratorname.IsEmpty() || loginpwd.IsEmpty() || NoQued==0||PortNum==0 || administratropwd=="AdministratorPWD Error" ||loginname.IsEmpty() )
{
MessageBox(NULL,"读取系统配置文件错误,软件不能运行!","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("读取系统配置文件错误,软件不能运行!");

return;
}

CString str = "Provider=SQLOLEDB.1;Password="+ loginpwd+";Persist Security Info=True;User ID=" + loginname + ";Initial Catalog=" + dbname + ";Data Source=" + servername ;
strConnection = str;

// socket部分
WSADATA wsadata;
int wsaret=WSAStartup(0x202,&wsadata);
if(wsaret)
{
MessageBox(NULL,"初始化SOCKET错误,软件不能运行!","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("初始化SOCKET错误,软件不能运行!");

return;
}

ServerSocket server;
server.setnoQued (NoQued);
if ( !server.open(PortNum) )
{
MessageBox(NULL,"初始化服务器IP地址错误,软件不能运行!","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("初始化服务器IP地址错误,软件不能运行!");
return;
}

createSocket = NULL;
handler = NULL;
createSocket = new CreateSocketHandlerImpl();
handler = new ServerSocketHandler;
handler->setServerSocket(server);
handler->setCreateSocketHandler((CreateSocketHandler *)createSocket);
if(handler->create())
{

}
else
{
MessageBox(NULL,"创建于客户端的通讯器失败,软件不能运行","故障服务提示",MB_OK | 0x00200000L);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
AddLogtoQ("创建于客户端的通讯器失败,软件不能运行");
return ;
}

// 第三部分:创建守护线程
if( !m_processAlarm.create() ) // 创建处理报警信息的线程
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
string str = "创建守护线程失败";
AddLogtoQ(str);
return;
}

if(!m_filemappingprocee.create()) // 创建文件映射守护线程
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
string str = "创建文件映射线程失败";
AddLogtoQ(str);
return;
}

ServiceStatus.dwCurrentState = SERVICE_RUNNING;
if ( !SetServiceStatus( ServiceStatusHandle, &ServiceStatus ) )
{
// 注册系统当前的状态,处理错误情况
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
return;
}
AddLogtoQ( "成功启动故障报警服务");
g_isRunning = true;

// 创建任务栏图标的接收窗口
AddTrayIconWindows();

while (1)
{
MSG msg;
BOOL bRet;
if ( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Sleep(100);

if ( !g_isRunning )
{
break;
}
}
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

return;
}


MFCClass 2004-01-08
  • 打赏
  • 举报
回复
帮帮我这个孤独的人

2,643

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧