ptrennum
asked on
FindFirstFile not recognizing files with a netowrk path
local path = C:\untitled1.bmp
network path = H:\ChilkatFtp.exe
For some reason when I call FindFirstFile like this:
if ((findHndl=FindFirstFile(( LPCSTR)(Fi lePath), &fileInfo)) != INVALID_HANDLE_VALUE)
and the FilePath provided is a netwrok path it returns the INVALID_HANDLE, if the FilePath is a local path the if statment is executed. I need to execute the if statement wether or not it is a network path or a local path. Are there any toher functions where this is not a problem or am I missing something here?
THanks
PT
network path = H:\ChilkatFtp.exe
For some reason when I call FindFirstFile like this:
if ((findHndl=FindFirstFile((
and the FilePath provided is a netwrok path it returns the INVALID_HANDLE, if the FilePath is a local path the if statment is executed. I need to execute the if statement wether or not it is a network path or a local path. Are there any toher functions where this is not a problem or am I missing something here?
THanks
PT
BTW, just as additional info - the above works fine for mapped shares here. Are you sure you are using it like
if ((findHndl=FindFirstFile(( LPCSTR)"h: \\ChilkatFTP.exe" , &fileInfo)) != INVALID_HANDLE_VALUE)
?
if ((findHndl=FindFirstFile((
?
Another thing - try
if ((findHndl=FindFirstFile(( LPCSTR)(Fi lePath), &fileInfo)) != INVALID_HANDLE_VALUE) {
//...
} else {
DWORD dwError = GetLastError();
char acBuf [ 255];
wsprintf ( acBuf, "FindFirstFile() failed, GetLastError() == %d", dwError);
MessageBoxA(NULL,acBuf, "ERROR", MB_OK | MB_ICONERROR);
}
if ((findHndl=FindFirstFile((
//...
} else {
DWORD dwError = GetLastError();
char acBuf [ 255];
wsprintf ( acBuf, "FindFirstFile() failed, GetLastError() == %d", dwError);
MessageBoxA(NULL,acBuf, "ERROR", MB_OK | MB_ICONERROR);
}
ASKER
I worte a test app to just search for a file on the H: using FindFirstFile and it found it no prob, so Im' not sure why it won't work in that if statement?
((LPCSTR)(FilePath)
FilePath is a char[260] that is getting the right path.
((LPCSTR)(FilePath)
FilePath is a char[260] that is getting the right path.
ASKER
GetLastError returns a 3
That is
//
// MessageId: ERROR_PATH_NOT_FOUND
//
// MessageText:
//
// The system cannot find the path specified.
//
#define ERROR_PATH_NOT_FOUND 3L
Are you sure that the network drive is mapped to 'h:' and there is a 'ChilkatFtp.exe' in the root directory? If not, the behaviour of returning 'INVALID_HANDLE_VALUE' is OK under these circumstances:
Return Values
If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
If there is no such file, no handle can be returned.
//
// MessageId: ERROR_PATH_NOT_FOUND
//
// MessageText:
//
// The system cannot find the path specified.
//
#define ERROR_PATH_NOT_FOUND 3L
Are you sure that the network drive is mapped to 'h:' and there is a 'ChilkatFtp.exe' in the root directory? If not, the behaviour of returning 'INVALID_HANDLE_VALUE' is OK under these circumstances:
Return Values
If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
If there is no such file, no handle can be returned.
ASKER
I wrote this test app and it returns success with that path and when I print out the path that is being passed with FilePath it is the same. The if statement is executed if it is a local file but not if it is a network file but the test app finds the entwork file no problem
if ((findHndl=FindFirstFile(" H:\\Chilka tFtp.exe", &FileInfo)) != INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Found It!", "Debug", MB_TOPMOST);
if ((FileInfo.dwFileAttribute s&FILE_ATT RIBUTE_DIR ECTORY) != FILE_ATTRIBUTE_DIRECTORY)
MessageBox(NULL, "Its A File!", "Debug", MB_TOPMOST);
else
MessageBox(NULL, "Its A Directory!", "Debug", MB_TOPMOST);
FindClose(findHndl);
}
if ((findHndl=FindFirstFile("
{
MessageBox(NULL, "Found It!", "Debug", MB_TOPMOST);
if ((FileInfo.dwFileAttribute
MessageBox(NULL, "Its A File!", "Debug", MB_TOPMOST);
else
MessageBox(NULL, "Its A Directory!", "Debug", MB_TOPMOST);
FindClose(findHndl);
}
I tried the same with a mapped share here, and it works fine - are you sure you have the correct access rights for the network drive? Is 'GetLastError()' still '3' when running the code from your last comment?
ASKER
The problem was that the if statemant is part of the wndows service I have been writing. The service runs as a system process. Mr.SYSTEM doesn't see the mapped drives. how can I allow my service that runs as a SYSTEM process to see the drives??
PT
PT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did find a way do get the correct path so that at the very least the software could recognize the paths however I still get access denied so JKR is deffinetely right I will need to run it under a different account. I am going to post the code for converting the network paths to UNC paths in case anyone could benefit from it. THanks again JKR.
PT
if (pFilename[1] != ':')
return TRUE;
char RootName[MAX_PATH];
strncpy(RootName, pFilename, 3);
RootName[3]=0;
if (GetDriveType(RootName) == DRIVE_REMOTE)
{
BYTE Buffer[1024];
DWORD i=1024;
if (WNetGetUniversalName(pFil ename, UNIVERSAL_NAME_INFO_LEVEL, Buffer, &i) == ERROR_MORE_DATA)
return FALSE;
strcpy(pFilename, ((UNIVERSAL_NAME_INFO*)Buf fer)->lpUn iversalNam e);
}
return TRUE;
PT
if (pFilename[1] != ':')
return TRUE;
char RootName[MAX_PATH];
strncpy(RootName, pFilename, 3);
RootName[3]=0;
if (GetDriveType(RootName) == DRIVE_REMOTE)
{
BYTE Buffer[1024];
DWORD i=1024;
if (WNetGetUniversalName(pFil
return FALSE;
strcpy(pFilename, ((UNIVERSAL_NAME_INFO*)Buf
}
return TRUE;
Thanx :o)
You should have made clear what you wanted to achieve right away (i.e., getting the UNC name of a file), that would have made things a lot easier *s*
You should have made clear what you wanted to achieve right away (i.e., getting the UNC name of a file), that would have made things a lot easier *s*
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s.\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n",
GetLastError ());
return (0);
}
else
{
printf ("The first file found is %s\n",
FindFileData.cFileName);
FindClose(hFind);
return (1);
}
}
and the UNC name, e.g. \\server\share?