Board index » delphi » DLL calling a function in the calling EXE

DLL calling a function in the calling EXE

Hello:

I have an EXE, lets call it PMain.exe that just opens plugins (DLL's)
It tracks what DLL's are loaded and the plugin instance to run functions
from the plugin.

I got PMain.exe opening the DLL's and that works great.  What I want to
know is how can a DLL run a function in PMain.exe, and get data returned
to it.  This is so that the plugins do not have to be developed with
common code, the common code can be in PMain.exe.  I am sure this does
not matter, but all the plugins are dynamically loaded and unloaded at
different times depending on certain circumstances.

Thanks

Troy Yoner

 

Re:DLL calling a function in the calling EXE


I think you can probably use LoadLibraryEx() on the EXE itself (from the DLL
code) and find the function you are looking for and invoke it from the dll.
(I'm assuming your dynamically linking the dll ).

Quote
"Troy Yoner" <T...@yoner.com> wrote in message

news:3B27DBBD.E15F968A@yoner.com...
Quote
> Hello:

> I have an EXE, lets call it PMain.exe that just opens plugins (DLL's)
> It tracks what DLL's are loaded and the plugin instance to run functions
> from the plugin.

> I got PMain.exe opening the DLL's and that works great.  What I want to
> know is how can a DLL run a function in PMain.exe, and get data returned
> to it.  This is so that the plugins do not have to be developed with
> common code, the common code can be in PMain.exe.  I am sure this does
> not matter, but all the plugins are dynamically loaded and unloaded at
> different times depending on certain circumstances.

> Thanks

> Troy Yoner

Re:DLL calling a function in the calling EXE


Troy,

You could have the program pass the DLLs a function pointer. This
could either be the address of each function you want to call back or
the address of a dispatcher within the EXE.

Andrue Cope
[Bicester, UK]

Re:DLL calling a function in the calling EXE


How do you pass a function pointer?

Troy Yoner

Quote
Andrue Cope wrote:
> Troy,

> You could have the program pass the DLLs a function pointer. This
> could either be the address of each function you want to call back or
> the address of a dispatcher within the EXE.

> Andrue Cope
> [Bicester, UK]

Re:DLL calling a function in the calling EXE


Troy,

It's a standard part of the C++ programming language but if you
haven't come across them yet here's an example:

// This declares a type called TExampleCallBack
typedef bool ( TExampleCallBack* )( char * wibble );
// It points to a function which returns a bool and takes a char *

// This would be within the DLL -------------------------------

// This defines a function which accepts a function pointer as an
//   argument then calls the function pointed to:
bool callTheCallBack( TExampleCallBack CallBackFunctionPtr )
{
  if( CallBackFunctionPtr )
    return CallBackFunctionPtr( "An example" );
  else
  {
    ShowMessage( "Call back pointer was NULL" );
    return false;
  }

Quote
}

// Both of these would be within the EXE ----------------------

// The function to be called back
bool IWillBeCalledBack( char * wibble )
{
  ShowMessage( wibble );

Quote
}

// The function which calls the function defined first
void GoForIt()
{
  if( callTheCallBack( IWillBeCalledBack ) )
    ShowMessage( "Result was true" );
  else
    ShowMessage( "Result was false" );

Quote
}

The execution sequence would be:

  GoForIt()                    // EXE
    callTheCallBack()          // DLL
      IWillBeCalledBack()      // EXE (from DLL)

You can have structures with function pointer members in which case
you end up with a crude kind of object. Although crude these
'objects' can be language independant and easier to export from a DLL
than a true object.

Andrue Cope
[Bicester, UK]

Other Threads