Thursday, July 03, 2008

How to map/unmap the network disk in run-time

Source : http://www.scalabium.com/faq/dct0075.htm

Sometimes in run-time you must map/unmap the some remote resource. You can make it with the next code:

uses Windows;

var nw: TNetResource;
begin
nw.dwType := RESOURCETYPE_DISK;
nw.lpLocalName := nil;
nw.lpRemoteName := PChar('\\yourServerName\yourDiskOnServer');
nw.lpProvider := nil;
errCode := WNetAddConnection2(nw, nil, nil, 0);
if errCode = NO_ERROR then
<Ok, disk mapped>
end;

If the remote server have the password, then you must call the

errCode := WNetAddConnection2(nw, 'strPassword', nil, 0);

If you want to map this disk as own local resource, you must define the lpLocalName. For example,

nw.lpLocalName := 'F:';

When you want to un-map resource, you must call the WNetCancelConnection2 procedure.


0 Comments: