Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I've got a c++ dll that i use in my c# windows forms app.

C#
[DllImport("dllname.dll", EntryPoint = "?dllMethod@@YA_NPBDAAPAEAAH@Z", CallingConvention = CallingConvention.Cdecl)]
private static extern bool dllMethod([MarshalAs(UnmanagedType.LPStr)]string fname, out IntPtr descriptor, out int length);


In my c# app, i just want to serialize a hashtable with the IntPtr data that's returned by that Dll... So i'm doing this:

C#
[Serializable]
public class TopSurfDescriptor
{
  public IntPtr descriptor;
  public int length;
}
[Serializable]
public class Descriptors
{        
   private Hashtable descriptors=null;

   public void AddDescriptor(string key,TopSurfDescriptor descriptor)
   {
            descriptors.Add(key, descriptor);
   }
   public Descriptors()
   { 
      if (File.Exists(@"Descriptors.dat"))
      {
           FileStream fs = new FileStream(@"Descriptors.dat", FileMode.Open);
           try
           {
               BinaryFormatter formatter = new BinaryFormatter();
               descriptors = (Hashtable)formatter.Deserialize(fs);
           }
           catch (SerializationException){ throw; }

           finally {fs.Close(); }
       }
       else
           descriptors = new Hashtable();
   }

//AND
   public void Save()
   {
       FileStream fs = new FileStream(@"Descriptors.dat", FileMode.Create);

       BinaryFormatter formatter = new BinaryFormatter();
       try
       {
           formatter.Serialize(fs, descriptors);
       }
       catch (SerializationException e){ throw; }
       finally { fs.Close(); }
   }


if i dont close the app the serialization and deserialization works fine, but if i close it and run it again (from visual studio) in debug mode i can see the process of deserialization working good, (i see the hastable with all the data and the IntPtr pointing to the correct TopSurfDescriptor type, but if i pass this pointer to other method from the DLL i got the exception that the memory can be corrupt or dont have access...

when I analyze the serialized file (on disk) i seems that the data is not there (because the file is to small), but only the IntPtr. So, i think the problem is the fact that the object (memory) that is returned from the external dll is not beeing serializable.
Posted
Updated 12-Jul-12 4:51am
v2

1 solution

Your TopSurfDescriptor class is serializing the IntPtr as that is what it holds.
You would need to retrieve the data at the memory address and do customer serialization/deserialization if you wish to save/load it this way.
 
Share this answer
 
Comments
Robertodamaia 12-Jul-12 16:14pm    
yeah but this is my problem. I do i retrieve the memory address? I can't modify nothing inside the external DLL
DaveyM69 12-Jul-12 16:59pm    
Is the pointer a pointer to unmanaged memory? If so, this will copy the data to a byte[] which can be serialized:
[Serializable]
public class Data
{
private byte[] data;

public Data(IntPtr handle, int length)
{
data = new byte[length];
Marshal.Copy(handle, data, 0, length);
}
}
Robertodamaia 12-Jul-12 18:07pm    
man a bigggg thankk YOUUUUUU :D you're now my best friend :D
DaveyM69 12-Jul-12 18:14pm    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900