Skip to content

Commit a4de57e

Browse files
committed
Now using EventLog instead of OutputDebugString for logging.
1 parent 12fdb72 commit a4de57e

File tree

8 files changed

+119
-99
lines changed

8 files changed

+119
-99
lines changed

Definitions.pas

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,15 @@ DRIVE_LAYOUT_INFORMATION = record
251251
Function ImScsiOpenScsiAdapter(var PortNumber:Byte):THandle;
252252
Function ImScsiDeviceIoControl(device:THandle; ControlCode: DWORD; var SrbIoControl: TSrbIoControl; Size, Timeout: DWORD; var ReturnLength: DWORD):Boolean;
253253
Function decodeException(code:TRamErrors):String;
254+
Procedure DebugLog(msg:string;eventType:DWord = EVENTLOG_INFORMATION_TYPE);
254255

255256
implementation
256257

257258
Uses Math,Classes;
258259

260+
Var
261+
EventLogHandle:Integer;
262+
259263
procedure RtlInitUnicodeString(DestinationString: PUnicodeString; SourceString: LPWSTR); stdcall; external 'ntdll.dll';
260264
function RtlNtStatusToDosError(Status: NTSTATUS): ULONG; stdcall; external 'ntdll.dll';
261265
function NtClose(_Handle: THandle): NTSTATUS; stdcall; external 'ntdll.dll';
@@ -336,11 +340,11 @@ procedure InitializeObjectAttributes(var InitializedAttributes: TObjectAttribute
336340
ImScsiInitializeSrbIoBlock(SrbIoControl, Size, ControlCode, Timeout);
337341
if Not DeviceIoControl(Device, IOCTL_SCSI_MINIPORT, @SrbIoControl, Size, @SrbIoControl, Size, ReturnLength, NIL) then
338342
begin
339-
OutputDebugString(PAnsiChar(SysErrorMessage(RtlNtStatusToDosError(SrbIoControl.ReturnCode))));
343+
DebugLog(SysErrorMessage(RtlNtStatusToDosError(SrbIoControl.ReturnCode)),EVENTLOG_ERROR_TYPE);
340344
Result:=FALSE;
341345
Exit;
342346
end;
343-
OutputDebugString(PAnsiChar(SysErrorMessage(RtlNtStatusToDosError(SrbIoControl.ReturnCode))));
347+
DebugLog(SysErrorMessage(RtlNtStatusToDosError(SrbIoControl.ReturnCode)));
344348
Result:=SrbIoControl.ReturnCode >= 0;
345349
end;
346350

@@ -466,4 +470,14 @@ function GetFreeDriveList: TAssignedDrives;
466470
end;
467471
End;
468472

473+
Procedure DebugLog(msg:string;eventType:DWord = EVENTLOG_INFORMATION_TYPE);
474+
Begin
475+
ReportEvent(EventLogHandle,eventType,0,0,Nil,1,0,PChar(msg),Nil);
476+
end;
477+
478+
Initialization
479+
EventLogHandle:=RegisterEventSource(Nil,'Arsenal RamDisk');
480+
481+
Finalization
482+
DeregisterEventSource(EventLogHandle);
469483
end.

Main.pas

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ procedure TfrmUI.btnSaveClick(Sender: TObject);
120120
end;
121121

122122
procedure TfrmUI.btnUnmountClick(Sender: TObject);
123+
var
124+
msg:String;
123125
begin
124126
try
125127
ramDiskConfig.persistentFolder:=editFolder.Text;
@@ -131,7 +133,11 @@ procedure TfrmUI.btnUnmountClick(Sender: TObject);
131133
UpdateDismounted;
132134
end;
133135
Except
134-
On E:ERamDiskError do decodeException(E.ArsenalCode);
136+
On E:ERamDiskError do
137+
Begin
138+
msg:=decodeException(E.ArsenalCode);
139+
If msg<>'' then MessageDlg(msg,mtError,[mbOK],0);
140+
end
135141
else raise;
136142
end;
137143
end;
@@ -323,6 +329,7 @@ procedure TfrmUI.btnUninstallClick(Sender: TObject);
323329
procedure TfrmUI.FormShow(Sender: TObject);
324330
Var
325331
srvStatus:DWORD;
332+
msg:String;
326333
begin
327334
// aim -a -s 50M -t vm -m x:
328335
UpdateLetters;
@@ -336,7 +343,11 @@ procedure TfrmUI.FormShow(Sender: TObject);
336343
if GetRamDisk(ramDiskConfig) Then UpdateMounted
337344
Else UpdateDismounted;
338345
Except
339-
On E:ERamDiskError do decodeException(E.ArsenalCode);
346+
On E:ERamDiskError do
347+
Begin
348+
msg:=decodeException(E.ArsenalCode);
349+
If msg<>'' then MessageDlg(msg,mtError,[mbOK],0);
350+
end
340351
else raise;
341352
End;
342353
end;

RamCreate.pas

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,21 @@ procedure HideInfo;
104104
dw: DWORD;
105105
Begin
106106
Result:=False;
107-
OutputDebugString('Trying to query the version of Arsenal driver');
107+
DebugLog('Trying to query the version of Arsenal driver');
108108
ImScsiInitializeSrbIoBlock(check.SrbIoControl, sizeof(check), SMP_IMSCSI_QUERY_VERSION, 0);
109109
if Not DeviceIoControl(Device, IOCTL_SCSI_MINIPORT, @check, sizeof(check), @check, sizeof(check), dw, NIL) then
110110
Begin
111-
OutputDebugString('Arsenal driver does not support version checking');
111+
DebugLog('Arsenal driver does not support version checking',EVENTLOG_ERROR_TYPE);
112112
Exit;
113113
end;
114114
if dw < sizeof(check) then
115115
Begin
116-
OutputDebugString(PAnsiChar(Format('Arsenal driver reports the size of data structure for version check as %u which is less than expected %u',[dw,SizeOf(check)])));
116+
DebugLog(Format('Arsenal driver reports the size of data structure for version check as %u which is less than expected %u',[dw,SizeOf(check)]),EVENTLOG_ERROR_TYPE);
117117
Exit;
118118
end;
119119
if check.SrbIoControl.ReturnCode < IMSCSI_DRIVER_VERSION Then
120120
Begin
121-
OutputDebugString(PAnsiChar(Format('Arsenal driver reports version %u which is less than required %u',[check.SrbIoControl.ReturnCode,IMSCSI_DRIVER_VERSION])));
121+
DebugLog(Format('Arsenal driver reports version %u which is less than required %u',[check.SrbIoControl.ReturnCode,IMSCSI_DRIVER_VERSION]),EVENTLOG_ERROR_TYPE);
122122
Exit;
123123
end;
124124
Result:=True;
@@ -177,16 +177,16 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
177177
mustFormat, formatDone, mount_point_found:Boolean;
178178
Begin
179179
Result:=False;
180-
OutputDebugString('Trying to create a new RAM-disk');
180+
DebugLog('Trying to create a new RAM-disk');
181181
driver := ImScsiOpenScsiAdapter(portNumber);
182182
if driver = INVALID_HANDLE_VALUE then
183183
Begin
184-
OutputDebugString('Arsenal driver is not running');
184+
DebugLog('Arsenal driver is not running',EVENTLOG_ERROR_TYPE);
185185
Exit;
186186
end;
187187
if not ImScsiCheckDriverVersion(driver) then
188188
begin
189-
OutputDebugString('Arsenal driver version is not suitable');
189+
DebugLog('Arsenal driver version is not suitable',EVENTLOG_ERROR_TYPE);
190190
CloseHandle(driver);
191191
Raise ERamDiskError.Create(RamDriverVersion);
192192
end;
@@ -195,7 +195,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
195195
if not ImScsiDeviceIoControl(driver, SMP_IMSCSI_CREATE_DEVICE, create_data.SrbIoControl, SizeOf(create_data), 0, dw) then
196196
begin
197197
NtClose(driver);
198-
OutputDebugString(PAnsiChar(Format('Could not create the RAM-disk, error is "%s"',[SysErrorMessage(GetLastError)])));
198+
DebugLog(Format('Could not create the RAM-disk, error is "%s"',[SysErrorMessage(GetLastError)]),EVENTLOG_ERROR_TYPE);
199199
raise ERamDiskError.Create(RamCantCreate);
200200
end;
201201
NtClose(driver);
@@ -207,7 +207,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
207207

208208
while true do
209209
begin
210-
OutputDebugString('Disk not attached yet, waiting 200 msec');
210+
DebugLog('Disk not attached yet, waiting 200 msec');
211211
disk := ImScsiOpenDiskByDeviceNumber(create_data.Fields.DeviceNumber, portNumber, diskNumber);
212212
if disk <> INVALID_HANDLE_VALUE then Break;
213213
//printf("Disk not attached yet, waiting... %c\r", NextWaitChar(&wait_char));
@@ -220,7 +220,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
220220
begin
221221
while WaitForSingleObject(event, 200) = WAIT_TIMEOUT do
222222
begin
223-
OutputDebugString('Rescanning SCSI adapters, disk not attached yet. Waiting 200 msec');
223+
DebugLog('Rescanning SCSI adapters, disk not attached yet. Waiting 200 msec');
224224
// printf("Disk not attached yet, waiting... %c\r", NextWaitChar(&wait_char));
225225
end;
226226
CloseHandle(event);
@@ -234,7 +234,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
234234
if disk = INVALID_HANDLE_VALUE then
235235
begin
236236
dw:=GetLastError;
237-
OutputDebugString(PAnsiChar('Error reopening for writing ' + devPath + ': ' + SysErrorMessage(dw)));
237+
DebugLog('Error reopening for writing ' + devPath + ': ' + SysErrorMessage(dw),EVENTLOG_ERROR_TYPE);
238238
raise ERamDiskError.Create(RamNotAccessible);
239239
end;
240240

@@ -243,7 +243,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
243243
if not DeviceIoControl(disk, IOCTL_DISK_SET_DISK_ATTRIBUTES, @disk_attributes, sizeof(disk_attributes), NIL, 0, dw, NIL)
244244
And (GetLastError <> ERROR_INVALID_FUNCTION) then
245245
begin
246-
OutputDebugString('Cannot set disk in writable online mode');
246+
DebugLog('Cannot set disk in writable online mode',EVENTLOG_ERROR_TYPE);
247247
end;
248248
DeviceIoControl(disk, FSCTL_ALLOW_EXTENDED_DASD_IO, NIL, 0, NIL, 0, dw, NIL);
249249

@@ -253,19 +253,19 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
253253
begin
254254
if disk_size <> config.size then
255255
begin
256-
OutputDebugString(PAnsiChar('Disk ' + devPath + ' has unexpected size: ' + IntToStr(disk_size)));
256+
DebugLog('Disk ' + devPath + ' has unexpected size: ' + IntToStr(disk_size),EVENTLOG_ERROR_TYPE);
257257
mustFormat := False;
258258
end;
259259
end
260260
else if GetLastError <> ERROR_INVALID_FUNCTION then
261261
begin
262262
dw:=GetLastError;
263-
OutputDebugString(PAnsiChar('Can not query size of disk ' + devPath + ': ' + SysErrorMessage(dw)));
263+
DebugLog('Can not query size of disk ' + devPath + ': ' + SysErrorMessage(dw),EVENTLOG_ERROR_TYPE);
264264
mustFormat := False;
265265
end;
266266
if mustFormat then
267267
begin
268-
OutputDebugString('Will now create a partition on the RAM device');
268+
DebugLog('Will now create a partition on the RAM device');
269269
rand_seed := GetTickCount();
270270
while true do
271271
begin
@@ -284,7 +284,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
284284

285285
if DeviceIoControl(disk, IOCTL_DISK_SET_DRIVE_LAYOUT, @drive_layout, sizeof(drive_layout), NIL, 0, dw, NIL) then
286286
Begin
287-
OutputDebugString('Successfully created the partition');
287+
DebugLog('Successfully created the partition');
288288
Break;
289289
end;
290290
if GetLastError <> ERROR_WRITE_PROTECT then
@@ -294,7 +294,7 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
294294
end;
295295

296296
//printf("Disk not yet ready, waiting... %c\r", NextWaitChar(&wait_char));
297-
OutputDebugString('Disk is not yet ready for partitioning, waiting ...');
297+
DebugLog('Disk is not yet ready for partitioning, waiting ...');
298298

299299
ZeroMemory(@disk_attributes, sizeof(disk_attributes));
300300
disk_attributes.AttributesMask := DISK_ATTRIBUTE_OFFLINE or DISK_ATTRIBUTE_READ_ONLY;
@@ -305,18 +305,18 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
305305
end;
306306

307307
if not DeviceIoControl(disk, IOCTL_DISK_UPDATE_PROPERTIES, NIL, 0, NIL, 0, dw, NIL)
308-
And (GetLastError <> ERROR_INVALID_FUNCTION) then OutputDebugString('Error updating disk properties');
308+
And (GetLastError <> ERROR_INVALID_FUNCTION) then DebugLog('Error updating disk properties',EVENTLOG_ERROR_TYPE);
309309
CloseHandle(disk);
310310
start_time := GetTickCount();
311311
formatDone := false;
312312
numVolumes:=0;
313313
while true do
314314
begin
315-
OutputDebugString('Trying to find the volume (partition) by name');
315+
DebugLog('Trying to find the volume (partition) by name');
316316
volume := FindFirstVolume(volumeName, Length(volumeName));
317317
if volume = INVALID_HANDLE_VALUE then
318318
begin
319-
OutputDebugString('Error enumerating disk volumes');
319+
DebugLog('Error enumerating disk volumes',EVENTLOG_ERROR_TYPE);
320320
raise ERamDiskError.Create(RamCantEnumDrives);
321321
End;
322322

@@ -325,26 +325,26 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
325325
try
326326
repeat
327327
volumeName[48] := #0;
328-
OutputDebugString(PAnsiChar(Format('Quering volume %s',[volumeName])));
328+
DebugLog(Format('Quering volume %s',[volumeName]));
329329
volHandle := CreateFile(volumeName, 0, FILE_SHARE_READ or FILE_SHARE_WRITE, NIL, OPEN_EXISTING, 0, 0);
330330
if volHandle = INVALID_HANDLE_VALUE then Continue;
331331
if not ImScsiVolumeUsesDisk(volHandle, diskNumber) then
332332
begin
333333
CloseHandle(volHandle);
334-
OutputDebugString('This volume is not used (created) by Arsenal');
334+
DebugLog('This volume is not used (created) by Arsenal');
335335
continue;
336336
end;
337337

338338
CloseHandle(volHandle);
339339
Inc(numVolumes);
340340

341341
volHandle := CreateFile(volumeName, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, NIL, OPEN_EXISTING, 0, 0);
342-
if volHandle = INVALID_HANDLE_VALUE then OutputDebugString('Error opening volume in read/write mode')
342+
if volHandle = INVALID_HANDLE_VALUE then DebugLog('Error opening volume in read/write mode',EVENTLOG_ERROR_TYPE)
343343
else
344344
begin
345345
if Not DeviceIoControl(volHandle, IOCTL_VOLUME_ONLINE, NIL, 0, NIL, 0, dw, NIL) then
346346
begin
347-
OutputDebugString('Error setting volume in online mode');
347+
DebugLog('Error setting volume in online mode',EVENTLOG_ERROR_TYPE);
348348
end;
349349
CloseHandle(volHandle);
350350
end;
@@ -358,14 +358,14 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
358358
// we use the undocumented FMIFS.DLL instead of Format.COM or VDS or WMI or ShFormatDrive - it always takes at least 5 seconds
359359
formatDriveName:=volumeName;
360360
FormatEx(PWideChar(formatDriveName),FMIFS_HARDDISK,'NTFS','RAMDISK',True,4096,@FormatCallBack);
361-
OutputDebugString('Successfully created NTFS filesystem on the RAM-disk');
361+
DebugLog('Successfully created NTFS filesystem on the RAM-disk');
362362
if ShowProgress then HideInfo;
363363
end;
364364

365365
volumeName[48] := '\';
366366
if Not GetVolumePathNamesForVolumeName(volumeName, mountName, Length(mountName), dw) then
367367
begin
368-
OutputDebugString(PAnsiChar(Format('Error enumerating mount points for volume %s',[volumeName])));
368+
DebugLog(Format('Error enumerating mount points for volume %s',[volumeName]),EVENTLOG_ERROR_TYPE);
369369
continue;
370370
end;
371371

@@ -375,21 +375,21 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
375375
mountList.Text:=mountName;
376376
for i:=0 to mountList.Count-1 do
377377
begin
378-
OutputDebugString(PAnsiChar(Format('Now trying to get a drive letter for "%s"',[mountList[i]])));
378+
DebugLog(Format('Now trying to get a drive letter for "%s"',[mountList[i]]));
379379
if mountList[i] = '' then Break;
380380
if CompareText(mountPoint,mountList[i])<>0 then
381381
begin
382-
OutputDebugString('Removing the old mount point');
382+
DebugLog('Removing the old mount point');
383383
if Not DeleteVolumeMountPoint(PAnsiChar(mountList[i])) then
384384
begin
385385
dw:=GetLastError;
386-
OutputDebugString(PAnsiChar('Error removing old mount point "'+mountList[i]+'": ' + SysErrorMessage(dw)));
386+
DebugLog('Error removing old mount point "'+mountList[i]+'": ' + SysErrorMessage(dw),EVENTLOG_ERROR_TYPE);
387387
end;
388388
end
389389
else
390390
begin
391391
mount_point_found := true;
392-
OutputDebugString(PAnsiChar(Format('Mounted at %s',[mountPoint])));
392+
DebugLog(Format('Mounted at %s',[mountPoint]));
393393
// ImScsiOemPrintF(stdout, " Mounted at %1!ws!", mnt);
394394
end;
395395
end;
@@ -400,12 +400,12 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
400400
MountPoint[1] := ImDiskFindFreeDriveLetter();
401401
if MountPoint[1] = #0 then raise ERamDiskError.Create(RamNoFreeLetter)
402402
Else config.letter:=MountPoint[1];
403-
OutputDebugString(PAnsiChar('Will use drive letter ' + MountPoint[1]));
403+
DebugLog('Will use drive letter ' + MountPoint[1]);
404404
end;
405405
if not SetVolumeMountPoint(PAnsiChar(MountPoint), volumeName) then
406406
begin
407407
dw:=GetLastError;
408-
OutputDebugString(PAnsiChar('Error setting volume ' + volumeName + ' mount point to ' + MountPoint + ' : ' + SysErrorMessage(dw)));
408+
DebugLog('Error setting volume ' + volumeName + ' mount point to ' + MountPoint + ' : ' + SysErrorMessage(dw),EVENTLOG_ERROR_TYPE);
409409
end
410410
else Break;
411411
//MountPoint := '';
@@ -419,12 +419,12 @@ function FormatCallback (Command: TCallBackCommand; SubAction: DWORD; ActionInfo
419419
if formatDone or (numVolumes > 0) then break;
420420
if not mustFormat and ((GetTickCount() - start_time) > 3000) then
421421
begin
422-
OutputDebugString('No volumes attached. Disk could be offline or not partitioned.');
422+
DebugLog('No volumes attached. Disk could be offline or not partitioned.',EVENTLOG_ERROR_TYPE);
423423
break;
424424
end;
425425

426426
//printf("Volume not yet attached, waiting... %c\r", NextWaitChar(&wait_char));
427-
OutputDebugString('Volume not yet attached, waiting 200 msec');
427+
DebugLog('Volume not yet attached, waiting 200 msec');
428428
Sleep(200);
429429
end;
430430
LoadRamDisk(config);

0 commit comments

Comments
 (0)