Have trouble using GetPixel with VISUAL BASIC 2019

Jack Hu 1 Reputation point
2021-09-26T19:13:14.827+00:00

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:
System.Runtime.InteropServices.MarshalDirectiveException
HResult=0x80131535
Message=Cannot marshal 'return value': Invalid managed/unmanaged type combination.
Source=vbtest
StackTrace:
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?

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

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-09-26T20:39:17.143+00:00

    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