Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

L prefix for strings in C++

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
like image 900
Manjoor Avatar asked Nov 03 '10 12:11

Manjoor


1 Answers

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.

like image 76
harper Avatar answered Sep 20 '22 19:09

harper