CLR: .Net / Mono / Boo / Otros CLR > VB .NET

 Duda Control Joystick VB.NET

(1/1)

gdonaire:
Hola a todos

Soy nuevo en el foro y tengo una duda sobre como controlar un Joystick con VB.NET, utilizo Microsoft Visual Basic 2008 Express Edition
El ejemplo esta sacado de otro enlace

He intentado adaptar este codigo a VB.NET, bueno estoy empezando pero me da errores.
A continuacion muestro la clase utilizada para manejar el jostick


--- Código: Text --- Imports System.Runtime.InteropServices Public Class joystick    ' Joystick API functions     Public Declare Function joyGetPosEx Lib "winmm.dll" (ByVal uJoyID As Long, ByRef pji As JOYINFOEX) As Long    Public Declare Function joyGetDevCapsA Lib "winmm.dll" (ByVal uJoyID As Long, ByRef pjc As JOYCAPS, ByVal cjc As Long) As Long    Public Declare Function joyGetNumDevs Lib "winmm.dll" () As Integer      <StructLayout(LayoutKind.Sequential)> _    Public Structure JOYCAPS        Dim wMid As Short        Dim wPid As Short        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _        Dim szPname As String        Dim wXmin As Integer        Dim wXmax As Integer        Dim wYmin As Integer        Dim wYmax As Integer        Dim wZmin As Integer        Dim wZmax As Integer        Dim wNumButtons As Integer        Dim wPeriodMin As Integer        Dim wPeriodMax As Integer        Dim wRmin As Integer        Dim wRmax As Integer        Dim wUmin As Integer        Dim wUmax As Integer        Dim wVmin As Integer        Dim wVmax As Integer        Dim wCaps As Integer        Dim wMaxAxes As Integer        Dim wNumAxes As Integer        Dim wMaxButtons As Integer        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _        Dim szRegKey As String        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _        Dim szOEMVxD As String    End Structure     Public Structure JOYINFOEX        Dim dwSize As Integer        Dim dwFlags As Integer        Dim dwXpos As Integer        Dim dwYpos As Integer        Dim dwZpos As Integer        Dim dwRpos As Integer        Dim dwUpos As Integer        Dim dwVpos As Integer        Dim dwButtons As Integer        Dim dwButtonNumber As Integer        Dim dwPOV As Integer        Dim dwReserved1 As Integer        Dim dwReserved2 As Integer    End Structure     Public MYJOYEX As JOYINFOEX    Public MYJOYCAPS As JOYCAPS     Public Sub InitJoy()        'Get the joystick number in the system and about information        Dim xJa, xRj As Long        Dim xJn As Integer        xJa = joyGetNumDevs        Debug.Print("There are " & xJa & " joysticks")        Debug.Print("Longitud MYJOYCAPS: " & Len(MYJOYCAPS))        For xJn = 0 To xJa            Debug.Print(xJn)            xRj = joyGetDevCapsA(xJn, MYJOYCAPS, 404)             If Val(MYJOYCAPS.wPid) <> 0 Then                Debug.Print(MYJOYCAPS.wPid)           End If         Next     End SubEnd Class  
El problema es que cuando llamo al metodo InitJoy no me detecta ningun joystick (todos los wPid son cero).
No se si estoy realizando algo mal, agradeceria cualquier ayuda y/o comentario.

Gracias a todos.

Nebire:
Hola.

Nunca he usado esas API, pero un vistazo rápido al visor de API, confirma mis sospechas... Te adjunto la declaración original de estas API y las estructuras para VB.6 que obviamente deben ser correctamente interpretadas para vb > 6.0


--- Código: Visual Basic --- Public Declare Function joyGetPosEx Lib "winmm.dll" Alias "joyGetPosEx" (ByVal uJoyID As Long, pji As JOYINFOEX) As LongPublic Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As JOYCAPS, ByVal uSize As Long) As LongPublic Type JOYCAPS        wMid As Integer        wPid As Integer        szPname As String * MAXPNAMELEN        wXmin As Integer        wXmax As Integer        wYmin As Integer        wYmax As Integer        wZmin As Integer        wZmax As Integer        wNumButtons As Integer        wPeriodMin As Integer        wPeriodMax As IntegerEnd TypePublic Type JOYINFOEX        dwSize As Long                 '  tamaño de la estructura        dwFlags As Long                '  indicadores de lo que hay que devolver        dwXpos As Long                 '  posición x        dwYpos As Long                 '  posición y        dwZpos As Long                 '  posición z        dwRpos As Long                 '  timón/posición del 4º eje        dwUpos As Long                 '  posición del 5º eje        dwVpos As Long                 '  posición del 6º eje        dwButtons As Long              '  estado de los botones        dwButtonNumber As Long         '  número del botón que está presionado ahora        dwPOV As Long                  '  estado del punto de vista        dwReserved1 As Long            '  reservado para la comunicación del controlador winmm        dwReserved2 As Long            '  reservado para expansión futuraEnd Type   
Como puedes ver , debes cambiar los tipos de datos originales, un long en vb6.0 es un integer (int32) en vb2008.
Haz también las correcciones pertienentes en la sub InitJoy (xJa, xRj deben declarase como integer visto la devolución de las API).

luego yo haría los siguientes cambios:

