DLL Calling An App

Started by
11 comments, last by silvermace 19 years, 8 months ago
I know how to export functions from a DLL so that my app can call it but is it possible to do the opposite and give the DLL access to a number of functions in my App that it can call? For example my app may call : dllDoIt(); then in the dll i want to call the app back : void dllDoit() { appAddIt(); } How can this be done? Cheers
Advertisement
One solution is a callback function via function pointer.

Kuphryn
in an app you use GetProcAddress to get the dll's functions, why not reverse this? like my_SetProcAddress()

;)
All sounds good, I am actually writing a plugin mechanism for my App so i want to allow dll's to create entries on my app's menu etc. How do other popular App plugins (Max etc) achieve this? Do they make a certain number of functions available to the plugin dll?
From what I know, you would write code or script in the app that calls on the DLL. I've never written a max plug-in, but I remember seeing scripts you could write. In MS Office, for instance, you would write visual basic macros which call on your DLL. The visual basic would create the menus and interactions, while the DLL does the real dirty work (in C/C++ or whatever language).
cha cha cha
this is what i do in my editor

btw: IApplicationAccesor is just an abstract class that
accesses data on the App side, simple to do

just allocate on App side and pass to Dll when
calling pluginCallFunction.

Hope this helps,
Cheers
Dan

void (*APPMENUFUNC)( const char *title, int callId );int pluginInit( APPMENUFUNC addMenu ){     (*addMenu) ( "Do Something First", DLL_DOSOMETHING_FIRST );     (*addMenu) ( "Do Something Else",  DLL_DOSOMETHING_ELSE  );     (*addMenu) ( "Do Something Again", DLL_DOSOMETHING_AGAIN );     return 3; //number of menu items added}int pluginCallFunction( int callId, IApplicationAccesor *pApp ){  switch( callId )  {     case DLL_DOSOMETHING_FIRST:	return doSomethingFirst();     case DLL_DOSOMETHING_ELSE:  	doSomethingElse();     case DLL_DOSOMETHING_AGAIN:	doSomethingAgain();  }  return -1;}
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
So how does the plugin get hold of the functions to call in the app? GetProcAddress() cannot be used by a DLL because an App does not publish function names.

Is what I am trying to do a bad idea? Should a dll be calling an app?
What I did was that when I load my dlls, I pass in an instance of my app to the dll (usign GetModuleHandle()) which I can then use to later access "EXPORTed" functions from my main app. I dunno if there's any other way, but it worked for me...
What function did you use in your DLL to get the apps function addresses?
Well, that's the caveat... The main app HAS to publish the functions to be accessed by the dll. I don't think you can really run away from that. Hence the rational behind passing the HINSTANCE to the dll upon load-time.

This topic is closed to new replies.

Advertisement