Forum: VB.NET |
Thema:
AW: Re: deviceiocontrol problem |
Von:
G. Guest (
27.05.2006 04:20) |
hallo,
ich habe haargenau das gleiche problem
ich habe durch createfile ein handle zu meinem gerät das hat geklappt und errorcode=0 also alles in ordnung
jetzt versuche ich mit dem gerät zu kommunizieren via deviceiocontrol
nach dem aufruf bekomme ich errorcode=1 "unzulässige funktion" was könnte denn der grund sein?
was ist denn falsch mit meinem code? (wurde in VB .NET geschrieben)
Public Declare Auto Function CreateFile Lib "kernel32.dll" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As System.IO.FileShare, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As System.IO.FileMode, _
ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) _
As Microsoft.Win32.SafeHandles.SafeFileHandle
Public Declare Function DeviceIoControl Lib "kernel32" ( _
ByVal hDevice As Microsoft.Win32.SafeHandles.SafeFileHandle, _
ByVal dwIoControlCode As Integer, _
ByVal lpInBuffer As Integer, _
ByVal nInBufferSize As Integer, _
ByVal lpOutBuffer As IntPtr, _
ByVal nOutBufferSize As Integer, _
ByRef lpBytesReturned As Integer, _
ByVal lpOverlapped As Integer) As Boolean
'IOCTLs
Public IOCTL_ASYNC_IN As Integer = CTL_CODE(FILE_DEVICE_UNKNOWN, &H850, METHOD_BUFFERED, FILE_ANY_ACCESS)
Public IOCTL_USB_PACKET_SIZE As Integer = CTL_CODE(FILE_DEVICE_UNKNOWN, &H851, METHOD_BUFFERED, FILE_ANY_ACCESS)
Public IOCTL_API_VERSION As Integer = CTL_CODE(FILE_DEVICE_UNKNOWN, &H800, METHOD_BUFFERED, FILE_ANY_ACCESS)
Dim GUID_DEVINTERFACE_GRMNUSB As System.Guid
Dim DIDI As SP_DEVICE_INTERFACE_DATA
Dim DIDC As SP_DEVINFO_DATA
Dim DIDID As SP_DEVICE_INTERFACE_DETAIL_DATA
Dim theDevInfo As IntPtr
Dim len As Integer
Dim ip As IntPtr
Dim bytes As Byte() = Nothing
DIDI.cbSize = Marshal.SizeOf(DIDI)
DIDC.cbSize = Marshal.SizeOf(DIDC)
GUID_DEVINTERFACE_GRMNUSB = New System.Guid("2C9C45C28E7D4C08A12D816BBAE722C0")
//////klappt wunderbar////
theDevInfo = SetupDiGetClassDevs(GUID_DEVINTERFACE_GRMNUSB, vbNullString, IntPtr.Zero, DIGCF_DEVICEINTERFACE Or DIGCF_PRESENT)
If Not SetupDiEnumDeviceInterfaces(theDevInfo, IntPtr.Zero, GUID_DEVINTERFACE_GRMNUSB, 0, DIDI) Then
DisplayLastWin32Error()
Handle = 0
Return
End If
DIDID = New SP_DEVICE_INTERFACE_DETAIL_DATA
DIDID.cbSize = Marshal.SizeOf(DIDID)
If Not SetupDiGetDeviceInterfaceDetail(theDevInfo, DIDI, IntPtr.Zero, 0, len, IntPtr.Zero) Then
ip = Marshal.AllocHGlobal(len)
Marshal.StructureToPtr(DIDID, ip, True)
If SetupDiGetDeviceInterfaceDetail(theDevInfo, DIDI, ip, len, len, IntPtr.Zero) Then
bytes = New Byte(len - 1) {}
Marshal.Copy(ip, bytes, 0, len)
Else
MsgBox(Marshal.GetLastWin32Error)
End If
End If
''Get the USB packet size, which we need for sending packets
Dim usbpacketsize As IntPtr
usbpacketsize = Marshal.AllocHGlobal(13)
Dim theBytesReturned As Integer
Dim hDevice As Microsoft.Win32.SafeHandles.SafeFileHandle
hDevice = CreateFile( _
System.Text.Encoding.Unicode.GetString(bytes, 4, bytes.Length - 4), _
GENERIC_READ Or GENERIC_WRITE, IO.FileShare.ReadWrite, IntPtr.Zero, IO.FileMode.OpenOrCreate, 0, 0)
------------------- Ab Hier wird es problematisch---------------------------
If DeviceIoControl(hDevice, IOCTL_USB_PACKET_SIZE, 0, 0, usbpacketsize, Marshal.SizeOf(usbpacketsize), theBytesReturned, 0) Then
Dim erg As String
erg = Marshal.PtrToStringAnsi(ip)
Marshal.FreeHGlobal(ip)
Else
Marshal.FreeHGlobal(ip)
DisplayLastWin32Error()
End If
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!