Solved

FSTAT in a dll in VC++ 7

Posted on 2006-11-16
12
377 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
  • 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now