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

📄 imageservice.cpp

📁 图象处理
💻 CPP
字号:
/* A skeleton for writing an NT/2K service */
/* Author :- Nishant S */
/* EMail :- nish@inapp.com */

#include <windows.h>
#include <winsvc.h>
#include <iostream>
#include "TCPListener.h"
#include "UDPHandler.h"
#include "DirWatcher.h"

CTCPListener m_TCPListener;
CUDPHandler m_UDPHandler;

void ServiceMain(DWORD argc, LPTSTR *argv); 
void ServiceCtrlHandler(DWORD nControlCode);
BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
					 DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,
					 DWORD dwWaitHint);
BOOL StartServiceThread();
DWORD ServiceExecutionThread(LPDWORD param);
HANDLE hServiceThread;
void KillService();

TCHAR *strServiceName = "ImageService";
SERVICE_STATUS_HANDLE nServiceStatusHandle; 
HANDLE killServiceEvent;
BOOL nServiceRunning;
DWORD nServiceCurrentStatus;

void StopService() 
{
	SC_HANDLE schService, schSCManager;
    SERVICE_STATUS ssStatus; 
    DWORD dwOldCheckPoint; 
    DWORD dwStartTickCount;
    DWORD dwWaitTime;
 
    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

	if (schSCManager == NULL) return;

	schService = OpenService( 
        schSCManager,          // SCM database 
        "ImageServer",          // service name
        SERVICE_ALL_ACCESS); 
 
    if (schService == NULL)
	{
		CloseServiceHandle(schSCManager);
		return;
	}
 
	if (!QueryServiceStatus(schService, &ssStatus))
	{
		CloseServiceHandle(schSCManager);
		return;
	}

	if (ssStatus.dwCurrentState == SERVICE_STOPPED)
	{
		return;
	}

    if (!ControlService(
            schService,  // handle to service 
            SERVICE_CONTROL_STOP,           // number of arguments 
            &ssStatus) )      // no arguments 
	{
		CloseServiceHandle(schService);
		CloseServiceHandle(schSCManager);
		return;
	}
 
    // Check the status until the service is no longer start pending. 
 
/*    if (!QueryServiceStatus( 
            schService,   // handle to service 
            &ssStatus) )  // address of status information structure
	{
		CloseServiceHandle(schService);
		CloseServiceHandle(schSCManager);
		return;
	}*/
 
    // Save the tick count and initial checkpoint.

    dwStartTickCount = GetTickCount();
    dwOldCheckPoint = ssStatus.dwCheckPoint;

    while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) 
    { 
        // Do not wait longer than the wait hint. A good interval is 
        // one tenth the wait hint, but no less than 1 second and no 
        // more than 10 seconds. 
 
        dwWaitTime = ssStatus.dwWaitHint / 10;

        if( dwWaitTime < 1000 )
            dwWaitTime = 1000;
        else if ( dwWaitTime > 10000 )
            dwWaitTime = 10000;

        Sleep( dwWaitTime );

        // Check the status again. 
 
        if (!QueryServiceStatus( 
                schService,   // handle to service 
                &ssStatus) )  // address of structure
            break; 
 
        if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
        {
            // The service is making progress.

            dwStartTickCount = GetTickCount();
            dwOldCheckPoint = ssStatus.dwCheckPoint;
        }
        else
        {
            if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
            {
                // No progress made within the wait hint
                break;
            }
        }
    } 

    CloseServiceHandle(schService); 
	CloseServiceHandle(schSCManager);
}


void DelService()
{
	StopService();

    SC_HANDLE schService;
	SC_HANDLE schSCManager;
	schSCManager=OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
	if(!schSCManager) return ;
	schService = OpenService( 
        schSCManager,       // SCManager database 
        "ImageServer",       // name of service 
        DELETE);            // only need DELETE access 
 
    if (schService == NULL) return; 
 
    DeleteService(schService);
 
    CloseServiceHandle(schService); 
	CloseServiceHandle(schSCManager);
}

void main(int argc, char* argv[])
{
	if (argc == 2)
	{
		if (!strcmp(argv[1], "DeleteService"))
		{
			DelService();
			return;
		}
	}
	
	SERVICE_TABLE_ENTRY servicetable[]=
	{
		{strServiceName,(LPSERVICE_MAIN_FUNCTION)ServiceMain},
		{NULL,NULL}
	};
	BOOL success;
	success=StartServiceCtrlDispatcher(servicetable);
	if(!success)
	{
		//error occured
	}
}

