简体   繁体   English

用P / Invoke编组双参数

[英]Marshalling double parameters with P/Invoke

I have a problem with a P/Invoke call I'm trying to do. 我要执行的P / Invoke通话有问题。 I have to call a C++ class from a C# program. 我必须从C#程序中调用C ++类。 I have the source of this class so what I did is put it in a DLL project and create export functions to access it's main method. 我有该类的源代码,因此我所做的就是将其放在DLL项目中并创建导出函数以访问它的主要方法。 That should be enough to do what I need and keep things simple. 这应该足以满足我的需求并使事情变得简单。

My export method looks like this : 我的导出方法如下所示:

extern "C" _declspec(dllexport) void Inference(double *c1, double *c2, double *c3, double *result)
{
    /* somecode */
}

This compiles, and I can see the export in a dumpbin output. 这样就可以编译了,我可以在dumpbin输出中看到导出了。

Now the problem is, I can't call this method from my C# code because I always get a PInvokeStackInbalance exception, telling me that 现在的问题是,我无法从C#代码中调用此方法,因为我总是会收到PInvokeStackInbalance异常,告诉我

This is likely because the managed PInvoke signature does not match the unmanaged target signature. 这可能是因为托管PInvoke签名与非托管目标签名不匹配。

I tried calling the method with this : 我试着用这个调用方法:

[DllImport("InferenceEngine.dll")]
extern static unsafe void Inference(double *c1, double *c2, double *c3, double *result);

I also tried this : 我也尝试过这个:

[DllImport("InferenceEngine.dll")]
extern static void Inference(ref double c1, ref double c2, ref double c3, ref double result);

... which were both possible ways documented on MSDN but with no luck. ...这都是MSDN上记录的两种可能方法,但是没有运气。 Does anyone have any clue about what the problem is ? 有人对这个问题有什么线索吗?

Thanks ! 谢谢 !

You should declare your C++ function as __stdcall , which is the P/Invoke default: 您应该将C ++函数声明为__stdcall ,这是P / Invoke的默认设置:

extern "C" _declspec(dllexport) void __stdcall Inference(double *c1, double *c2, double *c3, double *result);

It's also possible to leave the C++ prototype alone and change the P/Invoke declaration: 也可以不使用C ++原型并更改P / Invoke声明:

[DllImport("InferenceEngine.dll", CallingConvention=CallingConvention.Cdecl)]

cdecl isn't used often with P/Invoke, probably because the Windows API is stdcall . cdecl / cdecl与P / Invoke一起使用,可能是因为Windows API是stdcall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM