Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# implementing system-dependent integer

Tags:

c#

integer

I need to define a system-dependent integer type, in order to be compatible with some low-level librarys. I've setup a x86 and x64 Project-Configuration, and defined conditional compilation symbols for them (IA32 and INTEL64).

So, I would like to do the following:

#if IA32
    typedef int SysInt;
    typedef uint SysUInt;
#elif INTEL64
    typedef long SysInt;
    typedef ulong SysUInt;
#endif

However, that doesn't work due to typedef is not available in C#. What's the best option to implement this?

Thanks in advance. Best regards.

like image 441
0xbadf00d Avatar asked Dec 04 '22 09:12

0xbadf00d


1 Answers

You want IntPtr and UIntPtr, which are 32 bits in 32-bit processes and 64 bits in 64-bit processes. Since these types automatically take the process's "bitness", there's no need for conditional compilation or two different projects configurations in order to use them.

If you actually want to do math on the values, though, you should cast them to long or ulong to do the math and then back to IntPtr or UIntPtr to pass them to your external libraries.

like image 177
Gabe Avatar answered Dec 21 '22 02:12

Gabe