如何从c#程序调用c ++函数? [英] How to call c++ functions from c# program?

查看:111
本文介绍了如何从c#程序调用c ++函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++开发了一个软件。

现在我正在尝试为它添加GUI。

使用Windows窗体。

我明白不建议用C ++写它

所以我开始用C#编写它。

这是一个托管代码。

然后我必须调用我的软件中的函数来执行处理和计算。

我明白我必须使用CLI托管代码包装C ++本机代码。

我该怎么做?以及如何传输参数?我需要一个代码示例的完整说明。


1.如何定义包装函数的原型? (字符串声明,如System :: String)

2.如何将System :: String从包装类函数转换为本机C ++函数?


3.什么是Invoke? PInvoke?


4. System :: String是否有任何C#方法将其转换为字符数组字符串并通过其他方式



谢谢。

I have developed a software in C++.
Now I am trying to add GUI for it .
using windows forms.
I understand that it is not recommended to write it in C++
So I have started to write it in C#.
This is a managed code.
Then I have to call the functions in my software to perform the processing and computing .
I understand that I have to wrap the C++ native code with a CLI managed code.
How do I do it ? and How do I transfer the parameters ? I need clear full instructions with a code sample.

1. How do I define the prototypes of the wrapping functions ? (strings declarations like System::String )
2. How do I convert System::String from the wrapper class functions to the native C++ functions ?

3. What is Invoke ? PInvoke ?

4. Is there any C# method for System::String to convert it to characters array string and via versa


Thanks.

推荐答案

如果您已将功能构建到DLL中,请确保已将其编译为COM / ActiveX对象。这样,您可以在C#项目中添加对它的引用,并且所有类将自动包装在C#代码中。或者,您可以使用C#中的System.Runtime.InteropServices命名空间使用DLLImport从DLL加载函数并手动包装函数。如果您选择这样做,则需要查看编组非托管类型。
If you''ve built your functionality into a DLL, ensure that you have compiled it as a COM/ActiveX object. That way you can add a reference to it within your C# project and all of the classes will automatically be wrapped in C# code. Alternatively, you can use the System.Runtime.InteropServices namespace within C# to load functions from your DLL using ''DLLImport'' and wrapping the functions manually. If you choose to do it this way, you need to look into Marshalling unmanaged types.


1。我可以获得更多细节和代码吗?

我还是根本不知道怎么做。


我可以更改我的软件,或写一些新功能,
如果你告诉我
- 没问题。


2.在这两种方式中我的软件都成了dll?


3.我是否必须在C#extern f(...中声明) )对于我的函数?

4.是真的,c#只能调用类方法,而不是正常函数?

5.如何在两者之间传递字符串他们?
1. can I get more details and code ?
I still don''t know how to do it at all.

I can change my software , or write some new functions,
if you tell me - no problem.

2. In both ways my software becomes the dll ?

3. do I have to declare in the C# extern f (..) for my functions ?
4. Is it true, that c# can call only class methods , and not "normal functions ?
5. How do I pass strings between them ?


我不能粘贴代码,你无法复制和粘贴。如果从MSDN中找到关于InteropServices的信息,那就更好了。


它们使用DLLImport属性声明为原型:


[DLLImport(" SomeDLL.DLL")]

public static extern void RandomFunction(int Parameter);


此函数不必在类中。您对正常的看法函数将是一个简单的C函数,它只是一个静态函数。如果一个静态函数在一个类中,那么类名就像一个名称空间来包含该函数。
I cant just paste code, you get nowhere from copying and pasting. Its better if you find out from MSDN about InteropServices.

They are declared as prototypes using the DLLImport attribute:

[DLLImport("SomeDLL.DLL")]
public static extern void RandomFunction(int Parameter);

This function doesn''t HAVE to be in a class. Your perception of a "normal" function would be a plain ''C'' function which is just a static function. If a static function is within a class, the class name just acts like a namespace to contain the function.


这篇关于如何从c#程序调用c ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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