-
December 7th, 2016, 04:23 AM
#1
'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
-
December 7th, 2016, 11:02 PM
#2
Re: 'int' and 'long' in C# (compared to Microsoft C)
-
December 8th, 2016, 04:37 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|