void ServiceMain(DWORD argc, LPTSTR *argv)
{
	BOOL success;
	nServiceStatusHandle=RegisterServiceCtrlHandler(strServiceName,
		(LPHANDLER_FUNCTION)ServiceCtrlHandler);
	if(!nServiceStatusHandle)
	{
		return;
	}
	success=UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,1,3000);
	if(!success)
	{
		return;
	}
	killServiceEvent=CreateEvent(0,TRUE,FALSE,0);
	if(killServiceEvent==NULL)
	{
		return;
	}
	success=UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,2,1000);
	if(!success)
	{
		return;
	}
	success=StartServiceThread();
	if(!success)
	{
		return;
	}
	nServiceCurrentStatus=SERVICE_RUNNING;
	success=UpdateServiceStatus(SERVICE_RUNNING,NO_ERROR,0,0,0);
	if(!success)
	{
		return;
	}
	WaitForSingleObject(killServiceEvent,INFINITE);
	CloseHandle(killServiceEvent);
}

BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
					 DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,
					 DWORD dwWaitHint)
{
	BOOL success;
	SERVICE_STATUS nServiceStatus;
	nServiceStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
	nServiceStatus.dwCurrentState=dwCurrentState;
	if(dwCurrentState==SERVICE_START_PENDING)
	{
		nServiceStatus.dwControlsAccepted=0;
	}
	else
	{
		nServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP			
			|SERVICE_ACCEPT_SHUTDOWN;
	}
	if(dwServiceSpecificExitCode==0)
	{
		nServiceStatus.dwWin32ExitCode=dwWin32ExitCode;
	}
	else
	{
		nServiceStatus.dwWin32ExitCode=ERROR_SERVICE_SPECIFIC_ERROR;
	}
	nServiceStatus.dwServiceSpecificExitCode=dwServiceSpecificExitCode;
	nServiceStatus.dwCheckPoint=dwCheckPoint;
	nServiceStatus.dwWaitHint=dwWaitHint;

	success=SetServiceStatus(nServiceStatusHandle,&nServiceStatus);

	if(!success)
	{
		KillService();
		return success;
	}
	else
		return success;
}

BOOL StartServiceThread()
{	
	DWORD id;
	hServiceThread=CreateThread(0,0,
		(LPTHREAD_START_ROUTINE)ServiceExecutionThread,
		0,0,&id);
	if(hServiceThread==0)
	{
		return false;
	}
	else
	{
		nServiceRunning=true;
		return true;
	}
}

DWORD ServiceExecutionThread(LPDWORD param)
{
	WSADATA wsaD;
	DWORD dwVersion = MAKEWORD(2,2);
	HKEY hkey;
	DWORD disposition, length,type;
	WCHAR szPath[MAX_PATH]; 

	RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\ImageServer", NULL, 
		NULL, KEY_QUERY_VALUE, 0, NULL, &hkey, &disposition);

	if (disposition == REG_CREATED_NEW_KEY)
	{
		RegSetValueExW(hkey, L"WorkPath", 0, REG_SZ, (BYTE *)"", 1);
		szPath[0] = 0;

		RegCloseKey(hkey);
	}
	else
	{
		RegCloseKey(hkey);

		RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\ImageServer", 0,
			KEY_QUERY_VALUE, &hkey);

		length = MAX_PATH * sizeof(WCHAR);
		if (RegQueryValueExW(hkey, L"WorkPath", 0, &type, (BYTE*)&szPath, &length) != ERROR_SUCCESS)
		{
			RegSetValueExW(hkey, L"WorkPath", 0, REG_SZ, (BYTE*)"", 1);
			szPath[0] = 0;
		}
		
		RegCloseKey(hkey);
	}

	WSAStartup( dwVersion, &wsaD );

	if (!m_TCPListener.Create(TCP_LISTEN_PORT))
		cout<<"Can't Create TCPListener!" <<endl;
	m_TCPListener.Start();

	DirWatcher dWatcher(szPath);
	dWatcher.WatchDirectories();
	do
	{
		Sleep(4000);
	} while (nServiceRunning);

	WSACleanup();
	return 0;
}

void KillService()
{
	nServiceRunning=false;
	SetEvent(killServiceEvent);
	UpdateServiceStatus(SERVICE_STOPPED,NO_ERROR,0,0,0);
}

void ServiceCtrlHandler(DWORD nControlCode)
{
	BOOL success;
	switch(nControlCode)
	{	
	case SERVICE_CONTROL_SHUTDOWN:
	case SERVICE_CONTROL_STOP:
		nServiceCurrentStatus=SERVICE_STOP_PENDING;
		success=UpdateServiceStatus(SERVICE_STOP_PENDING,NO_ERROR,0,1,3000);
		KillService();		
		return;
	default:
		break;
	}
	UpdateServiceStatus(nServiceCurrentStatus,NO_ERROR,0,0,0);
}

⌨️ 快捷键说明

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