none
passing byte[] array from C# to c++ unsigned char*buf com function parameter

    Question

  • i have a function in c++ com that gets an unsigned char *buffer parameter.
    when i use the dll in c# this function expects 'ref byte' parameter.

    how do i pass a byte[] buffer as a parameter to the c++ function and receive it as an unsigned char *buffer in the c++ function ?

    * already tried IntPTR , the compiler write that it cant convert system.Intptr to ref byte.


    unsafe
    {
    fixed (byte* p = buf)
    {
    IntPtr MyIntPtr = (IntPtr)p;

    i.WriteBuffer(MyIntPtr, buf.Length);
    }
    }
    Wednesday, June 06, 2007 11:55 AM

Answers

All replies

  • You might want to check out the Marshal class and its StringTo* methods.
    Friday, June 08, 2007 12:21 AM
    Moderator
  • Thanks! but i eventually i found  a much simplier answer to my problem :

     

    i just send the first byte!!!

     

    WriteBuffer(ref buf[0], buf.Length)

    Sunday, June 10, 2007 7:36 AM
  • Strange. I did same thing but got an error:
    Argument '1': cannot convert from 'ref byte' to 'byte*'

    Did I miss something?

    Saturday, June 16, 2007 12:03 PM
  • You will need to use the fixed keyword to be able to convert it to byte *. See the code in the first post.
    Saturday, June 16, 2007 9:48 PM
    Moderator
  • Thanks :-)
    I'm also wondering about another thing. Both my projects - C# and C++ - are for .net. I guessed the interoperation between two managed projects should be more simple. At least .net types should be understood by any language. Am I wrong?
    Saturday, June 16, 2007 11:25 PM
  •  Ralfeus wrote:
    Thanks :-)
    I'm also wondering about another thing. Both my projects - C# and C++ - are for .net. I guessed the interoperation between two managed projects should be more simple. At least .net types should be understood by any language. Am I wrong?

     

    You are not wrong. If you make a managed c++/c#/vb.net class library you can reference it and use it from other languages.

    Wednesday, June 27, 2007 12:19 AM
    Moderator
  •  

    I am using the same kind of functions. I am able to compile successfully, but at runtime there's "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt". Did you encounter same problem?

     

    Here are C++/C# function:

    C++: Scan(char* List);

    C# equivalent: Scan(ref byte List);

    Monday, September 10, 2007 9:00 PM