DLL + FILE * = ?
Members - Reputation: 122
Posted 18 June 2001 - 12:46 PM
Members - Reputation: 100
Posted 18 June 2001 - 01:18 PM
Original post by chrisbk
I have a problem with my DLL . let me explain it :
In the .exe code I have the following :
FILE *in = fopen(...);
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 :
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 ?
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!"
A division of DLC Multimedia
Moderators - Reputation: 1087
Posted 18 June 2001 - 02:29 PM
[Resist Windows XP''s Invasive Production Activation Technology!]
Members - Reputation: 122
Posted 18 June 2001 - 02:36 PM
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
Crossbones+ - Reputation: 518
Posted 18 June 2001 - 06:01 PM
--- to -----
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.
Members - Reputation: 1394
Posted 18 June 2001 - 11:35 PM
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.
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
Creative Asylum Ltd