--- Código: Visual Basic ---     ' cambiaría esto:    Public MYJOYCAPS As JOYCAPS     ' por esto    Public MYJOYCAPS() As JOYCAPS     ' esto:    xJa = joyGetNumDevs   ' por esto:    xJa = joyGetNumDevs - 1    ReDim MYJOYCAPS(0 To xJa)     ' esto:    Dim xJn As Integer    Debug.Print("Longitud MYJOYCAPS: " & Len(MYJOYCAPS))    For xJn = 0 To xJa         Debug.Print(xJn)         xRj = joyGetDevCapsA(xJn, MYJOYCAPS, 404)             If Val(MYJOYCAPS.wPid) <> 0 Then                Debug.Print(MYJOYCAPS.wPid)             End If    Next     ' por esto, añadiendo un bloque try ... catch:    Dim n As Integer     Debug.Print("Longitud MYJOYCAPS(0): " & Len(MYJOYCAPS(0)))    n = -1    Do While n < (xJa - 1)        n = n + 1         Debug.Print(n)        Try            xRj = joyGetDevCapsA(n, MYJOYCAPS(n), 404)               If MYJOYCAPS(n).wPid <> 0 Then                Debug.Print(MYJOYCAPS(n).wPid)             End If         catch             msgbox err.Description        Finally           '.    Loop    
Yo no tengo Joysticks así que no puedo probarlo...
Una vez que arregles ese desaguisado de los tipos de datos, si tienes más problemas vuelve a preguntar con el error devuelto

gdonaire:
Hola Nebire

Gracias por tus aclaraciones, siguiendo algunas indicaciones tuyas he cambiado los prototipos de las importaciones de DLL ya que eran erroneas.
Ahora el codigo funciona y me devuelve la informacion sobre el joystick conectado.


--- Código: Text ---Imports System.Runtime.InteropServices Public Class joystick    ' Joystick API functions    Private Declare Function joyGetPosEx Lib "winmm.dll" (ByVal uJoyID As Integer, ByRef pji As JOYINFOEX) As Integer    Private Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal uJoyID As Integer, ByRef pjc As JOYCAPS, ByVal cjc As Integer) As Integer    Private Declare Function joyGetNumDevs Lib "winmm.dll" () As Integer     <StructLayout(LayoutKind.Sequential)> _    Public Structure JOYCAPS        Dim wMid As Short        Dim wPid As Short        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _        Dim szPname As String        Dim wXmin As Integer        Dim wXmax As Integer        Dim wYmin As Integer        Dim wYmax As Integer        Dim wZmin As Integer        Dim wZmax As Integer        Dim wNumButtons As Integer        Dim wPeriodMin As Integer        Dim wPeriodMax As Integer        Dim wRmin As Integer        Dim wRmax As Integer        Dim wUmin As Integer        Dim wUmax As Integer        Dim wVmin As Integer        Dim wVmax As Integer        Dim wCaps As Integer        Dim wMaxAxes As Integer        Dim wNumAxes As Integer        Dim wMaxButtons As Integer        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _        Dim szRegKey As String        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _        Dim szOEMVxD As String    End Structure     Public Structure JOYINFOEX        Dim dwSize As Integer        Dim dwFlags As Integer        Dim dwXpos As Integer        Dim dwYpos As Integer        Dim dwZpos As Integer        Dim dwRpos As Integer        Dim dwUpos As Integer        Dim dwVpos As Integer        Dim dwButtons As Integer        Dim dwButtonNumber As Integer        Dim dwPOV As Integer        Dim dwReserved1 As Integer        Dim dwReserved2 As Integer    End Structure     '***  joyGetPosEx  Constants  ***     Public Const JOYSTICKID1 = 0    Public Const JOYSTICKID2 = 1    Public Const MMSYSERR_BASE = 0    ' The joystick driver is not present.     Public Const MMSYSERR_NODRIVER = (MMSYSERR_BASE + 6)    ' An invalid parameter was passed.     Public Const MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11)    ' Windows 95/98/Me: The specified joystick identifier is invalid.     Public Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2)    Public Const JOYERR_NOERROR = 0    'if successful or one of the following error values.    Public Const JOYERR_BASE = 160    ' Windows NT/2000/XP: The specified joystick identifier is invalid.     Public Const JOYERR_PARMS = (JOYERR_BASE + 5)    ' The specified joystick is not connected to the system.    Public Const JOYERR_UNPLUGGED = (JOYERR_BASE + 7)     Public JoyNum As Long    Private JoyInfoExtended As JOYINFOEX    Public MYJOYEX As JOYINFOEX      Public Sub InitJoy()        'Get the joystick number in the system and about information        Dim xJa, xRj As Long        Dim xJn As Integer        Dim sDJ As String        Dim CapX As JOYCAPS         xJa = joyGetNumDevs        ' The joyGetNumDevs function returns the number of joysticks supported by the         ' current driver or zero if no driver is installed.        If (xJa = 0) Then            MsgBox("There is no joystick driver installed.", MsgBoxStyle.Critical)        End If        Debug.Print("There are " & xJa & " joysticks")        For xJn = 0 To (xJa - 1)            Debug.Print(xJn)            xRj = joyGetDevCaps(xJn, CapX, 404)            Select Case xRj                Case MMSYSERR_NODRIVER                    'The joystick driver is not present or joystick identifier is invalid                    MsgBox("The joystick driver is not present or joystick identifier is invalid", MsgBoxStyle.Critical)                Case MMSYSERR_INVALPARAM                    ' An invalid parameter was passed or joystick identifier is invalid                    MsgBox("An invalid parameter was passed or joystick identifier is invalid", MsgBoxStyle.Critical)                Case Else                    ' default            End Select             If Val(CapX.wPid) <> 0 Then                sDJ = "Joystick " & Trim(Str(xJn + 1))                sDJ &= Str(CapX.wNumAxes) & " Axes"                sDJ &= Str(CapX.wNumButtons) & " Buttons "                sDJ &= CapX.szPname                Debug.Print(sDJ)            End If        Next    End Sub   End Class  
Gracias por la ayuda.

Saludos

cristian said:
hola amigo puedes montar ese codigo ya funcionando en vb.net para mirarlo te agradeceria

Navegación

[0] Índice de Mensajes

Ir a la versión completa