我有一个CLR C ++ dll,用于编写静态C ++库。 我有以下课程:
#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);
}
};
}
在C#中,我添加了对库的引用
LIB_WrapperNamespace.LIB_WrapperClass myLib = new LIB_WrapperNamespace.LIB_WrapperClass();
现在如何调用此函数,如何将字符串发送到char *?来自C#:
string myName = "NAME";
myLib.T_LibEx_ConsoleConnect(1,**myName**);
答案 0 :(得分:2)
API应该将该参数公开为wchar_t*
,因此您需要在C#中提供指针值。请尝试以下
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);
}
}
不幸的是,虽然您已经使用原始指针值公开了该方法,但是如果没有unsafe
代码,就无法在C#中使用它。另一种方法是暴露一个过载,它需要string^
。这可以在C#中使用,C ++ / CLI代码可以处理从string^
到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");
答案 1 :(得分:0)
为什么在将封送问题“暴露”给封装器的用户时,构建C ++ \ CLI项目来包装? C ++ \ CLI的想法是隐藏包装器内的编组故障。您应该为.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);
};
}
实现这个包装函数,你在感觉正确时编组,它看起来像这样:
#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);
}}