"Tim Schwartz" tim@sanityinternet.com wrote:
#include <stdio.h> #include <string.h> +#include <windows.h>
+int net_service(char *operation, char *service_name);
It's better to properly arrange the code to avoid forward declarations.
+int net_service(char *operation, char *service_name) +{
- SC_HANDLE SCManager, serviceHandle;
- int result = 0;
- char service_display_name[4096];
service_display_name should be queried and dynamically allocated.
- SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
- if(!SCManager)
- {
printf("Couldn't get handle to SCManager.\n");
return 0;
- }
- serviceHandle = OpenService(SCManager, service_name,
SC_MANAGER_ALL_ACCESS);
- if(!serviceHandle)
- {
printf("Couldn't get handle to service.\n");
return 0;
- }
You are leaking SCManager handle here.
- if(!GetServiceDisplayName(SCManager, service_name,
service_display_name, NULL))
Are you sure that last parameter can be NULL here? MSDN seems to disagree. Also did you test your code under Windows? This kind of application can be and should be IMO developed/tested in Windows.
- if(!strcasecmp(operation, "start"))
- {
printf("The %s service is starting.\n", service_display_name);
result = StartService(serviceHandle,0,NULL);
CloseServiceHandle(serviceHandle);
if(!result) printf("The %s service failed to start.\n",
service_display_name);
else printf("The %s service was started successfully.\n",
service_display_name);
return result;
- }
- return 0;
}
You are leaking the SCManager handle here as well, and both serviceHandle and SCManager are leaked if operation is not "start". Also the patch was wrapped by your mailer.