Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Mon programme bien dans le IDE mais pas hors du IDE pourquoi?

1 réponse
Avatar
armony
Salut,

j ai un gros probleme
j ai ecrit un programme dans Microsoft Visual Basic 2005 Express qui me
permet d envoyer des donnees a un appareil connecte par USB
quand je teste ce programme dans le IDE la communication entre les 2
appareils fonctionne a merveille
mais quand j execute le fichier exe dans le dossier debug ou release le
programme bloque au niveau de la methode WriteFile de Api et win32 error est
"Invalid Handle" bien que createfile apres avoir ete execute retourne un
handle valable

alors je ne comprends pas ce qui a pu se passer pendant le debugging! est-ce
la version express le problem? s il vous plait aidez-moi!

voici quelques declarations

Public Declare Auto Function CreateFile Lib "kernel32.dll" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As IntPtr

Public Declare Auto Function WriteFile Lib "kernel32.dll" _
(ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), _
ByVal nNumberOfBytesToWrite As Integer, _
ByRef lpNumberOfBytesWritten As Integer, _
ByRef lpOverlapped As IntPtr) As Integer

et une partie de mon code

hDevice = CreateFile(devicePath, GENERIC_READ Or GENERIC_WRITE, _
0, 0&, OPEN_EXISTING, 0&, 0&)

'Get the USB packet size, which we need for sending packets
If DeviceIoControl(hDevice, IOCTL_USB_PACKET_SIZE, IntPtr.Zero, 0,
usbpacketsize, 4, theBytesReturned, IntPtr.Zero) Then
If theBytesReturned > 0 Then
Dim bytes1() As Byte

bytes1 = New Byte(theBytesReturned - 1) {}

Marshal.Copy(usbpacketsize, bytes1, 0, theBytesReturned)

If theBytesReturned = 2 Then
usb_packet_size = BitConverter.ToInt16(bytes1, 0)
End If
End If
Else
Marshal.FreeHGlobal(ip)
Return
End If

'send the startsessionpacket
SendPacket(theStartSessionPacket)


la methode sendpacket:

Public Sub SendPacket(ByVal aPacket As Byte())
Dim theBytesReturned As Integer

//////////////// c est ici que mon programme plante et retourne le message
"invalid handle" //////////////////////////
Dim bool As Integer = WriteFile(hDevice, aPacket, aPacket.Length -
1, theBytesReturned, Nothing)

If bool = INVALID_HANDLE_VALUE Or bool = 0 Then
DisplayLastWin32Error()
theDevInfo = IntPtr.Zero
Return
End If

'If the packet size was an exact multiple of the USB packet
'size, we must make a final write call with no data
If usb_packet_size <> 0 Then
If (aPacket.Length Mod usb_packet_size = 0) Then
Dim newBytes() As Byte
newBytes = New Byte() {}
WriteFile(hDevice, newBytes, 0, theBytesReturned, IntPtr.Zero)
End If
End If
End Sub

1 réponse

Avatar
armony
en fait je devais declarer le dernier argument de WriteFile ByVal et non ByRef!

c etait ca la solution du problem

"armony" a écrit :

Salut,

j ai un gros probleme
j ai ecrit un programme dans Microsoft Visual Basic 2005 Express qui me
permet d envoyer des donnees a un appareil connecte par USB
quand je teste ce programme dans le IDE la communication entre les 2
appareils fonctionne a merveille
mais quand j execute le fichier exe dans le dossier debug ou release le
programme bloque au niveau de la methode WriteFile de Api et win32 error est
"Invalid Handle" bien que createfile apres avoir ete execute retourne un
handle valable

alors je ne comprends pas ce qui a pu se passer pendant le debugging! est-ce
la version express le problem? s il vous plait aidez-moi!

voici quelques declarations

Public Declare Auto Function CreateFile Lib "kernel32.dll" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As IntPtr

Public Declare Auto Function WriteFile Lib "kernel32.dll" _
(ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), _
ByVal nNumberOfBytesToWrite As Integer, _
ByRef lpNumberOfBytesWritten As Integer, _
ByRef lpOverlapped As IntPtr) As Integer

et une partie de mon code

hDevice = CreateFile(devicePath, GENERIC_READ Or GENERIC_WRITE, _
0, 0&, OPEN_EXISTING, 0&, 0&)

'Get the USB packet size, which we need for sending packets
If DeviceIoControl(hDevice, IOCTL_USB_PACKET_SIZE, IntPtr.Zero, 0,
usbpacketsize, 4, theBytesReturned, IntPtr.Zero) Then
If theBytesReturned > 0 Then
Dim bytes1() As Byte

bytes1 = New Byte(theBytesReturned - 1) {}

Marshal.Copy(usbpacketsize, bytes1, 0, theBytesReturned)

If theBytesReturned = 2 Then
usb_packet_size = BitConverter.ToInt16(bytes1, 0)
End If
End If
Else
Marshal.FreeHGlobal(ip)
Return
End If

'send the startsessionpacket
SendPacket(theStartSessionPacket)


la methode sendpacket:

Public Sub SendPacket(ByVal aPacket As Byte())
Dim theBytesReturned As Integer

//////////////// c est ici que mon programme plante et retourne le message
"invalid handle" //////////////////////////
Dim bool As Integer = WriteFile(hDevice, aPacket, aPacket.Length -
1, theBytesReturned, Nothing)

If bool = INVALID_HANDLE_VALUE Or bool = 0 Then
DisplayLastWin32Error()
theDevInfo = IntPtr.Zero
Return
End If

'If the packet size was an exact multiple of the USB packet
'size, we must make a final write call with no data
If usb_packet_size <> 0 Then
If (aPacket.Length Mod usb_packet_size = 0) Then
Dim newBytes() As Byte
newBytes = New Byte() {}
WriteFile(hDevice, newBytes, 0, theBytesReturned, IntPtr.Zero)
End If
End If
End Sub