【问题标题】:c++ call a c# DLL with Hashtable parameterc++ 调用带有 Hashtable 参数的 c# DLL
【发布时间】:2013-07-14 14:38:06
【问题描述】:

我正在尝试使用 CLI 作为此链接从 C++ 调用 C# DLL:http://tom-shelton.net/index.php/2008/11/01/calling-managed-code-from-a-dll-created-in-visual-c-2008/

一切似乎都很好。

但是如果C#函数有Hashtable参数,我就不知道怎么调用了。 像这样的 C# 函数:

public void DoSomething(Hashtable htb,int,string etc)

请帮助我如何在 C++ 中使用这种 C# 函数。

最好的问候

约翰

【问题讨论】:

  • 发布您已经拥有的代码、您尝试过的内容、无效的内容等。
  • map 或 unordered_map 不起作用。当我尝试调用
    unordered_map htb;
    DoSomething(htb,int,string etc);
    它发出错误 Error error C2664: 'void DoSomething(System::Collections::Hashtable ^,int,string,etc)' : 无法将参数 1 从 'std::tr1::unordered_map<_kty>' 转换为 'System::Collections::Hashtable ^'

标签: c# c++ hashtable managed


【解决方案1】:

尝试了几次失败后,我决定这样解决 我声明一个类

Class WrappedWhateverClass
{
private:
gcroot <CSharpClass ^> _caller;
public:
    gcroot <Hashtable^> htb;
void WrappedWhateverClass()
int DoSomethinginC( int , string, etc);
}

在代码中,我调用了 C# DoSomething 函数:

void WrappedWhateverClass::WrappedWhateverClass()
{
htb = gcnew Hashtable();
}
int WrappedWhateverClass::DoSomethinginC( int i, string str, etc)
{
_caller->DoSomething(htb,i,str, etc);
}

【讨论】:

    【解决方案2】:

    如果您使用 CLR 选项,那么您的 c++ 代码将成为托管代码。所以你可以使用

    System::Collections::Hashtable
    

    在你的 C++ 代码中使用 Hashtable

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      相关资源
      最近更新 更多