有关写一个windows服务

hjl0508 2014-04-14 01:58:39
我想写一个windows 服务程序,在网上查的要用到*.mc 文件 编译这个文件要用到mc 命令。
可是我执行mc -U sample.mc 却告诉我没有这个命令,我该怎么搞啊,写服务不用这个可以吗?
有没有人比较精通写windows服务,求给我介绍一下!我对这个是零基础,一头雾水啊!
...全文
220 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjl0508 2014-04-15
  • 打赏
  • 举报
回复
引用 13 楼 hjl0508 的回复:
[quote=引用 12 楼 xihu1364 的回复:] [quote=引用 7 楼 hjl0508 的回复:] [quote=引用 2 楼 xihu1364 的回复:] 本来想贴下windows服务代码,楼上说等赵老湿代码,我还是不贴了
哥求代码,我现在对服务是一窍不通。。。。。最好有简单的介绍,谢谢了[/quote]


#define FRONT_START 0				// 服务允许启动前置
#define FRONT_STOP	1				// 服务停止前置
#define FRONT_PAUSE 2				// 服务暂停前置

int _tmain(int argc, _TCHAR* argv[])
{
	// 安装服务
	if ((argc == 2) && (::strcmp(argv[1] + 1, "install") == 0))
	{
		InstallService(ServiceName);
		return 0;
	}

	hEvents[0] = CreateEvent(NULL, FALSE, FALSE, "FRONTSTART");
	hEvents[1] = CreateEvent(NULL, FALSE, FALSE, "FRONTSTOP");
	hEvents[2] = CreateEvent(NULL, FALSE, FALSE, "FRONTPAUSE");
	hEvents[3] = (HANDLE)0;

	
	// 定义服务入口表
	SERVICE_TABLE_ENTRY DispatchTable[] = 
	{
		{ServiceName, ServiceMain},
		{NULL, NULL}
	};

	// 向SCM注册服务并进入循环,直到所有服务退出该函数才返回
	if(!StartServiceCtrlDispatcher(DispatchTable))
	{
		int nError = GetLastError();
	}

	return 0;

}

// 安装服务
void InstallService(const char * szServiceName)
{
	SC_HANDLE handle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if(handle == NULL)
	{
		DWORD dw = ::GetLastError();
		OutputErrorMsg(dw);
		return;
	}
	char szFilename[256];
	::GetModuleFileName(NULL, szFilename, 255);
	SC_HANDLE hService = ::CreateService(handle, szServiceName,
		szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
		SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, szFilename, NULL,
		NULL, NULL, NULL, NULL);
	if(hService)
	{
		::CloseServiceHandle(hService);
	}
	else
	{
		DWORD dw = ::GetLastError();
		OutputErrorMsg(dw);
	}
	::CloseServiceHandle(handle);
}

