c# - 如何将 C++ 字符串从 C++dll 返回到 C# 应用程序
问题描述
我有一个 C# 应用程序使用的 C++ dll。我想将一个字符串从 C++ dll 返回到 C# 应用程序。下面是我的示例代码。 动态链接库
#include <iostream>
#include<comdef.h>
#include <iomanip>
#include <comutil.h> // #include for _bstr_t
extern "C" __declspec(dllexport) std::string getName();
std::string getName() {
std::string _name = "XYZ";
return _name;
}
C# 代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("NativeLibrary.dll") ]
public static extern string getName();
private void btnAdd_Click(object sender, EventArgs e)
{
string res = getName();
Console.WriteLine(res);
}
}
我能够成功构建它,但是当我运行应用程序时没有任何反应。我怎么能摆脱这个问题?
解决方案
推荐阅读
- python - 当属性名称使用类型匹配类名称时,为什么会引发 TypeError?
- react-native - 如何在博览会 BarCodeScanner 中删除多余的空白
- python - 如何根据列中具有相同值的行对行进行分组,然后在每个子集上运行我的代码?
- asp.net-mvc - 如何将数据库表中的列放入动态列表中?
- android - Flutter - 当用户移动到其他(预览)屏幕时如何正确暂停相机?
- mysql - 如何在mysql中使用radious通过lat long获得最近的地方
- sql - 'in statement' 中的通配符
- ip - 从 Pi ping 静态 IP 地址
- android - 在用户启用/禁用某些功能后打开应用设置并获得结果?
- arrays - 将新元素附加到数组导致应用程序崩溃的异常错误