Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Extended DLL

Hi,
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
0
jufer
Asked:
jufer
  • 3
  • 3
1 Solution
 
Answers2000Commented:
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

0
 
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 )
and
_BLOCK_TYPE_IS_VALID( pHead->nBlockUse)
and finaly
Debug Error, Damage before free block (#0) at 0x004511570

What have I to change ?
0
 
Answers2000Commented:
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jkrCommented:
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.
0
 
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.

EXE:
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 );


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

LIBRARY      "HEXCORDLL"
DESCRIPTION  'HEXCORDLL Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
  HexCorrect

Cpp:
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( ));
}



0
 
Answers2000Commented:
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)

#ifdef BUILDINGDLL
#define DLLFUNCTION __declspec(dllexport)
#else
#define DLLFUNCTION __declspec(dllimport)
#endif

void DLLFUNCTION HexCorrect( CStringList *csl ) ;

b. In EXE code #include the header

c. In DLL .cpp code do this

#define BUILDINGDLL
#include "header.h"

void DLLFUNCTION HexCorrect( CStringList *csl )
{
//whatever
}


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

0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now