Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extended DLL

Posted on 1998-10-05
7
Medium Priority
?
464 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
jkr earned 300 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

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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

670 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