473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to pass binary data from C# stream to fcn in Managed C++

I have a byte[] of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not usable in C#) whose WriteSample function takes a byte* parameter

Do I need to marshal data? I thought I did, so I ran into alot of issues with converting the byte[] to a string and then, in the MC++ DLL, marshalling that to an IntPtr via StringToHGlobalAnsi, then casting its ToPointer() to a char*, and finally doing a memcpy of the data from char* to the byte* buffer. I guess I was trying to go from a byte[] to a byte*

If someone can point me in the right direction please, or even be so kind as to explain what I need to do? I've been spending so much time researching these newsgroups but I'm just learning bits and pieces here and there. Thanks in advance!!!
Nov 17 '05 #1
1 3830
> I have a byte[] of binary data received from a NetworkStream (C# code)
that I need to pass to the IWMWriter object in a DLL written in Managed
extensions for C++ (since the Windows Media SDK is not usable in C#) whose
WriteSample function takes a byte* parameter.

Do I need to marshal data? I thought I did, so I ran into alot of issues with converting the byte[] to a string and then, in the MC++ DLL,
marshalling that to an IntPtr via StringToHGlobalAnsi, then casting its
ToPointer() to a char*, and finally doing a memcpy of the data from char* to
the byte* buffer. I guess I was trying to go from a byte[] to a byte*.
If someone can point me in the right direction please, or even be so kind

as to explain what I need to do? I've been spending so much time
researching these newsgroups but I'm just learning bits and pieces here and
there. Thanks in advance!!!
Pin the array. That would be the easies't way around it:

Byte buffer __gc[] = yourBufferFromC#Code;
Byte __pin* umBuffer = &buffer[0];

// now pass umBuffer to your unmananged function.

Now, you shouldn't normally do this if you expect the function you're
calling to keep the array for later use or anything, because as soon as the
__pin pointer goes out of scope, the memory will be released, and the next
GC cycle might just move the memory around, causing eventually an access
violation.

If this is your case, then I suggest using Marshal::Copy() to create a copy
of the array into an unmanaged memory buffer.
--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and...
1
by: wbaccay | last post by:
I have a byte of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not...
2
by: Peter Steele | last post by:
I have a managed C++ project that contains a simple structure: struct { <multiple members> } mydata; I want to write the contents of this structure out to disk retaining the exactly binary...
2
by: Feng | last post by:
Hi, I need a VB.Net function that reads in a stream of binary data coming in from a legacy data source. The data are actually hex numbers in binary format but the problem is that I don't know...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
4
by: dirtside | last post by:
I've search far and wide for an answer, so forgive me if this is a stupid question. (Well, it probably is.) Consider the following JavaScript: var foo = new Image(); foo.src =...
2
by: Michael Kolias | last post by:
Hello everyone and happy holidays! I have a situation where I want to parse on the fly a stream of binary data. I am developing an httpmodule for asp.net for file uploading. I do not whant to...
0
by: Gargi K | last post by:
I want to create a new xml document of the type Msxml.DOMDocument.4.0 Actaully In have to transfer some binary data in xml which requires to specify the dataType member of the xml node( which is...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
20
by: tomPee | last post by:
Hi, I've bumbed into a slight problem now, and I just don't seem to know how to fix it. What I want to do is the following: Send over a socket: 1. Number of files to be send (not as an integer,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.