我这样创建的服务:(代码片段) schService = CreateService(schSCManager,"Service1",
"The Display Name Needed", // 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
现在可以在windows控制面板的服务里面找到"The Display Name Needed"这一条了,而且可以Start和Stop。
问题是:
(1)在任务管理器里面,我没有看到我编译出来的该程序(myservice)的名字。难道是被rundll32之类的东西调用了?
(2)我在VC2005里面,如何挂接/调试这个正在运行的后台服务呢?谢谢!
"The Display Name Needed", // 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
现在可以在windows控制面板的服务里面找到"The Display Name Needed"这一条了,而且可以Start和Stop。
问题是:
(1)在任务管理器里面,我没有看到我编译出来的该程序(myservice)的名字。难道是被rundll32之类的东西调用了?
(2)我在VC2005里面,如何挂接/调试这个正在运行的后台服务呢?谢谢!
2. 为方便调试你的代码,在调用服务函数正式执行你的代码前加入 sleep(20000)和断点,等待足够时间,在这段时间内去挂接你的服务进程。
#include<stdio.h>
#include <Windows.h>
#include <WinDef.h>
#include <Winsvc.h>
#include <time.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[]){
if(argc>1) {
if(strcmp(argv[1],"-i")==0) {
if(InstallService())
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nError Installing Service\n");
}
if(strcmp(argv[1],"-d")==0) {
if(DeleteService())
printf("\n\nService UnInstalled Sucessfully\n");
else
printf("\n\nError UnInstalling Service\n");
}else{
printf("\n\nUnknown Switch Usage\n\nFor Install\
use Srv1 -i\n\nFor UnInstall use Srv1 -d\n");
return 1;
}
}else{
SERVICE_TABLE_ENTRY DispatchTable[]=
{{"Service1",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
return 0;
}void WINAPI ServiceMain(DWORD argc, LPTSTR *argv){
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("Service1",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
return;
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
return;
bRunning=true;
while(bRunning) {
Sleep(3000);
//这里是我们要做的事情....
OutputDebugString("abcde...........\n");
}
return;
}void WINAPI ServiceCtrlHandler(DWORD Opcode){
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
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(){
char strDir[1024];
SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
strncat_s(strDir,"\\..\\Debug\\myServic3e.exe",100);
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return false;
LPCTSTR lpszBinaryPathName=strDir;
schService = CreateService(schSCManager,"Service1",
"The Display Name Needed", // 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
if (schService == NULL)
return false;
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return true;
}BOOL DeleteService(){
SC_HANDLE schSCManager,hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
return false;
hService=OpenService(schSCManager,"Service1",SERVICE_ALL_ACCESS);
if (hService == NULL)
return false;
if(DeleteService(hService)==0)
return false;
if(CloseServiceHandle(hService)==0)
return false;
return true;
}我的问题就是,通过控制面板->服务->Start跑起来以后,在任务管理器里面能看到吗? 叫什么名字呢?如何挂载这个服务?
当然可以看见,用户名是system的什么叫挂载这个服务?
我的意思是说,让调试器Attach这个服务进程。我用词不太准确,呵呵。
或者API:OutputDebugString+工具DbgView。