Have trouble using GetPixel with VISUAL BASIC 2019

Jack Hu 1 Reputation point

source code:

Dim b_Found As Boolean
Dim myHwnd As Long
Dim myHdc As Long
Dim myColor As Color

myHwnd = FindWindow("Qt5154QWindowOwnDCIcon", "BlueStacks 0" )
If myHwnd Then
myHdc = GetWindowDC(myHwnd)
myColor = GetPixel(myHdc, 150, 300)
End If

Error code:
Message=Cannot marshal 'return value': Invalid managed/unmanaged type combination.
at vbtest.Module1.GetPixel(Int64 hdc, Int32 x, Int32 y)
at vbtest.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\MrJac\source\repos\vbtest\vbtest\Form1.vb:line 25
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

Can someone pls help me?

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,086 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 94,511 Reputation points

    Try some modifications:

    Declare Function GetPixel Lib "gdi32" (
        ByVal hdc As Long,
        ByVal x As Integer,
        ByVal y As Integer) As Integer
    . . .
    Dim c As Integer
    Dim myColor As Color
    c = GetPixel(myHdc, 150, 300)
    myColor = Color.FromArgb(c)

    (By the way, to make it work in both of 32- and 64-bit modes, I think that you should use IntPtr instead of Long).

    0 comments No comments