In C#, when messing with that system DLLImport/(unmanaged?) code stuff, I read somewhere it's important to use Int32 exact type instead of int. Is this true? And can someone please elaborate on why it's important to do this?
Asked
Active
Viewed 417 times
2
-
2Do you happen to remember where you read it? It would be interesting if you could post the link. – Mark Byers May 17 '10 at 23:42
-
Related: [62503](http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care) – hemp Jun 03 '10 at 06:50
2 Answers
6
I think it is more likely that you read about using IntPtr
instead of int
. As others have said, int
and Int32
are equivalent.
It is not really a problem to interchange int
and IntPtr
on a 32-bit system as they are the same size (4 bytes). The problem comes when on a 64-bit system - if you use int
instead of IntPtr
, it now has the wrong size (4 bytes instead of 8 bytes) and can cause errors.

adrianbanks
- 81,306
- 22
- 176
- 206
-
Using IntPtr incorrectly can be a problem for the same reason. If the type you are marshaling is an integer, you should use Int32 (or its alias.) If the type you are marshaling is a pointer, you should use IntPtr. Using IntPtr to marshal an integer is going to create problems since, on 64-bit Windows, an integer is still 32 bits (4 bytes). Similarly, using integer to marshal a pointer will create problems. Always use the correct managed type for the native type being marshaled and vice versa. – hemp May 18 '10 at 00:30
1
I don't believe that this is true. int
is an alias for Int32
. They mean the exact same thing and will be compiled to the same IL.
A list of aliases can be found here.

Mark Byers
- 811,555
- 193
- 1,581
- 1,452