LPCTSTR in DLLImport in C#

  • Thread starter Thread starter ORC
  • Start date Start date
O

ORC

Hi,

I have been Googling a lot trying to solve following problem, but without
success, so now I hope someone here will be able to help me (I have done a
lot of DLLImports but never where I had to import a LPCTSTR ):

In a native DLL:
void TestFunc(LPCTSTR TestString)
{
TestString = TEXT("Text to transfer");
MessageBox(NULL, TestString, NULL, NULL); // Works fine - messagebox
shows correct text!
}

In C#:
[DllImport("Test.dll", EntryPoint="TestFunc", SetLastError=true)]
public static extern void TestFunc(StringBuilder TestString);

private void buttonOK_Click(object sender, System.EventArgs e)
{
StringBuilder MyString = new StringBuilder(50)
TestFunc(MyString);
MessageBox.Show(MyString); // Doesn't work - MyString is always empty
}

Thanks
Ole
 
OK - found a solution:
It is neccesary to copy the text in the DLL like this:
wcscpy((unsigned short *)TestString, TEXT("Text to transfer");

Thanks,
Ole
 
Back
Top