C ++非托管DLL在c# [英] C++ unmanaged DLL in c#

查看:190
本文介绍了C ++非托管DLL在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求在我的项目中整合网络摄像头ZoneTrigger。网站提供的SDK也是C ++的样例。我已经能够得到很少的功能工作。我被困住的地方是一个函数,其中char *是一个参数传递。我做了很多挖掘,并已经知道,MarshalAs必须使用...

I've been asked to intergrate webcam ZoneTrigger in my project. The SDK provided in the site is C++ also the sample. I've been able to get few functions to work. the place i've been stuck is a function where char* is a parameter passed. I did a lot of digging and have come to know that MarshalAs has to be used...

我想从
导入的函数c ++代码

the function i'd like to import from c++ code

//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给出第一个char ..
它的工作原理的代码n给出第一个字符下面



i know that the problem is in SpotName,Its a byte[32], if i dont use marshalAs,instead i use just char it works n gives the first char.. the code where it works fine n gives first char is below

 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 []赋予

but if i put char[] it gives

Method's type signature is not Interop compatible. ERROR

我已经用完了选项。我应该使用MarshalAs或char []?
请帮助.....
感谢提前....

i've run out of options here.... where have i gone wrong? should i use MarshalAs or char[]??? please help..... thanks in Advance....

推荐答案

t看到你在使用 ZT_TRIG_STRUCT 的位置,但应声明如下:

I can't see where you are using ZT_TRIG_STRUCT, but that should be declared like this:

[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 。应该是:

The other problem you have is in the declaration of ZT_EnumerateHotSpots. That should be:

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

正如我读到的, SpotName 一个out参数,即你提供一个缓冲区,并且 ZT_EnumerateHotSpots 写入。

As I read it, SpotName is actually an out parameter, i.e. you supply a buffer and ZT_EnumerateHotSpots writes to it.

/ p>

You then call this like so

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

这篇关于C ++非托管DLL在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