String to WCHAR* conversion

I

IdleBrain

Hello

I am trying to call a C++ DLL from C#:

C# code:
[DllImport("Log")]
static extern void LogMsg(String wsString1);
static void Main(string[] args)
{
LogMsg("Works!");
}

C++ DLL:
__declspec(dllexport) ULONG LogMsg(WCHAR* wsString1)
{

}

The problem is that the wsString1 from C# is being converted to a
garbage message.
Can some one help me on this conversion from C# string to WCHAR*.

I read somewhere that C# string is converted to LPTSTR in C++.
So, I even tried:

__declspec(dllexport) ULONG LogMsgNew(LPTSTR lpString1)
{
LogMsg(T2W(lpString1));
}


Any help is greatly appreciated. Thanks in advance
 
I

IdleBrain

Thanks to MSDN:

[DllImport("Log")]
static extern void LogMsg([MarshalAs(UnmanagedType.LPWStr)]String
wsString1);
 

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