Click here to Skip to main content
15,897,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Export Member function / class Pin
David Crow24-Mar-05 5:43
David Crow24-Mar-05 5:43 
GeneralRe: Export Member function / class Pin
Steve Messer24-Mar-05 7:01
Steve Messer24-Mar-05 7:01 
GeneralRe: Export Member function / class Pin
cmk24-Mar-05 8:34
cmk24-Mar-05 8:34 
GeneralRe: Export Member function / class Pin
Steve Messer24-Mar-05 8:41
Steve Messer24-Mar-05 8:41 
GeneralRe: Export Member function / class Pin
cmk24-Mar-05 10:17
cmk24-Mar-05 10:17 
GeneralRe: Export Member function / class Pin
cmk24-Mar-05 10:24
cmk24-Mar-05 10:24 
GeneralRe: Export Member function / class Pin
Steve Messer24-Mar-05 13:21
Steve Messer24-Mar-05 13:21 
GeneralRe: Export Member function / class Pin
cmk24-Mar-05 20:49
cmk24-Mar-05 20:49 
smesser wrote:
This code works fine from the exe but externally it complains that CCOREDlg is unknown.
Maybe I was doing something wrong??


Yeah, maybe. Smile | :)

Here are two chunks that i whipped up to test things - it works.
- build the exe as a console application
- build the dll as a non-mfc dll
- exe project defines MYEXE_EXPORTS
- dll project defines MYDLL_EXPORTS
- build exe first, copy .lib file to dll project directory
- build dll, copy .dll file to exe output directory (Debug/Release, where .exe is)
- debug exe and step through code
- or copy exe and dll to empty dir and run exe dll, watch the magic happen.

First the exe code:
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
#include "stdafx.h"<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
#ifdef  MYEXE_EXPORTS<br />
#define MYEXE_API __declspec(dllexport)<br />
#else<br />
#define MYEXE_API __declspec(dllimport)<br />
#endif<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
// forward declare<br />
class  MyApp;<br />
<br />
// function prototypes for plugin exports<br />
typedef  bool  (*PLUGININIT)  ( MyApp *APP );<br />
typedef  void  (*PLUGINTERM)  ( void );<br />
typedef  bool  (*APPTOPLUGIN) ( long MSGID, char *MSGSTR );<br />
<br />
// function pointers to be used by app<br />
PLUGININIT   PluginInit  = NULL;<br />
PLUGINTERM   PluginTerm  = NULL;<br />
APPTOPLUGIN  AppToPlugin = NULL;<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
class  MYEXE_API  MyApp {<br />
public:<br />
	int   Run         ( void );<br />
	bool  PluginToApp ( long MSGID, char *MSGSTR );<br />
};<br />
<br />
//...............................................<br />
<br />
int  MyApp::Run( void )<br />
{<br />
	// simple test, app sends plugin msg, plugin echos back<br />
	AppToPlugin(1, "First  Msg");<br />
	AppToPlugin(2, "Second Msg");<br />
	return(0);<br />
}<br />
<br />
//...............................................<br />
<br />
// plugin calls this to send msg to app<br />
bool  MyApp::PluginToApp( long MSGID, char *MSGSTR )<br />
{<br />
	if( MSGSTR )  fprintf(stdout, "App got from Plugin: %ld %s \n", MSGID, MSGSTR);<br />
	return(true);<br />
}<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
int main( int argc, char* argv[] )<br />
{<br />
	MyApp  app;<br />
<br />
	// load the dll - the first arg to the program<br />
	// e.g. run as: > MyExe.exe  MyDll<br />
	HMODULE  lib = ::LoadLibrary(argv[1]);<br />
	if( !lib )  return(-1);<br />
<br />
	// get plugin exported functions<br />
	// note: the ones exported as C++ functions use the mangled names<br />
	PluginInit  = (PLUGININIT)  ::GetProcAddress(lib, "?PluginInit@@YA_NPAVMyApp@@@Z");<br />
	PluginTerm  = (PLUGINTERM)  ::GetProcAddress(lib, "?PluginTerm@@YAXXZ");<br />
	AppToPlugin = (APPTOPLUGIN) ::GetProcAddress(lib, "AppToPlugin");<br />
<br />
	if( !PluginInit )   return(-2);<br />
	if( !PluginTerm )   return(-3);<br />
	if( !AppToPlugin )  return(-4);<br />
<br />
	// tell plugin what app object it should talk to <br />
	if( !PluginInit(&app) )  return(-5);<br />
<br />
	// start app processing<br />
	int  rc = app.Run();<br />
<br />
	// tell plugin we are shutting down<br />
	PluginTerm();<br />
<br />
	::FreeLibrary(lib);<br />
<br />
	return(rc);<br />
}<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
<br />
Now the dll code:<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
#include "stdafx.h"<br />
#include "MyDll.h"<br />
#include <stdio.h><br />
<br />
// need to link to MyExe.lib to get MyApp methods<br />
#pragma  comment(lib, "MyExe")<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
BOOL APIENTRY DllMain( HANDLE MOD, DWORD REASON, LPVOID RES )<br />
{<br />
    switch( REASON ) {<br />
		case DLL_PROCESS_ATTACH :<br />
		case DLL_THREAD_ATTACH  :<br />
		case DLL_THREAD_DETACH  :<br />
		case DLL_PROCESS_DETACH :  break;<br />
    }<br />
    return TRUE;<br />
}<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
// Header File:<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
#ifdef  MYDLL_EXPORTS<br />
#define MYDLL_API __declspec(dllexport)<br />
#else<br />
#define MYDLL_API __declspec(dllimport)<br />
#endif<br />
<br />
//===============================================<br />
<br />
// included from app header<br />
class  MyApp {<br />
public:<br />
	MyApp( void );<br />
<br />
	long  MsgId;<br />
	int   Run         ( void );<br />
	bool  PluginToApp ( long MSGID, char *MSGSTR );<br />
};<br />
<br />
//===============================================<br />
<br />
// these we will export as C++ functions<br />
MYDLL_API  bool  PluginInit  ( MyApp *APP );<br />
MYDLL_API  void  PluginTerm  ( void );<br />
<br />
#ifdef __cplusplus<br />
extern "C" {<br />
#endif<br />
<br />
// this will be exported as a C function<br />
MYDLL_API  bool  AppToPlugin ( long MSGID, char *MSGSTR );<br />
<br />
#ifdef __cplusplus<br />
}<br />
#endif<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
// Source File:<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />
<br />
MyApp  *App = NULL;<br />
<br />
//===============================================<br />
<br />
bool  PluginInit( MyApp *APP )<br />
{<br />
	App = APP;<br />
	return(App?true:false );<br />
}<br />
<br />
//...............................................<br />
<br />
void  PluginTerm( void )<br />
{<br />
	App = NULL;<br />
}<br />
<br />
//...............................................<br />
<br />
#ifdef __cplusplus<br />
extern "C" {<br />
#endif<br />
<br />
bool  AppToPlugin( long MSGID, char *MSGSTR )<br />
{<br />
	if( MSGSTR )  fprintf(stdout, "Plugin got from App: %ld %s \n", MSGID, MSGSTR);<br />
<br />
	// echo back<br />
	if( App )  App->PluginToApp(MSGID, MSGSTR);<br />
<br />
	return(true);<br />
}<br />
<br />
#ifdef __cplusplus<br />
}<br />
#endif<br />
<br />
//////////////////////////////////////////////////////////////////////////<br />
//////////////////////////////////////////////////////////////////////////<br />



