Calling c++ method from .net

sidharthrshah
sidharthrshah used Ask the Experts™
on
I have a decompression algorithm in C++. I want to call the decompression method from .net C# program.
I have successfully created a dummy method in c++, created a dll from that code and added to the c# project file and successfully called that code.
However with this method I am not able to identify what parameters to pass and how.

The c++ decompress method definition is as follows:
--------------------------------------------------------------------------
lzo1z_decompress        ( const lzo_byte *src, short  src_len,
                                lzo_byte *dst, unsigned int dst_len,
                                lzo_voidp wrkmem /* NOT USED */ );
----------------------------------------------------------------------------
where
---------------------------------------------------------------------------
#define lzo_byte                unsigned char __LZO_MMODEL
#define lzo_voidp               void __LZO_MMODEL *
----------------------------------------------------------------------------
and the method call is as follows:
rCode = lzo1z_decompress(BcastCmpPacket.cCompData , cLen ,      rbuff , &oLen , &eCode);

On C# side:
I have a byte[] of compressed data to be passed on to the c++ code
and if possible the returning data should also be in the form of  byte[]

I require your help on the code to be written on both the c# side and c++ side.

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hi,

the dst array is allocated on which side? caller (c#) or calee (c++) ?
From the signature I'd suppose caller (not sure how it can know how large array it should allocate though).

c++:
lzo1z_decompress        ( const lzo_byte *src, short  src_len,
                                lzo_byte *dst, unsigned int dst_len,
                                lzo_voidp wrkmem /* NOT USED */ );

C#:
void lzo1z_decompress (    
  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] byte[] src, Int16 src_len,
  [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] byte[] dst, Int16 dst_len,
  IntPtr not_used);

If you will call only c#->c++, the SizeParamIndex should not be even needed. You have to pass the length in _len arguments yourself.

Author

Commented:
I directly passed the arguments without using marshal as method

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial