> How can I convert my byte array to unsigned char __gc*?
Take the address of the first array element. For example:
void F(unsigned char __gc* p) {}
Byte arr[] = new Byte[10];
F(&arr[0]);
Cheerio!

Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Nanditha Chandra - 17 Sep 2004 12:59 GMT
"Brandon Bray [MSFT]" <branbray@online.microsoft.com> wrote >
> Take the address of the first array element. For example:
>
> void F(unsigned char __gc* p) {}
>
> Byte arr[] = new Byte[10];
> F(&arr[0]);
Can you do that? I thought contiguious allocation is not guaranteed.
So, in Foo, Can I assume that the next 10 bytes is what I want.
Thanks,
D
Brandon Bray [MSFT] - 17 Sep 2004 21:42 GMT
> Can you do that? I thought contiguious allocation is not guaranteed.
> So, in Foo, Can I assume that the next 10 bytes is what I want.
Yes. Arrays are guaranteed to have contiguous allocation. Other than using
ExplicitLayout, arrays are the only way to get contiguous allocation.

Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Wild Wind - 18 Sep 2004 10:55 GMT
>> How can I convert my byte array to unsigned char __gc*?
>
[quoted text clipped - 6 lines]
>
> Cheerio!
Brandon,
Just wondering... do you not have to pin the &arr[0]
address before passing it to the F function, since arr
is managed and can be shifted around?
--
Akin
aknak at aksoto dot idps dot co dot uk
Brandon Bray [MSFT] - 20 Sep 2004 16:51 GMT
>> void F(unsigned char __gc* p) {}
>>
[quoted text clipped - 4 lines]
> address before passing it to the F function, since arr
> is managed and can be shifted around?
Because the argument to F used __gc to qualify the pointer, it notes that
the garbage collector is going to track this pointer and update it if the
memory gets moved around.
Now, if __gc hadn't been there (or if the __nogc keyword were used), then
the pointer would not be tracked by the garbage collector. This allows the
pointer to be used by native code. In that situation, you are correct that
the pointer needs to be pinned first.
Hope that makes sense. Cheerio!

Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/