[mapguide-trac] #1047: Error in marshalling of unmanaged strings in .NET wrappers in RFC 68 implementation

MapGuide Open Source trac_mapguide at osgeo.org
Mon Jul 27 01:03:55 EDT 2009


#1047: Error in marshalling of unmanaged strings in .NET wrappers in RFC 68
implementation
-----------------------+----------------------------------------------------
 Reporter:  anilpatel  |         Owner:     
     Type:  defect     |        Status:  new
 Priority:  medium     |     Milestone:  2.2
Component:  Web API    |       Version:     
 Severity:  major      |    Resolution:     
 Keywords:             |   External_id:     
-----------------------+----------------------------------------------------
Comment (by anilpatel):

 A slightly more efficient has been found and tested.  This approach avoids
 creation/destruction of the shared memory area, thereby reducing the
 number of memory allocations/deallocations.  The managed code accesses the
 returned string as an IntPtr directly and uses the .NET InteropServices to
 marshal the unmanaged Ansi string to a managed string.

 New approach:

 Changes required to unmanaged side:  NONE.  Code remains as it is in the
 vault/depot:

 {{{
 DllExport char* getClassName(void* ptrObj)
 {
   return ((MgObject*)ptrObj)->GetMultiByteClassName();
 }

 DllExport char* getNameSpace(void* ptrObj)
 {
   return ((MgObject*)ptrObj)->GetNameSpace();
 }
 }}}

 Changes required to the managed side DllImports (same as original
 proposal):

 {{{
   [DllImport("unManagedDllName", EntryPoint="getClassName")]
   public static extern IntPtr getClassName(IntPtr objectRef);

   [DllImport("unManagedDllName", EntryPoint="getNameSpace")]
   public static extern IntPtr getNameSpace(IntPtr objectRef);
 }}}

 Changes required to createObject overload:

 {{{
  static public object createObject(int id, IntPtr nameSpaceNamePtr, IntPtr
 classNamePtr, IntPtr cPtr, bool ownMemory)
   {
       // Marshall strings in nameSpaceNamePtr and classNamePtr from
 unmanaged
 char * to managed strings
       String nameSpaceName =
 System.Runtime.InteropServices.Marshal.PtrToStringAnsi(nameSpaceNamePtr);
       String className     =
 System.Runtime.InteropServices.Marshal.PtrToStringAnsi(classNamePtr);

       ...

 }}}

 The attached patch file has been updated to reflect the new proposal.

-- 
Ticket URL: <http://trac.osgeo.org/mapguide/ticket/1047#comment:2>
MapGuide Open Source <http://mapguide.osgeo.org/>
MapGuide Open Source Internals


More information about the mapguide-trac mailing list