Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[need help] Writing data to a USB device (HID)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Fronzel
Grandmaster Cheater Supreme
Reputation: 0

Joined: 07 Feb 2008
Posts: 1099
Location: Nexons backyard

PostPosted: Wed Dec 03, 2008 1:03 pm    Post subject: [need help] Writing data to a USB device (HID) Reply with quote

Hi all,


Still trying to send data to my USB keyboard (Luxeed "lightshow" deTA100) to control the lighting, but yet without any luck. I found this basic USB source pack of Intel for VB6 and although it looks promising it is quite hard to use for me.

At first i had to fix a bug in the OpenDevice function because it didnt get the devicename properly. Well now i get it running and it finds the correct USB device and opens it (I sniffed the communictaion with advanced USB port monitor).

Now when i try to send data to the device the real problem starts. I am using this:

Code:

Public Sub WriteUSBdevice(BufferPtr&, ByteCount&)
' This subroutine "writes" to an openned USB device
' Copy the user buffer into a local buffer so that a ReportID (=0) may be prepended
' The first byte will contain the ReportID (=0)
Dim ReportBuffer(256) As Byte
If ByteCount& > 254 Then ErrorExit ("Maximum ByteCount for WriteUSBdevice is 254")
Call CopyBuffer(BufferPtr&, AddressFor(ReportBuffer(1)), ByteCount&)
ReportBuffer(0) = 0 ' ReportID
Success = WriteFile(HidHandle&, AddressFor(ReportBuffer(0)), ByteCount& + 1, BytesWritten&, 0)
If (Success = 0) Then ErrorExit ("Could not write an Output Report")
End Sub



Well looks quite basic, too short too contain many bugs. However, the WriteFile always returns 0 which throws the error "Could not write output report". It does not even try to communicate with the device as Advanced USB monitor shows clearly that nocommunication towards or from the device happened. I must admit i dont fully understand the comment. It states to use a local buffer, this is BufferPtr& if i understood right. Then it says it needs to copy the user buffer to the local buffer, as far as i can tell the copy buffer function copies the BufferPtr to ReportBuffer.


Can anybody tell me whats wrong here? I am trying to send a 65 Byte Array to the keyboard, but iot simply doesnt send anything. Why does writefile always return 0. How can i debug this properly?

Or does anyone have a source for writing to a USB device? I would also take a look at sources in other languages, i would prefer .net or C, but i think everything would be fine right now. If i can get a C sample of an USB writer i might make a dll out of it, so i can use it from other languages. I looked at the microsoft driver developer kit and although it contains a section about USB this only generally describes how to write/read to a device, no samples or stuff. I did not find any helpfull class included in .net, or did i miss it?

_________________
Back to top
View user's profile Send private message
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Thu Dec 04, 2008 2:15 am    Post subject: Reply with quote

How are you getting HidHandle? Also, use GetLastError().


"If the function fails, or is completing asynchronously, the return value is zero (FALSE). To get extended error information, call the GetLastError function. "
It is possible it just isn't blocking the process while it writes it.
"Note The GetLastError code ERROR_IO_PENDING is not a failure; it designates the write operation is pending completion asynchronously. For more information, see Remarks."
If you get a different error code we can proceed from there.

_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish
Back to top
View user's profile Send private message
SXGuy
I post too much
Reputation: 0

Joined: 19 Sep 2006
Posts: 3551

PostPosted: Thu Dec 04, 2008 3:15 am    Post subject: Reply with quote

lol i posted the exact same thing.
Back to top
View user's profile Send private message
Fronzel
Grandmaster Cheater Supreme
Reputation: 0

Joined: 07 Feb 2008
Posts: 1099
Location: Nexons backyard

PostPosted: Thu Dec 04, 2008 4:40 am    Post subject: Reply with quote

I get the HID Handle from my OpenDevice function. I tried debugging the handle (Shown as a messagebox) and it looks absolutely OK. I must try out teh GetLastError, i somehow must have overlooked that function.

Code:

Public Function OpenUSBdevice(NameOfDevice$) As Boolean
' This function searches the system HID tables for NameOfDevice$
' If found then it opens the device and returns TRUE, else it returns FALSE
Dim HidGuid As Guid: Dim Success As Boolean: Dim Openned As Boolean: Dim Buffer(256) As Byte
Dim DeviceInterfaceData As Device_Interface_Data
Dim FunctionClassDeviceData As Device_Interface_Detail
'
'   First, get the HID class identifier
Call HidD_GetHidGuid(HidGuid.Data(0))
'   Get a handle for the Plug and Play node, request currently active HID devices
PnPHandle& = SetupDiGetClassDevs(HidGuid.Data(0), 0, 0, &H12)
If (PnPHandle& = -1) Then ErrorExit ("Could not attach to PnP node")
'
HidEntry& = 0: Openned = False
DeviceInterfaceData.cbsize = 28 'Length of data structure in bytes
'   Look through the table of HID devices
Do While SetupDiEnumDeviceInterfaces(PnPHandle&, 0, HidGuid.Data(0), HidEntry&, DeviceInterfaceData.cbsize)
'   There is a device here, get it's name
    FunctionClassDeviceData.cbsize = 5
    Success = SetupDiGetDeviceInterfaceDetail(PnPHandle&, DeviceInterfaceData.cbsize, _
            FunctionClassDeviceData.cbsize, UBound(FunctionClassDeviceData.DataPath), BytesReturned&, 0)
    If (Success = 0) Then ErrorExit ("Could not get the name of this HID device")
' Convert returned C string to Visual Basic String
    hidname$ = "": i& = 0
    Do While FunctionClassDeviceData.DataPath(i&) <> 0
        hidname$ = hidname$ & Chr$(FunctionClassDeviceData.DataPath(i&)): i& = i& + 1: Loop
' Can now open this HID device
    Dim SA As Security_Attributes
    HidHandle& = CreateFile(hidname$, &HC0000000, 3, SA, 3, 0, 0)
    If (HidHandle = -1) Then ErrorExit ("Could not open HID device")
' Is it OUR HID device?
    If HidD_GetProductString(HidHandle&, AddressFor(Buffer(0)), UBound(Buffer)) Then
        DeviceName$ = "": i& = 0
        Do While Buffer(i&) <> 0: DeviceName$ = DeviceName$ & Chr$(Buffer(i&)): i& = i& + 2: Loop
        If (StrComp(DeviceName$, NameOfDevice$) = 0) Then
            Openned = True: Exit Do: End If
        End If 'HidD_GetProductString
    Call CloseHandle(HidHandle&) ' Was not OUR HID device
    HidEntry& = HidEntry& + 1 ' Check next entry
    Loop 'SetupDiEnumDeviceInterfaces returns FALSE when there are no more entries
SetupDiDestroyDeviceInfoList (PnPHandle&)
OpenUSBdevice = Openned
End Function



In my final version i have changed the function a bit as DeviceName$ returned crap in some cases, for my luxeed it returns an empty string because of improper parsing (For some devices it works though). I just scan HIDName for the Vendor-ID and Product ID which works fine.

Tonight when Im back home i might give GetLastErropr a chance, but being frustrated already i started implementing a solution in VB.net 2008 as i could grab a more promising and recent source yesterday.

_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites