question

JackHu-7973 avatar image
0 Votes"
JackHu-7973 asked Viorel-1 answered

Have trouble using GetPixel with VISUAL BASIC 2019

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?

dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe the definition of GetPixel function is incorrect?


0 Votes 0 ·
 Module Module1
     Declare Function GetPixel Lib "gdi32" (
         ByVal hdc As Long,
         ByVal x As Integer,
         ByVal y As Integer) As Color
    
     Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
     Declare Function FindWindow Lib "user32" Alias "FindWindowA" (
         ByVal lpClassName As String,
         ByVal lpWindowName As String) As Long
 End Module


0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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).

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.