Jeg har kigget lidt på den kode.. Men kan ikke finde, det som jeg 100% skal bruge.
Kan godt se at programmet han har lavet virker, men er ikke sikker på det jeg laver.
Tror det af noget af det her, men kan ikke finde den helt rigtige kode: // cService.cpp: implementation of the cService class. // //////////////////////////////////////////////////////////////////////
// return Errorcode or 0 // Install the service when user clicks the Create button DWORD cService::Create() { SC_HANDLE hdlSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
SC_HANDLE hdlServ = CreateService( hdlSCM, // SCManager database ServiceName, // name of service ServiceDisplayName, // service name to display STANDARD_RIGHTS_REQUIRED, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type Calling, // service's binary Path name NULL, // no load ordering group NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password
DWORD Ret = 0; if (!hdlServ) Ret = ::GetLastError(); CloseServiceHandle(hdlServ); return Ret; }
// return Errorcode or 0 // Uninstall the service when user clicks the Delete button DWORD cService::Delete() { SC_HANDLE hdlSCM = OpenSCManager(NULL, NULL, STANDARD_RIGHTS_REQUIRED);
DWORD Ret = 0; if (!DeleteService(hdlServ)) Ret = ::GetLastError(); CloseServiceHandle(hdlServ); return Ret; }
// return Errorcode or 0 // Start the service when user clicks the Start button DWORD cService::Start() { SC_HANDLE hdlSCM = OpenSCManager(NULL, NULL, STANDARD_RIGHTS_REQUIRED);
DWORD Ret = 0; if (!StartService(hdlServ, 0, NULL)) Ret = ::GetLastError(); CloseServiceHandle(hdlServ); return Ret; }
// return Errorcode or 0 // Stop the service when user clicks the Stop button DWORD cService::Stop() { SC_HANDLE hdlSCM = OpenSCManager(NULL, NULL, STANDARD_RIGHTS_REQUIRED);
// Service Main Function called by Windows void cService::MainStart(DWORD argc, LPTSTR *argv) { CString Out = *argv; Output("Service Main -- Start with Arguments: \"" + Out + "\"");
hdlStat = ::RegisterServiceCtrlHandler(ServiceName, (LPHANDLER_FUNCTION) ApiServiceControlHandler); if (!hdlStat) { int Err = ::GetLastError(); Out.Format("Service Main -- RegisterServiceHandler Error : %d\n", Err); Output(Out); return; } else Output("Service Main -- RegisterServiceHandler");
EndlessLoop(); // Do the service work (write to logfile every 5 seconds) }
// Start the service (in the Exe which is running as Service!!) void cService::Dispatch() { SERVICE_TABLE_ENTRY DispTbl[] = { { ServiceName, (LPSERVICE_MAIN_FUNCTION) ApiServiceMainStarter}, { NULL , NULL} };
Output("Dispatch -- Before calling StartServiceCtrlDispatcher\n");
if (!::StartServiceCtrlDispatcher(DispTbl)) // does not return until service stopped { int Err = ::GetLastError(); CString Out; Out.Format("Dispatch -- StartServiceCtrlDispatcher Error : %d\n", Err); Output(Out); } else Output("Dispatch -- Returning from StartServiceCtrlDispatcher\n"); }
CString Command, Status; if (Opcode == SERVICE_CONTROL_STOP) { Command = "CONTROL_STOP"; strctStat.dwCurrentState = SERVICE_STOPPED; Status = "SERVICE_STOPPED"; } else if (Opcode == SERVICE_CONTROL_SHUTDOWN) { Command = "CONTROL_SHUTDOWN"; strctStat.dwCurrentState = SERVICE_STOPPED; Status = "SERVICE_STOPPED"; } else if (Opcode == SERVICE_CONTROL_INTERROGATE) { Command = "CONTROL_INTERROGATE"; strctStat.dwCurrentState = SERVICE_RUNNING; Status = "SERVICE_RUNNING"; } else { Command.Format("Command Opcode Nr. %d ", Opcode); strctStat.dwCurrentState = SERVICE_RUNNING; Status = "SERVICE_RUNNING"; }
if (Opcode != 0) Output("Control Handler -- received Command "+Command);
if (!SetServiceStatus (hdlStat, &strctStat)) { int Err = ::GetLastError(); CString Out; Out.Format("Control Handler -- Set Status Error : %d", Err); Output(Out); return; } else Output("Control Handler -- SetStatus "+Status); }
// writing the current time every 5 Seconds to the LOG file to demonstrate that service ir running void cService::EndlessLoop() { Output("EndlessLoop Start\n");
SYSTEMTIME Clock; CString Min, Sec, Out;
// This endlessloop is killed by Windows when the service is stopped or shutdown while (1) { if (hTimer != 0) { LARGE_INTEGER Elapse; Elapse.QuadPart = -50000000; // 5 Seconds in 100 Nanoseconds resolution (negative=relative) BOOL Res = ::SetWaitableTimer(hTimer, &Elapse, 0, NULL, NULL, TRUE);
Det er svært at hjælpe dig når du ikke skriver hvad du vil have hjælp til? Hvis du vil have flere eksempler/forklaringer på en Windows Service' funktioner, kan du fx kigge i afsnittet her:
Jeg forstår fint at du vil have dit program til at være en service, men hvilken del af programmeringen er det du har problemer med? Jeg har desværre ikke tid til at lave et helt program for dig, men du kan se fremgangsmetoden i eksemplerne fra "Using services"-afsnittet i MSDN (linket).
int _tmain(int argc, _TCHAR* argv[]) { int nResult = 0;
// Check if there is a second argument (first one is .exe filename) if(argc >= 2) { string arg(argv[1]); if(arg == "/install") // If the argument is "/install", create the service { cout << "Installing service" << endl; string strParameters = " /start"; // Collect any additional parameters for(int i=2; i<argc; ++i) strParameters += string(" ") + argv[i]; nResult = g_Service.Create(argv[0] + strParameters); } else if(arg == "/uninstall") // If the argument is "/uninstall", delete the service { cout << "Uninstalling service" << endl; nResult = g_Service.Delete(); } else if(arg == "/start") // If the argument is "/start", start the service { g_Service.Dispatch(); } }
if(nResult == 0) // Check for error { cout << "Done..." << endl; } else { // Get result text LPTSTR lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, nResult, 0, (LPTSTR)&lpMsgBuf, 0, NULL);
// Show result cout << lpMsgBuf << endl;
// Free the buffer. LocalFree(lpMsgBuf); }
return nResult; }
Den bruger klassen cService, som du kan se her:
// cService.h: interface for the cService class. // //////////////////////////////////////////////////////////////////////
#ifndef SERVICE_H #define SERVICE_H
#include <windows.h> #include "winsvc.h" #include <string> using namespace std;
const LPSTR ServiceName = "DemoService"; const LPSTR ServiceDisplayName = "A little Demo Service";
class cService { public: cService(); virtual ~cService();
// cService.cpp: implementation of the cService class. // //////////////////////////////////////////////////////////////////////
#include "cService.h" #include <fstream>
// global instance of Service cService g_Service;
// this function must be in global namespace (Windows API callback) void ApiServiceMainStarter(DWORD argc, LPTSTR *argv) { g_Service.MainStart(argc, argv); }
// this function must be in global namespace (Windows API callback) void ApiServiceControlHandler(DWORD Opcode) { g_Service.ControlHandler(Opcode); }
SC_HANDLE hdlServ = CreateService( hdlSCM, // SCManager database ServiceName, // name of service ServiceDisplayName, // service name to display STANDARD_RIGHTS_REQUIRED, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type strCmd.c_str(), // service's binary Path name 0, // no load ordering group 0, // no tag identifier 0, // no dependencies 0, // LocalSystem account 0); // no password
DWORD Ret = 0; if(!hdlServ) Ret = ::GetLastError(); CloseServiceHandle(hdlServ); return Ret; }
SERVICE_STATUS ServStat; DWORD Ret = 0; if(!ControlService(hdlServ, SERVICE_CONTROL_STOP, &ServStat)) Ret = ::GetLastError(); CloseServiceHandle(hdlServ); return Ret; }
// Service Main Function called by Windows void cService::MainStart(DWORD argc, LPTSTR *argv) { hdlStat = ::RegisterServiceCtrlHandler(ServiceName, (LPHANDLER_FUNCTION)ApiServiceControlHandler); if(!hdlStat) { int Err = ::GetLastError(); return; }
// Set status Running ControlHandler(0);
// Do the service work while(true) { SYSTEMTIME Clock; ::GetLocalTime(&Clock);
// Start the service (in the Exe which is running as Service!!) void cService::Dispatch() { SERVICE_TABLE_ENTRY DispTbl[] = { { ServiceName, (LPSERVICE_MAIN_FUNCTION)ApiServiceMainStarter}, { 0, 0} };
if(!::StartServiceCtrlDispatcher(DispTbl)) // does not return until service stopped { int Err = ::GetLastError(); } }
Du har filerne cService.h og cService.cpp, de skal tilføjes til dit projekt. Den første fil, ServiceTest.cpp, er et simpelt eksempel på hvordan du bruger cService-klassen.
Jeg kender ikke meget til Dev-Cpp og derfor heller ikke til dets måde at rapportere fejl på. Det ligner dog at filerne ikke rigtig er tilføjet projektet..?
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.