Problem calling C++ DLL from C# client
- From: <Debbie.Croft@xxxxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 22:06:35 +0100
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);
.
- Prev by Date: Re: switch or what?
- Next by Date: Re: switch or what?
- Previous by thread: A Security Problem
- Next by thread: Argh... XmlSerialization with inherited types
- Index(es):
Relevant Pages
|
|