Last active
November 24, 2017 13:09
-
-
Save UweRaabe/d647feab54f8db2a7a6a371a8d7c06e0 to your computer and use it in GitHub Desktop.
check if network connection is permanent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function IsPermanentConnection(const ALocalName: string): Boolean; | |
type | |
PNetResourceArray = ^TNetResourceArray; | |
TNetResourceArray = array [0 .. MaxInt div SizeOf(TNetResource) - 1] of TNetResource; | |
var | |
I, BufSize, NetResult: Integer; | |
Count, Size: LongWord; | |
NetHandle: THandle; | |
NetResources: PNetResourceArray; | |
begin | |
Result := false; | |
if WNetOpenEnum(RESOURCE_REMEMBERED, RESOURCETYPE_DISK, 0, nil, NetHandle) <> NO_ERROR then | |
Exit; | |
try | |
BufSize := 50 * SizeOf(TNetResource); | |
GetMem(NetResources, BufSize); | |
try | |
while True do begin | |
Count := $FFFFFFFF; | |
Size := BufSize; | |
NetResult := WNetEnumResource(NetHandle, Count, NetResources, Size); | |
if NetResult = ERROR_MORE_DATA then begin | |
BufSize := Size; | |
ReallocMem(NetResources, BufSize); | |
Continue; | |
end; | |
if NetResult <> NO_ERROR then | |
Exit; | |
for I := 0 to Count - 1 do begin | |
if SameText(ALocalName, string(NetResources[I].lpLocalName)) then begin | |
Exit(True); | |
end; | |
end; | |
end; | |
finally | |
FreeMem(NetResources, BufSize); | |
end; | |
finally | |
WNetCloseEnum(NetHandle); | |
end; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The parameter should be the drive letter followed by a colon:
IsPermanentConnection('X:');