将整个c / c ++无人值守代码项目用于manged代码c# [英] Using a whole c/c++ unmanged code project into manged code c#

查看:57
本文介绍了将整个c / c ++无人值守代码项目用于manged代码c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功创建了一个非托管c ++文件并将其用于我的c#应用程序,并使用dll方法导入并为其创建包装类。



现在我创建了一个用c / c ++创建的整个项目,它使用各种.lib,.in,.def,.asm,第三方dll和lib以及我的c和头文件。



现在我的c / c ++项目执行非常特定的耗时任务,c ++比c#提供更好的内存优化和速度,否则我会用c#创建它。 />


现在我有几个问题我需要帮助



1)如果我有两个或更多的文件非托管代码(c / c ++)但我想只使用一个文件,我可以使用它而不需要对其他文件进行任何必要的更改,我不需要在c#中访问它们。我将如何解决这个问题,如果我只围绕我的c ++代码的一个文件创建一个包装类,它反过来引用其他文件,或者我应该导出该文件的少数函数并再次保持其他文件不变(如果我可以做的话)那个)。



2)当我使用上述问题的灵魂时,.lib,.in,。asm等各种文件在我的过程中扮演什么角色?将它们用于托管代码。



3)我应该将项目的dependecies移除到其他dll和.lib,或者将它们组合成一个dll,它引用我项目的所有资源或各种依赖项(正在做这样的事情是有效的)



4)如何在如此大规模的无人管理代码和托管代码之间进行通信。



任何帮助都会受到赞赏。

i have successfully created a single file of unmanaged c++ and used it into my c# application with both methods dll import and by creating a wrapper class for it.

Now i have a created a whole project in c/c++ that uses various .lib, .in, .def,.asm, third party dll's and lib's along with my c and header files.

Now my c/c++ project performs very specific time consuming tasks, c++ offers better memory optimization and speed than c#, otherwise i would have created it in c#.

now i have few question's for which i need help

1)If i have two or more files of unmanaged code (c/c++) but i want to only use one file, can i use it without making any necessary changes to the other files which i do not require to be accessed in c#. how would i go about this, should i create a wrapper class around only one file of my c++ code which in turns refer to other files, or should i export the few functions of that file and again leave others files unchanged(if i could do that).

2)when using the soultion of my above question what role would the various files like .lib, .in , .asm would play during my process of using them into managed code.

3)should i remove the dependecies of project to other dll and .lib's or combine them into one dll which references to all of the resources or various dependencies of my project( is doing such a thing is efficient)

4)also how would the communication would take place between my unmanged and managed code on such a large scale.

any help would be appreciated.

推荐答案

在托管中使用非托管代码不是一个大问题,但这是一个非常不愉快的问题,使用P / Invoke和复杂的编组。你有一个非常好的机会摆脱撞击这些问题,真正无痛地解决问题,但看起来你无视它。



机会是,你工作使用Visual Studio和Microsoft编译器。您可以执行以下操作:使用C ++ / CLI编写代码,而不是普通C ++。您可以创建一个混合模式(托管+非托管)项目,该项目将生成二进制使用的可执行模块:它可以用作常规的非托管DLL;同时,它将扮演.NET程序集的主要模块的角色,可以被其他.NET程序集使用。



在混合模式下如果只使用 public ,您可以将您希望在托管ref类和结构中重用的现有C ++类包装在将引用程序集的程序集中。访问要公开的类型及其成员的说明符。



请参阅:https://msdn.microsoft.com/en-us/library/ms235282.aspx [ ^ ]。



另请参阅我过去的答案:

如何调用C ++ DLL ASP.NET? [ ^ ],

处理Windows窗体应用程序 [ ^ ],

参见:如何在C#中发出哔哔声 [ ^ ]。



关于C ++ / CLI的一些阅读:

http://en.wikipedia.org/wiki/C%2B%2B/ CLI [ ^ ],

http://www.ecma-international.org/publications/标准/ Ecma-372.ht m [ ^ ],

http://www.gotw.ca/publications/C++ CLIRationale.pdf [ ^ ] ,

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



它是否涵盖了您的所有问题?也许只需要一个额外的注意事项:删除任何寄生依赖关系对于任何产品的健康至关重要。



-SA
Using unmanaged code in managed is not a big problem, but this is a very unpleasant problem, using P/Invoke and perhaps complex marshaling. You have a very good opportunity to get away from ramming such problems, to resolve problems really painlessly, but it looks like you are ignoring it.

Chances are, you work with Visual Studio and Microsoft compilers. Here is what you can do: write your code in C++/CLI, not "plain" C++. You can create a mixed-mode (managed+unmanaged) project which would produce an executable module of binary use: it can be used as a regular unmanaged DLL; at the same time, it will play the role of the main module of a .NET assembly which can be used by other .NET assemblies.

In a mixed-mode project, you can wrap your existing C++ classes you want to reuse in the managed "ref" classes and structure which will be exposed to the assemblies referencing your assemblies if you just use public access specifiers to the type and their members you want to expose.

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

