witschi
Messages postés4Date d'inscriptionvendredi 9 octobre 2009StatutMembreDernière intervention15 juillet 2010
-
27 mai 2010 à 10:31
Mayzz
Messages postés2813Date d'inscriptionmardi 15 avril 2003StatutMembreDernière intervention 2 juin 2020
-
27 mai 2010 à 13:02
Bonjour,
Sa fait quelques jours que je me suis à la recherche d'une transformation pour cette structure en c++:
const unsigned MAX_VALS = 16 * 1024;
struct m_data
{
unsigned year, month, day, hour, min, sec;
unsigned rec_count;
short int pres[MAX_VALS];
short int humid[MAX_VALS];
short int temp1[MAX_VALS];
short int temp2[MAX_VALS];
};
en vb.net
Si je fais une structure comme celle-ci :
Public Const MAX_VALS As Integer = 16 * 1024
Public Structure MDL_DATA
Public year, month, day, hour, min, sec As UInt32
Public rec_count As UInt32
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_VALS)> _
Public pres() As Int16
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_VALS)> _
Public humid() As Int16
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_VALS)> _
Public temp1() As Int16
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_VALS)> _
Public temp2() As Int16
End Structure
J'ai une exception qui m'informe que ma structure est trop complexe ou trop grande.
Si je fais une structure comme celle-ci :
Public Structure MDL_DATA
Public year, month, day, hour, min, sec As UInt32
Public rec_count As UInt32
Public pres() As Int16
Public humid() As Int16
Public temp1() As Int16
Public temp2() As Int16
End Structure
Il me fait une erreur d'écriture au niveau de la mémoire quand il me remplie mes tableaux de "Int16"
witschi
Messages postés4Date d'inscriptionvendredi 9 octobre 2009StatutMembreDernière intervention15 juillet 2010 27 mai 2010 à 12:58
Bonjour,
J'ai trouvé la solution.
Si je définie ma fonction externe comme ceci cela ne fonctionne pas, on me dit que ma structure est trop grande :
Public Declare Auto Function mdl_read Lib "mdl.dll" (ByVal serialPointer As IntPtr, ByRef res As MDL_DATA, ByVal HWND As IntPtr) As Integer
Alors que si a la place de ByRef res As MDL_DATA je passe un pointer sa fonctionne
Public Declare Auto Function mdl_read Lib "mdl.dll" (ByVal serialPointer As IntPtr, ByVal resPtr As IntPtr, ByVal HWND As IntPtr) As Integer
Ce qui me donne pour passé la structure en tant que pointer :
Dim tmpPointer As IntPtr
tmpPointer = Marshal.StringToHGlobalAnsi(myDockingStation)
Dim res As Integer
'Try
Dim datas = New MDL_DATA
'try to clearn the old pointer <--- VRAIMENT NECESSAIRE ??
Try
Marshal.DestroyStructure(myDatasPointer, GetType(MDL_DATA))
Catch ex As Exception
Debug.WriteLine("UPLOAD destroyStruc.: " & ex.Message)
End Try
'build a pointer to the structure
myDatasPointer = Marshal.AllocHGlobal(Marshal.SizeOf(datas)) ' Ici on alloue la place manuellement et sa fonctionne!
Marshal.StructureToPtr(datas, myDatasPointer, False)
res = m_CommunicationDLL.mdl_read(tmpPointer, myDatasPointer, Me.Handle)
Voilà donc en résumé. Si la structure est trop grande on alloue la place "à la main" et on envoie un pointeur.