Im trying to load a function from a DLL only, however GetProcAddress() returns NULL.
I just wanted to try loading it in another way than what casey did with all that:
#define -> typedef thingy... :)
Person.h
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) { } |
Person.cpp
1 2 3 4 5 6 7 | #include "Person.h" extern "C" void printAge(Person *p) { printf("\nAge of Person: %d\n", p->age); } |
main.cpp
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; |
b.bat
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~ |