[英]C++ DLL Wrapper in vb.net Passing byte array of a byte array?
I m calling a function, but im stuck in the argument that i must pass: 我正在调用一个函数,但是我陷入了必须通过的参数中:
this is what i got. 这就是我得到的。
Function definition: 功能定义:
DWORD dllexp_SetLedData(PBYTE bytArray, int arySize)
And this from the SDK: 这是来自SDK的:
Parameters 参数
Name | 姓名| Type | 类型 Description 描述
bytArray | bytArray | Input | 输入| Pointer to a byte array converted from the LEDSETTING structure array. 指向从LEDSETTING结构数组转换的字节数组的指针。
arySize | arySize | Input | 输入| Size, in bytes, of the buffer indicated by bytArray. bytArray指示的缓冲区大小(以字节为单位)。Return Values 返回值
Value | 价值| Description 描述ERROR_SUCCESS(0x0) | ERROR_SUCCESS(0x0)| Success 成功
ERROR_INVALID_OPERATION(0x10DD) | ERROR_INVALID_OPERATION(0x10DD)| Fail 失败
VB.NET 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
Structure of LEDSETTING in C++ 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;
Structure i wrote in vb.net 我在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
So far, so good, BUT... I think the function expect an array of byte array.. but i dont know how to do that in vb.net. 到目前为止,还算不错,但是...我认为该函数期望字节数组的数组..但我不知道如何在vb.net中做到这一点。
The only that i can do, is this: 我唯一能做的是:
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
and i dont get an error, but ERROR_INVALID_OPERATION
而且我没有收到错误,但是ERROR_INVALID_OPERATION
I convert the structure in a byte array. 我将结构转换为字节数组。 with ToByteArray
function in my structure, that works ok, but i cant put this array into a byte array as for example: dim Array(10) as Byte()
is not allowed. 在我的结构中使用ToByteArray
函数,可以正常工作,但是我不能将此数组放入字节数组,例如: dim Array(10) as Byte()
is not allow。
I saw this in the SDK C++ example of use of the DLL im working with. 我在使用DLL im的SDK C ++示例中看到了这一点。 but i can't "translate" it to vb.net. 但我无法将其“翻译”为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);
Any light on this?.. Thank you very much! 对此有何看法?。非常感谢!
You are almost correct. 你几乎是正确的。 The C++ function expects a byte array of a GLEDSETTINGS
array. C ++函数需要GLEDSETTINGS
数组的字节数组。
To convert the entire GLEDSETTINGS
array into a byte array you have to create a new function and adapt your already existing code to work with a whole array instead. 要将整个GLEDSETTINGS
数组转换为字节数组,您必须创建一个新函数,并使现有代码适合于整个数组。
For this I'd recommend creating an Extension method . 为此,我建议创建一个扩展方法 。 Add a new Module
to your project: 向您的项目添加一个新Module
:
Imports System.Runtime.CompilerServices
Public Module Extensions
<Extension()>
Public Function ToByteArray(ByVal LedSettings As GLEDSETTINGS()) As Byte()
Dim StructSize As Integer = Marshal.SizeOf(GetType(GLEDSETTINGS))
Dim Length As Integer = LedSettings.Length * StructSize 'The total amount of memory that our LedSettings array requires.
Dim Bytes As Byte() = New Byte(Length - 1) {}
Dim ptr As IntPtr = Marshal.AllocHGlobal(Length) 'Allocate a memory section for our data.
'Iterate through every GLEDSETTINGS structure and put it in our memory section.
For i = 0 To LedSettings.Length - 1
Marshal.StructureToPtr(LedSettings(i), ptr + i * StructSize, True)
Next
Marshal.Copy(ptr, Bytes, 0, Length) 'Copy the data from our memory section into our byte array.
Return Bytes
End Function
End Module
Now you can call it like this: 现在您可以这样称呼它:
Dim LDTbrr As Byte() = LD.ToByteArray()
resp3 = GLed.dllexp_SetLedData(LDTbrr, LDTbrr.Length)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.