Calling an Exported Function in an EXE from Within a DLL | CodeGuru

Calling an Exported Function in an EXE from Within a DLL

Environment: VC6, SP4, 2K, SP2 This article explains a better way of calling a function in an EXE file from a dependent DLL of the same EXE. In other words, calling a DLL’s function from an EXE is the normal way; this is the reverse of the same. On CodeGuru I have found a couple […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 5, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6, SP4, 2K, SP2

This article explains a better way of calling a function in an EXE file from a dependent DLL of the same EXE. In other words, calling a DLL’s function from an EXE is the normal way; this is the reverse of the same.

On CodeGuru I have found a couple of FAQs talking about the same issue. They tell you to have a call back function to hold the pointer from the EXE and so on, but they look primitive. In this article, I have given a better method of calling functions from the EXE. I would be glad if this helps someone in need.

In the EXE:

// Do exactly as you would export a DLL…
extern “C”
{
  EXPORT void ExeFn(char * lpszMessage)
  {
    MessageBox(NULL,lpszMessage,”From Exe”,MB_OK);
  }
}
In the DLL:
        ...
// Get the handle of the EXE that loaded us.
FnPtrT FnPtr = (FnPtrT)::GetProcAddress(GetModuleHandle(NULL),
                "ExeFn");
if(FnPtr)
  (*FnPtr)("Message From The DLL");
else
  MessageBox(NULL,"It Did Not work :(","From DLL",MB_OK);
        ...

Downloads
Note: In this demo project, I have just implemented the above-mentioned exports. The EXE calls the DLL's function and that function calls a function in the EXE. Those functions do nothing more than display message boxes, so there's no point in giving a demo EXE. Just build the source; you must get the DLL and EXE.
How to Build It

Extract the source zip file. Open workspace "ExportingExe.dsw".
Ensure that ExportingExe is the active project. Now, you can build and run it.
Because Importing.DLL is a dependency of the other one, you don't have to bother about anything else.


Download source - 16 Kb
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.