...cmk

Save the whales - collect the whole set
GeneralRe: Export Member function / class Pin
Steve Messer25-Mar-05 5:25
Steve Messer25-Mar-05 5:25 
GeneralRe: Export Member function / class Pin
Steve Messer25-Mar-05 9:51
Steve Messer25-Mar-05 9:51 
GeneralRe: Export Member function / class Pin
Steve Messer24-Mar-05 19:11
Steve Messer24-Mar-05 19:11 
GeneralRe: Export Member function / class Pin
David Crow24-Mar-05 8:38
David Crow24-Mar-05 8:38 
GeneralRe: Export Member function / class Pin
Steve Messer24-Mar-05 13:27
Steve Messer24-Mar-05 13:27 
GeneralDundas OXMultiComboBox crashes Pin
Bernhard23-Mar-05 20:01
Bernhard23-Mar-05 20:01 
Questionado can not run when WinXP update to sp2? Pin
Lido Paul23-Mar-05 19:54
Lido Paul23-Mar-05 19:54 
QuestionEnd of file? Pin
sacoskun23-Mar-05 19:49
sacoskun23-Mar-05 19:49 
AnswerRe: End of file? Pin
Steve Mayfield23-Mar-05 20:28
Steve Mayfield23-Mar-05 20:28 
GeneralRe: End of file? Pin
sacoskun23-Mar-05 20:38
sacoskun23-Mar-05 20:38 
GeneralRe: End of file? Pin
toxcct23-Mar-05 22:20
toxcct23-Mar-05 22:20 
AnswerRe: End of file? Pin
eli1502197923-Mar-05 22:33
eli1502197923-Mar-05 22:33 
GeneralIt complies and links but .... Pin
brilliant10123-Mar-05 18:11
brilliant10123-Mar-05 18:11 
GeneralMFC/GDI Chart Pin
Anonymous23-Mar-05 17:54
Anonymous23-Mar-05 17:54 
GeneralCustom DateTimePicker needed Pin
Mr. Malloc23-Mar-05 17:20
Mr. Malloc23-Mar-05 17:20 
GeneralReading and Loading files Pin
Notsosuperhero23-Mar-05 15:51
Notsosuperhero23-Mar-05 15:51 
GeneralRe: Reading and Loading files Pin
David Crow23-Mar-05 16:43
David Crow23-Mar-05 16:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.