Bonjour,
Je souhaite savoir comment arreter et demarrer un service externe à l'application (Par exemple le service plug & play .. ou autre).
Serveur Win 2000
Langage : BCB 4 ou 5
Merci d'avance de votre aide
Bonjour,
Je souhaite savoir comment arreter et demarrer un service externe à l'application (Par exemple le service plug & play .. ou autre).
Serveur Win 2000
Langage : BCB 4 ou 5
Merci d'avance de votre aide
Avec la fonction Windows StartService
StartService
The StartService function starts a service.
BOOL StartService(
SC_HANDLE hService, // handle of service
DWORD dwNumServiceArgs, // number of arguments
LPCTSTR *lpServiceArgVectors // array of argument strings
// string pointers
);
Parameters
hService
Handle to the service. This handle is returned by the OpenService or CreateService function, and it must have SERVICE_START access.
dwNumServiceArgs
Specifies the number of argument strings in the lpServiceArgVectors array. If lpServiceArgVectors is NULL, this parameter can be zero.
lpServiceArgVectors
Pointer to an array of pointers that point to null-terminated argument strings passed to a service. Driver services do not receive these arguments. If no arguments are passed to the service being started, this parameter can be NULL. The service accesses these arguments through its ServiceMain function. The first argument (argv[0]) is the name of the service by default, followed by the arguments, if any, in the lpServiceArgVectors array.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Merci pour la rapidite de ta reponse.
Mais je ne sais pas comment passer les arguments.
Ou dois je indiquer le nom du service ou le nom de l'executable qui est lancé, que cela soit pour la fonction StartService ou ControlService.
Si tu pouvais me donner un exemple pour ces fonctions cela m'aiderait beaucoup.
Merci.
Syntaxe non garantie à 100%, mais c'est quelque chose comme ça:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 char szServiceName[] = "MonService"; SC_HANDLE hSCManager, hService; hSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); hService = OpenService(hSCManager, szServiceName, SERVICE_ALL_ACCESS); // Démarrage du service StartService(hService, 0, NULL); if (hService) CloseServiceHandle(hService); CloseServiceHandle(hSCManager);
La fonction pour demarrer le service fonctionne !
Par contre j'ai essayé d'adapter ton code pour arreter le service avec les commandes suivantes mais cela ne marche pas, pourrais tu me dire ou je me plante.
Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 hSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS); hService = OpenService(hSCManager, szServiceName, SERVICE_ALL_ACCESS); // Arret du service ControlService(hService, SERVICE_CONTROL_STOP, NULL); if (hService) CloseServiceHandle(hService); CloseServiceHandle(hSCManager);
Parce que le dernier argument de ControlService ne doit pas être NULL:
ControlService
The ControlService function sends a control code to a Win32-based service.
BOOL ControlService(
SC_HANDLE hService, // handle to service
DWORD dwControl, // control code
LPSERVICE_STATUS lpServiceStatus
// pointer to service status structure
);
Parameters
hService
Handle to the service. This handle is returned by the OpenService or CreateService function. The access required for this handle depends on the dwControl code requested.
dwControl
Specifies the requested control code. This value can be one of the standard control codes in the following table: Value Meaning
SERVICE_CONTROL_STOP Requests the service to stop. The hService handle must have SERVICE_STOP access.
SERVICE_CONTROL_PAUSE Requests the service to pause. The hService handle must have SERVICE_PAUSE_CONTINUE access.
SERVICE_CONTROL_
CONTINUE Requests the paused service to resume. The hService handle must have SERVICE_PAUSE_CONTINUE access.
SERVICE_CONTROL_
INTERROGATE Requests the service to update immediately its current status information to the service control manager. The hService handle must have SERVICE_INTERROGATE access.
SERVICE_CONTROL_
SHUTDOWN The ControlService function fails if this control code is specified.
This value can also be a user-defined control code, as described in the following table: Value Meaning
Range 128 to 255. The service defines the action associated with the control code. The hService handle must have SERVICE_USER_DEFINED_CONTROL access.
lpServiceStatus
Pointer to a SERVICE_STATUS structure to receive the latest service status information. The information returned reflects the most recent status that the service reported to the service control manager.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 SERVICE_STATUS ServStat; ControlService(hService, SERVICE_CONTROL_INTERROGATE, &ServStat); if (ServStat.dwCurrentState != SERVICE_STOPPED) { // Arrêt du service ControlService(hService, SERVICE_CONTROL_STOP, &ServStat); }
Ca marche sans aucun probleme.
Merci beaucoup pour ton aide.
Bonne journée.
salut
Il y a une possibiliter de passer le service en manuel ??
merci
Julien
@+ JujuBois
Bonjour
Moi, pour passer un service en manuel, j'utilise le registre
un peut barbare mais bon, ça marche
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 //infos et ecriture Aide et Support svcsreg->RootKey = HKEY_LOCAL_MACHINE; svcsreg->OpenKey("SYSTEM\\ControlSet003\\Services\\helpsvc", false);//chemin svcsreg->WriteInteger("Start", 3);//mettre en manuel Label2->Caption = svcsreg->ReadString("DisplayName");//nom du service Label4->Caption = svcsreg->ReadInteger("Start");//savoir si en manuel if(Label4->Caption == 2)//convertir 2 en automatique Label4->Caption = "Automatique"; if(Label4->Caption == 3)//convertir 3 en Manuel Label4->Caption = "Manuel"; if(Label4->Caption == 4)//convertir 4 en désactiver Label4->Caption = "Désactiver"; delete svcsreg;
A+
.
Why
.
//------------------
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager