-
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.
-
WNetAddConnection2 or NetUseAdd
-
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.
-
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.
-