// 服务控制处理器,接受SCM的指令并通知服务线程
void WINAPI ServiceCtrlHandler(DWORD dwControl)
{
	//MessageBox(NULL, "ServiceCtrlHandler", NULL, MB_SERVICE_NOTIFICATION);
	switch (dwControl)
	{
	case SERVICE_CONTROL_STOP:
		servicestatus.dwCurrentState = SERVICE_STOP_PENDING;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[1]);
		break;
	case SERVICE_CONTROL_PAUSE:
		servicestatus.dwCurrentState = SERVICE_PAUSE_PENDING ;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[2]);
		break;
	case SERVICE_CONTROL_CONTINUE:
		servicestatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[0]);
		break;
	}

}

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
	servicestatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	servicestatus.dwCurrentState = SERVICE_START_PENDING;
	servicestatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
	servicestatus.dwWin32ExitCode = 0;
	servicestatus.dwServiceSpecificExitCode = 0;
	servicestatus.dwCheckPoint = 0;
	servicestatus.dwWaitHint = 0;

	servicestatushandle = ::RegisterServiceCtrlHandler(ServiceName, ServiceCtrlHandler);
	if (servicestatushandle == (SERVICE_STATUS_HANDLE)0)
	{
		return;
	}
	// 设置服务状态为已开始但还没有初始化完成
	::SetServiceStatus(servicestatushandle, &servicestatus);

	CoInitialize(0);

	char cPath0[MAX_PATH] = {0};
	GetModuleFileName(NULL, cPath0, MAX_PATH);
	char* pPos = strrchr(cPath0, '\\');
	*(pPos + 1) = 0;
	sprintf(g_cLogPath, "%slog", cPath0);


	*(pPos + 1) = 0;
	char cPath[MAX_PATH] = {0};
	strcat(cPath, "E:\\test.exe");

	STARTUPINFO si = { sizeof(si) };


	// 服务已经启动
	servicestatus.dwCurrentState = SERVICE_RUNNING;
	SetServiceStatus(servicestatushandle, &servicestatus);

	DWORD dwRet;
	int iFlag = FRONT_START;			// 服务当前的状态为允许启动

	while(TRUE)
	{
		dwRet = WaitForMultipleObjects(4, hEvents, FALSE, 8 * 1000);
		if((iFlag == FRONT_START || iFlag == FRONT_PAUSE) && (dwRet == WAIT_OBJECT_0 + 1))			// 要求停止前置服务的事件发生
		{
			TerminateProcess(g_ProcessInfo.hProcess, 0);
			CloseHandle(g_ProcessInfo.hProcess);
			servicestatus.dwCurrentState = SERVICE_STOPPED;
			SetServiceStatus(servicestatushandle, &servicestatus);
			iFlag = FRONT_STOP;
			return ;
		}
		if((iFlag == FRONT_STOP || iFlag == FRONT_PAUSE) && (dwRet == WAIT_OBJECT_0))				// 要求启动前置服务的事件发生
		{
			iFlag = FRONT_START;
			servicestatus.dwCurrentState = SERVICE_RUNNING;
			SetServiceStatus(servicestatushandle, &servicestatus);
			hEvents[3] = (HANDLE)-1;
		}

		if((iFlag == FRONT_START) && (dwRet == WAIT_OBJECT_0 + 2))			// 要求暂停前置服务的事件发生
		{
			TerminateProcess(g_ProcessInfo.hProcess, 0);
			CloseHandle(g_ProcessInfo.hProcess);
			servicestatus.dwCurrentState = SERVICE_PAUSED;
			SetServiceStatus(servicestatushandle, &servicestatus);
			iFlag = FRONT_PAUSE;
			hEvents[3] = hEvents[2];				// 避免因为前置进程句柄失效导致的Wait函数总是返回WAIT_FAILED而不能收到其它事件
		}

		if(iFlag == FRONT_START)										// 只在允许前置启动的情况下才去连接数据库和启动前置
		{
			if(dwRet == WAIT_OBJECT_0 + 3 || dwRet == WAIT_FAILED)		// 前置进程意外退出或者还未启动
			{
				if(dwRet == WAIT_OBJECT_0 + 3)
				{
					Log("前置意外退出");
				}

				// 记录进程退出日志

				memset(&si, 0, sizeof(STARTUPINFO));
				si.cb = sizeof(STARTUPINFO);
				BOOL ret = CreateProcess(cPath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &g_ProcessInfo);
				int nError = GetLastError();
				if(ret)
				{
					hEvents[3] = g_ProcessInfo.hProcess;
					// 记录进程重新启动日志
					CloseHandle(g_ProcessInfo.hThread);

				}
			}
			else if(dwRet == WAIT_TIMEOUT)
			{

			}
		}
	}

	return ;
}

把主要的几个函数贴出来了,你看看[/quote] 谢谢了,内容不少,我先研究研究![/quote] 这个直接建一个win32 控制台程序,复制到cpp文件里面就行了么?还是需要别的操作。。。。小白,见谅!
hjl0508 2014-04-15
  • 打赏
  • 举报
