[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

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.
0
eguillot79
Asked:
eguillot79
  • 2
  • 2
1 Solution
 
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
 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now