Hi,
I'm using SharpDX for DirectInput joystick management...and I can't resolve the problem I get while setting cooperative level
The problem comes from here:
joystick.SetCooperativeLevel(hwnd, CooperativeLevel.Foreground)
SharpDX.SharpDXException was unhandled
HResult=-2147024809
Message=HRESULT: [0x80070057], Module: [SharpDX.DirectInput], ApiCode: [DIERR_INVALIDPARAM/InvalidParam], Message: Incorrect parameter.
Please would you please help me understand what I'm doing wrong? I've been searching for days to resolve this!!
Many thanks!
I'm using SharpDX for DirectInput joystick management...and I can't resolve the problem I get while setting cooperative level
VB.NET:
Imports SharpDX
Imports SharpDX.DirectInput
Imports System.Windows.Interop
Class MainWindow
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
MainForJoystick()
End Sub
'
Private Shared Sub MainForJoystick()
' Initialize DirectInput
Dim directInput As New DirectInput
' Find a Joystick Guid
Dim joystickGuid = Guid.Empty
For Each deviceInstance As DeviceInstance In directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices)
joystickGuid = deviceInstance.InstanceGuid
Next
' If Gamepad not found, look for a Joystick
If joystickGuid = Guid.Empty Then
For Each deviceInstance As DeviceInstance In directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)
joystickGuid = deviceInstance.InstanceGuid
Next
End If
' If Joystick not found, throws an error
If joystickGuid = Guid.Empty Then
Debug.Print("No joystick/Gamepad found.")
End If
' Instantiate the joystick
Dim hwnd As IntPtr = New WindowInteropHelper(Application.Current.MainWindow).EnsureHandle()
Dim joystick = New Joystick(directInput, joystickGuid)
[B]joystick.SetCooperativeLevel(hwnd, CooperativeLevel.Foreground)[/B]
Debug.Print("Found Joystick/Gamepad with GUID: {0}", joystickGuid)
' Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128
' Acquire the joystick
joystick.Acquire()
' Poll events from joystick
While True
joystick.Poll()
Dim datas = joystick.GetBufferedData()
For Each state As JoystickUpdate In datas
Debug.Print(state.ToString)
' Example detecting 'Up' button
If state.Offset = 60 Then
If state.Value = 128 Then
Debug.Print("Up button pressed")
End If
End If
Next
End While
End Sub
End Class
The problem comes from here:
joystick.SetCooperativeLevel(hwnd, CooperativeLevel.Foreground)
SharpDX.SharpDXException was unhandled
HResult=-2147024809
Message=HRESULT: [0x80070057], Module: [SharpDX.DirectInput], ApiCode: [DIERR_INVALIDPARAM/InvalidParam], Message: Incorrect parameter.
Please would you please help me understand what I'm doing wrong? I've been searching for days to resolve this!!
Many thanks!
Last edited: