I found that it takes one of three values:
A. -10: to retrieve input device handle
B. -11: to retrieve output device handle
C. -12: to retrieve error device handle
Although, DWORD is "unsigned 32-bit integer" and it is marshaled System.UInt32, System.UInt32 does not support negative values, therefore, it cannot take any value of the three. So, I have marshaled it as System.Int32 instead.
Now, the question is:
Do I have a misconception about DWORD? And why we marshal DWORD to System.UInt32 and GetStdHandle() requires negative values?
To be CLS compliant you can't use unsigned types (except for byte).
The correct CLS compliant way of marshalling DWORD is to int.
When marshaling, the size of the type is important : not what the resultant outcome is with integer types. Because int & uint are both the same size (4 byes) you don't have a problem.
With regards to negative values, look up how negative values are held in memory on google (e.g. here).
-1 = 0xFFFFFFFF for instance.
Marshaling to int is ok for DWORD unless dealing with values > 0x80000000. The top bit represents of the 32-bits in a DWORD signifies a negative value.
If the value is > 0x80000000 and you need it not to be negative covert it to a long which is 64-bits by doing the following :
Code:
static long IntToLong(int value)
{
return ((long)value) & 0xFFFFFFFFL;
}
Darwen.
Last edited by darwen; April 3rd, 2009 at 02:31 PM.
www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.
I am very thankful Darwen. The code segment was very brilliant.
But I know that the size reserved for the variable is more important, and System.UInt32 is non-CLS-compliant. But the question here, why GetStdHandle() requires DWORD? I think it would be OK if it requires INT instead!
Bookmarks