007
16.11.2004, 09:26 Uhr
Guybrush Threepwood
Gefürchteter Pirat (Operator)
|
So ungefährsollte das funktionieren:
C++: |
struct GLOBALS { bool bRunning; SERVICE_STATUS m_ServiceStatus; SERVICE_STATUS_HANDLE m_ServiceStatusHandle; }g_;
APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
if(strcmp(lpCmdLine,"") !=0) { if(strcmp(lpCmdLine,"-i")==0) { if(InstallService()) MessageBox(0,"Service erfolgreich installiert.","test",MB_APPLMODAL); else MessageBox(0,"Service konnte nicht installiert werden!","test",MB_APPLMODAL); } else if(strcmp(lpCmdLine,"-d")==0) { if(DeleteService()) MessageBox(0,"Service erfolgreich deinstalliert.","test",MB_APPLMODAL); else MessageBox(0,"Service konnte nicht deinstalliert werden!","test",MB_APPLMODAL); } else { MessageBox(0,"Unbekannter Parameter\n\nZum installieren: -i\n\nZum Deinstallieren: -d","test",MB_APPLMODAL); } } else { SERVICE_TABLE_ENTRY DispatchTable[]={{"Testservice",ServiceMain},{NULL,NULL}}; StartServiceCtrlDispatcher(DispatchTable); } return 0; //Programm Ende }
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { g_.m_ServiceStatus.dwServiceType = SERVICE_WIN32; g_.m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; g_.m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; g_.m_ServiceStatus.dwWin32ExitCode = 0; g_.m_ServiceStatus.dwServiceSpecificExitCode = 0; g_.m_ServiceStatus.dwCheckPoint = 0; g_.m_ServiceStatus.dwWaitHint = 0; g_.m_ServiceStatusHandle = RegisterServiceCtrlHandler("Testserver",ServiceCtrlHandler); if (g_.m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) { return; }
g_.m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; g_.m_ServiceStatus.dwCheckPoint = 0; g_.m_ServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (g_.m_ServiceStatusHandle, &g_.m_ServiceStatus)) {
} g_.bRunning=true; while(g_.bRunning) { //Hier kommt dann das Hauptprogramm hin }
return; }
//----------------------------------------------- // Kontrollhandels des Dienstes //----------------------------------------------- void WINAPI ServiceCtrlHandler(DWORD Opcode) { switch(Opcode) { case SERVICE_CONTROL_PAUSE: g_.m_ServiceStatus.dwCurrentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: g_.m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_STOP: g_.m_ServiceStatus.dwWin32ExitCode = 0; g_.m_ServiceStatus.dwCurrentState = SERVICE_STOPPED; g_.m_ServiceStatus.dwCheckPoint = 0; g_.m_ServiceStatus.dwWaitHint = 0; SetServiceStatus (g_.m_ServiceStatusHandle,&g_.m_ServiceStatus);
g_.bRunning=false;
break; case SERVICE_CONTROL_INTERROGATE: break; } return; }
BOOL InstallService() {
char strDir[1024]; SC_HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir); strcat(strDir,"\\test.exe");
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false;
LPCTSTR lpszBinaryPathName=strDir; schService = CreateService(schSCManager,"Test","Test2", // 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) return false; StartService(schService,0,0);
CloseServiceHandle(schService);
return true; }
BOOL DeleteService() { SC_HANDLE schSCManager, hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (schSCManager == NULL) return false;
hService=OpenService(schSCManager,"Testservice",SERVICE_ALL_ACCESS);
if (hService == NULL) return false;
if(DeleteService(hService)==0) return false;
if(CloseServiceHandle(hService)==0) return false; else return true; }
|
|