annotate vidix/dhahelperwin/dhasetup.c @ 24542:a36693ffbb17

it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
author faust3
date Mon, 17 Sep 2007 21:53:16 +0000
parents 40a61e5ef8ca
children a1c020529cc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24542
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
1 /* dhasetup - dhahelper setup program
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
2 *
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
3 * Copyright (c) 2004 - 2007 Sascha Sommer (MPlayer)
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
4 *
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
5 * Some parts from dhasetup.c source code <http://svn.tilp.info/cgi-bin/viewcvs.cgi/libticables/trunk/src/win32/dha/>
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
6 *
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
7 * Copyright (C) 2007 Romain Lievin (tilp)
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
8 *
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
9 * This program is free software; you can redistribute it and/or modify
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
10 * it under the terms of the GNU General Public License as published by
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
12 * (at your option) any later version.
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
13 *
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
14 * This program is distributed in the hope that it will be useful,
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
17 * GNU General Public License for more details.
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
18 *
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
19 * You should have received a copy of the GNU General Public License
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
20 * along with this program; if not, write to the Free Software
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
22 */
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
23
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
24 #include <windows.h>
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
25 #include <stdio.h>
24542
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
26 #include <winioctl.h>
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
27
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
28 static void print_last_error(char *s){
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
29 LPTSTR lpMsgBuf;
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
30
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
31 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
32 FORMAT_MESSAGE_FROM_SYSTEM |
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
33 FORMAT_MESSAGE_IGNORE_INSERTS,
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
34 NULL, GetLastError(),
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
35 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
36 (LPTSTR) & lpMsgBuf, 0, NULL);
24542
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
37 if(strlen(lpMsgBuf) >= 2)
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
38 lpMsgBuf[strlen(lpMsgBuf)-2] = 0;
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
39
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
40 printf("%s (%i -> %s)\n", s, GetLastError(), lpMsgBuf);
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
41 LocalFree(lpMsgBuf);
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
42 }
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
43
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
44 int main(int argc,char* argv[]){
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
45 SC_HANDLE hSCManager = NULL;
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
46 SC_HANDLE hService = NULL;
12058
faust3
parents: 12057
diff changeset
47 char path[MAX_PATH];
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
48 printf("dhasetup (c) 2004 Sascha Sommer\n");
12098
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
49 GetWindowsDirectory(path,MAX_PATH);
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
50 strcpy(path+strlen(path),"\\system32\\drivers\\dhahelper.sys");
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
51 if(argc==1){
12098
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
52 printf("Usage:\n");
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
53 printf("dhasetup install - Copies dhahelper.sys from the current directory to\n%s and configures it to start at boot.\n", path);
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
54 printf("dhasetup remove - Removes the dhahelper utility.\n");
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
55 return 0;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
56 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
57 hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
58 if(!strcmp(argv[1],"install")){
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
59 printf("Installing dhahelper...");
12453
faust3
parents: 12452
diff changeset
60 if(!CopyFile("dhahelper.sys",path,FALSE)){
12098
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
61 printf("Copying dhahelper.sys failed.\nEither dhahelper.sys is not in the current directory or you lack sufficient\nprivileges to write to %s.", path);
73ee9bd3b537 Error message improved, wording.
diego
parents: 12058
diff changeset
62 return 1;
12453
faust3
parents: 12452
diff changeset
63 }
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
64 // Install the driver
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
65 hService = CreateService(hSCManager,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
66 "DHAHELPER",
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
67 "DHAHELPER",
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
68 SERVICE_ALL_ACCESS,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
69 SERVICE_KERNEL_DRIVER,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
70 SERVICE_SYSTEM_START,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
71 SERVICE_ERROR_NORMAL,
12058
faust3
parents: 12057
diff changeset
72 path,
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
73 NULL,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
74 NULL,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
75 NULL,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
76 NULL,
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
77 NULL);
12450
238820c0ee48 report if the service creation failed
faust3
parents: 12098
diff changeset
78 if(!hService){
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
79 print_last_error("Unable to register DhaHelper Service");
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
80 return 1;
12450
238820c0ee48 report if the service creation failed
faust3
parents: 12098
diff changeset
81 }
24542
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
82
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
83 if(!StartService(hService, 0, NULL)){
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
84 print_last_error("Error while starting service");
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
85 return 1;
a36693ffbb17 it is no longer necessary to reboot the system after the dhahelperwin installation (based on code by Romain Lievin from the tilp project)
faust3
parents: 24540
diff changeset
86 }
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
87
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
88 printf("Success!\n");
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
89 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
90 else if(!strcmp(argv[1],"remove")){
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
91 SERVICE_STATUS ServiceStatus;
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
92
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
93 printf("Removing dhahelper... ");
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
94 hService = OpenService(hSCManager, "DHAHELPER", SERVICE_ALL_ACCESS);
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
95 if(!hService){
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
96 print_last_error("Error opening dhahelper service");
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
97 return 1;
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
98 }
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
99 if(!ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus))
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
100 print_last_error("Error while stopping service");
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
101 if(!DeleteService(hService))
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
102 print_last_error("Error while deleting service");
12058
faust3
parents: 12057
diff changeset
103 DeleteFile(path);
24540
40a61e5ef8ca Make dhasetup more verbose based on code by
faust3
parents: 22900
diff changeset
104 printf("Done!\n");
12057
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
105 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
106 else {
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
107 printf("unknown parameter: %s\n",argv[1]);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
108 }
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
109 CloseServiceHandle(hService);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
110 CloseServiceHandle(hSCManager);
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
111 return 0;
0f058e9cd422 Windows XP support
faust3
parents:
diff changeset
112 }