Passing arrays into unmanaged code

S

Steve Baer

I'm wrapping some unmanaged C++ classes with managed versions for use in the
..NET world. Everything is going great except I can't figure out a good
method for passing simple data type arrays into unmanaged classes.

Say I have a function
void UnManagedClass::FillOutArray(int size, int* data);
where data is an array of unmanaged ints and size gives the size of the
array.

I would like to wrap the function like so

void ManagedClass::FillOutArray(int __gc* data[])
{
int size = data->Length;
int __pin* pUnMgdData= (some way to pin the data array???)
m_internalPtr ->FillOutArray(size, pUnMgdData); //pointer to internal
instance of unmanaged class
}

I know I could create an array of ints in the function and then copy the
values to the data array after the internal unmanaged call, but I was hoping
there would be a slicker way.

Any help would be greatly appreciated. Thanks in advance,
-steve
 
B

Ben Schwehn

void ManagedClass::FillOutArray(int __gc* data[])
{
int size = data->Length;
int __pin* pUnMgdData= (some way to pin the data array???)


you can pin the first member of the array something like that:

int __pin* pUnMgdData = &data[0];

the entire array is now pinned and you have a pointer to the start of
the array

See MSDN:
Managed Extensions for C++ Specification
7.7 Pinning Pointers

hth
Ben
 

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