473,549 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unbalanced Stack?


I use the following keyboard hook to stop some windows functions. VS2005

This line is giving me the following error. Anyone know why or how to
fix it. As far as I can tell my functions are declared correctly.

Return CallNextHookEx( KeyboardHandle, Code, wParam, lParam)

PInvokeStackImb alance was detected
Message: A call to PInvoke function
'Utility!Utilit y.LowLevelKBHoo k::CallNextHook Ex' has unbalanced the
stack. This is likely because the managed PInvoke signature does not
match the unmanaged target signature. Check that the calling convention
and parameters of the PInvoke signature match the target unmanaged
signature.
This is the class that i am using to do this. To duplicate the error
just create a new form, create a new LowLevelKBHook object and then
press a key. You'll get the error. Thanks.

Chris

Imports System.Runtime. InteropServices
Imports System.Reflecti on
Imports System.Drawing
Imports System.Threadin g

Public Class LowLevelKBHook
Public Declare Function UnhookWindowsHo okEx Lib "user32" _
(ByVal hHook As Integer) As Integer

Public Declare Function SetWindowsHookE x Lib "user32" _
Alias "SetWindowsHook ExA" (ByVal idHook As Integer, _
ByVal lpfn As KeyboardHookDel egate, ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer

Private Declare Function GetAsyncKeyStat e Lib "user32" _
(ByVal vKey As Integer) As Integer

Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT ) As Integer

Public Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure

' Low-Level Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const LLKHF_EXTENDED As Integer = &H1
Private Const LLKHF_INJECTED As Integer = &H10
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const LLKHF_UP As Integer = &H80

' Virtual Keys
Public Const VK_TAB As Integer = &H9
Public Const VK_CONTROL As Integer = &H11
Public Const VK_ESCAPE As Integer = &H1B
Public Const VK_DELETE As Integer = &H2E

Private Const WH_KEYBOARD_LL As Integer = 13&
Public KeyboardHandle As Integer
' Implement this function to block as many
' key combinations as you'd like
Sub New()
Me.HookKeyboard ()
End Sub

Public Function IsHooked( _
ByRef Hookstruct As KBDLLHOOKSTRUCT ) As Boolean

Debug.WriteLine ("Hookstruct.vk Code: " & Hookstruct.vkCo de)
Debug.WriteLine (Hookstruct.vkC ode = VK_ESCAPE)
Debug.WriteLine (Hookstruct.vkC ode = VK_TAB)

If (Hookstruct.vkC ode = VK_ESCAPE) And _
CBool(GetAsyncK eyState(VK_CONT ROL) _
And &H8000) Then

Call HookedState("Ct rl + Esc blocked")
Return True
End If

If (Hookstruct.vkC ode = VK_TAB) And _
CBool(Hookstruc t.flags And _
LLKHF_ALTDOWN) Then

Call HookedState("Al t + Tab blockd")
Return True
End If

If (Hookstruct.vkC ode = VK_ESCAPE) And _
CBool(Hookstruc t.flags And _
LLKHF_ALTDOWN) Then

Call HookedState("Al t + Escape blocked")
Return True
End If

Return False
End Function

Private Sub HookedState(ByV al Text As String)
Debug.WriteLine (Text)
End Sub

Public Function KeyboardCallbac k(ByVal Code As Integer, _
ByVal wParam As Integer, _
ByRef lParam As KBDLLHOOKSTRUCT ) As Integer

If (Code = HC_ACTION) Then
Debug.WriteLine ("Calling IsHooked")

If (IsHooked(lPara m)) Then
Return 1
End If

End If

Return CallNextHookEx( KeyboardHandle, _
Code, wParam, lParam)

End Function
Public Delegate Function KeyboardHookDel egate( _
ByVal Code As Integer, _
ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT ) _
As Integer

<MarshalAs(Unma nagedType.Funct ionPtr)> _
Private callback As KeyboardHookDel egate

