• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# DLL + FILE * = ?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1chrisbk  Members

Posted 18 June 2001 - 12:46 PM

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

### #2Cyberdrek  Members

Posted 18 June 2001 - 01:18 PM

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(...);

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 ?

thks

chris

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

Cyberdrek
A division of DLC Multimedia

### #3Null and Void  Members

Posted 18 June 2001 - 02:29 PM

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!]

### #4Dactylos  Members

Posted 18 June 2001 - 02:36 PM

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

### #5Zipster  Members

Posted 18 June 2001 - 06:01 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.

### #6S1CA  Members

Posted 18 June 2001 - 11:35 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.