Thread: Mapping network drives

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    98

    Mapping network drives

    Hi. I am having to write a program which connects to an Access database across a network, on another domain from the one in which the program runs. Therefore, I need to provide network credentials in order to access the resource.

    At the moment, I am using a system() call to run a 'net use' command, and map a network drive - supplying the logon details in the process.

    Is there any way of incorporating that level of authorisation into a DAO open command, or is there a better way of mapping a network drive than using system() - it feels a bit clumsy to me?

    Thanks.
    There is no such thing as a humble opinion

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    WNetAddConnection2 or NetUseAdd

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Thanks.

    I looked up usage of WNetAddConnection2, and have got to the stage where I can get the code to compile and run. The following excerpt always results in an error (no. 487) ERROR_INVALID_ADDRESS...


    Code:
    NETRESOURCE nrMap;
    
    nrMap.dwType = RESOURCETYPE_DISK;
    nrMap.lpLocalName = "Z:\\";
    nrMap.lpRemoteName = "\\\\MyServer\\c$";
    
    DWORD retCode = WNetAddConnection2(&nrMap, "mypassword", "myuser", 0 );
    I have included winnetwk.h, and added mpr.lib to the library list.

    For obvious reasons, I've not posted the actual host, password or uid, but I am very sure that genuine entries are correct. Can anyone see what I am doing wrong? Z:\ is not already in use.
    There is no such thing as a humble opinion

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    NETRESOURCE nrMap = { 0 };
    
    nrMap.dwType = RESOURCETYPE_DISK;
    nrMap.lpLocalName = "Z:";
    nrMap.lpRemoteName = "\\\\MyServer\\c$";
    
    DWORD retCode = WNetAddConnection2(&nrMap, "mypassword", "myuser", 0 );
    It's always a good idea to zero out api structures before use in case you don't specify every member. In this case, you are not providing a value for the lpProvider member.

    The sample linked to from the WNetAddConnection2 documentation shows that it expects "Z:" rather than "Z:\\".

    Anyone can read a password embedded in a program. Therefore, if you are connecting as admin they will have total access to the target machine. You may want to consider making an account for DB access and only give it the required access to the database.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Thanks again!
    There is no such thing as a humble opinion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. image analysis theory: mapping a network
    By elninio in forum C++ Programming
    Replies: 5
    Last Post: 10-30-2008, 01:23 PM
  2. Need help with easy Network setup
    By the dead tree in forum Tech Board
    Replies: 9
    Last Post: 04-08-2005, 07:44 PM
  3. Get mapped network drives on local machine
    By nvoigt in forum Windows Programming
    Replies: 4
    Last Post: 10-19-2004, 10:15 AM
  4. WinXP Network Connections pop-up
    By DavidP in forum Tech Board
    Replies: 1
    Last Post: 10-02-2002, 05:36 PM
  5. Files on network drives
    By FillYourBrain in forum Windows Programming
    Replies: 5
    Last Post: 09-05-2002, 12:51 PM