Menu

Confused by typedef and GetProcAddress

2008-07-12
2012-09-26
  • Fred Harris

    Fred Harris - 2008-07-12

    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

     
    • Fred Harris

      Fred Harris - 2008-07-14

      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

       
    • Fred Harris

      Fred Harris - 2008-07-14

      I received an answer elsewhere. I'll post it tomorrow.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.