Skip to content

Instantly share code, notes, and snippets.

@UweRaabe
Created November 28, 2016 10:57
Show Gist options
  • Save UweRaabe/5811d0414d769f5ba74ed152c7f3c3c6 to your computer and use it in GitHub Desktop.
Save UweRaabe/5811d0414d769f5ba74ed152c7f3c3c6 to your computer and use it in GitHub Desktop.
add and delete a network connection
function NetUseAdd(const RemoteName: string): string;
var
NetResource: TNetResource;
dwResult, dwBufSize, dwFlags: DWORD;
hRes: DWORD;
localName: array[0..1024] of Char;
begin
dwFlags := CONNECT_REDIRECT;
ZeroMemory(@NetResource, sizeof(TNetResource));
with NetResource do begin
dwType := RESOURCETYPE_DISK;
lpLocalName := nil;
lpRemoteName := PChar(RemoteName);
lpProvider := nil;
end;
dwBufSize := Length(localName);
hRes := WNetUseConnection(0, NetResource, nil, nil, dwFlags, localName, dwBufSize, dwResult);
if hRes = NO_ERROR then
result := localName
else
result := '';
end;
function NetUseDelete(const LocalName: string): Boolean;
var
hRes: DWORD;
begin
hRes := WNetCancelConnection2(PChar(LocalName), CONNECT_UPDATE_PROFILE, true);
result := (hres = NO_ERROR);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment