C++ cli 无法从非托管C+;调用托管CLI方法的约定+;
我试图从非托管代码调用托管方法。但是,托管代码要求我使用“(y-CLRCLAK)调用约定,而我的非托管C++代码拒绝让我使用Y-YCLCLCALL调用约定而不使用/CLR选项。我不相信我想这样做,因为非托管项目不是我的,我不想改为托管项目 我已经在托管端构建了所有这些委托和函数指针封送,正如我在CodeGuru和MSDN上看到的那样,但是这个错误仍然会弹出,除非我使用不能成为ref类成员的u stdcall约定调用静态函数。该方法需要是ref类的实例 我能够绕过这个问题的唯一方法是从程序集中的非托管代码执行方法调用,跳过调用后ESP寄存器的弹出(添加4)(该调用在参数中有一个参数,因此是“4”)。这让我可以有效地打电话。这一选择太糟糕了 有人对如何解决这个问题有什么想法吗?这肯定是可能的。我遗漏了一些简单但重要的信息 以下是我的委托定义(所有这些都属于单个引用类ManagedSensor):C++ cli 无法从非托管C+;调用托管CLI方法的约定+;,c++-cli,marshalling,unmanaged,managed,C++ Cli,Marshalling,Unmanaged,Managed,我试图从非托管代码调用托管方法。但是,托管代码要求我使用“(y-CLRCLAK)调用约定,而我的非托管C++代码拒绝让我使用Y-YCLCLCALL调用约定而不使用/CLR选项。我不相信我想这样做,因为非托管项目不是我的,我不想改为托管项目 我已经在托管端构建了所有这些委托和函数指针封送,正如我在CodeGuru和MSDN上看到的那样,但是这个错误仍然会弹出,除非我使用不能成为ref类成员的u stdcall约定调用静态函数。该方法需要是ref类的实例 我能够绕过这个问题的唯一方法是从程序集中的非
希望有人对这个问题有一个好的答案。我可能错过了一些愚蠢的东西。这是受支持的。您可以使用一个属性告诉CLR它为委托创建的thunk应该使用_cdecl调用约定。让它看起来像这样:
using namespace System::Runtime::InteropServices;
[UnmanagedFunctionPointer(CallingConvention::Cdecl)]
delegate void OxpOnReceivedMeasurement(std::vector<OBX> *obxes);
使用命名空间System::Runtime::InteropServices;
[非托管函数指针(调用约定::Cdecl)]
委托void OxpOnReceivedMeasurement(标准::向量*对象);
再次感谢!在不到24小时内完成两次。这比用内联汇编吹走一条add-esp,4语句要好得多。非常感谢。
void LocalOxpMeasurementCallback(std::vector<OBX> *obxes);
// Now to make the 'delegate' unmanaged function pointer usable for unmanged code
// The argument is the unmanaged callback.
OxpOnReceivedMeasurement ^oxpMeasurementCallbackFP = gcnew OxpOnReceivedMeasurement(this, &ManagedSensor::LocalOxpMeasurementCallback);
// Lock it so the garbage collector doesnt get rid of it or move it
gch = GCHandle::Alloc(oxpMeasurementCallbackFP);
// Pass the marshaled function pointer to the unmanaged code
IntPtr ip = Marshal::GetFunctionPointerForDelegate(oxpMeasurementCallbackFP);
// fnOnReceiveMeasurement is defined in OxpLibTransports.h which matches the delegate
// this passes the function pointer to the unmanaged Sensor class to be used when a
// measurement is received
_sensor->RegisterForMeasurementCallback((fnOnReceiveMeasurement)(ip.ToPointer()));
using namespace System::Runtime::InteropServices;
[UnmanagedFunctionPointer(CallingConvention::Cdecl)]
delegate void OxpOnReceivedMeasurement(std::vector<OBX> *obxes);