回复
引用 16 楼 xihu1364 的回复:
自己不去研究,那不是还是不知道。 “小白,见谅!” -----> “小白,工资能给我拿吗?”
额。。。我这不是一边研究一边问么。。。而且现在研究的基本搞定了。。。服务已经能运行了。。。这样问不是有点着急么。。。 不要这样打击我,脸皮薄。。。。
版主大哥 2014-04-15
  • 打赏
  • 举报
回复
自己不去研究,那不是还是不知道。 “小白,见谅!” -----> “小白,工资能给我拿吗?”
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
《Windows核心编程》
hjl0508 2014-04-14
  • 打赏
  • 举报
回复
引用 12 楼 xihu1364 的回复:
[quote=引用 7 楼 hjl0508 的回复:] [quote=引用 2 楼 xihu1364 的回复:] 本来想贴下windows服务代码,楼上说等赵老湿代码,我还是不贴了
哥求代码,我现在对服务是一窍不通。。。。。最好有简单的介绍,谢谢了[/quote]


#define FRONT_START 0				// 服务允许启动前置
#define FRONT_STOP	1				// 服务停止前置
#define FRONT_PAUSE 2				// 服务暂停前置

int _tmain(int argc, _TCHAR* argv[])
{
	// 安装服务
	if ((argc == 2) && (::strcmp(argv[1] + 1, "install") == 0))
	{
		InstallService(ServiceName);
		return 0;
	}

	hEvents[0] = CreateEvent(NULL, FALSE, FALSE, "FRONTSTART");
	hEvents[1] = CreateEvent(NULL, FALSE, FALSE, "FRONTSTOP");
	hEvents[2] = CreateEvent(NULL, FALSE, FALSE, "FRONTPAUSE");
	hEvents[3] = (HANDLE)0;

	
	// 定义服务入口表
	SERVICE_TABLE_ENTRY DispatchTable[] = 
	{
		{ServiceName, ServiceMain},
		{NULL, NULL}
	};

	// 向SCM注册服务并进入循环,直到所有服务退出该函数才返回
	if(!StartServiceCtrlDispatcher(DispatchTable))
	{
		int nError = GetLastError();
	}

	return 0;

}

// 安装服务
void InstallService(const char * szServiceName)
{
	SC_HANDLE handle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if(handle == NULL)
	{
		DWORD dw = ::GetLastError();
		OutputErrorMsg(dw);
		return;
	}
	char szFilename[256];
	::GetModuleFileName(NULL, szFilename, 255);
	SC_HANDLE hService = ::CreateService(handle, szServiceName,
		szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
		SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, szFilename, NULL,
		NULL, NULL, NULL, NULL);
	if(hService)
	{
		::CloseServiceHandle(hService);
	}
	else
	{
		DWORD dw = ::GetLastError();
		OutputErrorMsg(dw);
	}
	::CloseServiceHandle(handle);
}

// 服务控制处理器,接受SCM的指令并通知服务线程
void WINAPI ServiceCtrlHandler(DWORD dwControl)
{
	//MessageBox(NULL, "ServiceCtrlHandler", NULL, MB_SERVICE_NOTIFICATION);
	switch (dwControl)
	{
	case SERVICE_CONTROL_STOP:
		servicestatus.dwCurrentState = SERVICE_STOP_PENDING;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[1]);
		break;
	case SERVICE_CONTROL_PAUSE:
		servicestatus.dwCurrentState = SERVICE_PAUSE_PENDING ;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[2]);
		break;
	case SERVICE_CONTROL_CONTINUE:
		servicestatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[0]);
		break;
	}

}

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
	servicestatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	servicestatus.dwCurrentState = SERVICE_START_PENDING;
	servicestatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
	servicestatus.dwWin32ExitCode = 0;
	servicestatus.dwServiceSpecificExitCode = 0;
	servicestatus.dwCheckPoint = 0;
	servicestatus.dwWaitHint = 0;

	servicestatushandle = ::RegisterServiceCtrlHandler(ServiceName, ServiceCtrlHandler);
	if (servicestatushandle == (SERVICE_STATUS_HANDLE)0)
	{
		return;
	}
	// 设置服务状态为已开始但还没有初始化完成
	::SetServiceStatus(servicestatushandle, &servicestatus);

	CoInitialize(0);

	char cPath0[MAX_PATH] = {0};
	GetModuleFileName(NULL, cPath0, MAX_PATH);
	char* pPos = strrchr(cPath0, '\\');
	*(pPos + 1) = 0;
	sprintf(g_cLogPath, "%slog", cPath0);


	*(pPos + 1) = 0;
	char cPath[MAX_PATH] = {0};
	strcat(cPath, "E:\\test.exe");

	STARTUPINFO si = { sizeof(si) };


	// 服务已经启动
	servicestatus.dwCurrentState = SERVICE_RUNNING;
	SetServiceStatus(servicestatushandle, &servicestatus);

	DWORD dwRet;
	int iFlag = FRONT_START;			// 服务当前的状态为允许启动

	while(TRUE)
	{
		dwRet = WaitForMultipleObjects(4, hEvents, FALSE, 8 * 1000);
		if((iFlag == FRONT_START || iFlag == FRONT_PAUSE) && (dwRet == WAIT_OBJECT_0 + 1))			// 要求停止前置服务的事件发生
		{
			TerminateProcess(g_ProcessInfo.hProcess, 0);
			CloseHandle(g_ProcessInfo.hProcess);
			servicestatus.dwCurrentState = SERVICE_STOPPED;
			SetServiceStatus(servicestatushandle, &servicestatus);
			iFlag = FRONT_STOP;
			return ;
		}
		if((iFlag == FRONT_STOP || iFlag == FRONT_PAUSE) && (dwRet == WAIT_OBJECT_0))				// 要求启动前置服务的事件发生
		{
			iFlag = FRONT_START;
			servicestatus.dwCurrentState = SERVICE_RUNNING;
			SetServiceStatus(servicestatushandle, &servicestatus);
			hEvents[3] = (HANDLE)-1;
		}

		if((iFlag == FRONT_START) && (dwRet == WAIT_OBJECT_0 + 2))			// 要求暂停前置服务的事件发生
		{
			TerminateProcess(g_ProcessInfo.hProcess, 0);
			CloseHandle(g_ProcessInfo.hProcess);
			servicestatus.dwCurrentState = SERVICE_PAUSED;
			SetServiceStatus(servicestatushandle, &servicestatus);
			iFlag = FRONT_PAUSE;
			hEvents[3] = hEvents[2];				// 避免因为前置进程句柄失效导致的Wait函数总是返回WAIT_FAILED而不能收到其它事件
		}

		if(iFlag == FRONT_START)										// 只在允许前置启动的情况下才去连接数据库和启动前置
		{
			if(dwRet == WAIT_OBJECT_0 + 3 || dwRet == WAIT_FAILED)		// 前置进程意外退出或者还未启动
			{
				if(dwRet == WAIT_OBJECT_0 + 3)
				{
					Log("前置意外退出");
				}

				// 记录进程退出日志

				memset(&si, 0, sizeof(STARTUPINFO));
				si.cb = sizeof(STARTUPINFO);
				BOOL ret = CreateProcess(cPath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &g_ProcessInfo);
				int nError = GetLastError();
				if(ret)
				{
					hEvents[3] = g_ProcessInfo.hProcess;
					// 记录进程重新启动日志
					CloseHandle(g_ProcessInfo.hThread);

				}
			}
			else if(dwRet == WAIT_TIMEOUT)
			{

			}
		}
	}

	return ;
}

