简体   繁体   English

C# - DLLImport和函数默认值

[英]C# - DLLImport and function default values

I'm interfacing with a native 3rd party C++ DLL via C# and the provided interop layer looks like below: 我通过C#与本地第三方C ++ DLL连接,提供的互操作层如下所示:

C#: C#:

[DllImport("csvcomm.dll")]
public static extern int CSVC_ValidateCertificate(byte[] certDER, int length);

C++: C ++:

CSVC_Status_t CSVCOMM_API CSVC_ValidateCertificate(BYTE* certDER, DWORD length, 
    DWORD context = CONTEXT_DEFAULT);

Note, there are only two parameters in the C# extern definition since the the C++ function provides a default value for the third parameter. 注意,C#extern定义中只有两个参数,因为C ++函数为第三个参数提供了一个默认值。 Is this correct? 它是否正确? I was receiving some non-deterministic results when using the provided definition, but when I added the third parameter like below, it seems to be working correctly each time rather than sporadically. 在使用提供的定义时,我收到了一些不确定的结果,但是当我添加第三个参数时,它似乎每次都正常工作而不是零星。

[DllImport("csvcomm.dll")]
public static extern int CSVC_ValidateCertificate(byte[] certDER, int length, 
    int context);

Any ideas? 有任何想法吗? Would the addition of the 3rd parameter really fix this issue? 添加第3个参数真的可以解决这个问题吗?

The optional parameter in C++ is resolved at compile time. C ++中的可选参数在编译时解析。 When you call into this via P/Invoke, you need to always specify all three parameters. 当您通过P / Invoke调用此函数时,您需要始终指定所有三个参数。

If you want to have an optional parameter, you'll need to make a C# wrapper around this method with an overload that provides the optional support (or a C# 4 optional parameter). 如果你想有一个可选参数,你需要使用一个提供可选支持(或C#4可选参数)的重载来围绕这个方法创建一个C#包装器。 The actual call into the C++ library should always specify all three arguments, however. 但是,对C ++库的实际调用应始终指定所有三个参数。

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

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