title: byte[] to byte* in C#
body: <p>I created 2 programs - in C# and C++, both invoke native methods from <strong>C</strong> dll. C++ works fine, because there are the same data types, C# doesn't work. </p>
  
  <p>And native function parameter is <code>unsigned char*</code>. I tried <code>byte[]</code> in C#, it didn't work, then I tried:</p>
  
  <pre><code>fixed(byte* ptr = byte_array) {
    native_function(ptr, (uint)byte_array.Length);
  }
  </code></pre>
  
  <p>It also doesn't work. Is it correct to convert byte array to <code>byte*</code> in such way? Is it correct to use byte in C# as <code>unsigned char</code> in <strong>C</strong>?</p>
  
  <p>EDIT:
  This stuff returns the wrong result:</p>
  
  <pre><code>byte[] byte_array = Encoding.UTF8.GetBytes(source_string);
  nativeMethod(byte_array, (uint)byte_array.Length);
  </code></pre>
  
  <p>This stuff also returns the wrong result:</p>
  
  <pre><code> byte* ptr;
   ptr = (byte*)Marshal.AllocHGlobal((int)byte_array.Length);
   Marshal.Copy(byte_array, 0, (IntPtr)ptr, byte_array.Length);
  </code></pre>