How can I resolve the error ERROR_BAD_NET_NAME when calling WNetAddConnection2?

I'm still trying to solve the problem that I am facing, the first part was that I apparently needed to call WNetAddConnection2 to use CreateFile to open the file through a network share.

Having done this, now I get the message ERROR_BAD_NET_NAME from the WNet call ... 2.

A 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 at startup, but it would probably be bad to assume that, of course. The folder maps to local Z: I can receive, read, write and delete files from the destination folder on the computer 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 the problem is that I cannot use Z:\Documents\somefile.txt , but rather I should use the \\ DOMAIN \ PATH \ FOLDER notation. If so, how do I programmatically receive this information so that I can provide it as input? I misunderstood that I can convert the file name to \\\\Z\\Documents\\somefile.txt ? If so, is there a resource for doing this, or should I parse the string myself?

+1
c ++ file-io winapi networking
source share
1 answer

You are right why it does not work. You pass the name of the local file (e.g. Z:\Documents\somefile.txt ) when you need to pass the name of the share (e.g. \\myserver\sharename ).

If the share is already connected to Z: you do not need to call WNetAddConnection2 .

If the resource is not already connected to Z: then you cannot automatically convert Z:\Documents\somefile.txt to a remote name because Windows does not know what Z: represents.

If the shared resource cannot be connected, your program must know in advance what proportion Z: must connect to and establish the connection itself. You can connect the shared resource to Z: and use the existing path or connect it without a drive letter and adjust the path yourself; you just need to replace Z:\ with \\myserver\sharename\ .

+1
source share

All Articles