vb.net中的C ++ DLL Wrapper传递字节数组的字节数组?

时间:2018-03-04 06:02:37

标签: c++ arrays vb.net

我正在调用一个函数,但我仍然坚持我必须通过的论点:

这就是我得到的。

功能定义:

DWORD  dllexp_SetLedData(PBYTE bytArray, int arySize)

这来自SDK:

  

参数

     

姓名|输入|说明

     

bytArray |输入|指向从LEDSETTING转换的字节数组的指针   结构阵列   arySize |输入| bytArray指示的缓冲区大小(以字节为单位)。

     

返回值
  价值|描述

     

ERROR_SUCCESS(0x0)|成功
  ERROR_INVALID_OPERATION(0x10DD)|失败

VB.NET

<DllImport("GLedApi.dll", setLastError:=False, callingConvention:=CallingConvention.Cdecl)> _
Public Shared Function dllexp_SetLedData(bytArray As Byte(), arySize As Int32) As Integer
End Function

C ++中LEDSETTING的结构

typedef struct tagLedSettingData {
   BYTE Reserve0;
   BYTE Mode_Sel;           //LED Mode
   BYTE MaxBrightness;      //default set to 100
   BYTE MinBrightness;      //defautl set to 0
   DWORD dwColor;          //0xWWRRGGBB, WW:0 -> WLED turn off, WW:0xFF -> WLED turn on
   WORD wTime_base0;      //light on time, in millisecond
   WORD wTime_base1;      //Interval time, in millisecond
   WORD wTime_base2;      //Cycle time, light on + light off <= Cycle time, this for Flash mode only
   BYTE CtrlVal0;
   BYTE CtrlVal1;
   } LedSettingData, *LedSettingData_Ptr;

我在vb.net写的结构

Public Structure GLEDSETTINGS
    Public Sub New(LedMod1 As ModeSelOptions, MaxB As Byte, MinB As Byte, dwColor1 As UInteger, aWtime0 As UShort, _
                   awtime1 As UShort, awtime2 As UShort, actrlVal0 As Byte, actrlVal1 As Byte)

        Reserved0 = &H0
        LedMod = LedMod1
        MaxBrightness = MaxB
        MinBrightness = MinB
        dwColor = dwColor1
        wTime0 = aWtime0
        wTime1 = awtime1
        wTime2 = awtime2
        CtrlVal0 = actrlVal0
        CtrlVal1 = actrlVal1

    End Sub

    Private Reserved0 As Byte
    Public LedMod As ModeSelOptions
    Public MaxBrightness As Byte  ' max 100
    Public MinBrightness As Byte
    Public dwColor As UInteger  ' &h0FFFFFF
    Public wTime0 As UShort
    Public wTime1 As UShort
    Public wTime2 As UShort
    Public CtrlVal0 As Byte
    Public CtrlVal1 As Byte

    Public Enum ModeSelOptions As Byte
        Defecto = 0
        Pulse
        Music
        ColorCycle
        Statico
        Flash
        Transition
        DigiModA
        DigiModB
        DigiModC
        DigiModD
        DigiModE
        DigiModF
        DigiModG
        DigiModH
        DigiModi
    End Enum
    Public Enum LedType As Integer
        NA
        A_LED
        D_LED_TYPE1
        D_LED_TYPE2
    End Enum

    Function ToByteArray() As Byte()

        Dim size As Integer = Marshal.SizeOf(Me)
        Dim arr As Byte() = New Byte(size - 1) {}
        Dim ptr As IntPtr = Marshal.AllocHGlobal(size)
        Marshal.StructureToPtr(Me, ptr, True)
        Marshal.Copy(ptr, arr, 0, size)
        Marshal.FreeHGlobal(ptr)
        Return arr

    End Function

End Structure

到目前为止,非常好,但是......我认为该函数需要一个字节数组数组..但我不知道如何在vb.net中这样做。

我唯一能做的就是:

Dim LD(iMaxDivs - 1) As GLEDSETTINGS

    For I = 0 To iMaxDivs - 1
        Dim L As New GLEDSETTINGS(GLEDSETTINGS.ModeSelOptions.Statico, 100, 0, &HFFFF11FFUI, 1000, 100, 0, &H0, &H0)
        LD(I) = L


    Next

    'Dim LDParam(LDTama) As Byte

    Dim LDTbrr As Byte() = LD(0).ToByteArray
    Dim LDTama As Integer = Marshal.SizeOf(LD(0))


    resp3 = GLed.dllexp_SetLedData(LDTbrr, LDTama)
    Debug.WriteLine("SetLedData: " & resp3)
    If resp3 = GLed.ERROR_INVALID_OPERATION Then
        Exit Sub

    End If

我没有收到错误,但ERROR_INVALID_OPERATION

我在字节数组中转换结构。在我的结构中使用ToByteArray函数,这可以正常工作,但我不能将此数组放入字节数组中,例如:dim Array(10) as Byte()是不允许的。

我在使用DLL的SDK C ++示例中看到了这一点。但我无法将其“翻译”为vb.net。

pSettingData = new LedSettingData[iMaxDivs];
int dLen = iMaxDivs * sizeof(LedSettingData);
//ZeroMemory(pSettingData, dLen);

for (int i = 0; i < iMaxDivs; i++)
{
    (pSettingData + i)->Mode_Sel = sd.Mode_Sel;
    (pSettingData + i)->MaxBrightness = sd.MaxBrightness;
    (pSettingData + i)->MinBrightness = sd.MinBrightness;
    (pSettingData + i)->dwColor = sd.dwColor;
    (pSettingData + i)->wTime_base0 = sd.wTime_base0;
    (pSettingData + i)->wTime_base1 = sd.wTime_base1;
    (pSettingData + i)->wTime_base2 = sd.wTime_base2;
    (pSettingData + i)->CtrlVal0 = sd.CtrlVal0;
    (pSettingData + i)->CtrlVal1 = sd.CtrlVal1;
}

pfSetLedData((PBYTE)pSettingData, dLen);

对此有何看法?非常感谢你!

0 个答案:

没有答案