Passing C# delegate to C++ with byte array parameter

J

Josh

Hi All,

I am trying to write a C# program to call a routine in managed C++.
In the routine, the C# program passes a delegate to the managed C++.
The delegate looks like this in c++:
public __delegate void FoundPidDelegate (
unsigned char *pData,
);

The C++ is able to call the C# because my routine in C# is unsafe and
looks like this:
static private void MyFoundPidFunction (byte *pData)

Is there any way to make a "safe" C# function that looks like this:
static private void MyFoundPidFunction (byte []pData)
and lets me pass it as a delegate to the C++?

Thanks,

Josh
 
M

Mattias Sjögren

Is there any way to make a "safe" C# function that looks like this:
static private void MyFoundPidFunction (byte []pData)
and lets me pass it as a delegate to the C++?

Change the delegate signature to

public __delegate void FoundPidDelegate (
unsigned char pData __gc[],
);



Mattias
 
J

Josh

When I arrive back in my C# program, the pData array only has one
element and if I try to look at pData[1], I get an error.

In C++ I am allocating the memory like this:
unsigned char arrManaged __gc [] = new unsigned char
__gc[iDataLen];
Then I call the C# delegate routine like this:
m_pFoundFunc(arrManaged);

What am I doing wrong?

Thanks!

Josh

Mattias Sjögren said:
Is there any way to make a "safe" C# function that looks like this:
static private void MyFoundPidFunction (byte []pData)
and lets me pass it as a delegate to the C++?

Change the delegate signature to

public __delegate void FoundPidDelegate (
unsigned char pData __gc[],
);



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top