error c# : operator == cannot be applied to operands of type 'System.IntPtr' and 'int

This is a discussion on error c# : operator == cannot be applied to operands of type 'System.IntPtr' and 'int within the C# Programming forums, part of the General Programming Boards category; Hi All, I get this error operator == cannot be applied to operands of type 'System.IntPtr' and 'int' firstly I ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    6

    error c# : operator == cannot be applied to operands of type 'System.IntPtr' and 'int

    Hi All,

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

    firstly I had this code

    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

    Code:
     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;
                    }
                   
                }
            }
    also used this to gain top window info (the text box displays each window handle fine btw)

    Code:
             [ DllImport("user32.dll") ]
    
          static extern int GetForegroundWindow();
    
          [ DllImport("user32.dll") ]
    
          static extern int GetWindowText(int hWnd, StringBuilder text, int count);
    Can anyone please help, not sure whats wrong the handle im sure is an int in both cases?

    Thanks in advance!!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    This is C forum. And it's C ONLY.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    That is c#

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    oops I see can a mod please move

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Moved as requested.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Try:
    Code:
    if (((PokerTable)listBoxTables.Items[i]).HWND.ToInt32() == handle)
    Assuming HWND is an intptr

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Or simply cast

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    Or declare GetForegroundWindow properly then it'll actually, you know, work. PInvoke.net has done the donkey work of converting the winapi signatures to their C# equivalents, no need to do it yourself.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    thanks valaris that worked an absolute treat!!

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by mxadam View Post
    thanks valaris that worked an absolute treat!!
    No problem, but I didn't pay attention to your pinvoke signatures. adeyblue is right, you can have a better solution by just properly declaring your signature and let the runtime marshal.

    Maybe a better question would be why are you even calling functions like SetForegroundWindow?
    Last edited by valaris; 07-14-2010 at 06:10 PM.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Maybe a better question would be why are you even calling functions like SetForegroundWindow?
    There are many occasions where .NET does not offer some specific functionality found in the Win32 API. When this occurs you have no choice except to use P/Invoke or wrap some of the Win32 API calls in managed C++ and call through to the the actual API.

  12. #12
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Bubba View Post
    There are many occasions where .NET does not offer some specific functionality found in the Win32 API. When this occurs you have no choice except to use P/Invoke or wrap some of the Win32 API calls in managed C++ and call through to the the actual API.
    Sure, I was just saying he should investigate rather or not this is really one of those cases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 08:09 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21