aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/lib/zlportio/ddkint.pas
blob: 65b208d9e2fe87968a5de1f73e4b217de4fde664 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
{ -----------------------------------------------------------------------------}
{ Copyright 2000-2001, Zloba Alexander.  All Rights Reserved.                  }
{ This unit can be freely used and distributed in commercial and private       }
{ environments, provided this notice is not modified in any way.               }
{ -----------------------------------------------------------------------------}
{ Feel free to contact me if you have any questions, comments or suggestions at}
{   zal@specosoft.com (Zloba Alexander)                                        }
{ You can always find the latest version of this unit at:                      }
{   http://www.specosoft.com                                                   }

{ -----------------------------------------------------------------------------}
{ Date last modified:  08/10/2001                                              }
{ -----------------------------------------------------------------------------}
{ Description:                                                                 }
{   This unit include service function to work with NT drivers and some        }
{    constant from ntddk.h                                                     }
{------------------------------------------------------------------------------}
{ Revision History:                                                            }
{ 1.00:  + First public release                                                }
{ 1.10:  + added compiler directives for correct compilation                   }
{ 1.20:  + optimized code                                                      }
{ 1.30:  + added constant for compatibility with delphi 3.0                    }
{------------------------------------------------------------------------------}

{$A-,H-}
unit ddkint;

interface
uses
    windows,
    winsvc;

function CTL_CODE(const DeviceType,Func,Method,Access:Cardinal):cardinal;

const
 FILE_DEVICE_BEEP               = $00000001;
 FILE_DEVICE_CD_ROM             = $00000002;
 FILE_DEVICE_CD_ROM_FILE_SYSTEM = $00000003;
 FILE_DEVICE_CONTROLLER         = $00000004;
 FILE_DEVICE_DATALINK           = $00000005;
 FILE_DEVICE_DFS                = $00000006;
 FILE_DEVICE_DISK               = $00000007;
 FILE_DEVICE_DISK_FILE_SYSTEM   = $00000008;
 FILE_DEVICE_FILE_SYSTEM        = $00000009;
 FILE_DEVICE_INPORT_PORT        = $0000000a;
 FILE_DEVICE_KEYBOARD           = $0000000b;
 FILE_DEVICE_MAILSLOT           = $0000000c;
 FILE_DEVICE_MIDI_IN            = $0000000d;
 FILE_DEVICE_MIDI_OUT           = $0000000e;
 FILE_DEVICE_MOUSE              = $0000000f;
 FILE_DEVICE_MULTI_UNC_PROVIDER = $00000010;
 FILE_DEVICE_NAMED_PIPE         = $00000011;
 FILE_DEVICE_NETWORK            = $00000012;
 FILE_DEVICE_NETWORK_BROWSER    = $00000013;
 FILE_DEVICE_NETWORK_FILE_SYSTEM= $00000014;
 FILE_DEVICE_NULL               = $00000015;
 FILE_DEVICE_PARALLEL_PORT      = $00000016;
 FILE_DEVICE_PHYSICAL_NETCARD   = $00000017;
 FILE_DEVICE_PRINTER            = $00000018;
 FILE_DEVICE_SCANNER            = $00000019;
 FILE_DEVICE_SERIAL_MOUSE_PORT  = $0000001a;
 FILE_DEVICE_SERIAL_PORT        = $0000001b;
 FILE_DEVICE_SCREEN             = $0000001c;
 FILE_DEVICE_SOUND              = $0000001d;
 FILE_DEVICE_STREAMS            = $0000001e;
 FILE_DEVICE_TAPE               = $0000001f;
 FILE_DEVICE_TAPE_FILE_SYSTEM   = $00000020;
 FILE_DEVICE_TRANSPORT          = $00000021;
 FILE_DEVICE_UNKNOWN            = $00000022;
 FILE_DEVICE_VIDEO              = $00000023;
 FILE_DEVICE_VIRTUAL_DISK       = $00000024;
 FILE_DEVICE_WAVE_IN            = $00000025;
 FILE_DEVICE_WAVE_OUT           = $00000026;
 FILE_DEVICE_8042_PORT          = $00000027;
 FILE_DEVICE_NETWORK_REDIRECTOR = $00000028;
 FILE_DEVICE_BATTERY            = $00000029;
 FILE_DEVICE_BUS_EXTENDER       = $0000002a;
 FILE_DEVICE_MODEM              = $0000002b;
 FILE_DEVICE_VDM                = $0000002c;
 FILE_DEVICE_MASS_STORAGE       = $0000002d;
 FILE_DEVICE_SMB                = $0000002e;
 FILE_DEVICE_KS                 = $0000002f;
 FILE_DEVICE_CHANGER            = $00000030;
 FILE_DEVICE_SMARTCARD          = $00000031;
 FILE_DEVICE_ACPI               = $00000032;
 FILE_DEVICE_DVD                = $00000033;
 FILE_DEVICE_FULLSCREEN_VIDEO   = $00000034;
 FILE_DEVICE_DFS_FILE_SYSTEM    = $00000035;
 FILE_DEVICE_DFS_VOLUME         = $00000036;
 FILE_DEVICE_SERENUM            = $00000037;
 FILE_DEVICE_TERMSRV            = $00000038;
 FILE_DEVICE_KSEC               = $00000039;

 FILE_DEVICE_KRNLDRVR           = $80ff;

 METHOD_BUFFERED   =              0;
 METHOD_IN_DIRECT  =              1;
 METHOD_OUT_DIRECT =              2;
 METHOD_NEITHER    =              3;

 FILE_ANY_ACCESS     =            0;
 FILE_SPECIAL_ACCESS =   (FILE_ANY_ACCESS);
 FILE_READ_ACCESS    =      ( $0001 );    // file & pipe
 FILE_WRITE_ACCESS   =      ( $0002 );    // file & pipe

 {$IFDEF VER100 or VER110}
 // for compatibilty with delphi 3.0
const
  SERVICE_KERNEL_DRIVER         = $00000001;
  SERVICE_DEMAND_START          = $00000003;
  SERVICE_ERROR_NORMAL          = $00000001;

{$ENDIF}

function driverstart(const name:pchar):integer;
function driverstop(const name:pchar):integer;

// for this function must have Administrators or Power users rigths
function driverinstall(const path,name:pchar):integer;
function driverremove(const name:pchar):integer;


// exlpanation function
function messagestring(const error:integer):string;

implementation

function CTL_CODE(const DeviceType,Func,Method,Access:Cardinal):cardinal;
begin
 Result := DeviceType shl 16 or Access shl 14 or Func shl 2 or Method;
end;


function driverinstall(const path,name:pchar):integer;
var hService: SC_HANDLE;
    hSCMan  : SC_HANDLE;
begin

  Result := 0;

  hSCMan := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if hSCMan = 0 then  begin
   result := getlasterror;
   exit;
  end;

  hService := CreateService(hSCMan, name,name,
              SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,
              SERVICE_ERROR_NORMAL, path,
              nil, nil, nil, nil, nil);

  if (hService = 0) then begin
    result := getlasterror;
    CloseServiceHandle(hSCMan);
    exit;
  end
  else
    CloseServiceHandle(hService);
  CloseServiceHandle(hSCMan);
end;

function driverstart(const name:pchar):integer;
var
  hService: SC_HANDLE;
  hSCMan  : SC_HANDLE;
  args:pchar;
begin

  hSCMan := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
  if hSCMan = 0 then  begin
   result := getlasterror;
   exit;
  end;

  // get a handle to the service
  hService := OpenService(hSCMan, name, SERVICE_START);
  if hService <> 0 then Begin
     // start the driver
    args := nil;
    Result := 0;
    if integer(StartService(hService, 0, args ))=0 then
      result := getlasterror;
    CloseServiceHandle(hService);
  end
  else
    result := getlasterror;
  CloseServiceHandle(hSCMan);
end;

function driverstop(const name:pchar):integer;
Var
  serviceStatus: TServiceStatus;
  hService: SC_HANDLE;
  hSCMan  : SC_HANDLE;
begin

  hSCMan := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
  if hSCMan = 0 then  begin
   result := getlasterror;
   exit;
  end;

  // get a handle to the service
  hService := OpenService(hSCMan, Name, SERVICE_STOP);
  if hService <> 0 then Begin
     // start the driver
    Result := 0;
    if integer(ControlService(hService, SERVICE_CONTROL_STOP, serviceStatus))=0 then
      result := getlasterror;
    CloseServiceHandle(hService);
  end
  else
    result := getlasterror;
  CloseServiceHandle(hSCMan);
end;

function driverremove(const name:pchar):integer;
Var
  hService: SC_HANDLE;
  hSCMan  : SC_HANDLE;
begin

  hSCMan := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if hSCMan = 0 then  begin
    result := getlasterror;
    exit;
  end;

  // get a handle to the service
  hService := OpenService(hSCMan, Name, SERVICE_ALL_ACCESS);
  if hService <> 0 then Begin
    // remove driver description from the registry
    Result := 0;
    if integer(DeleteService(hService)) = 0 then
      result := getlasterror;
    CloseServiceHandle(hService);
  end
  else
    result := getlasterror;
  CloseServiceHandle(hSCMan);
end;

function messagestring(const error:integer):string;
var p:pchar;
begin
  GetMem(p, 200);
  FillChar(p^, 200, 0);
  formatmessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,error,0,p,199,nil);
  Result := p;
  freemem(p,200);
end;

end.