I am still trying to resolve a problem I’m having, the first part of it was that I apparently need to call WNetAddConnection2 in order to use CreateFile to open a file across a network share.
Having done so, I am now receiving an ERROR_BAD_NET_NAME return from the WNet…2 call.
The remote resource is a mapped network folder on a windows network (the client, where we are, is windows xp). The network resource should be connected on startup, but it’d probably be bad to assume that, of course. The folder maps to local Z:. I am able to access, read, write and delete files from the destination folder on the machine using Explorer.
HANDLE initFile ( LPCTSTR iNCfileName ) {
DWORD dw;
HANDLE fHandle=NULL;
NETRESOURCE nr = {0}; //new structure for network resource
nr.dwType = RESOURCETYPE_ANY; //generic resource (any type allowed)
nr.lpLocalName = NULL; //does not use a device
// typical iNCfileName is std::string a="Z:\\Documents\\somefile.txt".c_str()
nr.lpRemoteName = (char*)iNCfileName;
//"\\\\DOMAIN\\PATH\\FOLDER";
nr.lpProvider = NULL; //no provider
// CONNECT_CURRENT_MEDIA ??
DWORD ret = WNetAddConnection2 (&nr, NULL, NULL, CONNECT_TEMPORARY);
//...
return fHandle;
}
I think that the problem is I cannot use Z:\Documents\somefile.txt but rather should be using the \\DOMAIN\PATH\FOLDER notation. If that’s the case, how do I programatically obtain that information so that I can provide it as input? Did I misunderstand the orignal answerer that I can convert the file name to \\\\Z\\Documents\\somefile.txt? If so is there a resource to perform this or should I parse the string myself?
You are right about why it’s not working. You are passing a local file name (e.g.
Z:\Documents\somefile.txt) when you should be passing a share name (e.g.\\myserver\sharename).If the share is already connected to
Z:then you don’t need to callWNetAddConnection2.If the share is not already connected to
Z:then there’s no way you can automatically convertZ:\Documents\somefile.txtto a remote name because Windows has no idea whatZ:represents.If the share might not be connected then your program needs to know in advance what share
Z:is supposed to connect to and make the connection itself. You can either connect the share toZ:and use the existing path, or connect it with no drive letter and adjust the path yourself; you just need to replace theZ:\with\\myserver\sharename\.