C ++ int & long marshalling in c #

I am currently porting a proprietary dll (API) to C # and I have some problems sorting some of the data types used.

For example, an API header file defines the following types:

typedef unsigned int  tINT;  /* natural unsigned */
typedef signed int    tINTs; /* natural signed */
typedef unsigned int  tLONG; /* 32 bit unsigned */
typedef unsigned long tPTR;  /* 32/64-bit pointer */

Then, for example, I have the following function definition:

tINTs SomeApiFunction ( const tPTR hdl, tINT param );

I usually sort intdirectly using C # int( Int32) or an unsigned version. API tLONGand tINTare the same (at least for the API). But I'm not sure with tPTR. I have now done this uintin C # because it is a 32-bit int, but I'm not sure how it longwill behave on a 64-bit machine.

What do I need to do to make sure that the API will work correctly on both 32-bit and 64-bit machines?

Here is what I would do now:

[DllImport( "api.dll" )]
public static extern int SomeApiFunction ( uint hdl, uint param );

, API , , () typedefs. , , .

+3
4

IntPtr. IntPtr 4 32- 8 64- .Net.

+3

C/++. IntPtr. MSVC, 64- , 32 . .

+3

.

IntPtr #.

+2

int uint, - API .

However, for pointers, it is better to use IntPtron the C # void*side and on the C / C ++ side.

+1
source

Source: https://habr.com/ru/post/1782348/


All Articles