日期:2014-05-18  浏览次数:20922 次

再再次来个调用C++dll问题 求好人,热心人士!!!
先贴下代码:
 public struct CapInfoStruct
  {
  public byte[] Buffer;
  public Int32 Heigth;
  public Int32 Width;
  public Int32 OffsetX;
  public Int32 OffsetY;
  public Int32 Exposure;
  // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  public sbyte[] Gain;
  public sbyte Control;
  public sbyte InternalUse;
  //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  public sbyte[] ColorOff;
  // [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  public sbyte[] Reserved;
  }
  public class a
  {

  //[DllImport("MyDLL.dll")] 
  [DllImport("HQDLAPI.dll")]
  public static extern IntPtr HqDLInitialize(
  ref string pFilterName,
  out int pIndex,
  ref CapInfoStruct pCapInfo,
  out IntPtr hCamera
  );
  [DllImport("HQDLAPI.dll")]
  public static extern IntPtr HqDLSetCapInfo(ref IntPtr hCamera, ref CapInfoStruct pCapInfo);
  [DllImport("HQDLAPI.dll")]
  public static extern IntPtr HqDLStartView(ref IntPtr hCamera, ref string pFilterName, ref uint dwStyle, ref int x, ref int y,
  ref int nwidth, ref int nheight, ref IntPtr Hwparent, ref IntPtr nidorhmenu);
  [DllImport("HQDLAPI.dll")]
  public static extern IntPtr HqDLSetCapInfo1(ref IntPtr hCamera, ref CapInfoStruct pCapInfo);
  }
private void button1_Click(object sender, EventArgs e)
  {
   
  CapInfoStruct cap = new CapInfoStruct();
  byte[] buff;
  buff = new byte[1];
  // MessageBox.Show("a");
  cap.Buffer = buff;
  cap.Heigth = 480;
  cap.Width = 640;
  cap.Exposure = 50;
  cap.OffsetX = 0;
  cap.OffsetY = 0;
  sbyte[] str = new sbyte[3];
  str[0] = 26;
  str[1] = 24;
  str[2] = 34;
  cap.Gain = str;
  cap.Control = 0;
  string stg = "DLC";
  int nIndex;
  IntPtr camera;
  IntPtr b= a.HqDLInitialize(ref stg,out nIndex,ref cap,out camera);
  try
{
a.HqDLSetCapInfo(ref camera, ref cap);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
  }
红色部分函数c++说明为:
HqDLSetCapInfo
函数: HQDL_API HqDLSetCapInfo( IN HANDLE hCamera , IN CapInfoStruct *pCapInfo );
功能: 实时设置视频参数
参数: hCamera 通过调用HqDLInitialize得到的设备句柄
下面说下问题:
我现在主要存在的问题是:
当程序运行到红色部分代码是 执行完a.HqDLSetCapInfo(ref camera, ref cap)函数后程序自动退出,求指教!!

------解决方案--------------------
先试一下。不行就把C++的函数声明贴一下,并作一下说明(越详细越好)
C# code
public partial class Form1 : Form
    {
        const int MAX_HEIGHT = 480;
        const int MAX_WIDTH = 640;
        
        IntPtr buffer = IntPtr.Zero;

        [StructLayout(LayoutKind.Sequential)]
        public struct CapInfoStruct
        {
            public IntPtr Buffer;
            public Int32 Heigth;