把主要的几个函数贴出来了,你看看[/quote] 谢谢了,内容不少,我先研究研究!
版主大哥 2014-04-14
  • 打赏
  • 举报
回复
引用 7 楼 hjl0508 的回复:
[quote=引用 2 楼 xihu1364 的回复:] 本来想贴下windows服务代码,楼上说等赵老湿代码,我还是不贴了
哥求代码,我现在对服务是一窍不通。。。。。最好有简单的介绍,谢谢了[/quote]


#define FRONT_START 0				// 服务允许启动前置
#define FRONT_STOP	1				// 服务停止前置
#define FRONT_PAUSE 2				// 服务暂停前置

int _tmain(int argc, _TCHAR* argv[])
{
	// 安装服务
	if ((argc == 2) && (::strcmp(argv[1] + 1, "install") == 0))
	{
		InstallService(ServiceName);
		return 0;
	}

	hEvents[0] = CreateEvent(NULL, FALSE, FALSE, "FRONTSTART");
	hEvents[1] = CreateEvent(NULL, FALSE, FALSE, "FRONTSTOP");
	hEvents[2] = CreateEvent(NULL, FALSE, FALSE, "FRONTPAUSE");
	hEvents[3] = (HANDLE)0;

	
	// 定义服务入口表
	SERVICE_TABLE_ENTRY DispatchTable[] = 
	{
		{ServiceName, ServiceMain},
		{NULL, NULL}
	};

	// 向SCM注册服务并进入循环,直到所有服务退出该函数才返回
	if(!StartServiceCtrlDispatcher(DispatchTable))
	{
		int nError = GetLastError();
	}

	return 0;

}

// 安装服务
void InstallService(const char * szServiceName)
{
	SC_HANDLE handle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if(handle == NULL)
	{
		DWORD dw = ::GetLastError();
		OutputErrorMsg(dw);
		return;
	}
	char szFilename[256];
	::GetModuleFileName(NULL, szFilename, 255);
	SC_HANDLE hService = ::CreateService(handle, szServiceName,
		szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
		SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, szFilename, NULL,
		NULL, NULL, NULL, NULL);
	if(hService)
	{
		::CloseServiceHandle(hService);
	}
	else
	{
		DWORD dw = ::GetLastError();
		OutputErrorMsg(dw);
	}
	::CloseServiceHandle(handle);
}

