J
Jeff A
I am trying to use pInvoke and I need to pass some data from a C++ dll
to C# app and vice-a-versa.
The data which needs to be passed to C# app is WCHAR* in the dll. How do
I marshal them? Do I need to allocate memory to the string in the dll?
I used this in the DLL
void GetMsg(LPTSTR msg)
{
wcsncpy(msg,lpszTheData,256);
}
and this in C# app
[DllImport("MyDll.dll")]
public static extern int GetMsg([MarshalAs(UnmanagedType.LPStr)]
ref string msg);
static void Main(string[] args)
{
string msg="Hello from .NET";
try
{
GetMsg(ref msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine(msg);//this gives exception saying msg is
null
Console.ReadKey();
}
I also tried without the ref keyword and tried UnmanagedType.LPTStr &
UnmanagedType.LPWStr but none is working.
Please help.
Thanks & Regards,
Jeff
to C# app and vice-a-versa.
The data which needs to be passed to C# app is WCHAR* in the dll. How do
I marshal them? Do I need to allocate memory to the string in the dll?
I used this in the DLL
void GetMsg(LPTSTR msg)
{
wcsncpy(msg,lpszTheData,256);
}
and this in C# app
[DllImport("MyDll.dll")]
public static extern int GetMsg([MarshalAs(UnmanagedType.LPStr)]
ref string msg);
static void Main(string[] args)
{
string msg="Hello from .NET";
try
{
GetMsg(ref msg);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine(msg);//this gives exception saying msg is
null
Console.ReadKey();
}
I also tried without the ref keyword and tried UnmanagedType.LPTStr &
UnmanagedType.LPWStr but none is working.
Please help.
Thanks & Regards,
Jeff