Small Problem with mouse emulation code that works mostly.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have some code below that works mostly in vb.net but when it gets to ExtHubPort and after it doesnt work i think i might have incorrect formatting of code somewhere or a simple error because if I get it the same sounds as the vbscript except when
mouse device is plugged in it gives me this error in device manager: "device could not start error 10". Here is the code but to plainly put it the code just adds an emulated external usb hub to the systems hub and then usb device to that hub:
<pre class="prettyprint lang-vb Option Explicit On
Imports System
Imports Microsoft.Dsf


Module Test_Emulator
Class MouseEmulator
Const IID_IDSFBus = "{E927C266-5364-449E-AE52-D6A782AFDA9C}"
Const IID_ISoftUSBDevice = "{9AC61697-81AE-459A-8629-BF5D5A838519}"
Const IID_EHCICtrlrObj = "{16017C34-A2BA-480B-8DE8-CD08756AD1F8}"

Private MouseX As Integer
Private MouseY As Integer

Private devicenum As Integer

Public Property X As Integer
Public Property Y As Integer
Public Property Deviceordernumber As Integer

Public Function AddMouseDeviceAndMove(ByVal numdevices As Integer, ByVal clickstate As String, onlysendinputreport As Boolean) As Integer
On Error GoTo Errorhandler

Const IID_IDSFBus = "{E927C266-5364-449E-AE52-D6A782AFDA9C}"
Const IID_ISoftUSBDevice = "{9AC61697-81AE-459A-8629-BF5D5A838519}"
Const IID_EHCICtrlrObj = "{16017C34-A2BA-480B-8DE8-CD08756AD1F8}"
devicenum = Deviceordernumber
MouseX = X
MouseY = Y

Dim ExtHub As SoftUSBHub = New SoftUSBHub
WriteLine "Setting diagnostics for external hub"
SetEndpointDiagnostics(ExtHub.SoftUSBDevice)

WriteLine "Enumerating simulated devices to look for a simulated EHCI controller"
Dim CtrlrDev As DSFDevice
CtrlrDev = EnumSimulatedDevices(IID_IDSFBus)

If CtrlrDev Is Nothing Then
MsgBox("Could not find simulated a EHCI controller. Did you remember to run softehcicfg.exe /install?")
End If

Dim CtrlrObj As SoftEHCICtrlr = CtrlrDev.Object(IID_EHCICtrlrObj)
CtrlrObj = CtrlrDev.Object(IID_EHCICtrlrObj)
Dim RootHubPort As SoftEHCIRootHubPort
RootHubPort = CtrlrObj.Ports(1)
MsgBox("waiting for usb hub to be plugged into port 1 of root hub")
PromptAndWaitForEnterKey "Press enter to plug external hub into port 1 of root hub"
RootHubPort.HotPlug(ExtHub.SoftUSBDevice.DSFDevice)

Dim GenericHIDDev As SoftHidReceiver.Interop.HIDDevice = New SoftHidReceiver.Interop.HIDDevice

Dim GenericHIDDSFDev As Microsoft.Dsf.DSFDevice

GenericHIDDSFDev = GenericHIDDev.DSFDevice
Dim GenericHIDUSBDev As SoftUSBDevice
GenericHIDUSBDev = New SoftUSBDevice
GenericHIDUSBDev = GenericHIDDSFDev.Object(IID_ISoftUSBDevice)

Make this a 1.1 USB device
Configure1dot1Device(GenericHIDUSBDev)
WriteLine "Setting generic hid device logging to true"

GenericHIDDev.Logging = True

WriteLine "Setting diagnostics for GenericHID device"
SetEndpointDiagnostics(GenericHIDUSBDev)

WriteLine "Create HID report descriptors for a generic mouse."
WriteLine "You can change the device to a mouse by loading these report descriptors instead."
Dim ReportDescriptorsMouse(66) As Object
ReportDescriptorsMouse(0) = CByte(&H5) Usage Page
ReportDescriptorsMouse(1) = CByte(1) Generic Controls
ReportDescriptorsMouse(2) = CByte(&H9) Usage
ReportDescriptorsMouse(3) = CByte(&H2) Mouse
ReportDescriptorsMouse(4) = CByte(&HA1) Collection
ReportDescriptorsMouse(5) = CByte(&H1) Application
ReportDescriptorsMouse(6) = CByte(&H9) Usage
ReportDescriptorsMouse(7) = CByte(&H1) Pointer
ReportDescriptorsMouse(8) = CByte(&HA1) Collection
ReportDescriptorsMouse(9) = CByte(&H0) Physical

