gpt4 book ai didi

c# - 将 C# 字符串传递给 C++/CLI 不会在 CPP 程序中显示字符串值

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:05 25 4
gpt4 key购买 nike

我想通过 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 System::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 是您的 friend :

在C#中创建一个接口(interface)

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#接口(interface):

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 "..\CsharpLibrary\bin\Debug\CsharpLibrary.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)bin\NETFX 4.0 Tools\tlbexp.exe""$(TargetPath)"/out:"$(TargetDir)$(TargetName).tlb"

注册类(class):C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe "$(TargetPath)"

尽情享受吧!

关于c# - 将 C# 字符串传递给 C++/CLI 不会在 CPP 程序中显示字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15635050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com