-1

If I am not correct, the codes following are used to copy an array of bytes to a position of memory in C#:

byte[] byteName = Encoding.ASCII.GetBytes("Hello There");

int positionMemory = getPosition();

Marshal.Copy(byteName, 0, new IntPtr(positionMemory), byteName.length);

How can I achieve this in native C++?

halfer
  • 19,824
  • 17
  • 99
  • 186
olidev
  • 20,058
  • 51
  • 133
  • 197
  • 2
    strcpy/strncpy. These are very basic functions that any C book will tell you. – Pubby May 15 '12 at 23:20
  • @Pubby I am not C, C++ programmer. I am a C# programmer. I need to convert the code in C# for a C++ project. thanks for pointing that to me. – olidev May 15 '12 at 23:26
  • 2
    I downvoted mostly because I get 'memcpy' as first result when googling *"C++ copy bytes"*. You also should check out `std::copy` which is more general purpose. Anyway, good luck. – Pubby May 15 '12 at 23:29
  • 2
    btw, you don't really need to use Marshal to copy byte array to another byte array. The Array class have all you need. Only use marshaling when you have no other choice. – Samy Arous May 15 '12 at 23:30
  • 1
    @olidev: There's very little reason to copy stuff into random areas of memory in C++, don't do that unless programming microcontrollers. – Mooing Duck May 15 '12 at 23:36
  • I'll join downvote feast for a 3 post reason to get the same answer in different forms – Ulterior May 16 '12 at 00:07

2 Answers2

6

use a pointer and memcpy:

void * memcpy ( void * destination, const void * source, size_t num );

Suppose you want to copy an array A of length n into an array B

memcpy (B, A, n * sizeof(char));

This is more C than C++, the string class have copy capabilities you can use.

  size_t length;
  char buffer[20];
  string str ("Test string...");
  length=str.copy(buffer,6,5);
  buffer[length]='\0';

Here's a more specific sample with a complete code:

#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;
int main()
{

    string s("Hello World");
    char buffer [255];
    void * p = buffer; // Or void * p = getPosition()
    memcpy(p,s.c_str(),s.length()+1);
    cout << s << endl;
    cout << buffer << endl;
    return 0;
}

let me know if you need more details

Samy Arous
  • 6,794
  • 13
  • 20
  • He seems to be wanting to copy a string literal `"Hello There"` to the location returned by `getPosition()`. Can you show this more clearly in your samples? – Mooing Duck May 15 '12 at 23:34
  • 1
    As you already pointed out, copying memory blocks to a random position is not really safe. Didn't want to go there, but since you asked :p – Samy Arous May 15 '12 at 23:42
1

memcpy(), memmove(), CopyMemory(), and MoveMemory() can all be used as native equivilents of Marshal.Copy(). As for the position handling, all the .NET code is doing as casting the integer to a pointer, which you can do in C++ as well. The .NET code you showed is equivilent to the following:

std::string byteName = "Hello There"; 
int positionMemory = getPosition(); 
memcpy(reinterpret_cast<void*>(positionMemory), byteName.c_str(), byteName.length()); 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770