-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvolume.pas
93 lines (70 loc) · 3.15 KB
/
volume.pas
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
unit volume;
{$MODE Delphi}
interface
uses Windows, LCLIntf, LCLType, LMessages, sysutils;
const
kernel32 = 'kernel32.dll';
type
TFindFirstVolume = function (lpszVolumeName : PAnsiChar; cchBufferLength : DWORD): THANDLE; stdcall;
TFindNextVolume = function (hFindVolume : THANDLE; lpszVolumeName : PAnsiChar; cchBufferLength : DWORD): BOOL; stdcall;
TFindVolumeClose = function (hFindVolume : THANDLE): BOOL; stdcall;
TFindFirstVolumeMountPoint = function (lpszRootPathName : PAnsiChar; lpszVolumeMountPoint : PAnsiChar; cchBufferLength : DWORD): THANDLE; stdcall;
TFindNextVolumeMountPoint = function (hFindVolumeMountPoint : THANDLE; lpszVolumeMountPoint : PAnsiChar; cchBufferLength : DWORD): BOOL; stdcall;
TFindVolumeMountPointClose = function (hFindVolumeMountPoint : THANDLE): BOOL; stdcall;
TGetVolumeNameForVolumeMountPoint = function (lpszVolumeMountPoint : PAnsiChar; lpszVolumeName : PAnsiChar; cchBufferLength : DWORD): BOOL; stdcall;
TDeleteVolumeMountPoint = function (lpszVolumeMountPoint : PAnsiChar) : BOOL; stdcall;
TSetVolumeMountPoint = function (lpszVolumeMountPoint : PAnsiChar; lpszVolumeName : PAnsiChar) : BOOL; stdcall;
procedure LoadVolume;
var
JFindFirstVolume : TFindFirstVolume;
JFindNextVolume : TFindNextVolume;
JFindVolumeClose : TFindVolumeClose;
JFindFirstVolumeMountPoint : TFindFirstVolumeMountPoint;
JFindNextVolumeMountPoint : TFindNextVolumeMountPoint;
JFindVolumeMountPointClose : TFindVolumeMountPointClose;
JGetVolumeNameForVolumeMountPoint : TGetVolumeNameForVolumeMountPoint;
JDeleteVolumeMountPoint : TDeleteVolumeMountPoint;
JSetVolumeMountPoint : TSetVolumeMountPoint;
implementation
var
VolumeLoaded : Boolean = False;
procedure LoadVolume;
var
hModule : hInst;
Error : DWORD;
function GetAddress(ProcName : PChar) : Pointer;
begin
Result := GetProcAddress(hModule, ProcName);
if Result = nil then
begin
raise Exception.Create('Could not find procedure ' + ProcName);
end;
end;
begin
if VolumeLoaded then exit;
// Load WinINET...
hModule := GetModuleHandle(kernel32);
if hModule = 0 then
begin
// kernel32 is not yet loaded... (unlkely but...)
hModule := LoadLibrary(kernel32);
if hModule = 0 then
begin
Error := GetLastError;
raise Exception.Create('Error loading kernel32 Library. ' + SysErrorMessage(Error));
end;
end;
// by here we have an hModule or have raised an exception
// Map the function addresses...
JFindFirstVolume := GetAddress('FindFirstVolumeA');
JFindNextVolume := GetAddress('FindNextVolumeA');
JFindVolumeClose := GetAddress('FindVolumeClose');
JFindFirstVolumeMountPoint := GetAddress('FindFirstVolumeMountPointA');
JFindNextVolumeMountPoint := GetAddress('FindNextVolumeMountPointA');
JFindVolumeMountPointClose := GetAddress('FindVolumeMountPointClose');
JGetVolumeNameForVolumeMountPoint := GetAddress('GetVolumeNameForVolumeMountPointA');
JDeleteVolumeMountPoint := GetAddress('DeleteVolumeMountPointA');
JSetVolumeMountPoint := GetAddress('SetVolumeMountPointA');
VolumeLoaded := True;
end;
end.