[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

FindFirstFile not recognizing files with a netowrk path

Posted on 2005-04-22
12
Medium Priority
?
2,477 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:ptrennum
  • 7
  • 5
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13843314
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?
0
 
LVL 86

Expert Comment

by:jkr
ID: 13843346
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)

?
0
 
LVL 86

Expert Comment

by:jkr
ID: 13843806
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);
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ptrennum
ID: 13843858
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.
0
 

Author Comment

by:ptrennum
ID: 13843977
GetLastError returns a 3
0
 
LVL 86

Expert Comment

by:jkr
ID: 13844089
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.
0
 

Author Comment

by:ptrennum
ID: 13844135
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);
      }
0
 
LVL 86

Expert Comment

by:jkr
ID: 13844185
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?
0
 

Author Comment

by:ptrennum
ID: 13844233
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13844270
>> The service runs as a system process.  Mr.SYSTEM doesn't see the mapped drives

Argh, that's the reason - LocalSystem has almost Admin proivileges, but NO network access at all. You'll need to set the service to run under a different account...
0
 

Author Comment

by:ptrennum
ID: 13845477
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;
0
 
LVL 86

Expert Comment

by:jkr
ID: 13847753
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*
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question