◆ Snt_GetDeviceList (取得设备信息名单)                        取得每个现在被连接的设备的信息表。    <構文>
int   Snt_GetDeviceList(PDEV_INFORALL pDevLst);    <引数>
      pDevLst  :设备的信息名单构造体指针    <返回値>
      BCT_ERR_NODEVICE       :没有连接的设备
BCT_ERR_OPEN          :有开放的设备,信息表不被设定。
      BCT_ERR                 :其它Error
      上記以外         :连接的设备数量    <设备信息表的构造体>
typedef struct _DEV_INFO {
unsigned char Dev_Info[MAX_UINFO];        //
unsigned char Dev_Ser[MAX_UINFO];         //序号
unsigned char PortName[MAX_UPORT];        //USBxxx
} DEV_INFO, *PDEV_INFO;typedef struct _DEV_INFORALL {
long          Dev_Cnt;                    //连接设备的数量
DEV_INFO      Dev_U_Informa[MAX_DEV];
} DEV_INFORALL, *PDEV_INFORALL;#define MAX_DEV   99    //Max Device
#define MAX_UINFO  256    //Max UINFO
#define MAX_UPORT  8     //Max UPORT注1)没有设定PortName 弦("USBxxx")。
注2)这个机能的实行如果设备被开放,因为设备号码被确定,关全部的设备之后请再次要求。
※构造体member的alignment因为成为1字节请注意以后被指定的全部构造体。------------------------------------------------------
上面是这个函数的原文。
下面是我写的c#.net,但是调用后没有得到设备。        public const short MAX_DEVICE = 99;
        public const short MAX_UINFO = 256;        public const short MAX_PIPE = 8;        [StructLayout(LayoutKind.Sequential)]
        public struct INFO
        {
           // [VBFixedArray(255), MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public byte[] Dev_Info;
           // [VBFixedArray(255), MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
            public byte[] Dev_Ser;
           // [VBFixedArray(7), MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public byte[] PortName;
            public void Initialize()
            {
                Dev_Info = new byte[256];
                Dev_Ser = new byte[256];
                PortName = new byte[8];
            }
        }        public struct INFORALL
        {
            public int Dev_Count;
            //[VBFixedArray(MAX_DEVICE - 1), MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DEVICE)]
            public INFO[] Dev_Information;
            public void Initialize()
            {
                Dev_Information = new INFO[MAX_DEVICE];
                for (int i = 0; i <= (MAX_DEVICE - 1); i++)
                {
                    Dev_Information[i] = new INFO();
                    Dev_Information[i].Initialize();
                }
            }
        }        [DllImport("Snt_usb.dll", EntryPoint = "Snt_GetDeviceList", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        public static extern int Snt_GetDeviceList(ref INFORALL deviceinfo);
调用的方法为:
            int lRet = -1;
            Dev_Info.Initialize();
            lRet = Snt_GetDeviceList(ref Dev_Info);
            if (lRet <= 0)
            {
                MessageBox.Show("失败");
            }

解决方案 »

  1.   

    运行后lret=0,无法查找到设备。不知道是哪部分出错,请大家帮忙看看。
      

  2.   

    运行后lret=0,无法查找到设备。不知道是哪部分出错,请大家帮忙看看。
      

  3.   

    这样转换:  [StructLayout(LayoutKind.Sequential)]
      public struct INFO
      {
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
         public byte[] Dev_Info;
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
         public byte[] Dev_Ser;
         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
         public byte[] PortName;  
      }  public struct INFORALL
      {
        public int Dev_Count;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DEVICE)]
        public INFO[] Dev_Information;
      }  [DllImport("Snt_usb.dll"CharSet = CharSet.Ansi,CallingConvention = CallingConvention.StdCall)]
      public static extern int Snt_GetDeviceList(ref INFORALL deviceinfo);使用:
      INFORALL Dev_Info=new INFORALL();
      int lRet = -1;  lRet = Snt_GetDeviceList(ref Dev_Info);
      

  4.   

    个人认为问题在于两个Initialize()函数内的new。注意最终调用dll时,应该传递的是“结构数组”,而非“引用数组”。
    这样:
    public static extern int Snt_GetDeviceList([In, Out] INFO[] arrINFO);
    所有Initialize()去掉,调用时:
    INFO[] arrINFO = new INFO[iCount];
    lRet = Snt_GetDeviceList(arrINFO);