// 服务控制处理器,接受SCM的指令并通知服务线程
void WINAPI ServiceCtrlHandler(DWORD dwControl)
{
	//MessageBox(NULL, "ServiceCtrlHandler", NULL, MB_SERVICE_NOTIFICATION);
	switch (dwControl)
	{
	case SERVICE_CONTROL_STOP:
		servicestatus.dwCurrentState = SERVICE_STOP_PENDING;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[1]);
		break;
	case SERVICE_CONTROL_PAUSE:
		servicestatus.dwCurrentState = SERVICE_PAUSE_PENDING ;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[2]);
		break;
	case SERVICE_CONTROL_CONTINUE:
		servicestatus.dwCurrentState = SERVICE_CONTINUE_PENDING;
		::SetServiceStatus(servicestatushandle, &servicestatus);
		SetEvent(hEvents[0]);
		break;
	}

}

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
	servicestatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	servicestatus.dwCurrentState = SERVICE_START_PENDING;
	servicestatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
	servicestatus.dwWin32ExitCode = 0;
	servicestatus.dwServiceSpecificExitCode = 0;
	servicestatus.dwCheckPoint = 0;
	servicestatus.dwWaitHint = 0;

	servicestatushandle = ::RegisterServiceCtrlHandler(ServiceName, ServiceCtrlHandler);
	if (servicestatushandle == (SERVICE_STATUS_HANDLE)0)
	{
		return;
	}
	// 设置服务状态为已开始但还没有初始化完成
	::SetServiceStatus(servicestatushandle, &servicestatus);

	CoInitialize(0);

	char cPath0[MAX_PATH] = {0};
	GetModuleFileName(NULL, cPath0, MAX_PATH);
	char* pPos = strrchr(cPath0, '\\');
	*(pPos + 1) = 0;
	sprintf(g_cLogPath, "%slog", cPath0);


	*(pPos + 1) = 0;
	char cPath[MAX_PATH] = {0};
	strcat(cPath, "E:\\test.exe");

	STARTUPINFO si = { sizeof(si) };


	// 服务已经启动
	servicestatus.dwCurrentState = SERVICE_RUNNING;
	SetServiceStatus(servicestatushandle, &servicestatus);

	DWORD dwRet;
	int iFlag = FRONT_START;			// 服务当前的状态为允许启动

	while(TRUE)
	{
		dwRet = WaitForMultipleObjects(4, hEvents, FALSE, 8 * 1000);
		if((iFlag == FRONT_START || iFlag == FRONT_PAUSE) && (dwRet == WAIT_OBJECT_0 + 1))			// 要求停止前置服务的事件发生
		{
			TerminateProcess(g_ProcessInfo.hProcess, 0);
			CloseHandle(g_ProcessInfo.hProcess);
			servicestatus.dwCurrentState = SERVICE_STOPPED;
			SetServiceStatus(servicestatushandle, &servicestatus);
			iFlag = FRONT_STOP;
			return ;
		}
		if((iFlag == FRONT_STOP || iFlag == FRONT_PAUSE) && (dwRet == WAIT_OBJECT_0))				// 要求启动前置服务的事件发生
		{
			iFlag = FRONT_START;
			servicestatus.dwCurrentState = SERVICE_RUNNING;
			SetServiceStatus(servicestatushandle, &servicestatus);
			hEvents[3] = (HANDLE)-1;
		}

		if((iFlag == FRONT_START) && (dwRet == WAIT_OBJECT_0 + 2))			// 要求暂停前置服务的事件发生
		{
			TerminateProcess(g_ProcessInfo.hProcess, 0);
			CloseHandle(g_ProcessInfo.hProcess);
			servicestatus.dwCurrentState = SERVICE_PAUSED;
			SetServiceStatus(servicestatushandle, &servicestatus);
			iFlag = FRONT_PAUSE;
			hEvents[3] = hEvents[2];				// 避免因为前置进程句柄失效导致的Wait函数总是返回WAIT_FAILED而不能收到其它事件
		}

		if(iFlag == FRONT_START)										// 只在允许前置启动的情况下才去连接数据库和启动前置
		{
			if(dwRet == WAIT_OBJECT_0 + 3 || dwRet == WAIT_FAILED)		// 前置进程意外退出或者还未启动
			{
				if(dwRet == WAIT_OBJECT_0 + 3)
				{
					Log("前置意外退出");
				}

				// 记录进程退出日志

				memset(&si, 0, sizeof(STARTUPINFO));
				si.cb = sizeof(STARTUPINFO);
				BOOL ret = CreateProcess(cPath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &g_ProcessInfo);
				int nError = GetLastError();
				if(ret)
				{
					hEvents[3] = g_ProcessInfo.hProcess;
					// 记录进程重新启动日志
					CloseHandle(g_ProcessInfo.hThread);

				}
			}
			else if(dwRet == WAIT_TIMEOUT)
			{

			}
		}
	}

	return ;
}

