Conversion MemoryStream to void*

Hello,

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

long TPMTECCOMAPIEXPORT __cdecl OpSndRcvCl(
    void * psndbuffer,  /* Pointer to send buffer */
    void **rcvbuffer    /* Pointer to pointer to receive/error buffer */

thanks eg.
eguillot79Asked:
Who is Participating?
 
AlexFMCommented:
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);
0
 
softplusCommented:
(void *)&mydata
:)
0
 
eguillot79Author Commented:
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 :)
0
 
eguillot79Author Commented:
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 ?
0
 
AlexFMCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.