Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
875 views
in Technique[技术] by (71.8m points)

c++ - Why is CreateFile failing to open a file across a network share?

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 (mydomainValidationS....??)?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've encountered this before. When using a path like \DOMAINPATHFILE.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...