将回调函数从32位移植到64位
问题描述:
我在使用Visual Studio 2008 Professional构建。当我使用Win32编译时,它编译得很好。将回调函数从32位移植到64位
但是当我切换到64位,然后我得到这个编译错误:
error C2664: 'lineInitializeExA' : cannot convert parameter 3 from 'void (__cdecl *)(DWORD,DWORD,DWORD,DWORD,DWORD,DWORD)' to 'LINECALLBACK'
None of the functions with this name in scope match the target type
一些定义/的typedef ::
typedef unsigned long DWORD;
#define CALLBACK __stdcall
在tapi.h
而且LINECALLBACK的定义是这样的:
typedef void (CALLBACK * LINECALLBACK)(
DWORD hDevice,
DWORD dwMessage,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1,
DWORD_PTR dwParam2,
DWORD_PTR dwParam3
);
在Windows上无符号长整型在32和64位平台上是32位宽。所以肯定这不是问题。
任何想法为什么?以及如何解决?
这是代码。
#include <tapi.h> // Windows tapi API
#include <stdio.h>
/*
I know it says tapi32.lib but I believe that is just old naming -
don't think 32 bit specific. and in any case don't get to linking phase
*/
#pragma comment(lib,"tapi32.lib")
void CALLBACK my_callback(DWORD dwDevice,
DWORD nMsg,
DWORD dwCallbackInstance,
DWORD dwParam1,
DWORD dwParam2,
DWORD dwParam3) {
printf("my_callback called\n");
}
int main() {
LONG result = -1;
DWORD dwAPIInit = 0x00020002;
HLINEAPP happ; // application handle
DWORD numlines; // Number of line devices in system.
result = lineInitializeEx (&happ, GetModuleHandle(0),
my_callback, "TAPITEST", &numlines, &dwAPIInit, 0);
return 0;
}
****编辑。不知道为什么,但我所看到的DWORD_PTR为:
typedef unsigned long DWORD_PTR;
但在检查时使用:
typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
所以我的回调定义是错误的!
答
在参数的声明本
void (CALLBACK * LINECALLBACK)(
DWORD hDevice,
DWORD dwMessage,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1,
DWORD_PTR dwParam2,
DWORD_PTR dwParam3
);
而言是不一样的,因为这:
void CALLBACK my_callback(
DWORD dwDevice,
DWORD nMsg,
DWORD dwCallbackInstance,
DWORD dwParam1,
DWORD dwParam2,
DWORD dwParam3
):
参数3至图6是在第二的第一decarations和整数指针。
由于omn 32bit Windows DWORD
s具有与可能编译的指针相同的大小。
在64位Windows上,指针的大小与DWORD
不同。
确实,但DWORD和DWORD_PTR都是无符号长整型,所以它们应该是等价的。刚刚尝试过,并修复! – 2014-10-10 11:25:07
也许,也许不是。显然它不是这种情况。你确定DWORD_PTR没有被typedefed为DWORD *吗?如果是这种情况,它将是一个64位的类型,而DWORD可能是一个32位的类型。 – Clearer 2014-10-10 11:27:34
Ahhh intellisence给了我错误的信息 - typedef for DWORD_PTR实际上是:typedef ULONG_PTR DWORD_PTR,* PDWORD_PTR;这就解释了。谢谢。 – 2014-10-10 11:29:26