See also my past answers:
How to invoke C++ DLL in ASP.NET?[^],
Dealing with windows form application[^],
see also: How to play beeps in C#[^].

Some reading on C++/CLI:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
https://msdn.microsoft.com/en-us/library/dtefa218.aspx[^].

Did it cover all your questions? Maybe it will take just one extra note: removing any parasitic dependencies is critically important for the health of any products.

—SA




据我所知,基本上你的问题陈述是:

在Unmanaged C ++中有一个项目。您想在C#应用程序中调用此项目中的函数。

以下是步骤:



编写复杂的常见做法或者耗时的C ++算法代码,以利用它提供的速度和效率。

然后从C ++代码导出函数,模块将向其他模块显示。

然后构建这个模块是一个.dll。

然后将此.dll的引用添加到C#或VB应用程序。

通过调用.dll公开的函数来消耗C ++ .dll公开的功能。 br />
试图详细介绍每一步都会耗费大量时间。您将需要谷歌获取有关任何步骤的更多详细信息。

现在有不同的方法从VC ++ dll导出函数。



1 )。文件数无关紧要。最好将您的C ++项目构建为dll。 这是创建VC ++ dll的简单演练 [ ^ ]。

请在这些行上更多Google 。仅导出将从C#应用程序中使用的函数。您可以从同一文件或单独的文件中导出函数。您只能从1个文件导出函数,而其他文件不会受到干扰。



2)。所有.lib,.in,.def,.asm和第三方dll都将成为您将构建的新VC ++ DLL的依赖项。尝试了解.lib文件和.def文件是什么。 C#应用程序无需担心这些问题。但要正常工作,vc ++ dll将需要在定义的位置使用其他二进制文件。谷歌更多关于如何管理DLL的依赖关系。



3)。可以将依赖项放在单独的模块中。我认为这更像是一个设计决策,只有你知道你的项目有多大或多,才能制作。



4)。我不知道你的项目规模。如果你对调用C ++ dll的延迟感到困扰,那么当代码工作时更好地担心这些优化。



这里有一些有用的链接:

如何制作C ++类 [ ^ ]

HowTo:从DLL导出C ++类 [< a href =http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL\"target =_ blanktitle =New Window> ^ ]

公开本地管理 - C ++ / CLI与P / Invoke [ ^ ]



写入-d-in-cc-for-net-互操作性 [ ^ ]



我试着给你指点。一个详细的答案将是非常冗长的。



希望它有所帮助!!
Hi,
as i understand, basically your problem statement is:
There is a project in Unmanaged C++. You wan to call the functions in this Project in your C# application.
Here are the steps:

It is common practice to write complex or time consuming algorithmic code in C++, to leverage the speed and efficiency it offers.
Then export functions from C++ code which the module will reveal to other modules.
Then build this module as a .dll .
Then add the reference of this .dll to the C# or VB application.
Consume the functionality exposed by C++ .dll by calling the functions exposed by the .dll.
Trying to cover every step in detail will consume a lot of time. You will have to google for more details on any step.
Now there are different methods of exporting functions from a VC++ dll.

1). Number of files do not matter. Its better to build your C++ project as dll . here is a simple walkthrough for creating VC++dll[^].
Kindly google more on these lines. Export only the functions that you will consume from C# application. You can export functions from same file or from separate files. You can export functions only from 1 file while other files will not get disturbed.

2). All the .lib, .in, .def,.asm, and third party dlls will become a dependency for the new VC++ dll you will build. Try to understand what a .lib file and .def file are. The C# application need not bother about these. But to work correctly, the vc++ dll will need these other binaries in a defined location. Google more about how to manage dependencies of the dll.

3). Putting the dependencies in a separate module can be done . I think this is more of a design decision which only you can make since you know how large or complex your project is.

4). I do not know the scale of your project. If you are bothered about latency in calling the C++ dll, then better worry about these optimizations when you have the code working.

here are a few useful links:
How to Marshal a C++ Class[^]
HowTo: Export C++ classes from a DLL[^]
Exposing native to managed - C++/CLI vs. P/Invoke[^]

writing-a-dll-in-c-c-for-net-interoperability[^]

I just tried to give you pointers. A detailed answer would be very lengthy.

hope it helps !!


你创造了一些混乱 - 现在你得到清理。使用不同语言并最终尝试将它们混合起来总是一个坏主意。



1.使用类或命名空间来分离和清理代码。

2.尽可能少地包含生成的C ++内容并通过C#使用它们。

3.是的!是!是!每个被删除的依赖项都会让生活更轻松

4.您可以使用回调和委托来了解正在发生的事情。



这里有一些<一个href =>示例项目如何完成。
You created somehow a mess - now you got to cleanup. It is always a bad idea to use different languages and trying to mix them up in the end.

1. use classes or namespace to separate and cleanup your code.
2. include the resulting C++ stuff in as little dll as possible and use them via C#.
3. YES! YES! YES! every removed dependency makes life easier
4. You can use callbacks and delegate to get informed what is going on.

Here is some sample project how it can be done.


这篇关于将整个c / c ++无人值守代码项目用于manged代码c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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