[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


FSTAT in a dll in VC++ 7

Posted on 2006-11-16
Medium Priority
Last Modified: 2010-04-24
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

Question by:aureliuh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
LVL 45

Expert Comment

ID: 17962553
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!!!

Author Comment

ID: 17963346
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
LVL 45

Expert Comment

ID: 17963397
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.


Author Comment

ID: 17963641
I agree with you but everything looks fine. I just checked out.

Author Comment

ID: 17963653
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
LVL 45

Expert Comment

ID: 17963667
Thats rather strange ... If that were to be the case, errno should have been EBADF

Author Comment

ID: 17963780
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
LVL 45

Expert Comment

ID: 17963802
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

Author Comment

ID: 17968216
The problem is that ftstat wants Handle as int not a FILE*.

Author Comment

ID: 17968281
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.

Accepted Solution

PashaMod earned 0 total points
ID: 18012833
Closed, 500 points refunded.
Community Support Moderator

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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