Skip to content

Instantly share code, notes, and snippets.

@UweRaabe
Last active November 24, 2017 13:09
Show Gist options
  • Save UweRaabe/d647feab54f8db2a7a6a371a8d7c06e0 to your computer and use it in GitHub Desktop.
Save UweRaabe/d647feab54f8db2a7a6a371a8d7c06e0 to your computer and use it in GitHub Desktop.
check if network connection is permanent
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;
@UweRaabe
Copy link
Author

The parameter should be the drive letter followed by a colon: IsPermanentConnection('X:');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment