none
Invoking an Exe from a DLL and calling its function RRS feed

  • Question

  • Hi All,

    I have a DLL and an EXE. Both written in VC++.

    I want to programatically invoke the EXE from the DLL and then call some functions of the EXE from the DLL? Please help me in understanding how this can be achieved?

    Basically the flow of execution of the functions should be 

    DLL -> EXE -> other modules.

    Thanks.


    Arun Kumar M

    Tuesday, March 22, 2016 1:33 PM

Answers

  • Hi All,

    I have a DLL and an EXE. Both written in VC++.

    I want to programatically invoke the EXE from the DLL and then call some functions of the EXE from the DLL? Please help me in understanding how this can be achieved?

    Basically the flow of execution of the functions should be

    DLL -> EXE -> other modules.

    Thanks.


    Arun Kumar M

    You can export functions from an EXE using __declspec(dllexport) just as you can for a DLL, and use GetProcAddress() to obtain appropriate function pointer. See for example

    http://www.codeguru.com/cpp/w-p/dll/article.php/c3649/Calling-an-Exported-Function-in-an-EXE-from-Within-a-DLL.htm

    Another way would be to pass a function pointer (or object) from the EXE to the DLL, and have the DLL use that. This way only the DLL would have to export functions.


    David Wilkinson | Visual C++ MVP

    Tuesday, March 22, 2016 1:58 PM

All replies

  • Hi All,

    I have a DLL and an EXE. Both written in VC++.

    I want to programatically invoke the EXE from the DLL and then call some functions of the EXE from the DLL? Please help me in understanding how this can be achieved?

    Basically the flow of execution of the functions should be

    DLL -> EXE -> other modules.

    Thanks.


    Arun Kumar M

    You can export functions from an EXE using __declspec(dllexport) just as you can for a DLL, and use GetProcAddress() to obtain appropriate function pointer. See for example

    http://www.codeguru.com/cpp/w-p/dll/article.php/c3649/Calling-an-Exported-Function-in-an-EXE-from-Within-a-DLL.htm

    Another way would be to pass a function pointer (or object) from the EXE to the DLL, and have the DLL use that. This way only the DLL would have to export functions.


    David Wilkinson | Visual C++ MVP

    Tuesday, March 22, 2016 1:58 PM
  • Hi,

    I agree with David Wilkinson

    GetProcAddress can also be called from a DLL on an EXE to get and execute functions from within an EXE

    If you have free time, Please see the article.

    https://john.nachtimwald.com/2012/07/15/calling-functions-in-exe-from-plugins-in-windows/

    if your original question, Please mark the thread to help other people with same problems to save time.

    Best Regards,

    Hart

    Wednesday, March 23, 2016 2:17 AM
    Moderator
  • Hi David, Hart,

    Thanks for the advice.

    The information's in these links are for the cases where the DLL is loaded by the EXE and hence the DLL can call the exported functions of the EXE.

    But for my case EXE and DLL are independent of each other or the EXE is invoked by the DLL using CreateProcess(). In this case how to access the exported functions of the EXE from the DLL ?

    Thanks.


    Arun Kumar M

    Wednesday, March 23, 2016 12:52 PM
  • But for my case EXE and DLL are independent of each other or the EXE is invoked by the DLL using CreateProcess(). In this case how to access the exported functions of the EXE from the DLL ?


    You need a module handle in order to use GetProcAddress to obtain an exported function.  However, the module must be mapped into the process making the GetProcAddress call, which will not be the case when an EXE is started using CreateProcess.

    However, you can still use an exported function from an EXE by using LoadLibrary to load the EXE just as if it was a DLL.  The exported function will be accessible to GetProcAddress through the module loaded into the calling process address space, not through the instance started through CreateProcess.


    • Edited by RLWA32 Wednesday, March 23, 2016 1:26 PM
    Wednesday, March 23, 2016 1:25 PM