Why is CreateFile failing to open a file across a

2019-01-27 01:47发布

I wrote a small program which frequently opens small, user text files and until now haven't encountered any problems with read/write access or any sorts of conflict. The files are selected in another piece of software which I have no control over, and are passed to me as a string.

When attempting to open a file from a mapped network drive I am getting a "The system cannot find the path specified" error (GetLastError() = 3).

The call is shown below, *iNCfileName = "z:\\Validation\\Sample Files\\1_1-4 120MM.CC", where Z: is a mapped folder on our domain.

iNCfile = CreateFile( iNCfileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if ( iNCfile == INVALID_HANDLE_VALUE ) 
{
    string msg; // lots of better ways to get this printed ... but ...
    dw = GetLastError();
    msg = iNCfileName;
    msg += ": ";
    msg += _com_error(dw).ErrorMessage();
    print_error(dw , (char*)msg.c_str() );
    return 102;
}

The file opens from my program if I copy it to the local hard drive. It also opens in notepad from the mapped drive.

Could this be a problem between the "Z:\whatever.txt" mapped representation and the true file name (\mydomain\Validation\S....??)?

If so, how can I convert from one to the other in a programmatic way (assume I won't know the domain/share names ahead of time)?

If it makes any difference I use VS2010 and the application executes on a Win XP machine.

Related: my follow up question

2条回答
爷、活的狠高调
2楼-- · 2019-01-27 01:56

I just had the same issue; trying to create a file using API CreateFileW under a mapped drive ( Z:\folder ) did not worked; howerver, after researching this subject i tried to create the file using the real path ( \\Shared_computer_name\folder\ ) immediately worked successfully.

Now I have to work a function to retrieve the real name of a mapped drive, to use it when necessary... just found WNetGetUniversalName, have to make it to work.

查看更多
▲ chillily
3楼-- · 2019-01-27 02:09

I've encountered this before. When using a path like \\DOMAIN\PATH\FILE.TXT I had to first call WNetAddConnection2().

Here is my code (of course you can exclude the NULL members):

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
nr.lpRemoteName = "\\\\DOMAIN\\PATH\\FOLDER"; //For me, this pointed to an account's documents folder, and from there I could use a subfolder
nr.lpProvider = NULL; //no provider

DWORD ret = WNetAddConnection2 (&nr, NULL, NULL, CONNECT_TEMPORARY); //add connection

Don't forget the header and library.

查看更多
登录 后发表回答