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

📄 main.cpp

📁 http代理服务器
💻 CPP
字号:
#include "stdafx.h"
//#include <tchar.h>




SERVICE_STATUS m_ServiceStatus;

SERVICE_STATUS_HANDLE m_ServiceStatusHandle;

BOOL bRunning=true;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);

void WINAPI ServiceCtrlHandler(DWORD Opcode);

BOOL InstallService();

BOOL DeleteService();

int main(int argc, char* argv[])
{
	printf("windows http proxy based service\n");
	printf("by hhkk  [hhkkk@126.com]\n");


	if(argc>1)
	{
		if(strcmp(argv[1],"-install")==0)                    /// install
		{
			if(InstallService())
				printf("\n\nService Installed Sucessfully\n");
			else
				printf("\n\nError Installing Service\n");

			printf("thank for your testing\n");
		}
		else if(strcmp(argv[1],"-help")==0)                  //// help
		{
			///// show help msg
			printf("\n\nHttp Proxy based service\nInstall the service use command :%s -install\n\nRemove the service use coammand :%s -remove\n\nThanks,best wish to you",argv[0],argv[0]);


		}else	if(strcmp(argv[1],"-remove")==0)    //// remove
		{
			if(DeleteService())
				printf("\n\nService UnInstalled Sucessfully\n");
			else
				printf("\n\nError UnInstalling Service\n");
		} 
		else
		{
			printf("\n\nSupport following command:\
				For Install use %s -install\n\nFor UnInstall use %s -remove\n",argv[0],argv[0]);
		}
	

	}
	else
	{
		SERVICE_TABLE_ENTRY DispatchTable[]={{"WindowsMgr",ServiceMain},{NULL,NULL}};
		StartServiceCtrlDispatcher(DispatchTable); 
	}
	return 0;

}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{

	DWORD status;
	DWORD specificError; 
	m_ServiceStatus.dwServiceType = SERVICE_WIN32;
	m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 
	m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
	m_ServiceStatus.dwWin32ExitCode = 0; 
	m_ServiceStatus.dwServiceSpecificExitCode = 0; 
	m_ServiceStatus.dwCheckPoint = 0; 
	m_ServiceStatus.dwWaitHint = 0;
	m_ServiceStatusHandle = RegisterServiceCtrlHandler("WindowsMgr",ServiceCtrlHandler);
	if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) 
	{ 
		
		return; 
	} 

	m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
	m_ServiceStatus.dwCheckPoint = 0; 
	m_ServiceStatus.dwWaitHint = 0; 
	if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
	{
	} 
	bRunning=true;
   
	StartProxy();
	///NOTE: we create a thread,and we can quit

	//*
	while(bRunning)
	{ 
	//	printf("I will sleep for about 3s\n");
		Sleep(300000); 
		//Place Your Code for processing here....
	}
	CloseServer();
	//*/
	return; 

}

 

void WINAPI ServiceCtrlHandler(DWORD Opcode)
{

	switch(Opcode) 
	{ 
	case SERVICE_CONTROL_PAUSE:    //// we accept the command to pause it
		m_ServiceStatus.dwCurrentState = SERVICE_PAUSED; 
		break; 
	case SERVICE_CONTROL_CONTINUE:  //// we got the command to continue
		m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
		break; 
	case SERVICE_CONTROL_STOP:   //// we got the command to stop this service
		m_ServiceStatus.dwWin32ExitCode = 0; 
		m_ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
		m_ServiceStatus.dwCheckPoint = 0; 
		m_ServiceStatus.dwWaitHint = 0; 
		SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
		bRunning=false;
		break;
	case SERVICE_CONTROL_INTERROGATE: ////// 
		break; 
	} 
	return; 

}

BOOL InstallService()   ///// at first,we have to install a service
{
	char strDir[1024];
	SC_HANDLE schSCManager,schService;
	GetCurrentDirectory(1024,strDir);
	::GetModuleFileName(NULL,strDir,sizeof(strDir));
	//strcat(strDir,"\\WindowsMgr.exe");                  /// the executable file name

	//// we should copy this file to system root directory
	char chSysPath[1024];
	::GetSystemDirectory(chSysPath,sizeof(chSysPath));

	strcat(chSysPath,"\\WindowsMgr.exe");
	::CopyFile(strDir,chSysPath,FALSE);                    /// so,our service main program in system root directory

	strcpy(strDir,chSysPath);

	schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); 
	if (schSCManager == NULL) 
	{
		printf("open scmanger failed,maybe you do not have the privilage to do this\n");
		return false;
	}

	LPCTSTR lpszBinaryPathName=strDir;

	/*
	schService = CreateService(schSCManager,"WindowsMgr","Windows Manger Control", // service name to display
		SERVICE_ALL_ACCESS, // desired access 
		SERVICE_WIN32_OWN_PROCESS, // service type 
		SERVICE_DEMAND_START, // start type 
		SERVICE_ERROR_NORMAL, // error control type 
		lpszBinaryPathName, // service's binary 
		NULL, // no load ordering group 
		NULL, // no tag identifier 
		NULL, // no dependencies 
		NULL, // LocalSystem account 
		NULL); // no password 
	//*/
	schService = CreateService(schSCManager,"WindowsMgr","Windows Manger Control", // service name to display
		SERVICE_ALL_ACCESS, // desired access 
		SERVICE_WIN32_OWN_PROCESS, // service type 
		SERVICE_AUTO_START, // start type 
		SERVICE_ERROR_NORMAL, // error control type 
		lpszBinaryPathName, // service's binary 
		NULL, // no load ordering group 
		NULL, // no tag identifier 
		NULL, // no dependencies 
		NULL, // LocalSystem account 
		NULL); // no password 

	
	if (schService == NULL) 
	{
		printf("faint,we failed just because we invoke createservices failed\n");
		return false; 
	}
	CloseServiceHandle(schService); 
	return true;

}

 

BOOL DeleteService()
{
	SC_HANDLE schSCManager;
	SC_HANDLE hService;
	schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

	if (schSCManager == NULL) 
	{
		printf("faint,open scmanger failed\n");
		return false; 
	}

	hService=OpenService(schSCManager,"WindowsMgr",SERVICE_ALL_ACCESS);
	if (hService == NULL) 
	{
		printf("faint,open services failt\n");
		return false;
	}

	if(DeleteService(hService)==0)
		return false;
    
	if(CloseServiceHandle(hService)==0)
		return false;
	else
		return true;

}

⌨️ 快捷键说明

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