DLL + FILE * = ?

Started by
4 comments, last by chrisbk 22 years, 10 months ago
Hi all, I have a problem with my DLL . let me explain it : In the .exe code I have the following : FILE *in = fopen(...); btm->load(in); Where btm is an object created by the DLL (this also mean that the ''load'' function is located inside the DLL). In the load function I have something that looks like : void load(in) { char header[2]; fread(header,2,1,in); .. } And this crash (Access violation) on the fread. (in != null) It also crash if I do fseek/fclose/whatever. I really don''t know what is happening (and that''s annoying me..) Anyone knows what cause this crash ? thks chris
Advertisement
quote:Original post by chrisbk

Hi all,

I have a problem with my DLL . let me explain it :

In the .exe code I have the following :

FILE *in = fopen(...);
btm->load(in);

Where btm is an object created by the DLL (this also mean that the ''load'' function is located inside the DLL).

In the load function I have something that looks like :


void load(in)
{
char header[2];
fread(header,2,1,in);
..
}


And this crash (Access violation) on the fread. (in != null)
It also crash if I do fseek/fclose/whatever.

I really don''t know what is happening (and that''s annoying me..)
Anyone knows what cause this crash ?


thks


chris





Try this just for fun, instead of fread(header,2,1,in), replace it with fread(&header, 2, 1, in); If I remember correctly, it''s been a long while since I used fread, that should solve your problem...




"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!
[Cyberdrek | ]
Since header is an array it will automatically be passed by pointer. You would be passing a pointer to a pointer by doing &header.

[Resist Windows XP''s Invasive Production Activation Technology!]
quote:
You would be passing a pointer to a pointer by doing &header.


Actually not, though it might seem so. In C++ (perhaps in C too, don't know) the following holds: If 'foobar' is declared as a statically allocated array, then (foobar == &foobar) is true.

Edited by - Dactylos on June 18, 2001 9:37:03 PM
I can see that you tweaked your parameters a bit. Just as test, change:
fread(header,2,1,in); 

--- to -----
fread(header,1,2,in); 


Now that I know that having an item 2 bytes long and reading once is the same as having an item 1 byte long and reading twice, but who knows how fread is working internally. Just try it.
If there isn''t a parameter problem, then the next most (very) likely thing is that the DLL was built with a different version of the C runtime library to the .exe...


If you build the .exe with the "Debug Multithreaded DLL" version of the C runtime library and you build your DLL with the "Single Threaded, static lib" version, then the handle from one will be invalid with the other... Even different versions of the same CRT can cause that.

...you get the same problem with memory allocation - if you allocate something in the DLL and try to release it in the .exe (assuming you''d DLL with different CRT to EXE), you''d get a crash.


Solutions ?:

1. Don''t try to share CRT handles/resources out of a DLL - its an unsafe programming practice - better is to wrap a version of the fopen call inside the DLL, so any handle used in the DLL is always using the same CRT.

2. If you must do it, ensure the DLL has exactly the same CRT as the EXE - setting both to use one of the CRT DLLs is the best option (eg. both should use "Single Threaded DLL"

3. The alternative is not to use the CRT, instead use Windows functions (CreateFile, ReadFile, CloseHandle etc) in both DLL and .exe, and pass HANDLEs between DLL and EXE rather than FILE*s

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement