This is my first post! I can’t figure out how to modify/cast the return address obtained from the Windows Api function GetProcAddress() without using a typedef. I’m aware that using a typedef is the standard way to do this, but one should be able to do it without a typedef, shouldn’t one???
I’ll provide a very simple example below for anyone to use if someone would be willing to instruct me on this. Lets make a simple wrapper around the printf function and export it from a dll like so….
//dllWork.cpp
#include <windows.h>
#include <stdio.h>
extern "C" __declspec(dllexport) void Prt(char* msg)
{
printf("%s\n",msg);
}
This can be compiled with the Microsoft C++ compiler like so…
>cl dllWork.cpp /LD
All it does when called is output to the console the asciiz string passed to it with printf. Below is a host program to explicitely load it and obtain the address of the Prt function with GetProcAddress()…
//Begin dllHost.cpp
#include <windows.h>
#include <stdlib.h>
typedef void (*FNPTR)(char*); <<<note typedef!!!
int main(void)
{
char szMsg[]="Hello, World!";
HINSTANCE hIns;
FNPTR pFn;
hIns=LoadLibrary("dllWork.dll");
if(hIns)
{
pFn=(FNPTR)GetProcAddress(hIns,"Prt"); <<<note use of typedef
pFn(szMsg);
FreeLibrary(hIns);
}
system("PAUSE");
return 0;
}
>cl dllHost.cpp
As you can see, it uses a typedef in the customary manner to obtain a usable function pointer out of GetProcAddress(). The program calls Prt in the dll passing it the asciiz string “Hello, World!”.
My question is, “How can this be done without typedef?” I’ve tried for hours, and the only conclusion I can come to is that typedef is magic or witchcraft and beyond the ken of any human understanding. Would anyone be willing to prove me wrong? Honestly, I've been trying to do this for hours and can't get it.
PS Wow!!! I love your code tags and syntax highlighting!!!