将c#字符串传递给c++ /CLI不会在CPP程序中显示字符串值
Passing a C# string to C++/CLI does not show the string value in the CPP program
我想通过CLI/c++从c++调用一个c#函数。
c#代码private string _text = " ";
public void setText(string text)
{
// _text = text;
_text = "HI World";
}
理想情况下setText应该只有注释行。_text = "HI World"就是一个例子。
public string getText()
{
return _text;
}
c++/CLI代码头:gcroot<Bridge> _managedObject;
virtual void setText(std::string text);
virtual std::string getText();
CPP文件std::string CStringBridge::getText()
{
//_managedObject = gcnew Bridge();返回(marshal_as (_managedObject -> getText ()));}
void CStringBridge::setText(std::string text)
{
//_managedObject = gcnew Bridge();_managedObject -> setText (gcnew系统::String (text.c_str ()));}
IStringBridgeWrapper* IStringBridgeWrapper::CreateInstance(void)
{
return ((IStringBridgeWrapper *)new CStringBridge());
}
注意:当我使用以下代码
时virtual void setText(System::String^ text);
virtual System::String^ getText();
我得到以下错误3395
*__declspec(dllexport)不能应用于具有__clrcall调用约定的函数*
,所以我坚持使用std::string
当我从c++/CLI代码中使用库,并从我的c++程序中调用时,应该打印"Hi World";相反,什么也不打印
c++控制台应用程序
IStringBridgeWrapper *pBridge = IStringBridgeWrapper::CreateInstance();
pBridge->setText(std::string("I am here"));
pBridge->getText();
我认为字符串没有被正确传递。
任何解决这个问题的想法都将受到欢迎。
编辑
我已经在注释之后更新了代码,但是什么也没有显示。
gcroot创建一个句柄,但不为它分配内存。但是由于Bridge没有分配内存,因此应用程序无法工作。我的代码在文章的同一行- http://www.codeproject.com/Articles/10020/Using-managed-code-in-an-unmanaged-application。
COM是你的朋友
用c#创建一个接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CsharpLibrary
{
// Since the .NET Framework interface and coclass have to behave as
// COM objects, we have to give them guids.
[Guid("56A868B1-0AD4-11CE-B03A-0020AF0BA770"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IStringHolder
{
String GetText();
void SetText(String s);
}
}
实现c#接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CsharpLibrary
{
[Guid("C6659361-1625-4746-931C-36014B146679")]
public class MyStringHolder : IStringHolder
{
String _text;
public String GetText()
{
return this._text;
}
public void SetText(String value)
{
_text = value;
}
}
}
从c++中创建并调用c#对象
#include <windows.h>
#include <stdio.h>
#pragma warning (disable: 4278)
// To use managed-code servers like the C# server,
// we have to import the common language runtime:
#import <mscorlib.tlb> raw_interfaces_only
#pragma warning (disable: 4278)
// To use managed-code servers like the C# server,
// we have to import the common language runtime:
#import <mscorlib.tlb> raw_interfaces_only
#import "..CsharpLibrarybinDebugCsharpLibrary.tlb" no_namespace named_guids
int main(int argc, char* argv[])
{
HRESULT hr = S_OK;
IStringHolder *pStringHolder = NULL;
//
// Initialize COM and create an instance of the InterfaceImplementation class:
//
CoInitialize(NULL);
hr = CoCreateInstance( __uuidof(MyStringHolder),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IStringHolder),
reinterpret_cast<void**>(&pStringHolder));
if(SUCCEEDED(hr))
{
_bstr_t sHelloWorld = SysAllocString( L"Hello, World" );
hr = pStringHolder->SetText(sHelloWorld);
SysFreeString(sHelloWorld);
}
//
// Be a good citizen and clean up COM
//
CoUninitialize();
return hr;
}
在c#方面,你必须生成类型库并通过构建后事件注册类:
生成类型库:"$(FrameworkSDKDir)binNETFX 4.0 Toolstlbexp.exe" "$(TargetPath)"/: " (TargetDir)名为. tlb (TargetName)美元"
注册类:C:Windows Microsoft.NET Framework v4.0.30319 RegAsm.exe " $(定位路径)"
享受吧!
我想通过CLI/c++从c++调用一个c#函数。
等等……你想从c#中调用c++函数,对吗?这就是c++/CLI的优点所在。包装c++代码,使其可在托管环境中访问。如果你真的想要从c++调用c#代码,你应该查看COM注册你的c#代码。如果你使用c++/CLI,你的整个c++程序将被拖入。net的世界,你可以从一开始就使用c#。
在c++/CLI中,ref (. net)类的整个公共类接口应该只由托管类型组成。应该是System::String^
,而不是std::string
。
- 声明要在 qml 和 cpp 文件中使用的全局字符串
- 我如何在函数 cpp 中返回字符串
- 在.cpp文件之间传递全局字符串变量?
- CPP 中的字符串加法关联性
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 在Qt-cpp中通过像素获取QString的子字符串
- 使用Regex解析cpp中的字符串并创建映射
- 在 cpp 自己的版本中对字符串进行哈希处理
- 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的time_t格式?
- 标记字符串,接受CPP中给定字符集之间的所有内容
- 如何在 CPP 中将 Typedef basic_string字符串转换为<char>字节数组
- CPP - strlen 不会输出多个单词的整个长度 [仅限 C 字符串]
- 如何在yaml-cpp中写一个版本字符串作为文字(不是字符串)
- CPP Windows 字符串转换混淆了 CStringA 和 LPCWSTR
- 如何使用 cpp 将 z3 表达式转换为字符串
- 在CPP中消毒密码字符串
- 提升正则表达式 cpp 用于查找 %% 之间的字符串,输出不包括 % 字符本身
- CPP中的映射字符串
- 使用文件名作为字符串从任意souce .cpp文件中调用函数