0

Possible Duplicate:
Typedef function pointer?

TE0300_Open = (_TE0300_Open)GetProcAddress(hInstLibrary, "TE0300_Open");
typedef int (WINAPI *_TE0300_Open)(unsigned int* PHandle, int CardNo);

Can someone explain me what this piece of code does? I know that typedef is used to assign alternative names to existing types but I don't understand this case at all.

Community
  • 1
  • 1
Reginald
  • 21
  • 2
  • Possible duplicate of [Typedef function pointer?](http://stackoverflow.com/questions/4295432/typedef-function-pointer) and [Typedef with two sets of brackets?](http://stackoverflow.com/questions/9515739/typedef-with-two-sets-of-brackets) – In silico Apr 07 '12 at 01:47

2 Answers2

4

typedef int (WINAPI *_TE0300_Open)(unsigned int* PHandle, int CardNo);

This line typedefs a function pointer to a WINAPI calling convention function returning an int, and taking an unsigned int * and an int. The function pointer type is given the alias _TE0300_Open.

Consider the following example:

typedef void (*func)();

void foo (func f) //notice we have a nice type name here
{
    cout << "Calling function...";
    f();
}

void bar(){}

int main()
{
    foo (bar);
}

I believe C++11 added support for less icky syntax when using function pointers as well:

using func = void (*)();  

As for your GetProcAddress call, this loads a function from a library. You assign it to a function pointer, and you can use that function pointer as you would the original function.

In your example, you can now call TE0300_Open as you would normally call _TE0300_Open. It should also be noted that _TE0300_Open is a name that is reserved for the implementation.

chris
  • 60,560
  • 13
  • 143
  • 205
2

It's declaring a typedef, _TE0300_Open, for a function-pointer.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680