• Welcome to Jose's Read Only Forum 2023.
 

ProgEx16 - Using A typedef With Function Pointers

Started by Frederick J. Harris, November 07, 2009, 03:10:40 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/*
  ProgEx16
 
  In this example we'll use C's typedef keyword to try to bring some sanity into
  this discussion of this rather complicated subject.  We might not succeed
  though; I say that because most folks doing this kind of work seem to prefer
  using typedefs.  As for myself, I think it makes it worse, but I suppose I'm
  in the minority.  In any case, if you deal with C/C++ code you had better
  start getting used to typedefs because they are everywhere.  A very simple
  example would be if you get tired of writing 'unsigned int' for variable
  declarations.  A very simple thing to do would be this...
 
  typedef unsigned int UINT;
 
  Then when you need to declare an unsigned int variable you can do this...
 
  UINT iNumber=25;
 
  instead of this...
 
  unsigned int iNumber=25;
 
  In other words, 'UINT' will be substituted for 'unsigned int'.  However, it
  can get considerably slicker than that.  In the last example ( ProgEx15 ) we
  had a function pointer declaration like so...
 
  void (__stdcall* pFn)(char*);
 
  ...and we had to put this cast in front of GetProcAddress() so that this
  Api function would assign its result to pFn...
 
  pFn=(void (__stdcall*)(char*))GetProcAddress(hInstance,"PRNT");
 
  If we use a typedef to create an alias for pFn like so...
 
  typedef void (__stdcall* FNPTR)(char*);
 
  ...what will happen then is that by simply using the character string 'FNPTR'
  in front of GetProcAddress() for the cast, the string
 
  void (__stdcall*)(char*)
 
  will actually be interpreted by the compiler as being there, and the compile
  will succeed.  It then looks like this...
 
  pFn=(FNPTR)GetProcAddress(hInstance,"PRNT");
 
  Here is another example from MSDN that involves a simple function declaration:
     
  typedef void DRAWF( int, int );

  This example provides the type DRAWF for a function returning no value and
  taking two int arguments. This means, for example, that the declaration

  DRAWF box;

  is equivalent to the declaration

  void box( int, int );
 
  Below is ProgEx15 modified to use a typedef.  Note that if you want the
  program to work you need to place DllPwrBasic.dll that we made in one of the
  earlier examples into this directory so that this program can load it.
*/

#include <windows.h>
#include <stdio.h>
typedef void (__stdcall* FNPTR)(char*);    //A rather complicated use of typedef


int main(void)
{
HINSTANCE hInstance;   
void (__stdcall* pFn)(char*);             //Declaration of function pointer
                                           //pFn that can be used to call any
hInstance=LoadLibrary("DllPwrBasic.dll"); //function for which you can obtain
if(hInstance)                             //its address if the function returns
{                                         //void and takes a single char* as a
    pFn=(FNPTR)GetProcAddress(hInstance,"PRNT");//parameter
    if(pFn)                          //In other words, assign to pFn the address
       pFn("Hello, World!\n");       //of "PRNT" in DllPwrBasic.dll cast to a
    else                             //standard call function that returns void,
       puts("Couldn't Find pFn!");   //i.e., nothing, and takes a char* as its
    FreeLibrary(hInstance);          //sole parameter.
}
else
    puts("Couldn't Load Library!");
getchar();
   
return 0;   
}