This project demonstrates how to build a basic Windows DLL in C/C++, including:
- Exported and non-exported functions
- Use of
__declspec(dllexport)and__declspec(dllimport) - Handling DLL load/unload events using
DllMain - Providing a header file for consumers of the DLL
This repository contains two main files:
- mydll.cpp – Implementation of the DLL
- mydll.h – Public header for import/export macros
This function is marked with __declspec(dllexport) and can be called from any external program that loads the DLL:
DECLDIR void Share();When called, it prints:
I am an exported function, can be called outside the DLL
This function is not exported, meaning it can only be used inside the DLL:
void Keep();It prints:
I am not exported, can be called only within the DLL
The DLL defines DllMain, which is executed automatically by Windows whenever:
- The DLL is loaded
- A process or thread attaches/detaches
- The DLL is unloaded
In DLL_PROCESS_ATTACH, both Share() and Keep() are called automatically.
#ifdef DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR void Share();
void Keep();
}This header:
- Exports
Share()when building the DLL - Imports
Share()when used by external programs - Uses
extern "C"to prevent C++ name mangling
DECLDIR void Share() { ... }
void Keep() { ... }
BOOL APIENTRY DllMain(HANDLE hModule, DWORD reason, LPVOID reserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
Share();
Keep();
break;
...
}
}This file includes:
- The implementation of
Share()andKeep() - The
DllMainfunction that runs on DLL load/unload
-
Create a new Win32 Project
-
Choose DLL
-
Add
mydll.cppandmydll.h -
Ensure Preprocessor Definitions contains:
DLL_EXPORT -
Build → Build Solution
-
Visual Studio will generate:
mydll.dllmydll.lib
#include <Windows.h>
#include <iostream>
typedef void (*ShareFunc)();
int main()
{
HMODULE dll = LoadLibrary("mydll.dll");
if (!dll)
{
std::cout << "Failed to load DLL\n";
return 1;
}
ShareFunc Share = (ShareFunc)GetProcAddress(dll, "Share");
if (Share)
Share();
FreeLibrary(dll);
return 0;
}using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("mydll.dll")]
public static extern void Share();
static void Main()
{
Share();
}
}Share()is the only exported functionKeep()cannot be used externallyDllMainwill execute automatically upon DLL loadextern "C"ensures function names remain unmangled