Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy BYTE* to CByteArray

1,204 views
Skip to first unread message

Peter Schmitz

unread,
Oct 7, 2007, 11:06:02 AM10/7/07
to
Hi,

how can I copy an allocated buffer of type BYTE* (e.g. BYTE* buffer = new
BYTE[200];) into a variable of type CByteArray? I could not find a solution
in the samples provided...

Best wishes,

Peter

Scott McPhillips [MVP]

unread,
Oct 7, 2007, 11:24:03 AM10/7/07
to
"Peter Schmitz" <PeterS...@discussions.microsoft.com> wrote in message
news:7C7199FF-6117-4F03...@microsoft.com...


CByteArray barray;
barray.SetSize(200 );
CopyMemory(barray.GetData(), buffer, 200);

--
Scott McPhillips [VC++ MVP]

Norbert Unterberg

unread,
Oct 7, 2007, 11:42:04 AM10/7/07
to

Peter Schmitz schrieb:

> how can I copy an allocated buffer of type BYTE* (e.g. BYTE* buffer = new
> BYTE[200];) into a variable of type CByteArray? I could not find a solution
> in the samples provided...

I don't find the method, eiher.
But why do you want to use the ancient CByteArray class? Use a modern
std::vector<> instead:

#include <vector>
std::vector<BYTE> v(buffer, buffer+200);

Or if you define v somewhere else and need to copy the bytes:

#include <vector>
std::vector<BYTE> v;
v.assign(buffer, buffer+200);

Norbert

Joseph M. Newcomer

unread,
Oct 7, 2007, 12:58:25 PM10/7/07
to
You should not be copying it at all. If you have a CByteArray, it doesn't need to exist.
If it needs to exist and has a fixed size, you don't need to use new to get it.

CByteArray b;
b.SetSize(200);

now put your values in b[i].

If you have the data coming from some outside source (which again, it is very unlikely you
would need to use new to get it!) you might have

void CopyToByteArray(CByteArray & b, LPBYTE data, UINT len)
{
b.SetSize(len);
memcpy(b.GetData(), len, data);
}

but the fact that you said new BYTE[200] is very suspect. There is virtually no need to
use new at all. Please show the code and why you thinhk you need to use new to allocate a
fixed size array, or why you even think you need an array at all.
joe

On Sun, 7 Oct 2007 08:06:02 -0700, Peter Schmitz <PeterS...@discussions.microsoft.com>
wrote:

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages