Avec les outils du framework :
'Imports System
'Imports System.Runtime.InteropServices
Private Function StructToByteArray(ByVal Struct As Object) As Byte()
Dim StructSize As Integer = Marshal.SizeOf(Struct)
Dim StructPrt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Struct))
Dim StructBytes() As Byte = CType(Array.CreateInstance(GetType(Byte), StructSize), Byte())
Marshal.StructureToPtr(Struct, StructPrt, False)
Marshal.Copy(StructPrt, StructBytes, 0, StructSize)
Marshal.FreeHGlobal(StructPrt)
Return StructBytes
End Function
<!-- Coloration syntaxique vb/vba/vb.net : http://charles.racaud.free.fr/code-syntaxing/ -->
Coloration syntaxique vb/vba/vb.net
popeye63
Messages postés6Date d'inscriptionlundi 12 janvier 2004StatutMembreDernière intervention15 janvier 2009 15 janv. 2009 à 10:45
Ouaip merci pour ton aide....
J'ai écris la fonction inverse pour ceux que ca interesse à savoir passer d'un tableau de byte en une structure...
Public
Function ByteArrayToStruct(
ByVal tableauByte()
As
Byte,
ByVal StrucTType
As Type,
ByVal size
As
Integer)
As
Object
Dim p
As IntPtr = Marshal.AllocHGlobal(size)
Dim Result
As
ObjectMarshal.Copy(tableauByte, 0, p, size)
Private Function ByteArrayToStruct(Of T)(ByVal StructSrc() As Byte) As T
Dim StructSize As Integer = Marshal.SizeOf(GetType(T))
Dim StructPrt As IntPtr = Marshal.AllocHGlobal(StructSize)
Dim Struct As T
Marshal.Copy(StructSrc, 0, StructPrt, StructSize)
Struct = CType(Marshal.PtrToStructure(StructPrt, GetType(T)), T)
Marshal.FreeHGlobal(StructPrt)
Return Struct
End Function
<!-- Coloration syntaxique vb/vba/vb.net : http://charles.racaud.free.fr/code-syntaxing/ -->
Coloration syntaxique vb/vba/vb.net