LPCTSTR in DLLImport in C#

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
 
O

ORC

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top