• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

FSTAT in a dll in VC++ 7

Hi there,

I am tring to find a solution for a problem. I have a dll which exports a function which receive an int as param.

1. From the main application I have opped a file with:

 fh1 = open( "c:\\reporting1.txt", _O_RDONLY );
   if( fh1 == -1 ){
      printf( "Open failed on input file" );
      printf( "Open succeeded on input file\n" );

2. I have called the function from dll with the following signature:

void writ(int fd); - where fd = file descriptor

In this function I have the following code:

if(fstat(fd, &buf) == -1) {
            LPVOID lpMsgBuf;
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                  (LPTSTR) &lpMsgBuf,

               MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
              // Free the buffer.
               LocalFree( lpMsgBuf );      
            printf("Wrong fstat descriptor %d\n", fd);
      printf("OK fstat descriptor %d\n", fd);

The problem is thet everytime the fstat(fd, &buf) will be equals -1. First time I thaught that the problems is the the file descriptor is not ok so I have added some line into this function so I can read form the file with descriptor fd. Everithing was fine
The only problem is with fstat.

I think there is a problem with the dll.

Please give me some ideeas

Thank you very much

  • 6
  • 4
1 Solution
What is the error number and error message? (errno and GetLastError())
If fd is a valid file descriptor and buf is a valid stat buffer, then there is no reason why the call to fstat would fail!!!
aureliuhAuthor Commented:
The GetLastError returns Operation sucessful. Everything looks fine . The only problem is that fstat is allways -1. If I mode fstat code seq into the main app after I oppend the file is working ok.
The problem is only in dll
Is this code you posted above, exact copy paste from the DLL ?

A simple typo like using = instead of == , or an omission like if (ret = fstat() == -1 ) can be the cause.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

aureliuhAuthor Commented:
I agree with you but everything looks fine. I just checked out.
aureliuhAuthor Commented:
I have openned the file in the dll and it worked. So the problem is that the file descriptor doesn't exist anymore if I open the file fro the mine application
Thats rather strange ... If that were to be the case, errno should have been EBADF
aureliuhAuthor Commented:
I think I have found the problem but I do not know how to solve it. You know that there are 2 types of file handlers, one is like 0,1,2,3.. and the other one is like 1900... The problem is that every process starts with 3 file handles 0,1,2 (stdin,stdout,stderr) and the 3 that is my oppend file.

When I pass to dll the file handle I use 3 but in the dll doesn't know who is 3 and I saw that fstat doesn't work wth system handles like 1990
looks like you are having problems converting between fd and FILE *

try this
FILE * fp = fdopen(fd, mode);
where mode is desired permissions on the file (as used in fopen()) e.g. "r", "rw" etc

Use this fp in place of fd as argument to fstat
aureliuhAuthor Commented:
The problem is that ftstat wants Handle as int not a FILE*.
aureliuhAuthor Commented:
I have found the answer to my problem!!!!

Fstat and open are C function, these return file handle but from C env.

After many research I have found that C file handle could not be shared between application or between application and dlls, each app or dll has its own table with file handle.

This is the reason that in my dll I could not find the file with the handle received from the main application

Thank you very much anyway.
Closed, 500 points refunded.
Community Support Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now