Hi All,

I get this error operator == cannot be applied to operands of type 'System.IntPtr' and 'int'

firstly I had this code

private void SNGPH_KeyUp(object sender, KeyEventArgs e)
        {
            
        
            if (e.KeyCode != Keys.ControlKey)
                return;

            POINT curpos = new POINT();
            User32.GetCursorPos(ref curpos);
            IntPtr handle = User32.WindowFromPoint(curpos);
            handle = User32.GetAncestor(handle, 2);
            
            for (int i = 0; i < listBoxTables.Items.Count; i++)
                if (((PokerTable)listBoxTables.Items[i]).HWND == handle)
                {
                    listBoxTables.SelectedIndex = i;
                    setFormsAndDoCalc(true, null);
                    e.Handled = true;
                    return;
                }

            pictureBox1.Image = SystemIcons.Error.ToBitmap();
            labelWarning.Text = "Couldn't find selected window from pokertables";
        }

which is fine when you click control key over a table it then finds the table in the list (listboxtable) and executes it. Now I tried to make this into every foreground window to do the same thing and show handle in text bar.

Heres the code I get an error with

private void GetActiveWindow()
        {
            const int nChars = 256;
            int handle = 0;
            StringBuilder Buff = new StringBuilder(nChars);
            handle = GetForegroundWindow();

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                this.textBox1.Text = handle.ToString();

                for (int i = 0; i < listBoxTables.Items.Count; i++)
                if (((PokerTable)listBoxTables.Items[i]).HWND == handle)
                {
                    listBoxTables.SelectedIndex = i;
                    setFormsAndDoCalc(true, null);
                    return;
                }
               
            }
        }

Can anyone please help, not sure whats wrong the handle im sure is an int in both cases?

Thanks in advance!!

also used this to gain top window info (the text box displays each window handle fine btw)

[ DllImport("user32.dll") ]
		static extern int GetForegroundWindow();
			
		[ DllImport("user32.dll") ]
		static extern int GetWindowText(int hWnd, StringBuilder text, int count);

All I can say is the information in the error message tells you where the error is (sort of)...

"operator == cannot be applied to operands of type 'System.IntPtr' and 'int'"

You have defined:

IntPtr handle = User32.WindowFromPoint(curpos);
handle = User32.GetAncestor(handle, 2);
 
for (int i = 0; i < listBoxTables.Items.Count; i++)
if (((PokerTable)listBoxTables.Items[i]).HWND == handle)

In your first segment which I'm assuming worked correctly as you say you started getting errors after, when you add this:

int handle = 0;
            StringBuilder Buff = new StringBuilder(nChars);
            handle = GetForegroundWindow();

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                this.textBox1.Text = handle.ToString();

                for (int i = 0; i < listBoxTables.Items.Count; i++)
                if (((PokerTable)listBoxTables.Items[i]).HWND == handle)

It almost feels like I'm back watching Sesame Street :twisted: "One of these things is not like the others..."

Sorry had to say it... In your first example you defined handle as "IntPtr handle" and in your second example you defined handle as "int handle = 0;"... Your error says you can't use == to compare IntPtr and Int and that 2nd definition of handle is why.

Hope that helps :) Please remember to mark solved once your issue has been resolved.

This article has been dead for over six months. Start a new discussion instead.