简体   繁体   English

C ++ / C#互操作性类型转换

[英]C++/C# Interoperability Type Conversion

I am developing a program which uses dll driver file. 我正在开发一个使用dll驱动程序文件的程序。 I successfully linked the dll into my code but have a problem with one method 我已成功将dll链接到我的代码中,但是一种方法有问题

Dll function: DLL功能:

word NdEnumDevices(const char* const ** devs);

I've linked it to c# like this 我已经将它链接到c#

[DllImport("NeurobitDrv.dll")]
    public static extern ushort NdEnumDevices(ref string[] devs);

As i don't have much experience with C++ I dont really understand what const char* const ** devs means. 由于我没有太多的C ++经验,所以我不太了解const char* const ** devs含义。 I know for sure that this argument receives an array of strings but i only get one element where should be 3 我肯定知道此参数接收到一个字符串数组,但我只得到一个元素,其中应为3

And it Throws out an Access violation Exception 并抛出访问冲突异常

Can someone tell me what type should i use in c#?? 有人可以告诉我在C#中应该使用哪种类型吗?


I've worked around it, just hardcoded string array with device names and it works just fine :D 我已经解决了这个问题,只是使用设备名称对字符串数组进行了硬编码,并且效果很好:D

const char* const ** devs is likely the same as const char* const *devs[] which would be const char* const ** devs可能与const char* const *devs[]相同

pointer to 
  an array of
    constant pointers to
      constant characters.

This is most likely caused be the ref (although I'm not too sure about that). 这很可能是由ref引起的(尽管我不太确定)。 A ref to an array of strings could be compiled as a pointer to an array of strings. 对字符串数组的ref可以编译为指向字符串数组的指针。

As pointed out in this question, you should be safe to use const char* for that. 正如这个问题所指出的那样,使用const char*应该是安全的。

I think what you could be missing is the length of the array. 我认为您可能缺少的是数组的长度。 The function should be declared as 该函数应声明为

word NdEnumDevices(const char* devs[], unsigned int length);
// C#
[DllImport("NeurobitDrv.dll")]
private static extern ushort NdEnumDevices(string[] devs, UInt32 length);

public static ushort NdEnumDevices(string[] devs) {
    NdEnumDevices(devs, devs.Length); // Delegate for length
}

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

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