c# - C# 中的 C++ 非托管 DLL

标签 c# c++ dll pinvoke

我被要求将网络摄像头 ZoneTrigger 集成到我的项目中。站点中提供的 SDK 也是 C++ 示例。我已经能够让一些功能正常工作。我陷入困境的地方是一个函数,其中 char* 是传递的参数。我做了很多挖掘,并且知道必须使用 MarshalAs...

我想要导入的函数 C++代码

//header file
struct ZT_TRIG_STRUCT 
{
int aSize;      //STRUCT size
int CameraIndex;
int SpotIndex;
int SpotType;
char SpotName[32];
DWORD Dummy[16];
};

typedef int (WINAPI *f_ZT_EnumerateHotSpots)(int SpotIndex, char *Name, int *SpotType);
/*
You application can call this functions to retrieve information about spots by iterating the SpotIndex param.
Name is a pointer to a 32 byte buffer supplied by your application.
SpotType is a pointer to an 32 bit integer in your application.
Return value:
0 = Success, the Name and SpotType have been initialized
1 = Error, we have lost communication with Zone Trigger
2 = Enumaration is over, all spots have been enumerated. Your application should increment SpotIndex until this function returns 2.
*/

//code
//Enumerate current spots in Zone Trigger
int i=0;
char SpotName[32];
int SpotType;
check = ZT_EnumerateHotSpots(i, SpotName, &SpotType);
while (check == 0) 
{
    float sensitivity = ZT_GetSensitivity(i);
    printf("Zone Trigger spot: %s   Sensitivity: %0.1f%%\r\n", SpotName,     sensitivity*100);
    check = ZT_EnumerateHotSpots(++i, SpotName, &SpotType);
}

所以当转换为c#时

[DllImport("ZTcom.dll")]
    private static extern int ZT_EnumerateHotSpots(int i, [MarshalAs(UnmanagedType.LPWStr)]ref string SpotName, ref int SpotType);

 public unsafe struct ZT_TRIG_STRUCT 
    {
        public int aSize;       //STRUCT size
        public int CameraIndex;
        public int SpotIndex;
        public int SpotType;
      public string SpotName ;  
        //[MarshalAs(UnmanagedType.LPStr, SizeConst = 256)] string SpotName;
       // public IntPtr SpotName;
    }

//In code
int i = 0;
 string SpotName;
int SpotType;
check = ZT_EnumerateHotSpots(i, ref SpotName, ref SpotType);

上面的行给出:

Exception of type 'System.ExecutionEngineException' was thrown.

错误。

我知道问题出在 SpotName 中,它是一个字节 [32],如果我不使用 marshalAs,而是只使用 char 它可以工作 n 给出第一个字符.. 工作正常的代码 n 给出第一个字符如下

 public unsafe struct ZT_TRIG_STRUCT 
    {
        public int aSize;       //STRUCT size
        public int CameraIndex;
        public int SpotIndex;
        public int SpotType;
      public char SpotName ;              
    }

 [DllImport("ZTcom.dll")]
    private static extern int ZT_EnumerateHotSpots(int i, ref char SpotName, ref int SpotType);

 public char SpotName;
int i = 0;
            check = ZT_EnumerateHotSpots(i, ref SpotName, ref SpotType);
            while (check == 0)
            {
                float sensitivity = ZT_GetSensitivity(i);
                textBox1.Text = textBox1.Text + "\r\n" +"Zone Trigger spot: " + SpotName + "   Sensitivity: " + (sensitivity * 100).ToString();
                check = ZT_EnumerateHotSpots(++i, ref SpotName, ref SpotType);
            }

但是如果我输入 char[] 它会给出

Method's type signature is not Interop compatible. ERROR

我已经没有选择了......我哪里出了问题?我应该使用 MarshalAs 还是 char[]??? 请帮忙..... 提前致谢....

最佳答案

我看不到您在哪里使用 ZT_TRIG_STRUCT,但应该这样声明:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct ZT_TRIG_STRUCT 
{
    public int aSize;
    public int CameraIndex;
    public int SpotIndex;
    public int SpotType;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
    public string SpotName;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
    public uint[] Dummy;
}

您遇到的另一个问题是在 ZT_EnumerateHotSpots 的声明中。应该是:

[DllImport("ZTcom.dll", CharSet=CharSet.Ansi)]
private static extern int ZT_EnumerateHotSpots(
    int SpotIndex, 
    StringBuilder SpotName, 
    out int SpotType
);

正如我所读,SpotName 实际上是一个输出参数,即您提供一个缓冲区,ZT_EnumerateHotSpots 写入它。

然后你可以这样调用它

int SpotIndex = 0;
StringBuilder SpotName = new StringBuilder(32);
int SpotType;
int result = ZT_EnumerateHotSpots(SpotIndex, SpotName, out SpotType);

关于c# - C# 中的 C++ 非托管 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7318613/

相关文章:

c# - 从另一个静态类访问表单方法

c++ - 动态对象的动态数组

c++ - 在 C++ 中不使用 char 类型定义 '999e999' 值

c# - 引用 NuGet 包后避免在构建输出中使用多个 DLL

c++ - 无法使用 Visual C++ 2012 创建有效的 dll

c# - 这些重载不是模棱两可的吗?

c# - 如何将一维字符串数组结果绑定(bind)到数据网格列标题

c++ - std::mersenne_twister_engine 和随机数生成

c - DLL 中的函数未返回正确值

c# - 控制只浏览/选择文件夹,而不是 ASP.NET 中的文件?