I have a static library. This library have the following function defined
int WriteData(LPTSTR s)
The sample to call the function is
LPTSTR s = (LPTSTR) L"Test Data";
int n = WriteData(s);
WriteData return 0 on success and -1 on failure.
I am writing a dynamic DLL to export this function.
int TestFun(LPTSTR lpData)
{
return WriteData(lpData);
}
A C++ test application result
LPTSTR s = (LPTSTR) L"Test Data";
TestFun(s); //OK return 0
LPTSTR s = (LPTSTR) "Test Data";
TestFun(s); //Fail return -1
I have to call it from a c# application. I assume my DLL-Import signature would be:
[DllImport("Test.dll")]
private static extern int TestFun(String s);
My question is very simple How can i call it from .Net? As you can see i have control over
TestFun(LPTSTR lpData)
but no control over
WriteData(LPTSTR s)
Thanks everybody for their input. So far i am stuck on casting. I think my problem would be solved when i woul be able take input from user and write 2 line for casting in place of following line.
LPTSTR s = (LPTSTR) L"Test Data"); //<= How can ii take input from user and
TestFun(s); //OK return 0
The L prefix makes the string a wchar_t string.
You can use the Windows API function MultiByteToWideChar
to convert an ANSI string to a wchar_t string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With