我有一个包含静态 C++ 库的 CLR C++ dll。 我有以下类(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);
}
};
}
在 C# 中,我添加了对库的引用
LIB_WrapperNamespace.LIB_WrapperClass myLib = new LIB_WrapperNamespace.LIB_WrapperClass();
现在如何调用这个函数,如何将字符串发送到char*?来自 C#:
string myName = "NAME";
myLib.T_LibEx_ConsoleConnect(1,**myName**);
最佳答案
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");
关于c# - 从 C# 调用带有 char* 的 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17953842/