【发布时间】:2018-03-04 06:02:37
【问题描述】:
我正在调用一个函数,但我陷入了我必须通过的参数:
这就是我得到的。
函数定义:
DWORD dllexp_SetLedData(PBYTE bytArray, int arySize)
这来自 SDK:
参数
姓名 |类型 |说明
字节数组 |输入 |指向从 LEDSETTING 转换的字节数组的指针 结构数组。
arySize |输入 |由 byteArray 指示的缓冲区的大小(以字节为单位)。返回值
价值 |说明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);
对此有何启示?.. 非常感谢!
【问题讨论】:
-
奇怪的函数,因为它在正确声明时更容易使用。您可以自行修复,只需声明参数 GLEDSETTING() 而不是 byte()。对于长度参数传递 Marshal.SizeOf(GLEDSETTING.GetType() * yourarr.Length)。你的结构声明看起来不错,所以这应该很容易工作。
-
你是对的。这也有效。我使用
Marshal.SizeOf(LD(0).GetType) * LD.Length作为第二个参数。看起来还可以。我认为仍然有些东西不合时宜,因为功能有效,但不要将 LED 应用到应该在的地方.. 仍在分析.. -
已解决。奇怪的功能。强制我“申请”两次以确保正常运行,但我正在使用您的方法。谢谢。 @HansPassant