[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

keybd_event - PInvokeStackImbalance was detected

Posted on 2006-05-11
9
Medium Priority
?
855 Views
Last Modified: 2010-08-05
Hello Experts,

I've made this program that does the following:

1. Declare IP address to be used as a match.
2. Go through a list of IP address that are currently connected to local computer.
2.1 If Declared IP address is found and its connection state is equal to ESTABLISHED then Turn SCROLL LOCK ON.

I get an error at this part:

keybd_event(VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)

The crazy part about it is that it compiles and runs just fine! I hope you guys can help me out.
0
Comment
Question by:donniedarko801
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16665009
May we see your declaration for keybd_event?

It should be:

    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, _
        ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

You have a VB6 background...make sure that you are using Integer and not Long when dealing with API calls.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16666348
Stack imbalance means that you have a variable type incorrect.

Bob

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16666458
Why don't you use SendKeys class? It does the same as keybd_event.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:donniedarko801
ID: 16667597
Public Class Form1

 Private Const IP_TARGET = "10.10.1.1"
 Private Const LIGHT_ON = True
 Private Const LIGHT_OFF = False
 Private Const VK_SCROLL As Short = &H91
 Private Const KEYEVENTF_KEYUP As Short = &H2
 Private Const KEYEVENTF_EXTENDEDKEY As Short = &H1
 Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  Timer1.Interval = 1500 ' 1.5 seconds
  Timer1.Start() ' Begin to scan

 End Sub

 Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

  Dim Properties As Net.NetworkInformation.IPGlobalProperties = Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
  Dim Local_IP_List As Net.NetworkInformation.TcpConnectionInformation() = Properties.GetActiveTcpConnections()


  Dim Random_IP As Net.NetworkInformation.TcpConnectionInformation

  For Each Random_IP In Local_IP_List ' Search remote IP list for QA's IP address.
   ' If a match is found And currently active, Turn ON
   If Random_IP.RemoteEndPoint.Address.ToString = IP_TARGET And Random_IP.State.ToString = "Established" Then
    SetScrollLock(LIGHT_ON)
    Exit Sub
   End If

  Next Random_IP

  ' Otherwise Turn OFF
  SetScrollLock(LIGHT_OFF)

 End Sub

 Private Sub SetScrollLock(ByVal State As Boolean)

  If My.Computer.Keyboard.ScrollLock() <> State Then
   keybd_event(VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0)
   keybd_event(VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
  End If

 End Sub

End Class
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16667618
yup you were right!

incorrect

 Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

correct

 Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16667630
See my previous comment please...you have the wrong declaration for keybd_event().
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16667636
Lol...ignore my comment...we were cross-posting.  =)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16667645
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

.NET Long is 64 bytes length - be careful.
Again, SendKeys does the same without PInvoke problems.
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16668194
SendKeys Blows! Not 99% Reliable...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question