我正在尝试使用VS2019从C ++项目中编译以上代码 如果我将主文件更改为main.c,它可以毫无问题地编译 但是,如果将其更改为main.cpp,则会出现C2440类型转换问题。 我什至尝试用 我该如何进行类型转换? 答案 0 :(得分:2) 您可以先将inline BOOL FupVmCall(ULONG_PTR hypercall_number, void *context) {
#pragma section(".asm", read, execute)
__declspec(allocate(".asm")) static const BYTE CODE[] = {
0x0F, 0x01, 0xC1, // vmcall
0x74, 0x0E, // jz short errorWithCode
0x72, 0x04, // jb short errorWithoutCode
0x48, 0x33, 0xC0, // xor rax, rax
0xC3, // retn
// errorWithoutCode:
0x48, 0xC7, 0xC0, 0x02, 0x00, 0x00, 0x00, // mov rax, 2
0xC3, // retn
// errorWithCode:
0x48, 0xC7, 0xC0, 0x01, 0x00, 0x00, 0x00, // mov rax, 1
0xC3, // retn
};
typedef unsigned char(__stdcall * AsmVmxCallType)(
_In_ ULONG_PTR hypercall_number, _In_opt_ void *context);
#pragma warning(suppress : 4055)
AsmVmxCallType AsmVmxCall = (AsmVmxCallType)CODE;
__try {
return AsmVmxCall(hypercall_number, context) == 0;
} __except (EXCEPTION_EXECUTE_HANDLER) {
SetLastError(GetExceptionCode());
return FALSE;
}
}
C2440 'type cast': cannot convert from 'const BYTE [27]' to 'AsmVmxCallType'
AsmVmxCallType AsmVmxCall = (AsmVmxCallType)CODE;
extern "C"
包装,但没有解决问题。1 个答案:
CODE
转换为空指针,然后将其转换为函数指针:AsmVmxCallType AsmVmxCall = (AsmVmxCallType)(void *)CODE;