本人初学VB,最近收到别人一个C#写的DLL,因为其中并没有加入指针所以VB调用起来很不方便
后来他发给我一个C#使用的源代码作为参考,因为完全没学过C#所以有些东西不是很明白
  public partial class Form1 : Form
    {
        #region struc declare
        /// <summary>
        /// 原型定义
        /// </summary>
        const int MaxSensorCountPerChannel = 30;
        const int MaxFreq = 150;
        const int MaxChannelNum = 33;
        [StructLayout(LayoutKind.Sequential)]
        private struct SensorList
        {
            public int nSensorCount;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxSensorCountPerChannel)]
            public float[] fWaveLengthVec;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxSensorCountPerChannel)]
            public float[] fPowerDBVec;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct PerChannel
        {
            public int nChNum;
            public int nFreq;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxFreq)]
            public SensorList[] tSensorListVec;
        }
        #endregion        #region import dll
        /// <summary>
        /// 函数封装
        /// </summary>
        /// <returns></returns>
        [DllImport("ReadData.dll")]
        private static extern int SEN_Init();        [DllImport("ReadData.dll")]
        private static extern void SEN_Close();        [DllImport("ReadData.dll")]
        private static extern void SEN_ReadDSPData(IntPtr pp);
        [DllImport("ReadData.dll")]
        private static extern bool SEN_IsConnected();        [DllImport("ReadData.dll")]
        private static extern int SEN_SendParaData();
        #endregion        #region dll function
        private static int DllInit()
        {
            int rtn = -1;
            try
            {
                rtn = SEN_Init();
            }
            catch
            {
                rtn = -2;
            }
            return rtn;
        }        private static int IsConnected()
        {
            int ret = 0;
            if (!SEN_IsConnected())
            {
                ret = -1;
            }
            return ret;
        }        private static int SendParaData()
        {
            int ret = 0;
            if (SEN_SendParaData() != 0)
            {
                ret = -2;
            }
            return ret;
        }        private static void DspClose()
        {
            SEN_Close();
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        private static PerChannel[] GetDataUseDll()
        {
           PerChannel[] channelList = new PerChannel[MaxChannelNum];
           try
           {
               for (int i = 0; i < channelList.Length; i++)
               {
                   channelList[i] = new PerChannel();
               }
               IntPtr[] ptArray = new IntPtr[1];
               IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof (PerChannel))*MaxChannelNum);
               Marshal.Copy(ptArray, 0, pt, 1);               SEN_ReadDSPData( pt);
               for (int i = 0; i < channelList.Length; i++)
               {
                   channelList[i] =
                       (PerChannel)
                       Marshal.PtrToStructure((IntPtr) ((UInt32) pt + i*Marshal.SizeOf(typeof (PerChannel))),
                                              typeof (PerChannel));
               }               Marshal.FreeHGlobal(pt);
             }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }           return channelList;        }
        #endregion函数名称:SEN_ReadDSPData
int SEN_ReadDSPData(stCHANNEL_LIST pChannelData[11])
参数:
[out] stCHANNEL_LIST  pChannelData[11]:返回最多11通道100赫兹的波长数据。(0~10依次为1~11通道序列),数据结构说明详见下面。
结构体说明:
struct stCHANNEL_SENSOR_LIST {
int nSensorCount; 该通道的传感器数。
std::vector<float> fWaveLengthVec; 该通道的传感器波长序列。
std::vector<float> fPowerDBVec; 该通道的传感器功率序列。
};
struct stCHANNEL_LIST {
int nChNum; 通道号。
int nFreq; 该时间内采集的频率。
std::vector<stCHANNEL_SENSOR_LIST> tSensorListVec; 传感器序列。
};返回值:
0,表示读取成功;非0,读取失败。以上为函数名称说明,请问如果我要用VB申明和调用这个函数可行吗?VB应该怎么写申明和调用语句呢?Private Declare Function SEN_ReadDSPData Lib "ReadData.dll" _
         (ByVal stCHANNEL_LIST As Long, _
          ByVal pChannelData As Long, _
          ByVal nSensorCount As Long, _
          ByVal fWaveLengthVec As String, _
          ByVal fPowerDBVec As String, _
          ByVal nChNum As Long, _
          ByVal nFreq As Long, _
          ByVal tSensorListVec As String) As Long
