diff --git a/programs/net/Makefile.in b/programs/net/Makefile.in index 5db7fc0..9037323 100644 --- a/programs/net/Makefile.in +++ b/programs/net/Makefile.in @@ -4,7 +4,7 @@ SRCDIR = @srcdir@ VPATH = @srcdir@ MODULE = net.exe APPMODE = -mconsole -IMPORTS = kernel32 +IMPORTS = kernel32 advapi32 C_SRCS = net.c diff --git a/programs/net/net.c b/programs/net/net.c index f77c820..c11b467 100644 --- a/programs/net/net.c +++ b/programs/net/net.c @@ -18,15 +18,82 @@ #include #include +#include + +#define NET_START 0001 + +static BOOL net_service(int operation, char *service_name) +{ + SC_HANDLE SCManager, serviceHandle; + BOOL result = 0; + DWORD buffer_size = 0; + char *service_display_name = NULL; + + SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); + if(!SCManager) + { + printf("Couldn't get handle to SCManager.\n"); + return FALSE; + } + serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS); + if(!serviceHandle) + { + printf("Couldn't get handle to service.\n"); + CloseServiceHandle(SCManager); + return FALSE; + } + + if(!GetServiceDisplayName(SCManager, service_name, NULL, &buffer_size)) + { + /* Presently (May 14, 2007), this function returns success (non-zero) */ + /* According to MSDN, calling GetServiceDisplayName with the third parameter null should always return fail. */ + if((GetLastError() == ERROR_INSUFFICIENT_BUFFER) && buffer_size) + { + service_display_name = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, buffer_size + 1); + switch((int)service_display_name) + { + case STATUS_NO_MEMORY: + printf("STATUS_NO_MEMORY when allocating service_display_name.\n"); + operation = 0; + break; + case STATUS_ACCESS_VIOLATION: + printf("STATUS_ACCESS_VIOLATION when allocating service_display_name.\n"); + operation = 0; + break; + default: + if(!GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size)) HeapFree(GetProcessHeap(), 0, service_display_name); + break; + } + } + } + + switch(operation) + { + case NET_START: + if(service_display_name) printf("The %s service is starting.\n", service_display_name); + else printf("The %s service is starting.\n", service_name); + result = StartService(serviceHandle, 0, NULL); + + if(service_display_name) printf("The %s service ", service_display_name); + else printf("The %s service ", service_name); + if(!result) printf("failed to start.\n"); + else printf("was started successfully.\n"); + break; + } + + CloseServiceHandle(serviceHandle); + CloseServiceHandle(SCManager); + if(service_display_name != NULL) HeapFree(GetProcessHeap(), 0, service_display_name); + return result; +} int main(int argc, char *argv[]) { - int ret = 0; if (argc < 2) { printf("The syntax of this command is:\n\n"); - printf("NET [ HELP ]\n"); + printf("NET [ HELP | START ]\n"); return 1; } @@ -35,7 +102,23 @@ int main(int argc, char *argv[]) printf("The syntax of this command is:\n\n"); printf("NET HELP command\n -or-\nNET command /HELP\n\n"); printf(" Commands available are:\n"); - printf(" NET HELP\n"); + printf(" NET HELP NET START\n"); } - return ret; + + if(!strcasecmp(argv[1], "start")) + { + if(argc < 3) + { + printf("Specify service name to start.\n"); + return 1; + } + + if(!net_service(NET_START, argv[2])) + { + return 1; + } + return 0; + } + + return 0; }