I have a .Net Com Server being called by an unmanaged c++ program. They both
implement a common interface which passes a string by means of a double
pointer (c++ interface: [in, out] wchar_t**).
In the .Net Com server I am supposed to free the memory and allocate new
memory for this string since I have to read the string it points to, and the
point it to a new string to pass data back out.
I have used "ref string" which did not work since it was not allocating new
memory space for the strings sent back out.
I tried "ref StringBuilder" but that did not work because of a current bug
in the .Net framework, more than 16 characters being sent in will cause an
error to occur, see the following article:
Article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;317577
I used the workaround in this artice which says to use a pointer for this
("ref IntPtr") and I have tried this but i cannot get it to work. I cannot
read the incoming string or set the string. I just get garbled data or cause
memory errors to occur.
Any help would be appreciated!

Signature
-----------------
Eric Carlson
Eric,
>I used the workaround in this artice which says to use a pointer for this
>("ref IntPtr") and I have tried this but i cannot get it to work. I cannot
>read the incoming string or set the string. I just get garbled data or cause
>memory errors to occur.
Got some code to show? This should work.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Eric Carlson - 09 Dec 2004 17:09 GMT
When using Marshal.PtrToStringUni to try and get a string from the incoming
pointer, the string just contains garble. And when i try and write to the
pointer using Marshal.StringToCoTaskMemUni, the data is not correctly
written, since the c++ client has some kind of error (can't see the error
though).
public interface MyInterface
{
[PreserveSig]
int Validate([MarshalAs(UnmanagedType.LPWStr)] string PathName, ref IntPtr
Answers);
//The c++ interface definition:
//HRESULT Validate([in, string] wchar_t* PathName,[in, out, unique, string]
wchar_t** Answers);
}
public class ComServer : MyInterface
{
public ComServer()
{
}
public int Validate(string Pathname, ref IntPtr Answers)
{
string temp;
temp = Marshal.PtrToStringUni(Answers);
ValidatorForm VF = new ValidatorForm();
VF.PromptData.Text = CK_and_Dest_Pathname + "\n\r\n\r\n\r" + temp;
//Get some text to send back through 'Answers'
VF.ShowDialog();
Marshal.FreeCoTaskMem(Answers);
//Get text to send back
string str = VF.PromptAnswer;
Answers = Marshal.StringToCoTaskMemUni(str);
}