把主要的几个函数贴出来了,你看看
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
C:\>net help start 此命令的语法是: NET START [service] NET START 用于列出正在运行的服务。 service 指如下服务之一: ALERTER BROWSER CLIENT SERVICE FOR NETWARE CLIPBOOK DHCP CLIENT EVENTLOG FILE REPLICATION MESSENGER NET LOGON NT LM SECURITY SUPPORT PROVIDER PLUG AND PLAY REMOTE ACCESS CONNECTION MANAGER ROUTING AND REMOTE ACCESS RPCLOCATOR RPCSS SCHEDULE SERVER SPOOLER TCP/IP NETBIOS HELPER SERVICE UPS WORKSTATION 当在命令行下输入时,两个或多个字的服务名称必须包含在引号中。例如, NET START "NET LOGON" 启动网络登录服务。 NET START 还可以启动 Windows 中没有提供的网络服务。 NET HELP command | MORE 用于逐屏显示帮助。 C:\>net help stop 此命令的语法是: NET STOP service NET STOP 用于终止 Windows 服务。 终止一个服务可以取消这个服务正在使用的任何一个网络连接。同样的一些 服务是依赖于另外一些服务的。终止一个服务就会终止其它服务。 用户必须终止服务器服务的管理权限。不能终止事件记录服务。 service 指如下服务之一: ALERTER BROWSER CLIENT SERVICE FOR NETWARE CLIPBOOK DHCP CLIENT FILE REPLICATION MESSENGER NET LOGON NT LM SECURITY SUPPORT PROVIDER REMOTE ACCESS CONNECTION MANAGER ROUTING AND REMOTE ACCESS RPCLOCATOR SCHEDULE SERVER SPOOLER TCP/IP NETBIOS HELPER SERVICE UPS WORKSTATION NET STOP 还可以终止 Windows 没有提供的网络服务。 NET HELP command | MORE 用于逐屏显示帮助。
hjl0508 2014-04-14
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
为什么要搞心跳防护?因为被检测进程不时心脏停跳。 为什么被检测进程不时心脏停跳?因为被检测进程亚健康。 怎么知道被检测进程亚健康的原因: 在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象 让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏! 根治了被检测进程亚健康→被检测进程不再心脏停跳→不用再搞心跳防护。 越依赖心跳防护→被检测进程心脏停跳越频繁→被检测进程越不健康! ——架构师zhao4zhong1
您说的很对,不过我是刚刚开始学习c++带我的人应该是比较忙,给我个事儿让我先一边研究一边学习,就比如,dll,服务,进程,线程,进程间通信。。。。。我都得从开始一点点学,估计这够我忙好一阵子了。。。。。。希望在这个过程中能得到大家的帮助,谢谢!
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
怎么心跳防护“用来心跳防护不健康进程C”的进程B的不健康? 怎么心跳防护“用来心跳防护‘用来心跳防护不健康进程C”的进程B’的进程A”的不健康?
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
为什么要搞心跳防护?因为被检测进程不时心脏停跳。 为什么被检测进程不时心脏停跳?因为被检测进程亚健康。 怎么知道被检测进程亚健康的原因: 在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象 让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏! 根治了被检测进程亚健康→被检测进程不再心脏停跳→不用再搞心跳防护。 越依赖心跳防护→被检测进程心脏停跳越频繁→被检测进程越不健康! ——架构师zhao4zhong1
hjl0508 2014-04-14
  • 打赏
  • 举报
