473,756 Members | 6,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 StringToHGlobal Ansi, 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 16 '05 #1
1 2427
> 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 StringToHGlobal Ansi, 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 16 '05 #2

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

Similar topics

0
2477
by: Channing Jones | last post by:
Hello everyone, I am trying to store data in a binary field of an SQL-Server table using ADODB. So far, I have managed to store a record but not any data in the binary field. I only get exceptions on various part. I have tried both AppendChunk and using a stream object. Unfortunately all the examples I can find in the Net and on the Microsoft site
103
48741
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
28
2801
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
8
15613
by: TJ | last post by:
I need to be able to pass a pointer to a (managed code) Stream object to a COM method so it can serialize its data into the stream. I have a C# application that makes uses of Randolf Duke's MimeSniffer COM component (http://www.codeproject.com/internet/mimesniffer.asp). My application (pop3ImageGrabber) uses MimeSniffer to identify each image attachment in emails that have been collected from a POP3 mailbox. MimeSniffer gives the...
1
3846
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 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...
6
2728
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, every now and then the client would timeout and have to re-initiate the request... TIA!
2
1768
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 use the built-in control because it's inadequate. In theory I would use a que and feed data as it comes in and then have a parser that extracts the data from the que, parse them and do whatever.
15
2411
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with two int32's as its data members, the binary file of that class must have a size of 8 bytes (i.e. only contains class data members, not methods etc.). Is serialization the answer to the above problem? If I understand correctly, the reason that...
12
16164
by: Registered User | last post by:
I've read in a book: <quote> With a binary-mode stream, you can't detect the end-of-file by looking for EOF, because a byte of data from a binary stream could have that value, which would result in premature end of input. Instead, you can use the library function feof(), which can be used for both binary- and text-mode files: int feof(FILE *fp);
0
9212
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9973
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9779
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2612
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.