Calling c++ method from .net

Posted on 2008-11-04
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 */ );
#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.
Question by:sidharthrshah
    LVL 3

    Accepted Solution


    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).

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

    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 Closing Comment

    I directly passed the arguments without using marshal as method

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now