Jordan Daigle
Jordan Daigle

Reputation: 455

Calling a C++ function with void* parameter in a C# app

I have a c++ function that look like this :

int Compression::DecompressPacket(const void* inData, int inLength, void* outData, int outLength)
{
    int headerLength = ComputeDataHeaderLength(inData);
    return DecompressDataContent(static_cast<const unsigned char*>(inData) + headerLength, inLength - headerLength, outData, outLength);
}

The fonction is inside a class which is inside a c++ library.

In the other hand, I need to call this fonction on my c# application. The fonction ask me to enter parameters of types : "void*, int, void*, int".

When I try to make a void* in an unsafe function,

unsafe private void lstbox_packets_SelectedIndexChanged(object sender, EventArgs e)
{
   [...]
   byte[] value = byteValuePackets[lstbox_packets.SelectedIndices[0]];    
   void* pValue = &value;
   [...]
}

I get the error :

Error 8 Cannot take the address of, get the size of, or declare a pointer to a managed type ('byte[]')

I'm not very familiar with c++ and pointer but how i am suppose to pass a void* type in c# ?

Upvotes: 2

Views: 852

Answers (1)

Anton Savin
Anton Savin

Reputation: 41301

You shouldn't take the address of value, and also you have to use fixed statement:

fixed (void* pValue = value)
{
    //...
}

Upvotes: 2

Related Questions