Ik heb een klasse gemaakt. Maar zodra het programma gesloten worden, veroorzaakt het een foutmelding:

Het is misschien een beetje veel code om hier te plaatsen maar zo is het wel duidelijker.
Maar wat is de oorzaak? Ik zou niet weten wat ik fout doe?

Het is misschien een beetje veel code om hier te plaatsen maar zo is het wel duidelijker.
Maar wat is de oorzaak? Ik zou niet weten wat ik fout doe?
code:
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
| unit IOAccess;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Winsvc, IOCTLCodes;
type
TIOAccess = class(TComponent)
private
FWindowsNT: Boolean;
FDriverName: AnsiString;
FDriverDisplayName: AnsiString;
FDriverGroupName: AnsiString;
FDriverPath: AnsiString;
FDriverFullPath: AnsiString;
FDriverSourcePath: AnsiString;
FIOPMAccess: Boolean;
FDriverStarted: Boolean;
FDriverInstalled: Boolean;
FhndDeviceFile: THandle;
function InstallDriver: Integer;
function CopyDriver: Integer;
function StartDriver: Integer;
procedure SetIOPMAccess(OnFlag: Boolean);
function OpenDeviceFile(var hndFile: THandle): Integer;
protected
public
constructor Create(Owner : TComponent); override;
destructor Destroy; override;
procedure WritePortByte(Port: DWORD; Data: Byte);
procedure WritePortWord(Port: DWORD; Data: Word);
procedure WritePortDWord(Port: DWORD; Data: DWord);
function ReadPortByte(Port: DWORD): Byte;
function ReadPortWord(Port: DWORD): Word;
function ReadPortDWord(Port: DWORD): DWORD;
function StartDeviceDriver: Integer;
function StopDriver: Integer;
function InstallDeviceDriver: Integer;
function UninstallDriver: Integer;
property PortB[Port: DWORD]: Byte read ReadPortByte write WritePortByte;
property PortW[Port: DWORD]: Word read ReadPortWord write WritePortWord;
property PortDW[Port: DWORD]: DWORD read ReadPortDWord write WritePortDWord;
published
property IOPMAccess: Boolean read FIOPMAccess write SetIOPMAccess default False;
end;
procedure Register;
implementation
function TIOAccess.InstallDriver: Integer;
var hSCMan, hService: SC_HANDLE;
begin
Result := STATUS_SUCCESS;
// Try to open the Service Control Manager
hSCMan := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if hSCMan = 0 then
begin
Result := GetLastError;
Exit;
end;
// Check if the driver is already in the Service Control Manager
hService := OpenService(hSCMan, PChar(FDriverName), SERVICE_QUERY_STATUS);
if hService <> 0 then
begin
// The driver is already in the Service Control Manager, so it don't have
// to be installed
CloseServiceHandle(hService);
CloseServiceHandle(hSCMan);
Exit;
end;
hService := CreateService(hSCMan, PChar(FDriverName),
PChar(FDriverDisplayName), SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL, PChar(FDriverFullPath),
nil, nil, nil, nil, nil);
if hService = 0 then
begin
Result := GetLastError;
Exit;
end
else
begin
CloseServiceHandle(hService);
end;
CloseServiceHandle(hSCMan);
Result := STATUS_SUCCESS;
end;
function TIOAccess.UninstallDriver: Integer;
var hSCMan, hService: SC_HANDLE;
ServiceStatus: TServiceStatus;
begin
hSCMan := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if hSCMan = 0 then
begin
Result := GetLastError;
Exit;
end;
hService := OpenService(hSCMan, PChar(FDriverName), SERVICE_ALL_ACCESS);
if hService = 0 then
Result := GetLastError
else
begin
if not Bool(DeleteService(hService)) then
Result := GetLastError;
CloseServiceHandle(hService);
end;
CloseServiceHandle(hSCMan);
end;
function TIOAccess.CopyDriver: Integer;
var FDriverPath, DeviceDriverPath: AnsiString;
begin
Result := STATUS_SUCCESS;
CreateDirectory(PChar(FDriverPath), nil);
if not CopyFile(PChar(ExtractFileDir(Paramstr(0)) + '\IOAccess.sys'),
StrCat(PChar(FDriverPath), PChar('\IOAccess.sys')), False) then
Result := GetLastError;
end;
function TIOAccess.StartDriver: Integer;
var hService, hSCMan: SC_HANDLE;
ServiceArgVectors: PChar;
begin
Result := STATUS_SUCCESS;
hSCMan := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if hSCMan = 0 then
begin
Result := GetLastError;
Exit;
end;
hService := OpenService(hSCMan, PChar(FDriverName), SERVICE_START);
if hService = 0 then
begin
Result := GetLastError;
end
else
begin
ServiceArgVectors := nil;
if Integer(StartService(hService, 0, ServiceArgVectors)) = 0 then
Result := GetLastError;
CloseServiceHandle(hService);
end;
CloseServiceHandle(hSCMan);
end;
function TIOAccess.StopDriver: Integer;
var hService, hSCMan: SC_HANDLE;
ServiceStatus: TServiceStatus;
begin
Result := STATUS_SUCCESS;
hSCMan := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if hSCMan = 0 then
begin
Result := GetLastError;
Exit;
end;
hService := OpenService(hSCMan, PChar(FDriverName), SERVICE_STOP);
if hService = 0 then
begin
Result := GetLastError;
end
else
begin
if not Bool(ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)) then
Result := GetLastError;
CloseServiceHandle(hService);
end;
CloseServiceHandle(hSCMan);
end;
procedure TIOAccess.SetIOPMAccess(OnFlag: Boolean);
var IoctlCode: Cardinal;
Status: Integer;
Port32Struct: TIOAccessPortData;
IoctlResult: Boolean;
ReturnedLength: Cardinal;
begin
if FWindowsNT then
begin
if OnFlag then
IoctlCode := IOCTL_IOACCESS_IOPM_ON
else
IoctlCode := IOCTL_IOACCESS_IOPM_OFF;
FIOPMAccess := OnFlag;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @Port32Struct,
SizeOf(Port32Struct), nil, 0, ReturnedLength, nil);
end;
end;
function TIOAccess.OpenDeviceFile(var hndFile: THandle): Integer;
begin
Result := STATUS_SUCCESS;
hndFile := CreateFile(PChar('\\.\' + FDriverName), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,
0, 0);
if hndFile = INVALID_HANDLE_VALUE then
Result := GetLastError;
if GetLastError = ERROR_ALREADY_EXISTS then
Result := STATUS_SUCCESS;
end;
constructor TIOAccess.Create(Owner: TComponent);
var OS: TOSVersionInfo;
Buffer: array[1..MAX_PATH] of char;
begin
inherited Create(Owner);
// Check if the Operation System is Windows NT/2000. If the OS is
// Windows NT/2000 the driver have to be used.
OS.dwPlatformId := 0;
OS.dwOSVersionInfoSize := SizeOf(OSVERSIONINFO);
GetVersionEx(OS);
FWindowsNT := (OS.dwPlatformId=VER_PLATFORM_WIN32_NT);
FDriverName := 'IOAccess';
FDriverDisplayName := 'IOAccess Device Driver';
FDriverGroupName := 'I/O Port Drivers';
GetSystemDirectory(@Buffer, MAX_PATH);
FDriverPath := StrCat(@Buffer, PChar('\DRIVERS'));
FDriverFullPath := StrCat(PChar(FDriverPath), PChar('\' + FDriverName + '.sys'));
end;
destructor TIOAccess.Destroy;
begin
if FDriverStarted then
begin
if FhndDeviceFile <> 0 then
CloseHandle(FhndDeviceFile);
StopDriver;
end;
inherited Destroy;
end;
procedure TIOAccess.WritePortByte(Port: DWORD; Data: Byte);
var Port32Struct: TIOAccessPortData;
IoctlResult: Boolean;
IoctlCode: Cardinal;
DataLength: DWORD;
ReturnedLength: Cardinal;
Status: Integer;
begin
// First check if the OS is Windows NT/2000 or the program has access to IO
IoctlResult := False;
if (not FWindowsNT) or IOPMAccess then
begin
ASM
MOV EDX, Port;
MOV AL, Data;
OUT DX, AL;
end;
Exit;
end;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
Port32Struct.Port := Port;
Port32Struct.Data := Data;
Port32Struct.DataType := 1;
IoctlCode := IOCTL_IOACCESS_WRITE_PORT_BYTE;
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @Port32Struct,
SizeOf(Port32Struct), nil, 0, ReturnedLength, nil);
end;
procedure TIOAccess.WritePortWord(Port: DWORD; Data: Word);
var Port32Struct: TIOAccessPortData;
IoctlResult: Boolean;
IoctlCode: Cardinal;
DataLength: DWORD;
ReturnedLength: Cardinal;
Status: Integer;
begin
// First check if the OS is Windows NT/2000 or the program has access to IO
if (not FWindowsNT) or FIOPMAccess then
begin
ASM
MOV EDX, Port;
MOV AX, Data;
OUT DX, AX;
end;
Exit;
end;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
Port32Struct.Port := Port;
Port32Struct.Data := Data;
Port32Struct.DataType := 2;
IoctlCode := IOCTL_IOACCESS_WRITE_PORT_WORD;
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @Port32Struct,
SizeOf(TIOAccessPortData ), nil, 0, ReturnedLength, nil);
end;
procedure TIOAccess.WritePortDWord(Port: DWORD; Data: DWord);
var Port32Struct: TIOAccessPortData;
IoctlResult: Boolean;
IoctlCode: Cardinal;
DataLength: DWORD;
ReturnedLength: Cardinal;
Status: Integer;
begin
// First check if the OS is Windows NT/2000 or the program has access to IO
if (not FWindowsNT) or FIOPMAccess then
begin
ASM
MOV EDX, Port;
MOV EAX, Data;
OUT DX, EAX;
end;
Exit;
end;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
Port32Struct.Port := Port;
Port32Struct.Data := Data;
Port32Struct.DataType := 3;
IoctlCode := IOCTL_IOACCESS_WRITE_PORT_DWORD;
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @Port32Struct,
SizeOf(TIOAccessPortData ), nil, 0, ReturnedLength, nil);
end;
function TIOAccess.ReadPortByte(Port: DWORD): Byte;
var IoctlResult: Boolean;
IoctlCode: Cardinal;
DataLength: DWORD;
ReturnedLength: Cardinal;
Status: Integer;
PortNumber: DWORD;
DataBuffer: TIOAccessPortData;
BytesReturned, test: DWORD;
begin
// First check if the OS is Windows NT/2000 or the program has access to IO
if (not FWindowsNT) or IOPMAccess then
begin
ASM
MOV EDX, Port;
XOR EAX, EAX;
IN AL, DX;
MOV @Result, AL;
end;
Exit;
end;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
DataBuffer.Port := Port;
DataBuffer.DataType := 1;
IoctlCode := IOCTL_IOACCESS_READ_PORT_BYTE;
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @DataBuffer,
sizeof(DataBuffer), @DataBuffer, sizeof(DataBuffer),
BytesReturned, nil);
if IoctlResult then
Result := Byte(DataBuffer.Data)
else
Result := 0;
end;
function TIOAccess.ReadPortWord(Port: DWORD): Word;
var IoctlResult: Boolean;
IoctlCode: Cardinal;
DataLength: DWORD;
ReturnedLength: Cardinal;
Status: Integer;
PortNumber: DWORD;
DataBuffer: TIOAccessPortData;
BytesReturned: DWORD;
begin
// First check if the OS is Windows NT/2000 or the program has access to IO
if (not FWindowsNT) or FIOPMAccess then
begin
ASM
MOV EDX, Port;
XOR EAX, EAX;
IN AX, DX;
MOV Result, AX;
end;
Exit;
end;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
DataBuffer.Port := Port;
DataBuffer.DataType := 2;
IoctlCode := IOCTL_IOACCESS_READ_PORT_WORD;
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @DataBuffer,
sizeof(TIOAccessPortData), @Databuffer, sizeof(TIOAccessPortData),
BytesReturned, nil);
if IoctlResult then
Result := Word(DataBuffer.Data)
else
Result := 0;
end;
function TIOAccess.ReadPortDWord(Port: DWORD): DWORD;
var IoctlResult: Boolean;
IoctlCode: Cardinal;
DataLength: DWORD;
ReturnedLength: Cardinal;
Status: Integer;
PortNumber: DWORD;
DataBuffer: TIOAccessPortData;
BytesReturned: DWORD;
begin
// First check if the OS is Windows NT/2000 or the program has access to IO
if (not FWindowsNT) or FIOPMAccess then
begin
ASM
MOV EDX, Port;
XOR EAX, EAX;
IN EAX, DX;
MOV Result, EAX;
end;
Exit;
end;
Status := StartDeviceDriver;
if Status <> STATUS_SUCCESS then
Exit; // Driver is not installed or is not ready
DataBuffer.Port := Port;
DataBuffer.DataType := 3;
IoctlCode := IOCTL_IOACCESS_READ_PORT_DWORD;
IoctlResult := DeviceIoControl(FhndDeviceFile, IoctlCode, @DataBuffer,
sizeof(TIOAccessPortData), @Databuffer, sizeof(TIOAccessPortData),
BytesReturned, nil);
if IoctlResult then
Result := DataBuffer.Data
else
Result := 0;
end;
function TIOAccess.StartDeviceDriver:Integer;
var Status: Integer;
begin
Result := STATUS_SUCCESS;
if FWindowsNT then
begin
if not FDriverStarted then
begin
Result := StartDriver;
if Result <> STATUS_SUCCESS then
begin
//MessageBox(Application.Handle, PChar('Driver kan niet worden gestart'), PChar('Bericht'), MB_OK);
Exit; // The driver can't be started
end
else
begin
//MessageBox(Application.Handle, PChar('Driver is gestart'), PChar('Bericht'), MB_OK)
end;
Status := OpenDeviceFile(FhndDeviceFile);
// if Status = STATUS_SUCCESS then
//MessageBox(Application.Handle, PChar('Device file is geopend'), PChar('Bericht'), MB_OK)
// else
//MessageBox(Application.Handle, PChar('Device file is NIET GEOPEND'), PChar('Bericht'), MB_OK);
FDriverStarted := (Status = STATUS_SUCCESS);
end;
end;
end;
function TIOAccess.InstallDeviceDriver: Integer;
begin
Result := STATUS_SUCCESS;
if FWindowsNT then
begin
if (not FileExists(FDriverFullPath)) then
begin
Result := CopyDriver;
if Result <> STATUS_SUCCESS then // Some error occured by copying the
Exit; // driver to the system
end;
Result := InstallDriver; // You need Administrator access rights
if Result <> STATUS_SUCCESS then
begin
//MessageBox(Application.Handle, PChar('Driver kan niet worden geinstalleerd'), PChar('Bericht'), MB_OK);
Exit; // The driver can't be installed
end
else
begin
//MessageBox(Application.Handle, PChar('Driver is geinstalleerd'), PChar('Bericht'), MB_OK);
end;
end;
end;
procedure Register;
begin
RegisterComponents('IOAccess', [TIOAccess]);
end;
end. |