O
O
Oleg2014-12-24 02:49:57
Command line
Oleg, 2014-12-24 02:49:57

How to convert cli::array ^ to byte *??

Help me please. I've been scratching my head for two days now how to convert.
The essence of the program, as I understand it: you need to read a piece of a file, 65536 bytes in size.
And send it to the C memmove command. Which does a simple thing...move bytes.
And everything would be fine, but I am writing a program in visual c ++ 2013 in which there are wonderful tools like cli. Stubbornly there is a type mismatch error...here is the code:
This sets the variables and starts reading the file. The file is read correctly and well, but it is read into an array, but not into an array ... look at the following code ... it's clear where to go.

FileStream^ fs;
  try
  {
    fs = File::OpenRead(File);
  }
  catch (Exception^ ex)
.....
const int BUFFER_SIZE = 65536;
  char *szBuffer = new char[BUFFER_SIZE];
  unsigned char buf[BUFFER_SIZE];
  array<unsigned char>^ b = gcnew array<unsigned char>(BUFFER_SIZE);
  UTF8Encoding^ temp = gcnew UTF8Encoding(true);
  __int64 iPrevReaded = 0;
  __int64 Start = TimerInit();
  String^ g;
  int cbReaded = 0;


  // чтение из файла и вычисление TTH
  while ((cbReaded = fs->Read(b, 0, BUFFER_SIZE)) > 0)
  {
    g=temp->GetString(b);
    //cli::pin_ptr<System::Byte> p = g;
    tt_update(&ctx, b, cbReaded);

as you can see, this procedure processes the data coming from the file. And the type is a variable here byte *buffer.
Error text:
"2 IntelliSense: argument of type "cli::array ^" is incompatible with parameter of type "byte *" c:\Users\...\MyForm.cpp 112
"
void tt_update(TT_CONTEXT *ctx, byte *buffer, word32 len)
{

  if (ctx->index)
  { /* Try to fill partial block */
    unsigned left = BLOCKSIZE - ctx->index;
    if (len < left)
    {
    memmove(ctx->block + ctx->index, buffer, len);
    ctx->index += len;

Help please... I don't have the strength to google anymore...
Here's what I have... stackoverflow.com/questions/25600940/managed-c-cli...
but only the first 1 byte is converted this way... and you need 65535.
Here is the project https://github.com/master255/SimplyServer

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Grigoriev, 2014-12-24
@Master255

Use pin_ptr

array<Byte>^ bytes = File::ReadAllBytes( fileName );
pin_ptr<unsigned char> pptr = &bytes[0];
const int BUFFER_SIZE = 65536;
unsigned char buf [ BUFFER_SIZE ];
memcpy_s( buf, sizeof(buf), pptr, bytes->Length );

A
Armenian Radio, 2014-12-24
@gbg

If you've messed up C++, use std::move.
You're trying to shoot yourself in the foot.
When the garbage collector is actively used in one code, and they try to stick a low-level function from C, because they heard somewhere that it is fast, while the size is kept in an integer variable, I feel sad.
The proper textbook for writing TCP/IP applications is called "Using TCP/IP Effectively" by J. Sneijder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question