Public Sub HookKeyboard()
callback = New KeyboardHookDel egate(AddressOf KeyboardCallbac k)

KeyboardHandle = SetWindowsHookE x( _
WH_KEYBOARD_LL, callback, _
Marshal.GetHINS TANCE( _
[Assembly].GetExecutingAs sembly.GetModul es()(0)).ToInt3 2, 0)

Call CheckHooked()
End Sub

Public Sub CheckHooked()
If (Hooked()) Then
Debug.WriteLine ("Keyboard hooked")
Else
Debug.WriteLine ("Keyboard hook failed: " & Err.LastDllErro r)
End If
End Sub

Private Function Hooked() As Boolean
Hooked = KeyboardHandle <> 0
End Function

Public Sub UnhookKeyboard( )
If (Hooked()) Then
Call UnhookWindowsHo okEx(KeyboardHa ndle)
End If
End Sub

End Class
Nov 23 '05 #1
2 9884
"Chris" <no@spam.com> schrieb im Newsbeitrag:
I use the following keyboard hook to stop some windows functions. VS2005

This line is giving me the following error. Anyone know why or how to
fix it. As far as I can tell my functions are declared correctly.

Return CallNextHookEx( KeyboardHandle, Code, wParam, lParam)

PInvokeStackImb alance was detected
Message: A call to PInvoke function
'Utility!Utilit y.LowLevelKBHoo k::CallNextHook Ex' has unbalanced the
stack. This is likely because the managed PInvoke signature does not
match the unmanaged target signature. Check that the calling convention
and parameters of the PInvoke signature match the target unmanaged
signature.
[...]
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT ) As Integer


=> 'ByRef lParam As KBDLLHOOKSTRUCT '.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 23 '05 #2
Herfried K. Wagner [MVP] wrote:
"Chris" <no@spam.com> schrieb im Newsbeitrag:
I use the following keyboard hook to stop some windows functions. VS2005

This line is giving me the following error. Anyone know why or how to
fix it. As far as I can tell my functions are declared correctly.

Return CallNextHookEx( KeyboardHandle, Code, wParam, lParam)

PInvokeStackImb alance was detected
Message: A call to PInvoke function
'Utility!Utilit y.LowLevelKBHoo k::CallNextHook Ex' has unbalanced the
stack. This is likely because the managed PInvoke signature does not
match the unmanaged target signature. Check that the calling
convention and parameters of the PInvoke signature match the target
unmanaged signature.
[...]
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As KBDLLHOOKSTRUCT ) As Integer

=> 'ByRef lParam As KBDLLHOOKSTRUCT '.


Thanks...
Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
7715
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is how this would work with class objects. In this case do you have to push the object on the stack at the start of every public procedure etc. in the...
14
30074
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! ...
4
6708
by: Allen | last post by:
Hi all, What are some different approaches to dealing with stack overflows in C++? I'm especially interested in approaches concerned with speed for infrequent overflows. -- Best wishes, Allen
7
1739
by: El kroty | last post by:
Hi folks!! i'm trying to write k&r ex1-24 without a stack. I almost have it but there's a bug that makes me crazy and can't find it :'( whit some files it works and with others it doesn't. Here is the code, any clues?? Thanks. <code> #include <stdio.h> #include <stdlib.h> enum states { FREE, INQUOTE, INCOMMENT, SLASH, ASTERISK, SCAPE };
4
3607
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
8
2078
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string...
1
4205
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting errors of all kinds. I have decided to start from scratch. Any suggestions someone can give me would be greatly appreciated!! Here is the current code:...
11
1771
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items = def push(self, item): self.items.append(item)
0
1806
by: sangam56 | last post by:
Hi world. I have some hierarchical data in which there is unbalanced binary structure. I get the parent and its left and right child from database. I can count the total lelft and right children of any node. But my requirement is to display first three levels in a graphical binary representation. When I click any of the leaf node, I need to...
0
7554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7511
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7840
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5119
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1973
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.