Problem calling C++ DLL from C# client



I'm trying to use a C++ DLL from a C# program. A number of the routines in the DLL need to be passed string buffers through which that return results.

The problem is that is I call FunctionA then FunctionB, passing both of them different StringBuilder buffers, I the buffer return by FunctionB is corrupted with content previously returned by FunctionA.

Changing the capacity of the string buffers that I pass into the routines changes the behaviour, but does not solve the problem. The buffers I'm passing are easily big enough to hold the results. I tried using Delphi to call the DLL, passing the same sized buffers, and it's works flawlessly. My definitions are listed below. Any thoughts?

C++ DLL definitions
-------------------

int FAR PASCAL FunctionA(char *szInputParamter, char *szOutputParameter);
int FAR PASCAL FunctionB(LPSTR lpszInputParamter, int nInputParameter, LPSTR *lpszOutputParameter);

C# definitions
--------------

[DllImport("MyLibrary.dll")]
public static extern int FunctionA(string inputParamter, [MarshalAs(UnmanagedType.LPStr)] StringBuilder outputParameter);

[DllImport("MyLibrary.dll")]
public static extern int FunctionB(string inputParamter, int nInputParameter, [MarshalAs(UnmanagedType.LPStr)] StringBuilder outputParameter);

Calling from C#
---------------

StringBuilder outputParameter1 = new StringBuilder(8192);
int count = MyLibrary.FunctionA("some text", outputParameter);

StringBuilder outputParameter2 = new StringBuilder(8192);
int count = MyLibrary.FunctionB("some text", 0, outputParameter);

.



Relevant Pages

  • Re: am having a problem with pinvoke and StringBuilder[ ]
    ... If you don't have the source of the DLL then you are in a world of pain, that means you wont be able to use the interop marshaler, you need to "custom" marshal. ... public static extern int EnumerateBoards(ref int numBoards, ... Since the String class is immutable, I have used StringBuilder in my code ...
    (microsoft.public.dotnet.framework.clr)
  • Re: am having a problem with pinvoke and StringBuilder[ ]
    ... DLL: Hello from TestLib.dll ... Since the String class is immutable, I have used StringBuilder in my code ... public static extern int EnumerateBoards(ref int numBoards, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: am having a problem with pinvoke and StringBuilder[ ]
    ... Since the String class is immutable, I have used StringBuilder in my code ... the Stringin managed code was correctly printed by the DLL). ... public static extern int EnumerateBoards(ref int numBoards, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: pinvoke with StringBuilder[] not working for me
    ... public static extern int EnumerateBoards ... I want to pass a pointer to an array of char strings, ... int numBoards, StringBuilder boards); ...
    (microsoft.public.dotnet.framework.interop)
  • Re: No end of problems trying to use a C++ function....
    ... >> public static extern int IsValidCompressedFile(string srcFileName, ref ... > public static extern int IsValidCompressedFile(ref StringBuilder ...
    (microsoft.public.dotnet.languages.csharp)