• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • 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" );
  }
   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
aureliuh
Asked:
aureliuh
  • 6
  • 4
1 Solution
 
sunnycoderCommented:
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
 
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
0
 
sunnycoderCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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.
0
 
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
0
 
sunnycoderCommented:
Thats rather strange ... If that were to be the case, errno should have been EBADF
0
 
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
0
 
sunnycoderCommented:
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
 
aureliuhAuthor Commented:
The problem is that ftstat wants Handle as int not a FILE*.
0
 
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.
0
 
PashaModCommented:
Closed, 500 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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