?
Solved

Calling c++ method from .net

Posted on 2008-11-04
2
Medium Priority
?
648 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:sidharthrshah
2 Comments
 
LVL 3

Accepted Solution

by:
maliger earned 750 total points
ID: 22877663
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.
0
 

Author Closing Comment

by:sidharthrshah
ID: 31513020
I directly passed the arguments without using marshal as method
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question