Error in Direct Input Mouse Tester
Posted: Sun Mar 08, 2009 12:50 am
Im having an error when debugging the mouse test program that i just wrote for chapter 5
The line in the code in red below is causing this error:
LoaderLock was detected
DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput
Public Class Form1
Private mouse As DirectInput.Device
Private mousestate As DirectInput.MouseState
Private WithEvents clock As Timer
Public Function Init_Mouse() As Boolean
Try
'create new mouse device
mouse = New DirectInput.Device(SystemGuid.Mouse)
'set cooperative level for mouse
Dim cooplevel As CooperativeLevelFlags = _
CooperativeLevelFlags.Background Or _
CooperativeLevelFlags.NonExclusive
mouse.SetCooperativeLevel(Me, cooplevel)
'try to acquire the mouse
mouse.Acquire()
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Sub Mouse_Click(ByVal button As Integer)
TextBox4.Text = button.ToString
End Sub
Public Sub Mouse_Move(ByVal X As Integer, ByVal Y As Integer)
TextBox1.Text = X.ToString
TextBox2.Text = Y.ToString
End Sub
Public Sub Mouse_Wheel(ByVal Z As Integer)
TextBox3.Text = Z.ToString
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Init_Mouse() Then
MessageBox.Show("Error acquiring mouse device.")
Me.Close()
End If
'create the timer
clock = New Timer()
clock.Interval = 100
clock.Start()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
mouse.Unacquire()
mouse.Dispose()
mouse = Nothing
End Sub
Private Sub clock_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles clock.Tick
'poll mouse for movement and button presses
mousestate = mouse.CurrentMouseState
Dim b() As Byte = mousestate.GetMouseButtons()
If (b(0) > 0) Then Mouse_Click(1)
If (b(1) > 0) Then Mouse_Click(2)
If (b(2) > 0) Then Mouse_Click(3)
If mousestate.X <> 0 Or mousestate.Y <> 0 Then
Mouse_Move(mousestate.X, mousestate.Y)
End If
If mousestate.Z <> 0 Then
Mouse_Wheel(mousestate.Z)
End If
End Sub
End Class
Please Help! Thanks!
Fallental
The line in the code in red below is causing this error:
LoaderLock was detected
DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput
Public Class Form1
Private mouse As DirectInput.Device
Private mousestate As DirectInput.MouseState
Private WithEvents clock As Timer
Public Function Init_Mouse() As Boolean
Try
'create new mouse device
mouse = New DirectInput.Device(SystemGuid.Mouse)
'set cooperative level for mouse
Dim cooplevel As CooperativeLevelFlags = _
CooperativeLevelFlags.Background Or _
CooperativeLevelFlags.NonExclusive
mouse.SetCooperativeLevel(Me, cooplevel)
'try to acquire the mouse
mouse.Acquire()
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Sub Mouse_Click(ByVal button As Integer)
TextBox4.Text = button.ToString
End Sub
Public Sub Mouse_Move(ByVal X As Integer, ByVal Y As Integer)
TextBox1.Text = X.ToString
TextBox2.Text = Y.ToString
End Sub
Public Sub Mouse_Wheel(ByVal Z As Integer)
TextBox3.Text = Z.ToString
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Init_Mouse() Then
MessageBox.Show("Error acquiring mouse device.")
Me.Close()
End If
'create the timer
clock = New Timer()
clock.Interval = 100
clock.Start()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
mouse.Unacquire()
mouse.Dispose()
mouse = Nothing
End Sub
Private Sub clock_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles clock.Tick
'poll mouse for movement and button presses
mousestate = mouse.CurrentMouseState
Dim b() As Byte = mousestate.GetMouseButtons()
If (b(0) > 0) Then Mouse_Click(1)
If (b(1) > 0) Then Mouse_Click(2)
If (b(2) > 0) Then Mouse_Click(3)
If mousestate.X <> 0 Or mousestate.Y <> 0 Then
Mouse_Move(mousestate.X, mousestate.Y)
End If
If mousestate.Z <> 0 Then
Mouse_Wheel(mousestate.Z)
End If
End Sub
End Class
Please Help! Thanks!
Fallental