main.c:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | #include <windows.h>
#include <stdio.h>
__declspec(dllexport) void main_fun()
{
printf("Hello from main.exe\n");
}
int main()
{
HMODULE m = LoadLibraryA("library.dll");
void (*fun)() = (void*)GetProcAddress(m, "library_fun");
printf("Calling library_fun\n");
fun();
printf("Back in main()\n");
}
|
library.c:
| #include <stdio.h>
void main_fun();
__declspec(dllexport) void library_fun()
{
printf("Hello from library.dll\n");
main_fun();
printf("Finishing library_fun()\n");
}
|
Compiling:
| cl main.c
cl /LD library.c main.lib
|
Output of running main.exe:
| Calling library_fun
Hello from library.dll
Hello from main.exe
Finishing library_fun()
Back in main()
|
Note that __declspec(dllexport) or dllimport is not really needed. That's just one way how to export symbols. You can instead do /EXPORT or /DEF in linker arguments. It's your choice. They all do same thing.