-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathenumerate-network-share-via-NetShareEnum.pas
More file actions
202 lines (162 loc) · 4.46 KB
/
enumerate-network-share-via-NetShareEnum.pas
File metadata and controls
202 lines (162 loc) · 4.46 KB
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
uses
System.SysUtils, Winapi.Windows, Generics.Collections;
const
MAX_PREFERRED_LENGTH = DWORD(-1);
NERR_Success = 0;
STYPE_DISKTREE = $00000000;
STYPE_PRINTQ = $00000001;
STYPE_DEVICE = $00000002;
STYPE_IPC = $00000003;
STYPE_TEMPORARY = $40000000;
STYPE_SPECIAL = $80000000;
STYPE_MASK = $0000000F;
// ...
type
NET_API_STATUS = DWORD;
_SHARE_INFO_2 = record
shi2_netname : PWideChar;
shi2_type : DWORD;
shi2_remark : PWideChar;
shi2_permissions : DWORD; // Deprecated
shi2_max_uses : DWORD;
shi2_current_uses : DWORD;
shi2_path : PWideChar;
shi2_passwd : PWideChar;
end;
TShareInfo2 = _SHARE_INFO_2;
PShareInfo2 = ^TShareInfo2;
// ...
function NetShareEnum(
servername : PWideChar;
level : DWORD;
var bufptr : Pointer;
prefmaxlen : DWORD;
var entriedread : DWORD;
var totalentries : DWORD;
var resume_handle : DWORD
) : NET_API_STATUS; stdcall; external 'Netapi32.dll';
function NetApiBufferFree(
Buffer : Pointer
) : NET_API_STATUS; stdcall; external 'Netapi32.dll';
// ...
type
TNetworkShare = class
private
FName : String;
FRemark : String;
FPath : String;
FPasswd : String;
FType : DWORD;
FPermissions : DWORD;
FMaxUses : DWORD;
FCurrentUses : DWORD;
{@M}
function GetReadableType() : String;
public
{@C}
constructor Create(const AShareInfo2: TShareInfo2);
{@G}
property Name : String read FName;
property Remark : String read FRemark;
property Path : String read FPath;
property Passwd : String read FPasswd;
property ShareType : DWORD read FType;
property ReadableType : String read GetReadableType;
property Permissions : DWORD read FPermissions;
property MaxUses : DWORD read FMaxUses;
property CurrentUses : DWORD read FCurrentUses;
end;
// ...
(* TNetworkShare *)
constructor TNetworkShare.Create(const AShareInfo2: TShareInfo2);
begin
inherited Create();
///
FName := string(AShareInfo2.shi2_netname);
FRemark := string(AShareInfo2.shi2_remark);
FPath := string(AShareInfo2.shi2_path);
FPasswd := string(AShareInfo2.shi2_passwd);
FType := AShareInfo2.shi2_type;
FPermissions := AShareInfo2.shi2_permissions;
FMaxUses := AShareInfo2.shi2_max_uses;
FCurrentUses := AShareInfo2.shi2_current_uses;
end;
function TNetworkShare.GetReadableType(): string;
begin
case FType and STYPE_MASK of
STYPE_DISKTREE : result := 'Disk';
STYPE_PRINTQ : result := 'Print Queue';
STYPE_DEVICE : result := 'Communication Device';
STYPE_IPC : result := 'IPC';
STYPE_SPECIAL : result := 'Special';
STYPE_TEMPORARY : result := 'Temporary';
else
result := 'Unknown';
end;
end;
// ...
(* Local *)
function EnumerateNetworkShares(var AList : TObjectList<TNetworkShare>) : Cardinal;
begin
result := 0;
///
if not Assigned(AList) then
AList := TObjectList<TNetworkShare>.Create(True)
else begin
if not AList.OwnsObjects then
AList.OwnsObjects := True;
///
AList.Clear();
end;
///
var pShareInfo : PShareInfo2;
var ANumberOfEntries : DWORD;
var AEntriesRead : DWORD;
var AResumeHandle : DWORD := 0;
const LEVEL_2 = 2;
var AStatus := NetShareEnum(nil, LEVEL_2, Pointer(pShareInfo), MAX_PREFERRED_LENGTH, AEntriesRead, ANumberOfEntries, AResumeHandle);
try
if AStatus <> NERR_Success then
Exit();
///
var pShareInfoRow := pShareInfo;
for var I := 0 to ANumberOfEntries -1 do begin
AList.Add(TNetworkShare.Create(pShareInfoRow^));
///
Inc(pShareInfoRow);
end;
///
result := AList.Count;
finally
if pShareInfo <> nil then
NetApiBufferFree(pShareInfo);
end;
end;
// ...
var AList := TObjectList<TNetworkShare>.Create(True);
try
EnumerateNetworkShares(AList);
///
const NETWORK_SHARE_ROW_MASK = '%-10s | %-20s | %-10s | %s';
const ALine = StringOfChar('-', 80);
WriteLn(ALine);
WriteLn(Format(NETWORK_SHARE_ROW_MASK, [
'Name',
'Path',
'Type',
'Description'
]));
WriteLn(ALine);
for var ANetworkShare in AList do begin
WriteLn(Format(NETWORK_SHARE_ROW_MASK, [
ANetworkShare.Name,
ANetworkShare.Path,
ANetworkShare.ReadableType,
ANetworkShare.Remark
]));
end;
WriteLn(ALine);
finally
FreeAndNil(AList);
end;
// ...