PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/install/xbt/linux/misc/windows/nt_service.cpp

http://torrentpier2.googlecode.com/
C++ | 67 lines | 64 code | 3 blank | 0 comment | 6 complexity | c1dbee30b2ef0743e3fc4baf833a9161 MD5 | raw file
 1#include "stdafx.h"
 2#include "nt_service.h"
 3
 4#include <windows.h>
 5
 6int nt_service_install(const char* name)
 7{
 8	SC_HANDLE scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
 9	if (!scm)
10		return 1;
11	char file_name[MAX_PATH];
12	GetModuleFileName(NULL, file_name, MAX_PATH);
13	SC_HANDLE service = CreateService(scm, 
14		name, 
15		name, 
16		SERVICE_ALL_ACCESS, 
17		SERVICE_WIN32_OWN_PROCESS, 
18		SERVICE_AUTO_START, 
19		SERVICE_ERROR_NORMAL,
20		file_name, 
21		NULL, 
22		NULL, 
23		NULL, 
24		"NT AUTHORITY\\LocalService", 
25		NULL);
26	if (!service)
27	{
28		service = CreateService(scm, 
29			name, 
30			name, 
31			SERVICE_ALL_ACCESS, 
32			SERVICE_WIN32_OWN_PROCESS, 
33			SERVICE_AUTO_START, 
34			SERVICE_ERROR_NORMAL,
35			file_name, 
36			NULL, 
37			NULL, 
38			NULL, 
39			NULL, 
40			NULL);	
41	}
42	if (!service)
43	{
44		CloseServiceHandle(scm);
45		return 1;
46	}
47	CloseServiceHandle(service);
48	CloseServiceHandle(scm);
49	return 0;
50}
51
52int nt_service_uninstall(const char* name)
53{
54	SC_HANDLE scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
55	if (!scm)
56		return 1;
57	int result = 1;
58	SC_HANDLE service = OpenService(scm, name, DELETE);
59	if (service)
60	{
61		if (DeleteService(service))
62			result = 0;
63		CloseServiceHandle(service);
64	}
65	CloseServiceHandle(scm);
66	return result;
67}