Interface from C DLL to .NET

I have an outdated DLL written in C that I would like to call from a C # .NET application. The problem is that the DLL interface for the C DLL is quite complex. This is something like this:

__declspec(dllexport) void __stdcall ProcessChunk(
    void *p_prochdl,
    const BIG_INPUT_STRC *p_inparams,
    BIG_OUTPUT_STRC *p_outparams
);

BIG_INPUT_STRC / BIG_OUTPUT_STRC contains all kinds of things ... pointers to buffer arrays, enumerated parameters, integer parameters, etc. In short, they are complex.

First question: is there an easy way to get all the structure information contained in the DLL header file into the C # class, or do you need to literally copy and paste everything into C # and override it? That seems redundant.

In this regard, what is the correct way to pass structures to an unmanaged DLL from C #?

Finally, there is an example of how to properly transfer buffer arrays from C # to an unmanaged DLL? Alternatively, how can I pass a two-dimensional array to a DLL?

Thanks Greg

+3
source share
3 answers

Its a pretty straightforward way to do this soft phenomenon in C # using P / Invoke.

I believe that you will have to define data structures in C # manually.

I would advise you to look at this MSDN article on the subject

+3
source

You will need to make extensive use of .NET marshalling . First you need to redefine the C structures in the C # code, and then make sure everything is configured correctly using the MarshalAs attribute .

# C, Marshal.StructToPtr.

, , byte [], :

byte[] buffer = ...;
fixed(byte *pBuffer = buffer)
{
   // do something with the pBuffer
}

, , "" "".

, C, , , , :

someValue = buffer[(elementsPerDimensions * x) + y];

, , COM ?

+2

.

  • COM . , .

  • I would build my own second library with functions designed for P / INVOKED to create entries for the first and call.

Alternatively, you can create your own / managed C ++ library to handle marshaling.

0
source

All Articles