2

I have a C++ console application & a DLL. In the C++ application I see the following snippet ::

typedef DWORD (WINAPI* functABC)(unsigned long*);

functABC functABC111;
HMODULE handleDLL = LOadLibrary("a.DLL");
functABC111 = (functABC)GetProcAddress(handleDLL,"function_1");

At a high level I understand that we are getting the function pointer to the function in a.DLL "function_1()".

But want to understand the 1st 2 lines in the above snippet ::

typedef DWORD (WINAPI* functABC)(unsigned long*);
functABC functABC111;

2 questions :: 1) Is the name "functABC" just a random function pointer name?
2) What are we technically doing in these 2 lines. Declaring function pointer.
3) Why do we need to use WINAPI* in the function pointer declaration in 1st line.

THanks in advance.

codeLover
  • 3,720
  • 10
  • 65
  • 121

3 Answers3

4
  1. 'functABC' is a typedef to a function returning a DWORD taking an unsigned long pointer as a parameter

  2. First lines defines a typedef and second one creates a function pointer using it

  3. 'WINAPI' is a macro that's usually expanded to '__stdcall' which is the calling convention used by Microsoft to export functions from a .DLL

user1233963
  • 1,450
  • 15
  • 41
  • 2 questions :: "functABC" is just a typedef name, so obviously we can give anyName right. And can the function pointer variable created out of this typedef (functABC111 in our case) be used to point to any function with this signature?? If there are multiple functions with same signature then we need to create various function pointer variables like functABC111_1, functABC111_2 etc... all of the type functABC. AM I right ? – codeLover Sep 20 '13 at 09:47
  • Your third point is somewhat misleading. There is no requirement to use `__stdcall` when exporting a function from a .dll. You can just as well export a function using `__cdecl`. It's important that the function declaration and implementation have matching calling conventions though. Things are less complicated for x64: There is only a single calling convention. – IInspectable Sep 20 '13 at 10:18
3

3) Almost all Windows functions (from shell32.dll, user32.dll, and all other) are declared as __stdcall, or as WINAPI (same thing). It is not necessary to declare functions in DLL as WINAPI, but people just follow Microsoft's lead. The code will be a few bytes smaller, and execution a few nanoseconds shorter.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
2

2) What are we technically doing in these 2 lines. Declaring function pointer.

First a type is defined that can be used to point to any function that follows the prototype DWORD WINAPI funcName(unsigned long*);. Then a variable of that type is created.

3) Why do we need to use WINAPI* in the function pointer declaration in 1st line.

Because function_1 is using the WINAPI calling convention (usually defined as __stdcall). Or at least this code assumes that it does.

Michael
  • 57,169
  • 9
  • 80
  • 125