Extended DLL

I wrote a Extended DLL and I used the Wizard. My DLL use MFC (CStringList) and the function get a StringList as a pointer : MyDLLFunc( CStringList *cslGet );
If the DLL is the Debug Version and my Program too, it works, but if I take the Release Version, I have some errors. ( DLL Debug, Program Release => Error; DLL Release, Program Release => Error, too)
The same problem is, when my DLL function is empty!
So I need a DLL expert that explain me what can be wrong and why. Thanks to all experts
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

jkrConnect With a Mentor Commented:
Well, if you're using a C function (i.e. not a MFC derived member), you'll have to use the 'AFX_MANAGE_STATE()' macros as prologue and epilogue of your function to get it working.
1. One problem is that the CStringList class derives off CObject

CObject includes extra code in debug from release.

If the DLL and EXE are not both compiled to release, or both to debug - then the CStringList/CObject in the DLL differs from the CStringList in the main app, and the runtime will get confuses (object layout is not how the code is expected to be).

2. Additionally Presumably by "Extended DLL" - you mean MFC Extension DLL.  Unless MFC DLL is this, you won't be able to share MFC objects between the EXE and the DLL.

Can you tell us what the error you're getting is please

juferAuthor Commented:
So the Errors :(Release DLL, Release EXE)
The instruction ar 0x77f031d referenced memory 0xffff4. The memory could not be written

(Relase DLL, Debug Exe )
Debug Assertion :
File dbgheap.c, line 1011
expression _CrtsValidHeapPtr( puserData )
_BLOCK_TYPE_IS_VALID( pHead->nBlockUse)
and finaly
Debug Error, Damage before free block (#0) at 0x004511570

What have I to change ?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Compile both DLL and EXE to same (both to debug, or both to release).  Reason is:

"If the DLL and EXE are not both compiled to release, or both to debug - then the CStringList/CObject in the DLL differs from the CStringList in the main app, and the runtime will get confuses (object layout is not how the code is expected to be). "

Sorry what is confusing me, is are you getting an error if both are compiled to the same ?  [The original Q seems to say yes].  What error do you get when they are both compiled the same

To save (perhaps) asking another Q later, could you give source fragments from EXE and DLL showing the call
juferAuthor Commented:
Yes, if both are compiled with release, it gets some errors. Well I'm using VC5.0 (including Visual Service Pack). When I make my DLL in the VC 4.2 (just create a new DLL-workspace with the wizard and copy paste my code, it works !) So I make the same in VC5.0 and it doesn't work! Perhaps some wrong settings or a bug in VC5.

So I think ( or it looks like ) it's not the my code, perhaps it's the call.

I included the Lib-file and the H-file from the DLL. Then I just call the function.
//call the function in the dll
HexCorrect( &cslONE );

I included the function in the Def-File, like they do in the inside visual C5.0 book.
; HEXCORDLL.def : Declares the module parameters for the DLL.

DESCRIPTION  'HEXCORDLL Windows Dynamic Link Library'

    ; Explicit exports can go here

void HexCorrect( CStringList *csl )
  AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
  // not inserted this macro at the end of the function otherwise i get an error "redefinition"
  // AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

The DEF file has no effect (or atleast nothing good)

HexCorrect is a C++ function so name of function will not be exported (C++ mangles the name with type info)

1. Remove EXPORTS section from DEF file

2. Export the function in code

a. In header (used by both DLL and EXE)

#define DLLFUNCTION __declspec(dllexport)
#define DLLFUNCTION __declspec(dllimport)

void DLLFUNCTION HexCorrect( CStringList *csl ) ;

b. In EXE code #include the header

c. In DLL .cpp code do this

#include "header.h"

void DLLFUNCTION HexCorrect( CStringList *csl )

3. Forget AFX_MANAGE_STATE (remove them)

The purpose of this macro is to switch where MFC load's resources from.

For example if you look at LoadString API call it takes an hInstance, whereas the LoadString member of CString doesn't - why ?
Because CString asks MFC what hInstance to use.  AFX_MANAGE_STATE is used to tell MFC what current hInstance is.

If you load resources from the DLL using CString::LoadString and similar functions, then you need AFX_MANAGE_STATE, otherwise you don't

4. Make sure your DLL is an MFC extension DLL

juferAuthor Commented:
Ok guys,
thanks a lot for helping me. It didn't work on VC5 but on VC4.2 and that's ok for my need.
All Courses

From novice to tech pro — start learning today.