Conversion MemoryStream to void*

Posted on 2005-05-09
Last Modified: 2006-11-18

i need to call a function in a dll writing in c++ the parameters are : function(void* P1, void** P2)

i have prepared my data into a memorystream and now i want to convert into void*

what is the way to realise that ?

Declaration of the function in the c++ program

    void * psndbuffer,  /* Pointer to send buffer */
    void **rcvbuffer    /* Pointer to pointer to receive/error buffer */

thanks eg.
Question by:eguillot79
    LVL 13

    Expert Comment

    (void *)&mydata

    Author Comment

    thanks, but what is the type of mydata

    in my code mydata is my memorystream,

    if i want to access data inside i must use read methode with a buffer array.

    my problem is to convert the data into the memorystream to the (void*) type.

    sorry my english is not very good :)
    LVL 48

    Accepted Solution

    Use MemoryStream.GetBuffer Method to get byte[] array.
    Create unmanaged memory block using Marshal.AllocCoTaskMem Method. Copy information from  byte[] array to unmanaged memory block using Marshal function:
    public static void Copy(byte[] source, int startIndex, IntPtr destination, int length);
    Pass pointer to unmanaged block (IntPtr) to unmanaged function.

    C# function parameters must be:

    function(IntPtr P1, ref IntPtr P2)

    To read data returned by unmanaged function, make opposite operation: copy data from unmanaged memory block to managed byte[] array using Marshal function:
    public static void Copy(IntPtr source, byte[] destination, int startIndex, int length);

    Author Comment

    thanks AlexFm,

    to read data returner :
    how to know the lenght of the data returned in the IntPtr P2 to use the copy method of marshal or for declare the array byte ?
    LVL 48

    Expert Comment

    Read documentation about OpSndRcvCl function - number of bytes may be returned in the function return value, or written in the beginning of rcvbuffer, or be hard-coded, this is defined by function interface.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 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

    10 Experts available now in Live!

    Get 1:1 Help Now