BAHAL_KEYHAN
دوشنبه 28 فروردین 1391, 00:02 صبح
با عرض سلام خدمت اساتید عزیز
من یک array byte دارم که می خوام به structure تبدیلش کنم
این از structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, pack:=1)> _
Public Structure NetMatType
' UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B4BFF9E0-8631-45CF-910E-62AB3970F27B"'
Public Sub Initialize()
ReDim Preserve X(12)
ReDim Preserve Y(12)
ReDim Preserve M(12)
ReDim Preserve Lb(19)
For i = 0 To 19
Lb(i) = New StringBuilder(6, 6)
Next
ReDim Preserve D(99)
ReDim Preserve C(19)
ReDim Preserve Cb(2)
ReDim Preserve T(19)
ReDim Preserve Tb(2)
End Sub
Dim PcNo As Short '1 ~ 8 is valid and -1 means that no data has been Rxed.
<VBFixedArray(12)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=13)> _
Dim X As Byte()
<VBFixedArray(12)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=13)> _
Dim Y() As Byte
<VBFixedArray(12)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=13)> _
Dim M() As Byte
<VBFixedArray(19)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.LPStr, sizeconst:=20)> _
Dim Lb() As StringBuilder
<VBFixedArray(99)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.R4, sizeconst:=100)> _
Dim D() As Single
<VBFixedArray(19)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I4, sizeconst:=20)> _
Dim C() As Integer
<VBFixedArray(2)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=3)> _
Dim Cb() As Byte
<VBFixedArray(19)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I4, sizeconst:=20)> _
Dim T() As Integer
<VBFixedArray(2)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=3)> _
Dim Tb() As Byte
End Structure
اینم کد تبدیل
Public Function GetData() As Qtype
Dim MyGC As GCHandle = GCHandle.Alloc(Rawdata(1), GCHandleType.Pinned)
Dim Obj As New NetMatType
Dim Q As Qtype
Try
Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, Obj)
Catch ex As Exception
Windows.Forms.MessageBox.Show(ex.ToString)
End Try
Q = Qtype.FromNetMatType(Obj)
MyGC.Free()
Return Q
End Function
ولی روی خط
Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, Obj)
exception رخ میده
system.argumentException:the structure must not be a value class
parametr name: structure at system.Runtime.InteropServices.Marshal.PtrToStruct ureHelper(IntPtr ptr,Object structure,Boolean allowValueClasses)
این exception به خاطر چی رخ میده؟؟؟؟؟؟
من یک array byte دارم که می خوام به structure تبدیلش کنم
این از structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, pack:=1)> _
Public Structure NetMatType
' UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B4BFF9E0-8631-45CF-910E-62AB3970F27B"'
Public Sub Initialize()
ReDim Preserve X(12)
ReDim Preserve Y(12)
ReDim Preserve M(12)
ReDim Preserve Lb(19)
For i = 0 To 19
Lb(i) = New StringBuilder(6, 6)
Next
ReDim Preserve D(99)
ReDim Preserve C(19)
ReDim Preserve Cb(2)
ReDim Preserve T(19)
ReDim Preserve Tb(2)
End Sub
Dim PcNo As Short '1 ~ 8 is valid and -1 means that no data has been Rxed.
<VBFixedArray(12)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=13)> _
Dim X As Byte()
<VBFixedArray(12)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=13)> _
Dim Y() As Byte
<VBFixedArray(12)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=13)> _
Dim M() As Byte
<VBFixedArray(19)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.LPStr, sizeconst:=20)> _
Dim Lb() As StringBuilder
<VBFixedArray(99)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.R4, sizeconst:=100)> _
Dim D() As Single
<VBFixedArray(19)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I4, sizeconst:=20)> _
Dim C() As Integer
<VBFixedArray(2)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=3)> _
Dim Cb() As Byte
<VBFixedArray(19)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I4, sizeconst:=20)> _
Dim T() As Integer
<VBFixedArray(2)> _
<MarshalAs(UnmanagedType.ByValArray, arraysubtype:=UnmanagedType.I1, sizeconst:=3)> _
Dim Tb() As Byte
End Structure
اینم کد تبدیل
Public Function GetData() As Qtype
Dim MyGC As GCHandle = GCHandle.Alloc(Rawdata(1), GCHandleType.Pinned)
Dim Obj As New NetMatType
Dim Q As Qtype
Try
Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, Obj)
Catch ex As Exception
Windows.Forms.MessageBox.Show(ex.ToString)
End Try
Q = Qtype.FromNetMatType(Obj)
MyGC.Free()
Return Q
End Function
ولی روی خط
Marshal.PtrToStructure(MyGC.AddrOfPinnedObject, Obj)
exception رخ میده
system.argumentException:the structure must not be a value class
parametr name: structure at system.Runtime.InteropServices.Marshal.PtrToStruct ureHelper(IntPtr ptr,Object structure,Boolean allowValueClasses)
این exception به خاطر چی رخ میده؟؟؟؟؟؟