Re: Marshaling Delegates as Struct/Class Members



Bob,

Yes, this is. See my reply to your initial post for why.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Bob" <nobody@xxxxxxxxxxx> wrote in message
news:OOTqPaW0FHA.1924@xxxxxxxxxxxxxxxxxxxxxxx
> Furthermore, why can I marshal this class but I can't pin it?
>
> Int32 size = Marshal.SizeOf(typeof(TestObject));
> IntPtr ptr = Marshal.AllocCoTaskMem(size);
> Marshal.StructureToPtr(testObject, ptr, false);
> Marshal.FreeCoTaskMem(ptr);
>
> I would think anything on the managed heap that can be marshalled could be
> pinned. Is this wrong?
>
> Thanks
>
> "Bob" <nobody@xxxxxxxxxxx> wrote in message
> news:eQx8nST0FHA.3720@xxxxxxxxxxxxxxxxxxxxxxx
>>I understand delegates do not need to be pinned, but its the class object
>>itself I need pinned. Take this code:
>>
>>
>> [StructLayout(LayoutKind.Sequential)]
>> public class TestObject
>> {
>> [MarshalAs(UnmanagedType.FunctionPtr)]
>> public AddDelegate Add;
>> [MarshalAs(UnmanagedType.FunctionPtr)]
>> public SubtractDelegate Subtract;
>>
>> public delegate Int32 AddDelegate(Int32 a, Int32 b);
>> public delegate Int32 SubtractDelegate(Int32 a, Int32 b);
>> }
>>
>>
>> public class Program
>> {
>> private static void Main(
>> String[] args)
>> {
>> TestObject testObject = new TestObject();
>> GCHandle gcHandle = GCHandle.Alloc(testObject,
>> GCHandleType.Pinned);
>> }
>> }
>>
>>
>> The GCHandle.Alloc fails with:
>>
>>
>> System.ArgumentException was unhandled
>> Message="Object contains non-primitive or non-blittable data."
>> Source="mscorlib"
>> StackTrace:
>> at System.Runtime.InteropServices.GCHandle.InternalAlloc(Object
>> value, GCHandleType type)
>> at System.Runtime.InteropServices.GCHandle.Alloc(Object value,
>> GCHandleType type)
>>
>>
>> Is there an easy way to marshal delegates as part of a struct? Isn't
>> that what UnmanagedType.FunctionPtr is for? I don't want to manage
>> multiple objects and manually marshal stuff using
>> Marshal.GetFunctionPointerForDelegate? I -really- want to avoid that
>> route in this particular project.
>>
>> Thanks
>>
>>
>
>


.



Relevant Pages

  • Re: Marshaling Delegates as Struct/Class Members
    ... >I understand delegates do not need to be pinned, but its the class object ... > public class TestObject ... > value, GCHandleType type) ... > Is there an easy way to marshal delegates as part of a struct? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Multiple Exception Definitions in One File
    ... > public class ExcepOne extends Exception ... > UseExceps.java:10: an enclosing instance that contains Exceps.ExcepOne ... Non-static inner class objects have a reference to their enclosing outer ... Static inner class object don't have a reference to an enclosing outer class ...
    (comp.lang.java.programmer)
  • Re: Errors when deleting a row in a JTable.
    ... > The code below return a good class object but I've already the same stack ... > public Class getColumnClass(int columnIndex) { ... or TYPE of the wrapper class for some other simple type. ...
    (comp.lang.java)
  • Generic class object?
    ... Is it possible to get the class object from a type parameter? ... public class Test{ ... Regards, ... - Gijs Peek ...
    (comp.lang.java.programmer)