Solved

Adding MFC to regular DLL

Posted on 1998-07-26
14
388 Views
Last Modified: 2013-11-20
I've written a regular DLL (without MFC) for use with some console apps.
Later, I realised that the DLL is also needed for some Windows apps.
I need to know the changes necessary on my existing DLL to add in
some MFC functions like displaying dialog boxes.

Here is how my existing DLL looks like

export.cpp
----------
#include "myclass.h"
#include <windows.h>

BOOL WINAPI DllEntryPoint(HINSTANCE hDLL, DWORD dwReason, LPVOID Reserved)
{
      switch(dwReason)
      {
      case DLL_PROCESS_ATTACH: break;
      case DLL_PROCESS_DETACH: break;
      }

      return TRUE;      
}

void first_export_function(void);
...

export.def
----------
LIBRARY MYDLL

CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD SINGLE

EXPORTS

first_export_function

-----------------------------------------

Just a simple case, first_export_function needs to display a dialog
box to show its I/O process status when called by an application.

Thanks in advance for any help rendered.
0
Comment
Question by:gangclar
  • 8
  • 6
14 Comments
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Use MFC AppWizard (dll) to create the project and copy all the code for the exported functions. Look into the Advanced MFC Sample "DLLTRACE: Writes a DLL Statically Linked to the Framework Library" and add the following line of code to the beginning of every function exported from the DLL (see "Managing the State Data of MFC Modules" in Creating New Documents, Windows, and Views in the Visual C++ Programmer's Guide for discussion).

     AFX_MANAGE_STATE(AfxGetStaticModuleState());
0
 

Author Comment

by:gangclar
Comment Utility
I did that before, and received an error message "Unexpected end of file" for at the export.cpp file.
0
 

Author Comment

by:gangclar
Comment Utility
I did that before, and received an error message "Unexpected end of file" at the export.cpp file.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Check your source code carefully. There must be a syntax error. At the end of export.cpp, add a carriage return (a blank line). Is there a missing }?
0
 

Author Comment

by:gangclar
Comment Utility
I've checked that nothing is wrong, i.e. no missing ";" or "}" mismatched.

Do you happen to know where can I get samples of DLL codes that uses MFC, preferably a simple MFC DLL for teaching purpose.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
I mentioned the Advanced MFC Sample "DLLTRACE: Writes a DLL Statically Linked to the Framework Library" which comes with Visual C++.
0
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
Comment Utility
The error "Unexpected end of file" might be caused by including incorrect header files.

Don't include <windows.h>.

Include either "stdafx.h" or <afxwin.h>.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 23

Expert Comment

by:chensu
Comment Utility
And include your own header files after the standard header files.
0
 

Author Comment

by:gangclar
Comment Utility
I've looked at the referred sample, it's linking a DLL to the MFC framework. However, I'm attempting to use some MFC function within my codes that generate my existing DLL.

It looks complex to me, as I'm new to MFC.

Is there information/sample that show how to create MFC DLLs, especially the portion on exporting functions, e.g. how does the export definition file differs from the one I've quoted in the question ?

Thanks for the information
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
It seems that you want to create MFC extension DLLs. Look into the Advanced MFC Sample "DLLHUSK: Dynamically Links the MFC Library". And look into the documentation Visual C++ Programmer's Guide\Adding Program Functionality\Overviews\DLLs: Overview.
0
 

Author Comment

by:gangclar
Comment Utility
I don't want MFC ext DLLs.

It is possible to show me an example of a MFC DLL that will beep and count the number of beeps since called ? The beep counter should be displayed as a dialog box on screen when the function is called.

Thanks again.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Then the Advanced MFC Sample "DLLTRACE: Writes a DLL Statically Linked to the Framework Library" is similar to what you want. Just replace the code of the exported function with beeping and counting and displaying the dialog box.
0
 

Author Comment

by:gangclar
Comment Utility
I've tried DLLTRACE, but reported missing TRACE.LIB in my release directory.

I read  " void FAR PASCAL EXPORT ProcessDllIdle(); " in the traceapi.h.

I wonder shouldn't this be appearing in a .DEF file ?

0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
In Win32, FAR PASCAL EXPORT are defined as nothing.

You can also use __declspec(dllexport) to export the functions instead of using a DEF file.
0

Featured Post

IT, Stop Being Called Into Every Meeting

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!

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

10 Experts available now in Live!

Get 1:1 Help Now