Home > OS >  How do I map a network drive on a domain in code
How do I map a network drive on a domain in code

Time:09-24

I am using this function to map a network drive:

CONST
  MapNetDrvDefFlags     = CONNECT_TEMPORARY OR CONNECT_INTERACTIVE {$IFDEF CONSOLE } OR CONNECT_COMMANDLINE {$ENDIF };

FUNCTION MapNetworkDrive(D : CHAR ; CONST UNC,UserName,Password,Provider : STRING ; Flags : DWORD = MapNetDrvDefFlags) : DWORD;
  VAR
    NetResource : _NETRESOURCEW;
    LocalRes    : STRING;

  BEGIN
    FillChar(NetResource,SizeOf(_NETRESOURCEW),0);
    NetResource.dwType:=RESOURCETYPE_DISK;
    LocalRes:=D ':'; NetResource.lpLocalName:=POINTER(LocalRes);
    NetResource.lpRemoteName:=POINTER(UNC);
    NetResource.lpProvider:=POINTER(Provider);
    Result:=WNetAddConnection2W(NetResource,POINTER(Password),POINTER(UserName),Flags)
  END;

and it works fine on a standard SAMBA WORKGROUP network. But at a customer's place it doesn't. It does map the drive, but when I try to access it, I get an "invalid user or password" error.

One difference I notice is that if I execute a manual

NET USE <Drive>: <UNC>

on my own network (WORKGROUP), I get a simple prompt for a user name. But on the customer's network, I get a different prompt:

Skriv brugernavnet for 'xxxx.yyyy':

(translation: Enter user name for 'xxxx.yyyy')

I assume that this is a domain name, but how do I give that to the WNetAddConnection2W call? I have tried sending username as "xxxx.yyyy@user" but that doesn't work. Is there another place I need to supply a domain name (if this is, indeed, what it is)?

I have also tried specifying the xxxx.yyyy part as the "Provider" in the _NETRESOURCEW structure, but if I do that, I get an "Invalid provider" error.

CodePudding user response:

Here's an answer to a very similar question: https://stackoverflow.com/a/29420529/759049

In essence, I think that you should just use domain name in lpRemoteName and you'll be good to go.

  • Related