Solved

Extended DLL

Posted on 1998-10-05
7
428 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
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now