Joystick problems (odd ones)

orufet
03-09-2002, 06:45 PM
Ok, I'm having a really weird problem here; something I've never come across before. But then again, I've never played with joystick APIs before, either. Here's what happens: I ran the code in the API Guide for the joyGetPos API call, and it ran the program just fine. But, then all of a sudden, VB quits. It just closes, and doesn't even go though it's unloading routine. (When I open VB again, the project does NOT appear in the Recent list.) Odd. Well, I know I should be able to figure this out, but I just don't know what it is. Any ideas? Here's the code from the API-Guide, in case you don't have it.

' defines and structures
Const JOY_BUTTON1 = &H1
Const JOY_BUTTON2 = &H2
Const JOY_BUTTON3 = &H4
Const JOY_BUTTON4 = &H8
Const JOYERR_BASE = 160
Const JOYERR_NOERROR = (0)
Const JOYERR_NOCANDO = (JOYERR_BASE + 6)
Const JOYERR_PARMS = (JOYERR_BASE + 5)
Const JOYERR_UNPLUGGED = (JOYERR_BASE + 7)
Const MAXPNAMELEN = 32
Const JOYSTICKID1 = 0
Const JOYSTICKID2 = 1

Private Type JOYINFO
X As Long
Y As Long
Z As Long
Buttons As Long
End Type
Private Type JOYCAPS
wMid As Integer
wPid As Integer
szPname As String * MAXPNAMELEN
wXmin As Long
wXmax As Long
wYmin As Long
wYmax As Long
wZmin As Long
wZmax As Long
wNumButtons As Long
wPeriodMin As Long
wPeriodMax As Long
End Type

Private Declare Function joyGetDevCaps Lib "winmm.dll" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As JOYCAPS, ByVal uSize As Long) As Long
Private Declare Function joyGetNumDevs Lib "winmm.dll" () As Long
Private Declare Function joyGetPos Lib "winmm.dll" (ByVal uJoyID As Long, pji As JOYINFO) As Long
Private Function GetJoystick(ByVal joy As Integer, JI As JOYINFO) As Boolean
If joyGetPos(joy, JI) <> JOYERR_NOERROR Then
GetJoystick = False
Else
GetJoystick = True
End If
End Function
' If IsConnected is False then it returns the number of
' joysticks the driver supports. (But may not be connected)
'
' If IsConnected is True the it returns the number of
' joysticks present and connected.
'
' IsConnected is true by default.
Private Function IsJoyPresent(Optional IsConnected As Variant) As Long
Dim ic As Boolean
Dim i As Long
Dim j As Long
Dim ret As Long
Dim JI As JOYINFO

ic = IIf(IsMissing(IsConnected), True, CBool(IsConnected))

i = joyGetNumDevs

If ic Then
j = 0
Do While i > 0
i = i - 1 'Joysticks id's are 0 and 1
If joyGetPos(i, JI) = JOYERR_NOERROR Then
j = j + 1
End If
Loop

IsJoyPresent = j
Else
IsJoyPresent = i
End If

End Function
' Fills the ji structure with the minimum x, y, and z
' coordinates. Buttons is filled with the number of
' buttons.
Private Function GetJoyMin(ByVal joy As Integer, JI As JOYINFO) As Boolean
Dim jc As JOYCAPS

If joyGetDevCaps(joy, jc, Len(jc)) <> JOYERR_NOERROR Then
GetJoyMin = False

Else
JI.X = jc.wXmin
JI.Y = jc.wYmin
JI.Z = jc.wZmin
JI.Buttons = jc.wNumButtons

GetJoyMin = True
End If
End Function
' Fills the ji structure with the maximum x, y, and z
' coordinates. Buttons is filled with the number of
' buttons.
Private Function GetJoyMax(ByVal joy As Integer, JI As JOYINFO) As Boolean
Dim jc As JOYCAPS
If joyGetDevCaps(joy, jc, Len(jc)) <> JOYERR_NOERROR Then
GetJoyMax = False
Else
JI.X = jc.wXmax
JI.Y = jc.wYmax
JI.Z = jc.wZmax
JI.Buttons = jc.wNumButtons
GetJoyMax = True
End If
End Function
Private Sub Form_Paint()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim JInfo As JOYINFO
'Clear the form
Me.Cls
'Print the information to the form
Me.Print "Number of joysticks the driver supports:" + Str$(IsJoyPresent(False))
Me.Print "Number of connected joysticks:" + Str$(IsJoyPresent(True))
GetJoystick JOYSTICKID1, JInfo
Me.Print "Number of buttons:" + Str$(JInfo.Buttons)
GetJoyMax JOYSTICKID1, JInfo
Me.Print "Max X:" + Str$(JInfo.X)
Me.Print "Max Y:" + Str$(JInfo.Y)
Me.Print "Max Z:" + Str$(JInfo.Z)
GetJoyMin JOYSTICKID1, JInfo
Me.Print "Min X:" + Str$(JInfo.X)
Me.Print "Min Y:" + Str$(JInfo.Y)
Me.Print "Min Z:" + Str$(JInfo.Z)
End Sub


Thanks...:-\

Squirm
03-09-2002, 07:13 PM
Sounds like one of those API calls has a memory leak or something. :-\

orufet
03-09-2002, 07:43 PM
Well, I shut down, tested the Joystick connection (everything was fine) and turned on again. Same thing. So I tried joyGetPosEx, as found in an example on PSC. It generates an error saying that the memory could not be "read". The title is "vb6.exe - Application Error". The message give the memory location and all of that. Meaningless, I assume. Anyway, I click OK and VB shuts down. Any thoughts?

AndreRyan
03-12-2002, 04:58 AM
Maybe there is an Init API you have to call or something or you passed an invalid variable.

I had an error similar when using gdi's PtinRect function in a for loop, the loop got to the function then said "VB6 caused a page fault at GDI32.DLL"
In the end I made up a custom function which does the same thing

You could try DirectInput if you can't get it to work.

orufet
03-12-2002, 09:32 AM
Thanks for the ideas, but I've discovered it's a driver problem. The joystick won't work with anything. I'll deal with it. Thank, though

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum