armundle
armundle

Reputation: 1179

Why use __stdcall for function pointer

I was looking at some code like the one mentioned here: Function pointers for winapi functions (stdcall/cdecl), Function pointer and calling convention, etc.

What is the need, benefit for declaring the calling type of a function pointer as __stdcall?

Why declare the calling type for a function pointer at all?

Upvotes: 3

Views: 1382

Answers (1)

Marco A.
Marco A.

Reputation: 43662

Embedding a calling convention specifier in a function pointer allows you to use that calling convention when calling functions through that pointer. __stdcall is the calling convention used to call Win32 API functions.

The benefit of specifying it in a function pointer is being able to match a different calling convention according to your code's needs (e.g. when loading an external library's function via late binding). Library headers should specify them as a good programming practice.

There's a caveat though: if you embed a wrong calling convention in a function pointer, the compiler might be powerless to help you detect that and you might end up doing bad things at runtime.

Upvotes: 3

Related Questions