Solved

Extended DLL

Posted on 1998-10-05
7
450 Views
Last Modified: 2008-03-03
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
Comment
Question by:jufer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174411
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
 

Author Comment

by:jufer
ID: 1174412
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174413
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1174414
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
 

Author Comment

by:jufer
ID: 1174415
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174416
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
 

Author Comment

by:jufer
ID: 1174417
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question