Solved

Regulat dll using MFC

Posted on 2001-07-06
8
350 Views
Last Modified: 2013-11-20
I have a legacy C application that can use dlls to extend its functionality.
I want to create a "Regular dll with MFC statically linked" project that exports one function. I want the function to contain a dialog that contains two input boxes that is displayed when my legacy application calls the exported function.
When I created the dll project the IDE created for me a file containing code as shown bellow.
I want where should I code the exported function.
How it would look like.
Inside the function how can I create the dialog box with the two input boxes.

Thank you

+++++++++++++++++++dll code created by the VS IDE+++++++// test.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "test.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//     Note!
//
//          If this DLL is dynamically linked against the MFC
//          DLLs, any functions exported from this DLL which
//          call into MFC must have the AFX_MANAGE_STATE macro
//          added at the very beginning of the function.
//
//          For example:
//
//          extern "C" BOOL PASCAL EXPORT ExportedFunction()
//          {
//               AFX_MANAGE_STATE(AfxGetStaticModuleState());
//               // normal function body here
//          }
//
//          It is very important that this macro appear in each
//          function, prior to any calls into MFC.  This means that
//          it must appear as the first statement within the
//          function, even before any object variable declarations
//          as their constructors may generate calls into the MFC
//          DLL.
//
//          Please see MFC Technical Notes 33 and 58 for additional
//          details.
//

/////////////////////////////////////////////////////////////////////////////
// CTestApp

BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
     //{{AFX_MSG_MAP(CTestApp)
          // NOTE - the ClassWizard will add and remove mapping macros here.
          //    DO NOT EDIT what you see in these blocks of generated code!
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestApp construction

CTestApp::CTestApp()
{
     // TODO: add construction code here,
     // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTestApp object

CTestApp theApp;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
0
Comment
Question by:jc64
[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
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6260826
Well, as the comment in your code states:

Just put the function somewhere in the code, accoring to the specs of your legacy app

extern "C" <rettype> PASCAL EXPORT TheExportedFunction(<parameter list>)
{
  AFX_MANAGE_STATE(AfxGetStaticModuleState());
  // normal function body here
}

To make sure that the function is exported exactly the way you want (keeping the exat name without e.g. an underscore), additionally add a '.def' file to your project:

LIBRARY mydll
DESCRIPTION 'comment about all that'

EXPORTS

    TheExportedFunction
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6264249
If You use static linked MFC there are is no nedd to call MACRO AFX_MANAGE_STATE.
If you include definition file (.DEF) in your project there is no need to special declaration for exported function.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6287007
jc64, are you still with us?
0
Technology Partners: 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!

 

Author Comment

by:jc64
ID: 6291092
jkr, I am sorry I was away for work and just came back.

First I increated the points to 300 as this is important to me. second here are the steps I took to write an MFC dll that exports one function that when called displays a dialog that contains two text boxes.

1 - Create a new porject "MFC AppWizard[dll]"
2 - Regular DLL with MFC Statically linked
  The IDE created  the following files.

  - Test.h
  - Test.cpp
  - Test.DEF

  * Test = project Name
I added the project a new Form using insert-New Form
  The IDE Created two files
  - DTest.h
  - DTest.cpp

  * DTest = Dialog Name


Now I don't know how to proceed after that.

I want to create a C program and call the exported function from the dll above as

#include "What ever header file is necessary from the dll"

main()
{
   CAllFunctionExportedfromTheDLL();
}

Please advice me as to how to proceed and change different files.


Thank you to waiting.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 6291106
Well, you mainly have it there - just add your code to 'Test.cpp'

extern "C" <rettype> PASCAL EXPORT TheExportedFunction(<parameter list>)
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState());
 // normal function body here
}

and supply a separate header "What ever header file is necessary from the dll.h" - OK, just joking, let's call it 'thedll.h':

#ifdef __cplusplus
extern "C"
{
#endif
<rettype> PASCAL EXPORT TheExportedFunction(<parameter list>);
#ifdef __cplusplus
}
#endif

(the #ifdef stuff is necessary to not confuse the compiler in files with a '.c' extension)

Also, supply an entry for 'TheExportedFunction' in 'test.def', as shown above.

Then, compile the DLL and add the '.lib' file that is created to your C project, and it should work.
0
 

Author Comment

by:jc64
ID: 6291602
Jkr, I will try your suggestion and will get back to you as soon as finish.

Thanks
0
 

Author Comment

by:jc64
ID: 6291766
Jkr, I will try your suggestion and will get back to you as soon as finish.

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 6296539
Thanx, but you shouldn't accept an answer unless you really know that it works for you (though I have no doubt it will :o)
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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 105
how do i create updater to My Activex application? 3 108
List out all word 7 359
Not needed 13 134
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

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