0

I need to pass a byte array into a C++ com object from C#. C++ will then fill the buffer for C# to read.

c++ function definition

STDMETHODIMP CSampleGrabber::GetBuffer(byte* bd)
{
    int p=0;
    while (p< nBufSize) {
        bd[p]=pLocalBuf[p];
        p++;
    }

c# code :

byte[] byTemp = new byte[nBufSize];      
igb.GetBuffer(ref byTemp);

This crashes the program with no exception. Please can someone help. Thanks

SOLVED:

with

byte[] byTemp = new byte[nBufSize];

GCHandle h = GCHandle.Alloc(byTemp, GCHandleType.Pinned); 
igb.GetBuffer(h.AddrOfPinnedObject());

Thanks

4
  • These should be in completely separate projects. The C++ code should be compiled as a library or DLL. Once you've got them compiling separately, clarify your question, and we can help you integrate integrate the two parts. Commented Feb 8, 2012 at 15:57
  • If your as far as Jim has described, what is the interop signature for your GetBuffer method? Commented Feb 8, 2012 at 16:05
  • 1
    int GetBuffer(ref byte[] bd); is the interop signature. These are in two seperate projects what lead you to believe they werent ?? Commented Feb 8, 2012 at 16:17
  • Solved: with byte[] byTemp = new byte[nBufSize]; GCHandle h = GCHandle.Alloc(byTemp, GCHandleType.Pinned); igb.GetBuffer(h.AddrOfPinnedObject()); Commented Feb 8, 2012 at 22:38

2 Answers 2

2

The parameter should not be declared as ref. You want something like:

uint GetBuffer(byte[] bd);

If you include the ref you are passing a pointer to the array, when you just want the array. (And by array, I mean pointer to the first element.)

Sign up to request clarification or add additional context in comments.

4 Comments

I assume you meant to say pointer to a pointer to an array? (Leaving out "ref" will not pass the entire array by value!)
There is no "array" on the C++ side. There's a chunk of memory, and a pointer to the first element. (The data type of bd is int*, not int[nBufSize]) Since array subscript notation can be used with pointers, it's easy to confuse the two.
I was thinking that when you pass an array in a C-style language it's implicit that you're really passing a pointer. But I didn't express it very clearly. I'll update the text.
Solved: with byte[] byTemp = new byte[nBufSize]; GCHandle h = GCHandle.Alloc(byTemp, GCHandleType.Pinned); igb.GetBuffer(h.AddrOfPinnedObject());
0

I know this is an old question, but Google brought me here so it might bring someone else. If you're using P/Invoke to call:

... GetBuffer(byte* bd)

it should look something along the lines of

[DllImport("MyDll.dll")]
... GetBuffer(ref byte bd);

And a buffer array in c# should be passed in like this:

var arr = new byte[Length];
GetBuffer(ref arr[0]);

This also works with char*, as you can just pass in the same byte array reference and then use string s = Encoding.<encoding>.GetString(arr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.