The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

check if network connection is permanent

Posted by jpluimers on 2017/11/22

Thanks Uwe for the below code!

I was in a situation of a batch file running from a Task Scheduler that got broken because the user suddenly turned on persistent network connections.

So I needed to check if a shared drive-letter was indeed persistent or not and act accordingly.

The below code helped me merging the batch file with the Delphi application it would run in the background anyway.

It’s way better than checking of the global “persistence” flag for new connections has been set in the registry: [WayBackSaveConnections flag at HKCU\Software\Microsoft\Windows NT\CurrentVersion\Network\Persistent Connections (which is reflected both in the Windows UI and settable via net use /Persistent).

Source: check if network connection is permanent

Via: [WayBack] Anyone who can share how one can query if a drive letter mapping to a share is persistent across logon sessions? (yes, this is Windows, I know) – Jeroen Wiert Pluimers – Google+

–jeroen


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;

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.