ReportDescriptorsMouse(10) = CByte(&H5) Usage Page
ReportDescriptorsMouse(11) = CByte(&H9) Buttons
ReportDescriptorsMouse(12) = CByte(&H19) Usage Minimum
ReportDescriptorsMouse(13) = CByte(&H1) 1
ReportDescriptorsMouse(14) = CByte(&H29) Usage Maximum
ReportDescriptorsMouse(15) = CByte(&H5) 5
ReportDescriptorsMouse(16) = CByte(&H15) Logical Minimum
ReportDescriptorsMouse(17) = CByte(&H0) 0
ReportDescriptorsMouse(18) = CByte(&H25) Logical Maximum
ReportDescriptorsMouse(19) = CByte(&H1) 1
ReportDescriptorsMouse(20) = CByte(&H95) Report Count
ReportDescriptorsMouse(21) = CByte(&H5) 5
ReportDescriptorsMouse(22) = CByte(&H75) Report Size
ReportDescriptorsMouse(23) = CByte(&H1) 1
ReportDescriptorsMouse(24) = CByte(&H81) Input
ReportDescriptorsMouse(25) = CByte(2) Data Variable Absolute
ReportDescriptorsMouse(26) = CByte(&H95) Report Count
ReportDescriptorsMouse(27) = CByte(1) 1
ReportDescriptorsMouse(28) = CByte(&H75) Report Size
ReportDescriptorsMouse(29) = CByte(3) 3 padding
ReportDescriptorsMouse(30) = CByte(&H81) Input
ReportDescriptorsMouse(31) = CByte(1) Constant

ReportDescriptorsMouse(32) = CByte(&H5) Usage Page
ReportDescriptorsMouse(33) = CByte(&H1) Generic Desktop
ReportDescriptorsMouse(34) = CByte(&H9) Usage
ReportDescriptorsMouse(35) = CByte(&H30) X
ReportDescriptorsMouse(36) = CByte(&H9) Usage
ReportDescriptorsMouse(37) = CByte(&H31) Y
ReportDescriptorsMouse(38) = CByte(&H9) Usage
ReportDescriptorsMouse(39) = CByte(&H38) Wheel
ReportDescriptorsMouse(40) = CByte(&H15) Logical Minimum
ReportDescriptorsMouse(41) = CByte(&H81) -127
ReportDescriptorsMouse(42) = CByte(&H25) Logical Maximum
ReportDescriptorsMouse(43) = CByte(&H7F) 127
ReportDescriptorsMouse(44) = CByte(&H75) Report Size
ReportDescriptorsMouse(45) = CByte(&H8) 8
ReportDescriptorsMouse(46) = CByte(&H95) Report Count
ReportDescriptorsMouse(47) = CByte(&H3) 3
ReportDescriptorsMouse(48) = CByte(&H81) Input
ReportDescriptorsMouse(49) = CByte(&H6) Data variable relative

ReportDescriptorsMouse(50) = CByte(&H5) Usage Page
ReportDescriptorsMouse(51) = CByte(&HC) Consumer Devices
ReportDescriptorsMouse(52) = CByte(&H15) Logical Minimum
ReportDescriptorsMouse(53) = CByte(&H81) -127
ReportDescriptorsMouse(54) = CByte(&H25) Logical Maximum
ReportDescriptorsMouse(55) = CByte(&H7F) 127
ReportDescriptorsMouse(56) = CByte(&H75) Report Size
ReportDescriptorsMouse(57) = CByte(&H8) 8
ReportDescriptorsMouse(58) = CByte(&H95) Report Count
ReportDescriptorsMouse(59) = CByte(&H1) 1
ReportDescriptorsMouse(60) = CByte(&HA) Usage
ReportDescriptorsMouse(61) = CByte(&H38) AC Pan
ReportDescriptorsMouse(62) = CByte(&H2) Horizontal Wheel scroll
ReportDescriptorsMouse(63) = CByte(&H81) Input
ReportDescriptorsMouse(64) = CByte(&H6) Data variable relative

ReportDescriptorsMouse(65) = CByte(&HC0) End collection
ReportDescriptorsMouse(66) = CByte(&HC0) End collection

WriteLine "Create HID descriptors using desired report descriptor"
Dim HIDDescriptors(8) As Object
HIDDescriptors(0) = CByte(&H9)
HIDDescriptors(1) = CByte(&H21)
HIDDescriptors(2) = CByte(&H1)
HIDDescriptors(3) = CByte(&H1)
HIDDescriptors(4) = CByte(&H0)
HIDDescriptors(5) = CByte(&H1)
HIDDescriptors(6) = CByte(&H22)
HIDDescriptors(7) = CByte(UBound(ReportDescriptorsMouse) + 1) Descriptor length LOBYTE
HIDDescriptors(8) = CByte(&H0) Descriptor length HIBYTE

WriteLine "Populate Report Descriptors in generic hid device to create generic device with mouse"
GenericHIDDev.CreateCustomReportDescriptor(ReportDescriptorsMouse)
WriteLine "Populate HID Descriptors"
GenericHIDDev.CreateCustomHIDDescriptor(HIDDescriptors)
WriteLine "You can also create both Report and HID descriptors using just the BuildHIDDescriptors method."

WriteLine "Configuring GenericHID device is necessary to load descriptors"
GenericHIDDev.ConfigureDevice()


max of 12 mouse devices supported until i can confirm the kinect sdk will support more


Dim numcreated As Integer
numcreated = 0
MsgBox("plug in emulated mouse device")
Dim ExtHubPort As SoftUSBHubPort
ExtHubPort = ExtHub.Ports(1)
ExtHubPort.HotPlug(GenericHIDDSFDev)



starts at mouse 2 because physical or system mouse is mouse 1


Dim InputReport1(4) As Object
InputReport1(0) = CByte(0)
InputReport1(1) = CByte(MouseX)
InputReport1(2) = CByte(MouseY)
InputReport1(3) = CByte(0)
InputReport1(4) = CByte(0)

MsgBox("Wait for input report")
PromptAndWaitForEnterKey "Queue input reports for processing"
GenericHIDDev.QueueInputReport(InputReport1, 10)

PromptAndWaitForEnterKey "Wait for the device to finish enumerating. Press enter to start processing input reports."
GenericHIDDev.StartProcessing()
WriteLine "You may send additional input reports at this time..."
PromptAndWaitForEnterKey "Press enter at any time to stop processing input reports and start cleanup."
GenericHIDDev.StopProcessing()

PromptAndWaitForEnterKey "Press enter to unplug GenericHID device from external hub."
ExtHubPort.Unplug()

PromptAndWaitForEnterKey "Press enter to unplug external hub from root hub."
RootHubPort.Unplug()

PromptAndWaitForEnterKey "Press enter to terminate this script."
GenericHIDUSBDev.Destroy()
ExtHub.Destroy()



Return 1
Errorhandler:

MsgBox("Error:" + Err.Number.ToString + Err.Description.ToString + "Source of Error:" + Err.Source.ToString + "Line Number:" + Err.Erl.ToString)
Return 1
End Function





/////////////////////////////////////////////////////////////////////////////
Sub Configure1dot1Device

This routine configures the device as USB 1.1 by setting the version and
setting the correct MaxPacketSize on the devices endpoints
/////////////////////////////////////////////////////////////////////////////
Private Sub Configure1dot1Device(ByVal USBDevice)

Dim Config As Object
Config = Nothing
Dim Interfacei As Object
Interfacei = Nothing
Dim Endpoint As Object
Endpoint = Nothing
Dim EPType As Object
EPType = Nothing

WriteLine "Configuring GenericHID device as a USB 1.1 full speed device."

USBDevice.USB = &H110
For Each Config In USBDevice.Configurations
For Each Interfacei In Config.Interfaces
For Each Endpoint In Interfacei.Endpoints
EPType = Endpoint.Attributes And &H3
If (1 = EPType) Then
1023 is the maximum packet size for isochronous endpoints
of full speed devices
Endpoint.MaxPacketSize = 1023
Else
For all other endpoint types, 64 is the max packet size
Endpoint.MaxPacketSize = 64
End If
Next
Next
Next

End Sub





/////////////////////////////////////////////////////////////////////////////
Sub SetEndpointDiagnostics

