Below is dllWork.cpp which dll exports a function named Prt() which returns void and takes a pointer to a character string as a parameter, which parameter it outputs to the console with printf(). Following that is dllHost.cpp. This program uses LoadLibrary() to explicitely load dllWork.cpp, then uses GetProcAddress() to get the address of the single Prt() function in dllWork.cpp. It then passes the literal string “Hello, World!” to the Prt() function in dllWork, which function dutifully outputs the string to the console as shown in the output below.
As you can see at the beginning of dllHost.cpp I used a typedef in the typical manner of all C/C++ programmers to facilitate the casting of the return from GetProcAddress() to a usable pointer to void function that takes one char pointer parameter, i.e.,
typedef void (FNPTR)(char);
This all works as expected. However, there is a question here. How do you cast the return from GetProcAddress() without using a typedef? I’ve played with this a lot and have come to the conclusion that it can’t be done, and that what typedef does is pure, unadulterated magic and witchcraft, completely beyond the ken of human logic and/or understanding.
Below is dllWork.cpp which dll exports a function named Prt() which returns void and takes a pointer to a character string as a parameter, which parameter it outputs to the console with printf(). Following that is dllHost.cpp. This program uses LoadLibrary() to explicitely load dllWork.cpp, then uses GetProcAddress() to get the address of the single Prt() function in dllWork.cpp. It then passes the literal string “Hello, World!” to the Prt() function in dllWork, which function dutifully outputs the string to the console as shown in the output below.
As you can see at the beginning of dllHost.cpp I used a typedef in the typical manner of all C/C++ programmers to facilitate the casting of the return from GetProcAddress() to a usable pointer to void function that takes one char pointer parameter, i.e.,
typedef void (FNPTR)(char);
This all works as expected. However, there is a question here. How do you cast the return from GetProcAddress() without using a typedef? I’ve played with this a lot and have come to the conclusion that it can’t be done, and that what typedef does is pure, unadulterated magic and witchcraft, completely beyond the ken of human logic and/or understanding.
//Begin dllWork.cpp
include <windows.h>
include <stdio.h>
extern "C" __declspec(dllexport) void Prt(char* msg)
{
printf("%s\n",msg);
}
//End dllWork.cpp
//Begin dllHost.cpp
include <windows.h>
include <stdlib.h>
typedef void (FNPTR)(char);
int main(void)
{
char szMsg[]="Hello, World!";
HINSTANCE hIns;
FNPTR pFn;
hIns=LoadLibrary("dllWork.dll");
if(hIns)
{
pFn=(FNPTR)GetProcAddress(hIns,"Prt");
pFn(szMsg);
FreeLibrary(hIns);
}
system("PAUSE");
return 0;
}
//End dllHost.cpp
OUTPUT:
Hello, World!
Press any key to continue . . .
End Output
Here is the working example showing how to use the return address from GetProcAddress() without a typedef...
//Begin dllHost.cpp
include <windows.h>
include <stdlib.h>
int main(void)
{
char szMsg[]="Hello, World!";
void (pFn)(char) = 0;
HINSTANCE hIns;
hIns=LoadLibrary("dllWork.dll");
if(hIns)
{
pFn=(void(__cdecl)(char))GetProcAddress(hIns,"Prt");
pFn(szMsg);
FreeLibrary(hIns);
}
system("PAUSE");
return 0;
}
AncientDragon at www.daniweb.com was able to tell me how to do it.
http://www.daniweb.com/forums/thread134223.html
I received an answer elsewhere. I'll post it tomorrow.