im quite confused on how typedef int (__cdecl *MYPROC)(LPWSTR);
works exactly? how do you typedef a function? any explanation is greatly appreciated! thx :D

// A simple program that uses LoadLibrary and 
// GetProcAddress to access myPuts from Myputs.dll. 

#include <windows.h> 
#include <stdio.h> 

typedef int (__cdecl *MYPROC)(LPWSTR); 

VOID main(VOID) 
{ 
HINSTANCE hinstLib; 
MYPROC ProcAdd; 
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

// Get a handle to the DLL module.

hinstLib = LoadLibrary(TEXT("MyPuts.dll")); 

// If the handle is valid, try to get the function address.

if (hinstLib != NULL) 
{ 
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 

// If the function address is valid, call the function.

if (NULL != ProcAdd) 
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n"); 
}
// Free the DLL module.

fFreeResult = FreeLibrary(hinstLib); 
} 

// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess) 
printf("Message printed from executable\n"); 
}

how do you typedef a function?

You don't. The code you posted is a typedef for a pointer to a function.

Yes that is indeed a pointer to a function, a named one.

typedef void (*funcPtrType)(int someParam);
funcPtrType pSomeFunc = nullptr;
pSomeFunc = &someFuncWithTheRightParamsAndReturnType;
pSomeFunc(1000);

Edited 5 Years Ago by pseudorandom21: n/a

The typedef that you have shown is a typedef for a "function pointer" type. The format is a bit different than other "normal" typedefs. Here would be the simple format of such a typedef:

[B]typedef[/B] [I]return_type[/I] ([[I]calling_convention[/I]] *[B]identifier[/B]) ( [I]parameter_list[/I] );

This means that the following function signature (or prototype) would correspond to the function pointer type defined below:

//function signature (i.e. declaration or prototype):
int myFunction( char* s, int count);
//corresponding function pointer typedef:
typedef int (*myFunctionPtrType) (char*,int);

Which would allow it to be used as such:

int myFunction( char* s, int count) {
  //do something..
  return 0;
};

int main() {
  myFunctionPtrType p = &myFunction; //notice the syntax here, taking the address of myFunction.

  //now p can be called as if it was a function:
  char s[20];
  int result = p(s,20); //the call syntax is as if it was function.
  std::cout << "The result of calling p was: " << result << std::endl;
};

In the example you posted, the "__cdecl" keyword is simply the calling convention of the function (like __stdcall or __fastcall). These are important when using function pointers across different modules (and possibly different original programming languages). For instance, __cdecl is the default C calling convention and is supported by many other languages, and __stdcall is a standard calling convention that is mostly used by Win32 API to make it essentially callable from any programming language.

There are several other variants of this basic concept of "pointing to a function", which include pointer to member functions, function objects, and function types. Boost.Bind and Boost.Function are libraries that exploit those very well (and, with basic knowledge of templates, they are generally easier to use than classic function pointers and the like).

This article has been dead for over six months. Start a new discussion instead.