Advertisement

Hand FILE* to a DLL

Started by August 03, 2000 09:33 AM
6 comments, last by Metron 24 years, 4 months ago
Hi, I''ve got an executable and a DLL. I want to pass a file that has been opened with fopen (and stored in FILE *pFile) to the DLL. Now here''s the problem : I can read from the file within the executable. Once I have passed the pointer to the FILE to the DLL and I try to read from the file, I get an access violation within the fread function. Since I have MSDN, I have the sources and the access violation occurs in this line of code : _lock_str(stream); /* lock stream */ Does anyone know what I can do about this ? Is it possible to hand a FILE* to a DLL ? Kind regards, Metron
----------------------------------------http://www.sidema.be----------------------------------------
Maybe you could pass the filename and the current offset instead and have the function in the DLL have a temporary FILE* and open it from there? Let me illustrate with some code:

void DLL_Open(char *filename, long offset)
{
FILE *fp = fopen(filename, "rb");

// Go to the offset specified.

fseek(fp, offset, SEEK_SET);

// Ready to do file I/O...

fclose(fp);
}

Maybe that''ll work?
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
ofcourse it''s possible
just close your file before...
ofcourse it''s possible
just close your file before...

or just let the dll itself handle and open the file
Well,

I think that I do not have another choice... thanks anyways...

Metron
----------------------------------------http://www.sidema.be----------------------------------------
You can safely pass a FILE* to a DLL, provided they are linked with identical C run-times. That is, it is unsafe to pass a FILE* opened with the static single-threaded run-time to a function that uses the multithreaded DLL, or between different CRT versions. If you cannot ensure that, the safest way to pass an opened file would be with a Win32 HANDLE created via CreateFile()
Advertisement
I can not be sure that the DLL to which I pass the FILE* will be compiled with the same CRT libs... I programming a plug-in based application and since the plug ins are written by different people it would be hard to ensure they all use the same libs.

I do not want to use Windows appropriate API calls because I want to keep the code as portable as possible and afaik fopen is standard c...

Thanks anyways,
Metron
----------------------------------------http://www.sidema.be----------------------------------------
quote: Original post by Metron

I can not be sure that the DLL to which I pass the FILE* will be compiled with the same CRT libs... I programming a plug-in based application and since the plug ins are written by different people it would be hard to ensure they all use the same libs.

I do not want to use Windows appropriate API calls because I want to keep the code as portable as possible and afaik fopen is standard c...

Thanks anyways,
Metron


BUT, isn''t the point of a plugin API being able to SPECIFY how it''s developed also?

BeS
It's Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka

This topic is closed to new replies.

Advertisement