"CallbackOnCollectedDelegate was detected" - vb.net

I am trying to disable windows keys. Bt while running the application i get an error "CallbackOnCollectedDelegate was detected" Below is my code
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As IntPtr
Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As IntPtr) As Boolean
Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As IntPtr, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
Const WH_KEYBOARD_LL As Integer = 13
Structure KBDLLHOOKSTRUCT
Dim vkCode As Integer
Dim scanCode As Integer
Dim flags As Integer
Dim time As Integer
Dim dwExtraInfo As Integer
End Structure
Dim intLLKey As IntPtr
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, IntPtr.Zero, 0)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
UnhookWindowsHookEx(intLLKey)
End Sub
Private Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
Dim blnEat As Boolean = False
Select Case wParam
Case 256, 257, 260, 261
'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or
((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or
((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or
((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or
((lParam.vkCode = 92) AndAlso (lParam.flags = 1))
End Select
If blnEat = True Then
Return 1
Else
Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
End If
End Function
Any help is very much appreciated

The issue is the same as this post. The difference being that they're using C#. However, VB is doing the same thing; generating a delegate for you. For reference, this is what the decompiled code looks like in your form's Load event.
Private Sub Form1_Load(sender As Object, e As EventArgs)
Me.intLLKey = Form1.SetWindowsHookEx(13, New Form1.LowLevelKeyboardProcDelegate(Me.LowLevelKeyboardProc), IntPtr.Zero, 0)
End Sub
Note that it's creating a LowLevelKeyboardProcDelegate delegate for you. I won't completely rehash #HansPassant's answer here, as he does an excellent job describing the problem and solution; only say that you'll need to store your own reference to the LowLevelKeyboardProcDelegate delegate.

Related

VB.NET using ReadProcessMemory API

I am trying to use basic windows API functions in VB.NET. I have the following code:
Imports System.Runtime.InteropServices
Public Class testClass
Declare Function CreateProcessA Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String,
ByVal lpCommandLine As String, ByVal lpProcessAttributes As IntPtr,
ByVal lpThreadAttributes As IntPtr,
<MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandles As Boolean,
ByVal dwCreationFlags As Integer, ByVal lpEnvironment As IntPtr,
ByVal lpCurrentDirectory As String, ByVal lpStartupInfo As Byte(),
ByVal lpProcessInformation As IntPtr()) As <MarshalAs(UnmanagedType.Bool)> Boolean
Declare Function GetThreadContext Lib "kernel32" Alias "GetThreadContext" (ByVal hThread As IntPtr,
ByVal lpContext As UInteger()) As <MarshalAs(UnmanagedType.Bool)> Boolean
Declare Function ReadProcessMemory Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As IntPtr,
ByVal lpBaseAddress As IntPtr, ByRef lpBuffer As IntPtr,
ByVal nSize As Integer,
ByRef lpNumberOfBytesRead As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
Public Sub Test()
Dim locProcess As String = "C:\Windows\notepad.exe"
Dim iPtr1 As IntPtr = IntPtr.Zero
Dim startInfo As Byte() = New Byte(67) {}
Dim procInfo As IntPtr() = New IntPtr(3) {}
Dim cpResult = CreateProcessA(locProcess, vbNullString, iPtr1, iPtr1, False, 0, iPtr1, Nothing, startInfo, procInfo)
Dim pContext As UInteger() = New UInteger(178) {}
pContext(0) = &H10002
If GetThreadContext(procInfo(1), pContext) Then
Dim pAddress As New IntPtr(pContext(&H29) + 8L)
Dim pSize As New IntPtr(4)
Dim bAddress As IntPtr = IntPtr.Zero
Dim iPtr2 As IntPtr = IntPtr.Zero
If ReadProcessMemory(procInfo(0), pAddress, bAddress, CInt(pSize), iPtr2) <> 0 Then
MessageBox.Show("Success!")
Else
MessageBox.Show("ReadProcessMemory Error code is :" & Err.LastDllError)
End If
Else
MessageBox.Show("GetThreadContext Error code is :" & Err.LastDllError)
End If
End Sub
End Class
Public Class Form1
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim tc As New testClass
tc.Test()
End Sub
End Class
I am getting an error on ReadProcessMemory. The code results in
"ReadProcessMemory Error code is : 299".
I have been struggling to find the issue seeing as the error can be vague when dealing directly with windows DLLs. Any help on the issue would be greatly appreciated. Or, is there another solution to debugging properly (other than LastDllError) that may point me in the right direction of solving this issue. Thank you!

How to bring external application window on top? [duplicate]

This question already has an answer here:
Check process is running, then switch to it?
(1 answer)
Closed 5 years ago.
I have Outlook express always on top and Google chrome behind Outlook. How to bring running Google chrome on top of OutLook express using visual basic?
Following opens a new application but i want existing Google chrome to bring on top?
Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", AppWinStyle.MaximizedFocus)
EDIT:
Public Class Form1
Declare Auto Function FindWindow Lib "User32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Auto Function SetForegroundWindow Lib "User32.dll" (ByVal Hwnd As IntPtr) As Long
'Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Int32) As Int32
Declare Auto Function FindWindowEx Lib "User32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", AppWinStyle.MaximizedFocus)
Dim Handle As IntPtr = FindWindow("Notepad", Nothing)
If Handle.Equals(IntPtr.Zero) Then
End
End If
'Dim HandleChildOne As IntPtr = FindWindowEx(Handle, IntPtr.Zero, "Notepad", IntPtr.Zero)
'If HandleChildOne.Equals(IntPtr.Zero) Then
'End
'End If
Dim Result As Integer = SetForegroundWindow(Handle)
If Result.Equals(0) Then
End
Else
MsgBox("Above 0: success. https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx " & Result)
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End
End Sub
End Class
Method 1 of #Codexer works (Method 2, 3 also included for research later). Note that, Chrome window position/size get unexpectedly modified while applying ShowWindow(Handle, 9)
Public Class Form1
Declare Auto Function FindWindow Lib "User32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Auto Function SetForegroundWindow Lib "User32.dll" (ByVal Hwnd As IntPtr) As Long
Declare Auto Function FindWindowEx Lib "User32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Auto Function SetWindowPos Lib "User32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Declare Auto Function ShowWindow Lib "User32.dll" (handle As IntPtr, nCmdShow As Integer) As Boolean
Declare Auto Function IsIconic Lib "User32.dll" (handle As IntPtr) As Boolean
' Method 1
Private Sub StartOrShowProcess(ByVal strProcessName As String)
Try
Dim handle As IntPtr
Dim proc As Process() = Process.GetProcessesByName(strProcessName)
If proc.Count > 0 Then
For Each procP As Process In proc
handle = procP.MainWindowHandle
' Do we have handle and minimized or not minimized?
If handle <> 0 Then
ShowWindow(handle, 9)
SetForegroundWindow(handle)
End If
Next
Else 'Not running or started...
Process.Start(strProcessName)
End If
Catch ex As Exception
'Handle your error...
End Try
End Sub
' Method 2/3
Private Sub Old()
'=== Method 1: Target chrome > as new window
'Shell("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", AppWinStyle.MaximizedFocus)
'=== Method 2: Target chrome > Target specific TAB
Dim Handle As IntPtr = FindWindow(Nothing, "Nieuw tabblad - Google Chrome")
If Handle.Equals(IntPtr.Zero) Then
Handle = FindWindow(Nothing, "TITLE... - Google Chrome")
If Handle.Equals(IntPtr.Zero) Then
End
End If
End If
' !!!ShowWindow!!!! help to detect from minmize state
ShowWindow(Handle, 9)
Dim Result As Integer = SetForegroundWindow(Handle)
If Result.Equals(0) Then
End
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TopMost = True
StartOrShowProcess("chrome")
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End
End Sub
End Class

Launching application inside form goes wrong

I have this code in my project
Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Dim proc As Process
Private Sub irI_Click(sender As Object, e As EventArgs) Handles irI.Click
If b = True Then
proc = Process.Start(Application.StartupPath & "\Resources\puzzle_temaI\bin\slidePuzzle.exe")
proc.WaitForInputIdle()
SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
b = False
End If
End Sub
So, what i'm trying to do here is to open an application inside my panel1, and it works fine, the only problem is that 9/10 times i open it, it doesn't show inside the panel but somewhere else randomly.
I've run out of ideas and I'll appreciate any help.

VB.Net api affects windows forms

I am creating a application in vb.net and i am using some API functions.For example : GetForegroundWindow,SetWindowPos .So my app should change the active window's size and position when clicked.But it is affecting also the system forms like taskbar , StartMenu.How can i avoid this?
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Integer
End Function ''SETTTING THE WINDOW POSITION
<Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
Private ReadOnly HWND_TOP As New IntPtr(0)
Public Const SWP_FRAMECHANGED As Integer = &H20
Dim cX As Integer, cY As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
cX = CInt(Windows.Forms.Cursor.Position.X.ToString())
cY = CInt(Windows.Forms.Cursor.Position.Y.ToString())
If GetAsyncKeyState(1) <> 0 Then
If cX <= 10 Then
If GetForegroundWindow() <> 1 Then
SetWindowPos(GetForegroundWindow(), HWND_TOP, 0, 0, 100, 100, SWP_FRAMECHANGED)
End If
End If
End If
End Sub
<Runtime.InteropServices.DllImport("User32")>
Public Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ByRef lpdwProcessId As IntPtr) As IntPtr
End Function
Private Sub Button1_Click() Handles Button1.Click
Dim ID As IntPtr, hWnd As IntPtr '// hWnd By GetForegroundWindow
GetWindowThreadProcessId(hWnd, ID)
If Process.GetProcessById(ID).ProcessName.ToLower <> "Explorer".ToLower Then 'Without.exe
MessageBox.Show("Set Position")
End If
End Sub

how do I solve unbalance stack in visual basic 2012?

This is error that I got
Additional Information: A call to PInvoke function 'cobawebcamsss!cobawebcamsss.Form1::SendMessage' 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.
Here is the code :
Option Explicit On
Option Strict Off
Public Class Form1
Const WM_CAP As Short = &H400S
Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const SWP_NOMOVE As Short = &H2S
Const SWP_NOSIZE As Short = 1
Const SWP_NOZORDER As Short = &H4S
Const HWND_BOTTOM As Short = 1
Dim iDevice As Integer = 0
Dim hHwnd As Integer
' Declare Function
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
Private Sub LoadDeviceList()
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Do
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
If bReturn Then lstDevices.Items.Add(strName.Trim)
x += 1
Loop Until bReturn = False
End Sub
Private Sub OpenPreviewWindow()
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
btnSave.Enabled = True
btnStop.Enabled = True
btnStart.Enabled = False
Else
DestroyWindow(hHwnd)
btnSave.Enabled = False
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim data As IDataObject
Dim bmap As Image
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
ClosePreviewWindow()
btnSave.Enabled = False
btnStop.Enabled = False
btnStart.Enabled = True
If sfdImage.ShowDialog = DialogResult.OK Then
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
End If
End If
End Sub
Private Sub ClosePreviewWindow()
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
DestroyWindow(hHwnd)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadDeviceList()
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
OpenPreviewWindow()
btnStart.Enabled = False
btnStop.Enabled = True
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
ClosePreviewWindow()
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
End Class
and it got highlighted here when I break the operation
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
please help, I am new in visual basic so I try to learn from others program but it didn't work, it is originally from visual basic 2010 and I copied it to visual basic 2012
If you are getting an error about PInvoke..SendMessage, try replacing the line:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
With:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef RECT As IntPtr) As IntPtr
After that, worked fine on my Win7 x64.