afsifs-20050615
[openafs.git] / src / WINNT / install / NSIS / Service.cpp
1 /*
2       Service Installer for NSIS script
3       
4       Rob Murawski
5       
6       Released under terms of IBM Open Source agreement for OpenAFS
7       
8       */
9
10 #include <stdio.h>
11 #include <windows.h>
12 #include <tchar.h>
13
14 int main(int argc, char *argv[])
15 {
16         DWORD type, start;
17
18    if(argc<3)
19    {
20       printf("Insufficient arguments: Service ServiceName ServicePath DisplayName.\n");
21       return 1;
22    }
23    
24         SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
25         SC_HANDLE hService;
26
27         if(hSCM == NULL)
28         {
29                 printf("Could not open Service Control Manager. Aborting.\n");
30                 return 1;
31         }
32
33
34    if(*argv[1]!='u' && *argv[1]!='U')
35    {
36                 if (!stricmp(argv[2] + strlen(argv[2]) - 3, "sys"))
37                         {
38                         type = SERVICE_FILE_SYSTEM_DRIVER;
39                         start = SERVICE_DEMAND_START;
40                         }
41                 else
42                         {
43                         type = SERVICE_WIN32_OWN_PROCESS;
44                         start = SERVICE_AUTO_START;
45                         }
46                 hService = CreateService(hSCM, argv[1],
47                 _T(argv[3]),
48                 SERVICE_ALL_ACCESS,
49                 type,
50                 start,
51                 SERVICE_ERROR_IGNORE,
52                 argv[2],
53                 NULL,NULL,NULL, NULL, NULL );
54
55                 if (hService == NULL) 
56                 {
57                     printf("Create Service failed (%d)\n", GetLastError() );
58                     CloseServiceHandle(hSCM);
59                 }
60    }
61    else
62    {
63        hService = OpenService( hSCM, argv[2], DELETE);
64        if(hService!=NULL)
65            DeleteService( hService );
66    }
67    
68         CloseServiceHandle(hService);
69
70
71         CloseServiceHandle(hService);
72         CloseServiceHandle(hSCM);
73
74         return 0;
75 }