我有这样的代码: (EXE)C++的typedef访问冲突,同时呼吁指针
#include <Windows.h>
#pragma comment(lib, "user32.lib")
class Dummy;
typedef void(Dummy::*Referece)(int i);
typedef void(*InitCall)(void*, Referece);
class Dummy
{
public:
Dummy(){}
void callMe(int val)
{
MessageBoxA(0, "ok", "ok", 0);
}
};
int main()
{
Dummy* obj = new Dummy();
HMODULE ha= LoadLibraryA("aa.dll");
InitCall val = (InitCall)GetProcAddress(ha, "Init");
val(obj, &Dummy::callMe);
}
和我的DLL: (.H)
#pragma once
#define DLL_EXPORT __declspec(dllexport)
class Test;
typedef void (Test::*Reference)(int a);
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT Init(Test* Object, Reference reference);
#ifdef __cplusplus
}
#endif
(的.cpp)
#include "your.h"
void DLL_EXPORT Init(Test * Object, Reference reference)
{
(Object->*reference)(1);
}
我转载系统,应该是这样的原因,我不能改变代码在一边。 为什么我访问违规?调用“val(obj,ref)”我期望一个指向方法调用的类+偏移量的指针。
Maybe'LoadLibraryA'返回NULL或'GetProcAddress'返回NULL? – VTT
您是否使用Visual Studio? – Angew
是的,我在Visual Studio上,没有loadlibraryA正确加载所有。通过调试,我可以达到“(对象 - > *引用)(1);”但呼吁导致崩溃。 – Ixiodor