.NET Framework Bookmark and Share   
 index > 64-Bit .NET Framework Development > P/Invoke memory faults when marshaling ByRef AnsiBStr on x64
 

P/Invoke memory faults when marshaling ByRef AnsiBStr on x64

I am attempting to call functions within an unmanaged C library fromVB.NET.Both input and outputarguments to the C library functions are by-reference ANSI BSTRs, the memory for which is managed within the C library using SysAllocStringByteLen() and SysFreeString().In VB.NET, I am declaring the functions like this:


Declare Functionfcn_name Lib "lib_name.dll" ( _
<MarshalAs(UnmanagedType.AnsiBStr), [In]()> ByRefInArg As String, _
<MarshalAs(UnmanagedType.AnsiBStr), Out()> ByRefOutArg As String) _
As Int

On x86, this works as expected.

On x64, memory issues arise. When run on x64 XP/Server 2003, the returned unmanaged AnsiBStr never gets free'd after conversion to the managed OutArg. When run on x64 Vista/Server 2008, the heap gets corrupted.

If instead I manage the output marshaling myself, such as first defining:


Friend Module LIBMOD

Declare Functionfcn_name Lib "lib_name.dll" ( _
<MarshalAs(UnmanagedType.AnsiBStr), [In]()> ByRefInArg As String, _
<Out()> ByRefOutPtr As IntPtr) _
As Int

End Module

and then defining the function called within the code to be:

Private Function fcn_name( _
ByRefInArg As String, _
ByRefOutArg As String) _
As Int
DimOutPtr As IntPtr
Dim retval As Int

retval = LIBMOD.fcn_name(InArg, OutPtr)

IfOutPtr <> 0 Then
OutArg = Marshal.PtrToStringAnsi(OutPtr)
Marshal.FreeBSTR(OutPtr)
End If

Return retval
End Function

this works without fault on x64: FreeBSTR() is calling SysFreeString() on the unmanaged output, and neither memory leaks nor heap corruption occur.

This is more or less equivalent to what I'd expect to happen in the first case, but it seems as if the x64 framework is utilizing some mechanism other than SysFreeString() to free the returned AnsiBStr's.

Should two approaches be equivalent? If not, why the does the first work on x86 but not x64?

j_travis
An AnsiBStr is a very unusual animal. That kind of marshaling would not have been tested much. Post to connect.microsoft.com to tell them about the bug you found.

Hans Passant.
  • Marked As Answer byj_travis Wednesday, September 09, 2009 7:32 PM
  •  
nobugz
An AnsiBStr is a very unusual animal. That kind of marshaling would not have been tested much. Post to connect.microsoft.com to tell them about the bug you found.

Hans Passant.
  • Marked As Answer byj_travis Wednesday, September 09, 2009 7:32 PM
  •  
nobugz

You can use google to search for other answers

Custom Search

More Threads

• DllImport in an "Any CPU" application
• owc11 download to excel functionality
• How do I control custom dialog boxes in deploying time?
• Warning due to XBAP, native Code and 64 Bit ...
• Getting Smart on 64bit .NET
• Errors related to edkutils.lib
• Multiply 20 length digits in 32 bit CPU
• Registry access: crossing bitness in C#
• need driver for linksys compact G usb adapter for server 2005
• RegistryKey.OpenSubKey() (Vista vs 7)