4

在 xp 32bit 中,此行编译没有问题,但在 vista 64bit 中,此行:

m_FuncAddr = ::GetProcAddress (somthing);

给出以下错误

错误 C2440:“=”:无法从“FARPROC”转换为“int (__cdecl *)(void)”

GetProcAddress 定义为

WINBASEAPI FARPROC WINAPI GetProcAddress (somthing)

和 m_FuncAddr 作为

int (WINAPI *m_FuncAddr)();

据我了解,两者都是stdcall。

为了避免我不得不提出的错误

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

我的问题:

如果 m_FuncAddr 和 GetProcAddress 都有 stdcall 调用约定,为什么我必须用 cdecl 来“调用”它?

VS项目设置'默认调用约定(设置为cdecl)是否有可能覆盖上面的assignemet语句?

提前致谢!

[编辑]

为了澄清这个问题:

在等式的一侧(比如第 1 侧)我有

int __stdcall * m_FuncAddr

在另一边(第 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 ?

4

2 回答 2

3

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)();
于 2008-10-17T01:38:39.607 回答
2

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.

于 2008-10-17T01:36:12.250 回答