CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    'int' and 'long' in C# (compared to Microsoft C)

    I'm very new to C# and I needed to find a way to add WS_EX_NOACTIVATE to a window. I'm building as 32-bit code and after asking here (and doing a bit of research) it looked like I needed this in my program:-

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    
    // In the relevant function
        const int  GWL_EXSTYLE = (-20);
        const long WS_EX_NOACTIVATE = 0x08000000;
    
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        IntPtr hWnd = (IntPtr)FindWindow(null, "MyWindow");
        int style = GetWindowLong(hWnd, GWL_EXSTYLE);
        style |= WS_EX_NOACTIVATE;
        SetWindowLong(hWnd, GWL_EXSTYLE, style);
    I added all the above and sure enough, it seems to work. However...

    According to the function signatures for GetWindowLong() and SetWindowLong() my natural instinct would have been to do this:-

    Code:
        [DllImport("user32.dll")]
        static extern long GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        static extern long SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        IntPtr hWnd = (IntPtr)FindWindow(null, "MyWindow");
        long style = GetWindowLong(hWnd, GWL_EXSTYLE);
        style |= WS_EX_NOACTIVATE;
        SetWindowLong(hWnd, GWL_EXSTYLE, style);
    (i.e. to use long in a couple of places, rather than int). The above seems to compile and link okay but it crashes at runtime, with a stack corruption. Is that because long in C# is different from 'C'? IIRC they're both 32-bits in MSVC.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: 'int' and 'long' in C# (compared to Microsoft C)

    In C#, try..
    Code:
    var sizeInt = sizeof(int);
    var sizeLong = sizeof(long);
    Do the same in C++.

    https://msdn.microsoft.com/en-us/library/eahchzkf.aspx
    http://www.hanselman.com/blog/BackTo...orkAndCLR.aspx

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: 'int' and 'long' in C# (compared to Microsoft C)

    Thanks Arjay, that clarified the situation.

    In MSVC a long is 4 bytes whereas in C# it's 8 bytes.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured