如何将c#app中的字符串传递给c ++ cli dll中的类ctor? [英] How do I pass a string from a c# app to class ctor in a c++ cli dll?

查看:96
本文介绍了如何将c#app中的字符串传递给c ++ cli dll中的类ctor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2个项目的VS2013解决方案。一个是c ++ / cli dll。另一个是c#控制台应用程序。 dll包含一个类,其ctor采用'const WCHAR *'参数。在c#app中,我尝试创建该类的实例。我使用字符串文字作为ctor参数。构建错误是:



'MyLib.MyClass.Myclass(char *)'的最佳重载方法匹配有一些无效的参数



我有2个问题。



1.为什么VS2013认为参数是'char *'而不是'WCHAR *'?



2.如何将c#中的字符串传递给c ++?



这项工作的目的是查看使用c ++ / cli dll是否比使用dllimport / pinvoke更容易。

解决方案

如果这是C ++ / CLI,您可以编写托管CLI代码, ,基本上和C#一样。您只需要使用托管ref类。而不是 WCHAR * ,使用 System :: String ^ 。这些类型(如果你为它们及其一些成员使用 public 访问修饰符)将以与C#或VB.NET完全相同的方式暴露给引用程序集。 br />


您正在尝试对非托管类使用显式P / Invoke。

  1. VS2013不会思考任何内容;目前尚不清楚你是如何面对这样的事情的;但是,默认编组可以将 char * 编组为 System.String 。一般情况下,您需要使用 MarshalAs 属性: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute%28v=vs.110%29.aspx [ ^ ]。
  2. 我解释了上面的想法。但是你真的不需要将 System :: String 传递给C ++。使用托管类将其传递给C ++ / CLI。如果您必须继续使用C ++非托管代码,则可以使用ref托管类型进行封装。换句话说,在C ++项目中实现托管到非托管绑定的更加可维护的方式,而不是在C#项目中,没有明确使用P / Invoke。



    请参阅:

    https://msdn.microsoft.com/en-us/library/ ms235282.aspx [ ^ ],

    https://msdn.microsoft.com/en-us/ library / 2x8kf7zx.aspx [ ^ ],

    https://msdn.microsoft.com/en- us / library / eyzhw3s8.aspx [ ^ ]。





另见我过去的答案:

如何在ASP.NET中调用C ++ DLL? a> [ ^ ],

交易使用Windows窗体应用程序 [ ^ ],

如何在托管c ++类中捕获本机c ++异常? [ ^ ]。



-SA

I have a VS2013 solution with 2 projects. One is a c++/cli dll. The other is a c# console app. The dll contains a class whose ctor takes a 'const WCHAR *' parameter. In the c# app I tried to create an instance of the class. I used a string literal for the ctor parameter. The build error is:

The best overloaded method match for 'MyLib.MyClass.Myclass(char*)' has some invalid arguments

I have 2 questions.

1. Why does VS2013 think the parameter is 'char*' instead of 'WCHAR*'?

2. How can I pass a string from c# to c++?

The purpose of this work is to see if using a c++/cli dll is easier than just using dllimport/pinvoke.

解决方案

If this is C++/CLI, you can write managed CLI code which is, basically, the same as C#. You just need to use managed "ref" classes. Instead of WCHAR*, use System::String^. Such types (if you use public access modifiers for them and some of their members) will be exposed to the referencing assembly in exact same ways as in C# or VB.NET.

You are trying to use explicit P/Invoke for unmanaged classes.

  1. VS2013 does not "think" anything; it's not clear how you face such thing; however, default marshaling can marshal char* as System.String. In general case, you need to use MarshalAs attribute: https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute%28v=vs.110%29.aspx[^].
  2. I explained the idea above. But you don't really need to pass System::String to C++. Pass it to C++/CLI, using a managed class. If you have C++ unmanaged code you just have to continue to use, you can wrap in in your "ref" managed types. In other word it's much more maintainable way to implement managed to unmanaged binding inside C++ project, not inside C# project, without explicit use of P/Invoke.

    Please see:
    https://msdn.microsoft.com/en-us/library/ms235282.aspx[^],
    https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx[^],
    https://msdn.microsoft.com/en-us/library/eyzhw3s8.aspx[^].



See also my past answers:
How to invoke C++ DLL in ASP.NET?[^],
Dealing with windows form application[^],
How do I catch native c++ exception in managed c++ class?[^].

—SA


这篇关于如何将c#app中的字符串传递给c ++ cli dll中的类ctor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