1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdio.h> struct Person { char *name; int age; }; extern "C" void printAge(Person *p); typedef void PFNPRINTAGE(Person *p); void printAgeSTUB(Person *p) { } |
1 2 3 4 5 6 7 | #include "Person.h" extern "C" void printAge(Person *p) { printf("\nAge of Person: %d\n", p->age); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <windows.h> #include <stdlib.h> #include <string.h> #include "Person.h" int main() { Person p1 = {}; p1.age = 11; HMODULE PersonDLL = LoadLibrary("Person.dll"); PFNPRINTAGE *printAge = printAgeSTUB; if(PersonDLL) { printAge = (PFNPRINTAGE*)GetProcAddress(PersonDLL, "printAge"); DWORD err = GetLastError(); printAge(&p1); } FreeLibrary(PersonDLL); return 0; |
1 2 3 4 5 6 7 8 9 10 11 12 | @echo off IF NOT EXIST ..\build mkdir ..\build pushd ..\build cl -nologo -Zi ..\src\Person.cpp -LD cl -nologo -Zi ..\src\playGround.cpp /link -incremental:no user32.lib kernel32.lib winmm.lib gdi32.lib popd del *.cpp~ *.bat~ *.un~ *.txt~ |
1 2 3 | extern "C" __declspec(dllexport) void printAge(Person *p); |
1 | cl.exe ... /link /export:printAge |
1 | dumpbin /exports Person.dll |
mmozeiko
You did not instruct linker to export function from dll file.
You can do in two ways. One with compiler directive:
1 2 3 extern "C" __declspec(dllexport) void printAge(Person *p);
Another with linker argument:
1 cl.exe ... /link /export:printAge
You can verify if DLL has this symbol exported by using dumpbin.exe utility:
1 dumpbin /exports Person.dll
If it does not show your "printAge", that means something was not done properly.