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.