RomanM
RomanM

Reputation: 6721

stdcall over-ride by visual studio?

In xp 32bit this line compiles with not problem however in vista 64bit this line:

m_FuncAddr = ::GetProcAddress (somthing);

gives the following error

error C2440: '=' : cannot convert from 'FARPROC' to 'int (__cdecl *)(void)'

GetProcAddress is defined as

WINBASEAPI FARPROC WINAPI GetProcAddress (somthing)

And m_FuncAddr as

int (WINAPI *m_FuncAddr)();

From what I understand both are stdcall's.

To avoid the error I had to put

m_FuncAddr = (int (__cdecl *)(void))::GetProcAddress(somthing);

My Question:

If both m_FuncAddr and GetProcAddress have the stdcall calling convention why do I have to 'recall' it with cdecl ?

Is it possible that the VS project setting 'default calling convention (which is set to cdecl) over-rides the assignemet statment above ?

Thanks in advance!

[Edit]

To clerfiy the question:

On one side of the equation (say side 1) i have

int __stdcall * m_FuncAddr

On other side (side 2)

INT_PTR far __stdcall GetProcAddress

So how is it that i have to cast side 2 with cdecl if both are stdcalls ? Or am I not getting something ?

Upvotes: 4

Views: 3835

Answers (2)

Adam Mitz
Adam Mitz

Reputation: 6043

The return type should be INT_PTR (a 64-bit value in 64-bit builds). You shouldn't cast around this error -- the compiler is trying to tell you that something is wrong.

From WinDef.h:

#ifdef _WIN64
typedef INT_PTR (FAR WINAPI *FARPROC)();

So the declaration of m_FuncAddr should be:

INT_PTR (WINAPI *m_FuncAddr)();

Upvotes: 3

Nick
Nick

Reputation: 6846

It's a coincidence that it compiles correctly in 32bit; the correct syntax is:

typedef int (WINAPI *FFuncType)();
FFuncType m_FuncAddr;
m_FuncAddr = (FFuncType)::GetProcAddress (somthing);

You need to explicitly cast the result of ::GetProcAddress to the proper function signature. In 32bit, FARPROC happens to work with the signature you have, but probably not in 64bit.

Edit: Yes, in fact, looking at windef.h, the return type is INT_PTR in 64bit, so that's why you got the compiler error. You will still need to cast to the function signature as above for any function which does not happen to match the placeholder for FARPROC, though, so you should be doing it as above in general.

Upvotes: 2

Related Questions