[Mono-devel-list] Problems calling unmanaged code
Ben Maurer
bmaurer at ximian.com
Sun Aug 1 14:54:05 EDT 2004
On Sun, 2004-08-01 at 14:32, Ewen Cheslack-Postava wrote:
> Hello,
>
> Thanks, that worked perfectly. Just for my understanding: Is there a
> reason that you can't just pass the pointer? It is unsafe code and
> the address is fixed, so I thought that would work. If it requires
> deep understanding of how marshalling works, don't bother though b/c I
> only sort of understand it.
The C# spec does not allow you to take the address of a `managed'
structure. So, your code was illegal in the first place.
However, let me step you through what was happening. When you said
&blah, what it did was to take the address of the object as it appeared
to the runtime. At runtime, if you have
public unsafe struct _CameraAbilities
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] string model;
The string is still a System.String inside the structure. It is not a
fixed buffer.
When you took the address of this structure, it was not the correct
size. Rather than a 128 char model, you had a 4 byte pointer to a
System.String. The runtime does not know how to marshal pointers in any
special way.
-- Ben
More information about the Mono-devel-list
mailing list