tags:

views:

68

answers:

2

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?

+1  A: 

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
+3  A: 

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
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