469,945 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Help on C# custom marshaling

Hello,

I am trying to get c# custom marshaling working in a particular scenario but it does not appear to be working or not jumping into my marshaling class when I try to debug it. I am try to implement a performance improvement in the transfer of thousands of objects (ClientInfoDescriptor) over a remote call. The objects all have a model object (IDataDrivenModel) to describe them but there may only two or three model objects in total. These model object could be large objects. Instead of transfering duplicate models I would like to marshal in the model id and when I retrieve the objects on the client side I will un marshal the model id and lookup the model from the cache.

I would appreciate it if anyone could tell me where I may be going wrong or give me tips on how to better debug this custom marshaling. Or maybe there is a better way to get this performance improvement without custom marshaling?

Thanks,
JP

[Serializable]
public abstract class ADDInfoDescriptor : IDDInfoDescriptor
{
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(DDMarshaler))]
private IDataDrivenModel ddModel = null;
...
}

// Class that has attribute being customed marshaled
[Serializable]
public class ClientInfoDescriptor : ADDInfoDescriptor
{
...
}

// Marshaling class
public sealed class DDMarshaler : System.Runtime.InteropServices.ICustomMarshaler
{
private static DDMarshaler marshaler = null;
public static ICustomMarshaler GetInstance(string cookie)
{
if (marshaler == null)
{
marshaler = new DDMarshaler();
}
return marshaler;
}

public DDMarshaler()
{
}

public object MarshalNativeToManaged(System.IntPtr pNativeData)
{
int id = Marshal.ReadInt32(pNativeData);
return DDModelCache.Instance.GetModel(id);
}

public System.IntPtr MarshalManagedToNative(object managedObj)
{
IDataDrivenModel ddModel = (IDataDrivenModel)managedObj;
IntPtr unmanagedArray = Marshal.AllocHGlobal(sizeof(Int32));
Marshal.WriteInt32(unmanagedArray, ddModel.ID);
return unmanagedArray;
}

public void CleanUpManagedData(object ManagedObj)
{
}

public int GetNativeDataSize()
{
return sizeof(Int32);
}

public void CleanUpNativeData(System.IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
}
Feb 13 '08 #1
0 1615

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Scott Dowd | last post: by
reply views Thread by Ash | last post: by
4 posts views Thread by Vadym Stetsyak | last post: by
3 posts views Thread by Rudy Velthuis | last post: by
1 post views Thread by Nadav | last post: by
3 posts views Thread by Salvador | last post: by
4 posts views Thread by MSDousti | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.