473,386 Members | 1,763 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,386 software developers and data experts.

[VC 2k5] Q:Copying From unmanaged pointer into managed arrary, How?

Hi there,

Got a quick question, I have an app that I'm compiling with /clr and I have
a native function that passes in a pointer, and I need to copy from that
unmanaged memory into a managed Byte array but I can't quite figure out how
to properly cast everything, here's what I have right now.
static void CopyData(const void* buf, int size)
{
System::IntPtr^ ptrBuf=gcnew System::IntPtr((int)buf);
array<System::Byte>^ MyBuf=gcnew array<Byte>(size);
System::Runtime::InteropServices::Marshal::Copy(pt rBuf,(array<unsigned
char>^)MyBuf,0,size);
}

I'm getting an error that's saying none of the 16 overloads could convert
all the argument types.

So I'm guessing I am most likely not casting the destination buffer to a
correct type? I've already tried using Byte for my array but alas my weak
C++ skills just ain't helping.. :P Can anyone tell me where I'm going wrong?
Nov 23 '05 #1
3 1650
JAL
Marauderz.. How about:

Marshal::Copy(static_cast<IntPtr>(ptrBuf),MyBuf,0, size);

Jeff

"Marauderz" wrote:
Hi there,

Got a quick question, I have an app that I'm compiling with /clr and I have
a native function that passes in a pointer, and I need to copy from that
unmanaged memory into a managed Byte array but I can't quite figure out how
to properly cast everything, here's what I have right now.
static void CopyData(const void* buf, int size)
{
System::IntPtr^ ptrBuf=gcnew System::IntPtr((int)buf);
array<System::Byte>^ MyBuf=gcnew array<Byte>(size);
System::Runtime::InteropServices::Marshal::Copy(pt rBuf,(array<unsigned
char>^)MyBuf,0,size);
}

I'm getting an error that's saying none of the 16 overloads could convert
all the argument types.

So I'm guessing I am most likely not casting the destination buffer to a
correct type? I've already tried using Byte for my array but alas my weak
C++ skills just ain't helping.. :P Can anyone tell me where I'm going wrong?

Nov 23 '05 #2
JAL
Better yet, just create ptrBuf as a value:

System::IntPtr ipBuf((int)buf);

since IntPtr is a value type.

"JAL" wrote:
Marauderz.. How about:

Marshal::Copy(static_cast<IntPtr>(ptrBuf),MyBuf,0, size);

Jeff

Nov 23 '05 #3
Hey, that worked. Thanks!
"JAL" <JA*@discussions.microsoft.com> wrote in message
news:66**********************************@microsof t.com...
Better yet, just create ptrBuf as a value:

System::IntPtr ipBuf((int)buf);

since IntPtr is a value type.

"JAL" wrote:
Marauderz.. How about:

Marshal::Copy(static_cast<IntPtr>(ptrBuf),MyBuf,0, size);

Jeff

Nov 23 '05 #4

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

Similar topics

7
by: Lev | last post by:
Hi, I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
2
by: The unProfessional | last post by:
In my current project (my first project using vc w/ managed extensions), I'm directly #using <mscorlib.dll>, so it's necessary for me to use the __nogc and __gc constructs when defining classes or...
4
by: William F. Kinsley | last post by:
My understanding is that when I re-compile a existing MFC application with the /clr switch, that the code generated is managed(with some exceptions) but that the data isn't, i.e. not garbage...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.