⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myservice.cpp

📁 本程序是实现中国移动中国联通的网关程序.代码比较完整.
💻 CPP
字号:
// myservice.cpp

#include "NTServApp.h"
#include "myservice.h"

CMyService::CMyService()
:CNTService("")
{
	m_iStartParam = 0;
	m_iIncParam = 1;
	m_iState = m_iStartParam;
	strcpy(m_szName,"");
}
CMyService::CMyService(char* szName)
:CNTService(szName)
{
	m_iStartParam = 0;
	m_iIncParam = 1;
	m_iState = m_iStartParam;
	strcpy(m_szName,szName);
}

BOOL CMyService::OnInit()
{
	// Read the registry parameters
    // Try opening the registry key:
    // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<AppName>\Parameters
    HKEY hkey;
	char szKey[1024];
	strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\");
	strcat(szKey, m_szServiceName);
	strcat(szKey, "\\Parameters");
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                     szKey,
                     0,
                     KEY_QUERY_VALUE,
                     &hkey) == ERROR_SUCCESS) {
        // Yes we are installed
        DWORD dwType = 0;
        DWORD dwSize = sizeof(m_iStartParam);
        RegQueryValueEx(hkey,
                        "Start",
                        NULL,
                        &dwType,
                        (BYTE*)&m_iStartParam,
                        &dwSize);
        dwSize = sizeof(m_iIncParam);
        RegQueryValueEx(hkey,
                        "Inc",
                        NULL,
                        &dwType,
                        (BYTE*)&m_iIncParam,
                        &dwSize);
        RegCloseKey(hkey);
    }

	// Set the initial state
	m_iState = m_iStartParam;

	return TRUE;
}

void CMyService::Run()
{
	char tchar[_MAX_PATH];
	HINSTANCE hinstLib;
	DWORD ret;
	HANDLE h[2];
    while (m_bIsRunning) 
	{
		GetModuleFileName(NULL,tchar,MAX_PATH);
		sprintf(strrchr(tchar,'\\')+1,"gw.dll\0");
		if((hinstLib = LoadLibrary(tchar))==NULL)
		{
			FreeLibrary(hinstLib);
			AddLog("Can not load the gw.dll.\n");
			SleepEx(1000,1);
			goto cleanup;
		} 

		if((StopGW=(void (_stdcall *)())GetProcAddress(hinstLib,
			"StopGW"))==NULL)
		{ 
			AddLog(TEXT("Load DLL FUNCTION StopGW Error!!\n")); 
			goto cleanup;
		} 
		if((BeginGW=(DWORD (_stdcall *)(LPVOID))GetProcAddress(hinstLib,
			"BeginGW"))==NULL)
		{ 
			AddLog(TEXT("Load DLL FUNCTION BeginGW Error!!\n")); 
			goto cleanup;
		} 
		// create the event object. The control handler function signals
		// this event when it receives the "stop" control code.
		//
		hServerStopEvent = CreateEvent(
			NULL,    // no security attributes
			TRUE,    // manual reset event
			FALSE,   // not-signalled
			NULL);   // no name

		if ( hServerStopEvent == NULL)
			goto cleanup;
		h[0]=hServerStopEvent;

		h[1]=CreateThread(NULL,0,BeginGW,NULL,0,0);
		// Service is now running, perform work until shutdown
		//
		AddLog("服务程序%s开始运行.\n",m_szName);
		while (1)
		{
			ret=WaitForMultipleObjects(2,h,FALSE,20*1000);
			switch(ret)
			{
				case WAIT_FAILED:
				case WAIT_TIMEOUT:
#if 1
					GetExitCodeThread(h[1],&ret);
					if(ret!=STILL_ACTIVE)
					{
						StopGW();
						SleepEx(200,1);
						FreeLibrary(hinstLib);
						CloseHandle(h[1]);
						AddLog("服务线程退出: %d,重新启动服务线程.\n",ret);
						goto cleanup;
					}
#endif
					break;
				case WAIT_OBJECT_0:
					AddLog("得到消息,准备退出服务程序%s.\n",m_szName);
					StopGW();
					SleepEx(100,1);
					FreeLibrary(hinstLib);
					goto cleanup;
				case WAIT_OBJECT_0+1:
					GetExitCodeThread(h[1],&ret);
					AddLog("main thread return code: %d\n",ret);
					CloseHandle(h[1]);
					if(ret)
					{
						AddLog("服务异常线程退出(%d),重新启动服务线程.\n",ret);
						h[1]=CreateThread(NULL,0,BeginGW,NULL,0,0);
					}else
					{
						AddLog("服务线程正常退出,退出进程.\n");
						FreeLibrary(hinstLib);
						goto cleanup;
					}
					break;
			}
		}
	}

cleanup:
	AddLog("退出服务程序%s.\n",m_szName);
	//m_iState += m_iIncParam;
	SetStatus(SERVICE_STOPPED);
}

// Process user control requests
BOOL CMyService::OnUserControl(DWORD dwOpcode)
{
    switch (dwOpcode) {
    case SERVICE_CONTROL_USER + 0:

        // Save the current status in the registry
        SaveStatus();
        return TRUE;

    default:
        break;
    }
    return FALSE; // say not handled
}

// Save the current status in the registry
void CMyService::SaveStatus()
{
    DebugMsg("Saving current status");
    // Try opening the registry key:
    // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<AppName>\...
    HKEY hkey = NULL;
	char szKey[1024];
	strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\");
	strcat(szKey, m_szServiceName);
	strcat(szKey, "\\Status");
    DWORD dwDisp;
	DWORD dwErr;
    DebugMsg("Creating key: %s", szKey);
    dwErr = RegCreateKeyEx(	HKEY_LOCAL_MACHINE,
                           	szKey,
                   			0,
                   			"",
                   			REG_OPTION_NON_VOLATILE,
                   			KEY_WRITE,
                   			NULL,
                   			&hkey,
                   			&dwDisp);
	if (dwErr != ERROR_SUCCESS) {
		DebugMsg("Failed to create Status key (%lu)", dwErr);
		return;
	}	

    // Set the registry values
	DebugMsg("Saving 'Current' as %ld", m_iState); 
    RegSetValueEx(hkey,
                  "Current",
                  0,
                  REG_DWORD,
                  (BYTE*)&m_iState,
                  sizeof(m_iState));


    // Finished with key
    RegCloseKey(hkey);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -