PInvoke a été appeler Problème de type

oliv04860 - 13 mars 2013 à 14:59
 Utilisateur anonyme - 13 mars 2013 à 19:02
Bonjour à tous,

Lors de l'utilisation de ma fonction OpenCommunication() que vous pouvez voir si dessous j'ai un problème de Pile. J'ai pu lire que cela venait du type des paramètres car ils ne sont pas sur la même quantité de bite en C++ et en visual basic.

Cependant même en sachant ça je n'arrive pas à résoudre mon problème, est-ce que quelqu'un aurait une idée?

Voici le code en visual basic:


Module Module1

<DllImport("MP300Com.dll", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="_OpenCommunication@4")> _
Public Function OpenCommunication(ByVal Host As String) As Integer
End Function
<DllImport("MP300Com.dll", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="_CloseCommunication@0")> _
Public Function CloseCommunication() As Integer
End Function
<DllImport("MP300Com.dll", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="_SendFrame@20")> _
Public Function SendFrame(ByVal Port As Int32, ByVal NoResponse As Integer, ByVal TimeOut As Int16, ByVal command As String, ByVal response As StringBuilder) As Integer
End Function


Sub Main()
Dim r As Integer
Dim Buffer As StringBuilder
Dim strResult As String

Buffer = New StringBuilder(500)

r = OpenCommunication("USB:MP3.06.07.02")
If r = 0 Then
r = SendFrame(0, 0, -1, "HELO" + Chr(13), Buffer)
strResult = Buffer.ToString()
End If
CloseCommunication()
End Sub

End Module

Et la fonction OpenCommunication intégrée à la DLL:


int OpenCommunication(char * strport)

Merci d'avance

1 réponse

Utilisateur anonyme
13 mars 2013 à 19:02
Bonjour,

On peut lire ici :
When you use DllImport in VB, remember to set the CharSet to Unicode!

Donc, si l'on s'y fie, il faudrait déclarer tes fonction comme ceci :
<DllImport("MP300Com.dll", CharSet:=CharSet.Unicode, CallingConvention:=Call...


Il y a une seconde chose qui m'intrigue dans ton code :
Public Function SendFrame(ByVal Port As Int32, ByVal NoResponse As Integer, ByVal TimeOut As Int16, ByVal command As String, ByVal response As StringBuilder) As Integer
End Function
'...
r = SendFrame(0, 0, -1, "HELO" + Chr(13), Buffer)
strResult = Buffer.ToString() 

Afin de récupérer la réponse de ta fonction, tu devrais plutôt passer 'response' par référence (ByRef) :
... ByRef response As StringBuilder) As Integer
0
Rejoignez-nous