Reputation: 1784
I have a CLR C++ dll that wrraped a static C++ library. I have the following class:
#pragma once
#include <windows.h>
#include <sddl.h>
#include <LibEx.h>
using namespace System;
#using <mscorlib.dll>
namespace LIB_WrapperNamespace {
public ref class LIB_WrapperClass
{
public:
BOOL WINAPI T_LibEx_ConsoleConnect(IN DWORD num1, IN LPWSTR Name)
{
return LibEx_ConsoleConnect(num1,Name);
}
};
}
in C#, I add the reference to library
LIB_WrapperNamespace.LIB_WrapperClass myLib = new LIB_WrapperNamespace.LIB_WrapperClass();
Now how to call this function, how to send string to char*? from C#:
string myName = "NAME";
myLib.T_LibEx_ConsoleConnect(1,**myName**);
Upvotes: 0
Views: 1167
Reputation: 3719
Why building C++\CLI project to wrap something when you "expose" marshaling problems onto the user of your wrapper? The idea of C++\CLI is to hide the marshaling troubles inside the wrapper. You should declare the function natively for .NET:
#pragma once
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal_cppstd.h>
#include <vector>
namespace ClassLibrary2 {
public ref class Class1
{
public:
//Expose .NET types to .NET users.
System::Boolean T_LibEx_ConsoleConnect(System::UInt64 num1, System::String^ Name);
};
}
And implementing this wrapper function you marshal as you feel proper, it could look like this:
#include "ClassLibrary2.h"
namespace ClassLibrary2 {
System::Boolean Class1::T_LibEx_ConsoleConnect(
System::UInt64 num1,
System::String^ Name)
{
//Initialize marshaling infrastructure. You can use its instance many times
//through out life span of your application.
msclr::interop::marshal_context^ marshalContext = gcnew msclr::interop::marshal_context();
//Turn System::String into LPWSTR. Keep in mind that you are now the owner of
//memory buffer allocated for unmanagedName. You need to release it somewhere.
const wchar_t* clsConstChars = marshalContext->marshal_as<const wchar_t*>(Name);
LPWSTR unmanagedName = const_cast<LPWSTR>(clsConstChars);
//System::UInt64 num1 will be marshalled to DWORD natively by compiler.
return LibEx_ConsoleConnect(num1, unmanagedName);
}}
Upvotes: 0
Reputation: 755477
The API should be exposing that parameter as a wchar_t*
hence you need to provide a pointer value in C#. Try the following
IntPtr ptr = IntPtr.Zero;
try {
ptr = Marshal.StringToCoTaskMemUni("NAME");
unsafe {
myLib.T_LibEx_Consoleconnect(1, (char*)(ptr.ToPointer()));
}
} finally {
if (ptr != IntPtr.Zero) {
Marshal.FreeCoTaskMem(ptr);
}
}
Unfortunately though since you have exposed the method with a raw pointer value there is no way to use this from C# without a unsafe
code. An alternative approach would be to expose an overload which takes say a string^
. This would be usable from C# and the C++/CLI code could take care of the marshalling from string^
to LPWSTR
BOOL WINAPI T_LibEx_ConsoleConnect(DWORD num1, String^ Name) {
IntPtr ip = Marshal::StringToHGlobalUni(Name);
BOOL ret = T_LibEx_ConsoleConnect(num1, static_cast<LPWSTR>(ip.ToPointer()));
Marshal::FreeHGlobal(ip);
return ret;
}
// From C#
myLib.T_LibEx_ConsoleConnect(1, "NAME");
Upvotes: 2