This routine sets a diagnostic property on all the endpoints in the
specified simulated USB device so that you can see all transfers to and
from the device when running under a kernel debugger. This routine will
write information to the console describing every congiguration, interface,
and endpoint that it finds.
/////////////////////////////////////////////////////////////////////////////
Private Sub SetEndpointDiagnostics(ByVal USBDevice)

Const SOFTUSBENDPOINT_OBJECTFLAGS = 100
Const SOFTUSBENDPOINT_DONOTTRACETRANSFERS = &H0
Const SOFTUSBENDPOINT_TRACETRANSFERINPUT = &H1
Const SOFTUSBENDPOINT_TRACETRANSFEROUPUT = &H2
Const SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT = &H3

Dim Config As Object
Config = Nothing
Dim Interfacei As Object
Interfacei = Nothing
Dim Endpoint As Object
Endpoint = Nothing

Dim CtlFlags As Object
CtlFlags = SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT
Dim OtherFlags As Object
OtherFlags = SOFTUSBENDPOINT_TRACETRANSFERINPUTANDOUPUT
Dim FlagsType As Object
FlagsType = SOFTUSBENDPOINT_OBJECTFLAGS
Dim EPNum As Object
EPNum = 0
Dim EPDir As Object
EPDir = 0
Dim EPType As Object
EPType = 0

USBDevice.Endpoint0.SetObjectFlags(FlagsType, CtlFlags)

For Each Config In USBDevice.Configurations

WriteLine "Setting endpoint diagnostics for configuration " & Config.ConfigurationValue

For Each Interfacei In Config.Interfaces

MsgBox("Setting endpoint diagnostics for interface " + Interfacei.InterfaceNumber.ToString + " alternate " + Interfacei.AlternateSetting.ToString)

For Each Endpoint In Interfacei.Endpoints

EPNum = Endpoint.EndpointAddress And &HF
EPDir = Endpoint.EndpointAddress And &H80
If EPDir = 0 Then
EPDir = "OUT"
Else
EPDir = "IN"
End If

EPType = Endpoint.Attributes And &H3
Select Case EPType
Case 0
EPType = "Control"

Case 1
EPType = "Isoch"

Case 2
EPType = "Bulk"

Case 3
EPType = "Interrupt"
End Select

MsgBox("Endpoint.SetObjectFlags for " & EPType & " " & EPDir & " endpoint " & Hex(EPNum))
Endpoint.SetObjectFlags(FlagsType, OtherFlags)

Next

Next

Next

End Sub



/////////////////////////////////////////////////////////////////////////////
Function EnumSimulatedDevices

This function searches the collection of simulated devices
referenced by DSF.Devices for a device that exposes an ancillary
object from DSFDevice.Object with the specified GUID. If found it returns the
DSFDevice object otherwise it returns Nothing.
/////////////////////////////////////////////////////////////////////////////
Private Function EnumSimulatedDevices(ByVal SearchObjectGUID)
Dim DSF1 As DSF.DSF
DSF1 = New DSF.DSF
Dim DevSought As DSF.DSFDevice
DevSought = New DSF.DSFDevice
Dim Dev As DSF.DSFDevice
Dev = New DSF.DSFDevice
Dim ObjSought As Object
ObjSought = Nothing

For Each Dev In DSF1.Devices
If Dev.HasObject(SearchObjectGUID) Then
ObjSought = Dev.Object(SearchObjectGUID)
If Not ObjSought Is Nothing Then
DevSought = Dev
Exit For
End If
End If
Next

EnumSimulatedDevices = DevSought

End Function




/////////////////////////////////////////////////////////////////////////////
Sub Error

This routine writes the specified text to the console and
terminates the script
/////////////////////////////////////////////////////////////////////////////
Private Sub Error(msg)
WriteLine "Script terminating due to error"
MsgBox ("Error:" + msg)
WScript.Quit


End Sub

End Class


End Module[/code]
<br/>
The msgboxs mark important areas that will help if you wish to debug this code. I have a codeplex page in which the project has the dlls for both 32-bit and 64-bit OSs but if you have the dsf runtime you can manually get them from the GAC (c:windowsassembly
version and yes im sure because thats where i found them) and just paste into visual studios public assmeblies folder and it

View the full article
 
Back
Top