回复
引用 2 楼 xihu1364 的回复:
本来想贴下windows服务代码,楼上说等赵老湿代码,我还是不贴了
哥求代码,我现在对服务是一窍不通。。。。。最好有简单的介绍,谢谢了
hjl0508 2014-04-14
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
个人意见:服务不如隐藏窗口的进程好控制、好调试。(所以我从来不用服务)
我也不想啊,但是给的要求就是要写成服务。我初学c++就让我搞“心跳防护”程序,恢复心跳的监测程序还要写成服务,对我来说倍感压力啊,我正在一点点啃呢。。。。有精通或者了解心跳防护的,也可以给我指导一下,谢谢各位大神了。。。。
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
个人意见:服务不如隐藏窗口的进程好控制、好调试。(所以我从来不用服务)
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
用调试器(OD,WINDBG等)调试服务程序 To debug the initialization code of a service application, the debugger must be attached when the service is started. This is accomplished by creating a registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\ProgramName The ProgramName is the image file for the service application you are debugging. Do not specify a path. For example, the ProgramName might look like MyService.exe. Under this key create a string data value called Debugger. The value of this string should be set to the full path of the debugger that will be used. For example, c:\Debuggers\windbg.exe In addition to setting this registry key, the service application must be marked as "interactive". This allows your service to interact with the desktop, and allows the debugger window to appear on your desktop. This again requires modifying a registry key: you must bitwise-or the type entry for your service with 0x100 (this is the value for SERVICE_INTERACTIVE_PROCESS according to Winnt.h). The exact location and name of this registry entry varies. For example: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyServiceKey Finally, you need to adjust the service application timeout. Otherwise, the service application will kill the debugger within 20 seconds after starting. Adjusting the timeout involves setting an entry in the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control Under this key, create a DWORD data value called ServicesPipeTimeout. Set this entry to the amount of time in milliseconds that you want the service to wait before timing out. For example, 60,000 is one minute, while 86,400,000 is 24 hours. 设置ServicesPipeTimeout后需要重启系统才生效 Now, when the service is started, the debugger will also start. When the debugger starts, it will stop at the initial process breakpoint, before the service has begun running. This allows you to set breakpoints or otherwise configure your debugging session to let you monitor the startup of your service. Another option is to place calls to the DebugBreak function in your service from the point at which you would like to break into the debugger. (For more information, see DebugBreak in the Platform SDK documentation.) If your service is running with other services in a Service Host Process, you may need to isolate the service into its own Service Host Process.
赵4老师 2014-04-14
  • 打赏
  • 举报
回复
我没现成的服务代码,楼上坛友但帖无妨。 C:\>sc /? 错误: 未知命令 描述: SC 是用于与 服务控制管理器和服务进行通信的命令行程序。 用法: sc <server> [command] [service name] <option1> <option2>... 选项 <server> 的格式为 "\\ServerName" 可以键入 "sc [command]" 以获得命令的进一步帮助 命令: query-----------查询服务的状态, 或 枚举服务类型的状态。 queryex---------查询服务的扩展状态, 或 枚举服务类型的状态。 start-----------启动服务。 pause-----------向服务发送 PAUSE 控制请求。 interrogate-----向服务发送 INTERROGATE 控制请求。 continue--------向服务发送 CONTINUE 控制请求。 stop------------向服务发送 STOP 请求。 config----------更改服务的配置(永久)。 description-----更改服务的描述。 failure---------更改服务失败时所进行的操作。 sidtype---------更改服务的服务 SID 类型。 qc--------------查询服务的配置信息。 qdescription----查询服务的描述。 qfailure--------查询服务失败时所进行的操作。 qsidtype--------查询服务的服务 SID 类型。 delete----------(从注册表)删除服务。 create----------创建服务(将其添加到注册表)。 control---------向服务发送控制。 sdshow----------显示服务的安全描述符。 sdset-----------设置服务的安全描述符。 showsid---------显示与 任意名称相对应的服务 SID 字符串。 GetDisplayName--获取服务的 DisplayName。 GetKeyName------获取服务的 ServiceKeyName。 EnumDepend------枚举服务的依存关系。 下列命令不要求服务名称: sc <server> <command> <option> boot------------(ok | bad) 表明是否将最后一次启动 保存为最后一次的正确启动配置 Lock------------锁定服务数据库 QueryLock-------查询 SCManager 数据库的 LockStatus 示例: sc start MyService 是否要查看 QUERY 和 QUERYEX 命令的帮助? [ y | n ]: n
版主大哥 2014-04-14
  • 打赏
  • 举报
回复
本来想贴下windows服务代码,楼上说等赵老湿代码,我还是不贴了
BeanJoy 2014-04-14
  • 打赏
  • 举报
回复
用批处理命令sc,简单得得多。 等待赵老师的代码。

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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