Solved

FSTAT in a dll in VC++ 7

Posted on 2006-11-16
12
394 Views
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" );
  }
   else
   {
      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;
            FormatMessage(
                  FORMAT_MESSAGE_ALLOCATE_BUFFER |
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL,
                  GetLastError(),
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                  (LPTSTR) &lpMsgBuf,
                  0,
                  NULL
            );

               MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
              // Free the buffer.
               LocalFree( lpMsgBuf );      
            printf("Wrong fstat descriptor %d\n", fd);
      }else{
      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


0
Comment
Question by:aureliuh
[X]
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
12 Comments
 
LVL 45

Expert Comment

by:sunnycoder
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!!!
0
 

Author Comment

by:aureliuh
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
0
 
LVL 45

Expert Comment

by:sunnycoder
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.
0
Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

 

Author Comment

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

Author Comment

by:aureliuh
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
0
 
LVL 45

Expert Comment

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

Author Comment

by:aureliuh
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
0
 
LVL 45

Expert Comment

by:sunnycoder
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
0
 

Author Comment

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

Author Comment

by:aureliuh
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.
0
 

Accepted Solution

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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