Link to home
Start Free TrialLog in
Avatar of ptrennum
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)(FilePath), &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
Avatar of jkr
jkr
Flag of Germany image

Is the network share connected as 'h:\'? Can you reproduce that with

#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?
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)

?
Another thing - try

if ((findHndl=FindFirstFile((LPCSTR)(FilePath), &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);
}
Avatar of ptrennum
ptrennum

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.
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.
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:\\ChilkatFtp.exe", &FileInfo)) != INVALID_HANDLE_VALUE)
      {
            MessageBox(NULL, "Found It!", "Debug", MB_TOPMOST);
            if ((FileInfo.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
                  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?
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
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(pFilename, UNIVERSAL_NAME_INFO_LEVEL, Buffer, &i) == ERROR_MORE_DATA)
                  return FALSE;

            strcpy(pFilename, ((UNIVERSAL_NAME_INFO*)Buffer)->lpUniversalName);
      }

      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*