How to map array C to C #?

My question is related to trying to call a function written in C with C #. I looked at the header file that came with the C library to understand the functions as they exist in the C dll. Here is what I see:

C code (for a function called LocGetLocations):

typedef enum {
    eLocNoError,
    eLocInvalidCriteria,
    eLocNoMatch, 
    eLocNoMoreLocations,
    eLocConnectionError, 
    eLocContextError,
    eLocMemoryError
} tLocGetStatus; 

typedef void *tLocFindCtx;

typedef void *tLocation;    

PREFIX unsigned int POSTFIX LocGetLocations
(
    tLocFindCtx pCtx, 
    tLocation *pLoc,
    unsigned int pNumLocations,
    tLocGetStatus *pStatus
);

In C #, I have this:

[DllImport(@"VertexNative\Location.dll")]
public static extern uint LocGetLocations(IntPtr findContext, out byte[] locations, uint numberLocations, out int status);

The problem is that I don’t quite know how to handle the pLoc parameter in C #. I pass it as an array of bytes, although I'm not sure if this is correct. The C library documentation says this parameter is a pointer to an array of descriptors.

How can I get an array on the C # side and access its data?

An example I gave in C looks like this:

tLocation lLocation[20];

// other stuff

LocGetLocations(lCtx, lLocation, 20, &lStatus)

Any help would be greatly appreciated!

+3
2

, :

[DllImport(@"VertexNative\Location.dll")]
public static extern uint LocGetLocations(IntPtr findContext, [Out] IntPtr[] locations, uint numberLocations, out int status);

( - ):

IntPtr[] locations = new IntPtr[20];
int status;
// findContext is gotten from another method invocation
uint result = GeoCodesNative.LocGetLocations(findContext, locations, 20, out status);

!

+1

, , , . , C, . , #, , . , , .

+2

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


All Articles