我用以上的申明试却提示SEN_ReadDSPData必选参数,是否申明里写错了呢?请前辈们不吝赐教,谢谢。

解决方案 »

  1.   


    Public Partial Class Form1
    Inherits Form
    #Region "struc declare"
    ''' <summary>
    ''' 原型定义
    ''' </summary>
    Const MaxSensorCountPerChannel As Integer = 30
    Const MaxFreq As Integer = 150
    Const MaxChannelNum As Integer = 33
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure SensorList
    Public nSensorCount As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst := MaxSensorCountPerChannel)> _
    Public fWaveLengthVec As Single()
    <MarshalAs(UnmanagedType.ByValArray, SizeConst := MaxSensorCountPerChannel)> _
    Public fPowerDBVec As Single()
    End Structure
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure PerChannel
    Public nChNum As Integer
    Public nFreq As Integer
    <MarshalAs(UnmanagedType.ByValArray, SizeConst := MaxFreq)> _
    Public tSensorListVec As SensorList()
    End Structure
    #End Region #Region "import dll"
    ''' <summary>
    ''' 函数封装
    ''' </summary>
    ''' <returns></returns>
    <DllImport("ReadData.dll")> _
    Private Shared Function SEN_Init() As Integer
    End Function <DllImport("ReadData.dll")> _
    Private Shared Sub SEN_Close()
    End Sub <DllImport("ReadData.dll")> _
    Private Shared Sub SEN_ReadDSPData(pp As IntPtr)
    End Sub
    <DllImport("ReadData.dll")> _
    Private Shared Function SEN_IsConnected() As Boolean
    End Function <DllImport("ReadData.dll")> _
    Private Shared Function SEN_SendParaData() As Integer
    End Function
    #End Region #Region "dll function"
    Private Shared Function DllInit() As Integer
    Dim rtn As Integer = -1
    Try
    rtn = SEN_Init()
    Catch
    rtn = -2
    End Try
    Return rtn
    End Function Private Shared Function IsConnected() As Integer
    Dim ret As Integer = 0
    If Not SEN_IsConnected() Then
    ret = -1
    End If
    Return ret
    End Function Private Shared Function SendParaData() As Integer
    Dim ret As Integer = 0
    If SEN_SendParaData() <> 0 Then
    ret = -2
    End If
    Return ret
    End Function Private Shared Sub DspClose()
    SEN_Close()
    End Sub
    ''' <summary>
    ''' 获取数据
    ''' </summary>
    ''' <returns></returns>
    Private Shared Function GetDataUseDll() As PerChannel()
    Dim channelList As PerChannel() = New PerChannel(MaxChannelNum - 1) {}
    Try
    For i As Integer = 0 To channelList.Length - 1
    channelList(i) = New PerChannel()
    Next
    Dim ptArray As IntPtr() = New IntPtr(0) {}
    Dim pt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(PerChannel)) * MaxChannelNum)
    Marshal.Copy(ptArray, 0, pt, 1) SEN_ReadDSPData(pt)
    For i As Integer = 0 To channelList.Length - 1
    channelList(i) = CType(Marshal.PtrToStructure(DirectCast(DirectCast(pt, UInt32) + i * Marshal.SizeOf(GetType(PerChannel)), IntPtr), GetType(PerChannel)), PerChannel)
    Next Marshal.FreeHGlobal(pt)
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try Return channelList End Function
    #End Region
    End Class使用工具转换
    http://www.developerfusion.com/tools/convert/csharp-to-vb/
      

  2.   

    感谢2楼前辈帮忙看了下这个转换过来的好像很多不是VB语句吗?只想调用SEN_ReadDSPData这个函数的话,VB的申明和调用应该如何写呢?谢谢
      

  3.   

    Public Partial Class Form1
        Inherits Form
        #Region "struc declare"
        ''' <summary>
        ''' 原型定义
        ''' </summary>
        Const MaxSensorCountPerChannel As Integer = 30
        Const MaxFreq As Integer = 150
        Const MaxChannelNum As Integer = 33
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure SensorList
            Public nSensorCount As Integer
            <MarshalAs(UnmanagedType.ByValArray, SizeConst := MaxSensorCountPerChannel)> _
            Public fWaveLengthVec As Single()
            <MarshalAs(UnmanagedType.ByValArray, SizeConst := MaxSensorCountPerChannel)> _
            Public fPowerDBVec As Single()
        End Structure
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure PerChannel
            Public nChNum As Integer
            Public nFreq As Integer
            <MarshalAs(UnmanagedType.ByValArray, SizeConst := MaxFreq)> _
            Public tSensorListVec As SensorList()
        End Structure
        #End Region    #Region "import dll"
        ''' <summary>
        ''' 函数封装
        ''' </summary>
        ''' <returns></returns>
        <DllImport("ReadData.dll")> _
        Private Shared Function SEN_Init() As Integer
        End Function    <DllImport("ReadData.dll")> _
        Private Shared Sub SEN_Close()
        End Sub    <DllImport("ReadData.dll")> _
        Private Shared Sub SEN_ReadDSPData(pp As IntPtr)
        End Sub
        <DllImport("ReadData.dll")> _
        Private Shared Function SEN_IsConnected() As Boolean
        End Function    <DllImport("ReadData.dll")> _
        Private Shared Function SEN_SendParaData() As Integer
        End Function
        #End Region    #Region "dll function"
        Private Shared Function DllInit() As Integer
            Dim rtn As Integer = -1
            Try
                rtn = SEN_Init()
            Catch
                rtn = -2
            End Try
            Return rtn
        End Function    Private Shared Function IsConnected() As Integer
            Dim ret As Integer = 0
            If Not SEN_IsConnected() Then
                ret = -1
            End If
            Return ret
        End Function    Private Shared Function SendParaData() As Integer
            Dim ret As Integer = 0
            If SEN_SendParaData() <> 0 Then
                ret = -2
            End If
            Return ret
        End Function    Private Shared Sub DspClose()
            SEN_Close()
        End Sub
        ''' <summary>
        ''' 获取数据
        ''' </summary>
        ''' <returns></returns>
        Private Shared Function GetDataUseDll() As PerChannel()
            Dim channelList As PerChannel() = New PerChannel(MaxChannelNum - 1) {}
            Try
                For i As Integer = 0 To channelList.Length - 1
                    channelList(i) = New PerChannel()
                Next
                Dim ptArray As IntPtr() = New IntPtr(0) {}
                Dim pt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(PerChannel)) * MaxChannelNum)
                Marshal.Copy(ptArray, 0, pt, 1)            SEN_ReadDSPData(pt)
                For i As Integer = 0 To channelList.Length - 1
                    channelList(i) = CType(Marshal.PtrToStructure(DirectCast(DirectCast(pt, UInt32) + i * Marshal.SizeOf(GetType(PerChannel)), IntPtr), GetType(PerChannel)), PerChannel)
                Next            Marshal.FreeHGlobal(pt)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try        Return channelList    End Function
        #End Region
    End Class
      

  4.   

    请问这个C#的DLL只能转换VB.NET吗 VB无法调用SEN_ReadDSPData吗?
      

  5.   

    如果你vb6使用。可以不用转换。直接c#编译成dll。项目属性中选择:生成->选中:为COM互操作注册。
    编译生成tlb文件,注册后,在vb6中添加部件(add refrance)后,类似activeX的方式使用即可。
      

  6.   


    对不起我忘了说明 起因是对方发给我一个C#写的DLL 并且给了我其中的函数说明 但是
    函数名称:SEN_ReadDSPData
    int SEN_ReadDSPData(stCHANNEL_LIST pChannelData[11])
    参数:
    [out] stCHANNEL_LIST pChannelData[11]:返回最多11通道100赫兹的波长数据。(0~10依次为1~11通道序列),数据结构说明详见下面。
    结构体说明:
    struct stCHANNEL_SENSOR_LIST {
    int nSensorCount; 该通道的传感器数。
    std::vector<float> fWaveLengthVec; 该通道的传感器波长序列。
    std::vector<float> fPowerDBVec; 该通道的传感器功率序列。
    }; 
    struct stCHANNEL_LIST {
    int nChNum; 通道号。
    int nFreq; 该时间内采集的频率。
    std::vector<stCHANNEL_SENSOR_LIST> tSensorListVec; 传感器序列。
    };返回值:
    0,表示读取成功;非0,读取失败。这个有很多前辈说VB不能直接调用这个函数,所以对方发来了C#的调用实例给我参考下,现在已经有C#的DLL,因为VB调用一直出错所以在找解决办法