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

本文关键字:字符串 CPP 程序 显示 CLI c++ | 更新日期: 2023-09-27 18:06:06

我想通过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。

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

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 "..'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)"/: " (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