Passing arrays from managed to unmanaged code 
Author Message
 Passing arrays from managed to unmanaged code

Hi everyone, hoping someone out there will help me since I am by no means an
expert like the rest of you.

I have been wrapping an old library (written in C) in MC++, which has been
going okay, except now I have come to the point where I have to pass an
array to a C function. The array I want to pass (an __int16 array, by the
way) is managed. How do I pass it? I did look through the documentation, but
found no clear answers. I have found some stuff on data marshalling, but the
online docs offer no clear explanation of the mechanisms involved, the
purpose etc. At least, not that I have seen.
I suppose I could create an unmanaged temporary array and copy to-and-fro
but that's not very elegant and I suspect there must be a better way ... not
to mention that this array can be very, very large indeed.

Thanks for any help,

Marten



Sun, 10 Oct 2004 01:15:01 GMT  
 Passing arrays from managed to unmanaged code
Marten,

Quote:
> Hi everyone, hoping someone out there will help me since I am by no means
an
> expert like the rest of you.

> I have been wrapping an old library (written in C) in MC++, which has been
> going okay, except now I have come to the point where I have to pass an
> array to a C function. The array I want to pass (an __int16 array, by the
> way) is managed. How do I pass it? I did look through the documentation,
but
> found no clear answers. I have found some stuff on data marshalling, but
the
> online docs offer no clear explanation of the mechanisms involved, the
> purpose etc. At least, not that I have seen.

I assume you're not using P/Invoke, right?

If so, what you need to do is get a __nogc pointer out of a __gc pointer to
a managed array of __int16. To do this, you first need to pin down the
array. From memory, it should go something like this:

 short (__pin *ptr) = &MyInt16Array[0];

--
Tomas Restrepo



Sun, 10 Oct 2004 01:58:03 GMT  
 Passing arrays from managed to unmanaged code
Excellent, thank you. That was precisely what I was looking for.



Quote:
> Marten,

> > Hi everyone, hoping someone out there will help me since I am by no
means
> an
> > expert like the rest of you.

> > I have been wrapping an old library (written in C) in MC++, which has
been
> > going okay, except now I have come to the point where I have to pass an
> > array to a C function. The array I want to pass (an __int16 array, by
the
> > way) is managed. How do I pass it? I did look through the documentation,
> but
> > found no clear answers. I have found some stuff on data marshalling, but
> the
> > online docs offer no clear explanation of the mechanisms involved, the
> > purpose etc. At least, not that I have seen.

> I assume you're not using P/Invoke, right?

> If so, what you need to do is get a __nogc pointer out of a __gc pointer
to
> a managed array of __int16. To do this, you first need to pin down the
> array. From memory, it should go something like this:

>  short (__pin *ptr) = &MyInt16Array[0];

> --
> Tomas Restrepo




Sun, 10 Oct 2004 02:04:10 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. passing array from managed to unmanaged ????

2. How to Pass Array from managed to unmanaged !!??!!

3. passing from unmanaged to managed code

4. Referencing data from unmanaged code to managed code in C++ Wrapper class

5. calling managed code from unmanaged code

6. Calling managed code from unmanaged Code

7. How to pass a function pointer from Managed C++ to unmanaged c++

8. problem passing unmanaged pointer from one managed member function to another

9. moving arrays from un-managed to managed code.

10. Conversion of managed and unmanaged arrays

11. marshal an array from managed C++ to unmanaged C++

12. return an array from unmanaged C++ to managed C++

 

 
Powered by phpBB® Forum Software