不使用COM从VC ++ exe访问c#dll的函数 [英] Access function of c# dll from VC++ exe without using COM
问题描述
嗨朋友们,
我想做一个简单的项目,其中VC ++ exe调用C#dll的函数而不使用COM功能。我在网上搜索但没有找到更有用的东西。请建议我如何做这个以及一些链接供参考。
提前致谢
Hi friends,
I want to make a simple project in which VC++ exe calls C# dll's function without using COM functionality. I search it on net but nothing more useful get found. Please suggest me how to do this and also some links for reference.
Thanks in advance
推荐答案
此 [ ^ ]你可能会感兴趣。
This[^] could interest you.
首先在同一个解决方案中创建一个静态库项目并启用Common Language Runtime支持。不要打开EXE项目的CLR支持。
在库项目中添加一个类(.h + .cpp)。然后将所需的函数添加到类中,并仅在.cpp中实现它们。
注意:不应在.h文件中使用任何.NET框架类。例如,取一个函数
First create a static library project in the same solution and turn on Common Language Runtime support. Don't turn on CLR support for the EXE project.
Add a class to the library project(.h + .cpp). Then add your necessary functions to the class and implement them only in the .cpp.
Note: You shouldn't use any .NET framework classes in .h file. For example, take a function
void ShowSomeMessageBox();
让其实现,
Let its implementation be,
void YourClassName::ShowSomeMessageBox()
{
System::Windows::Forms::MessageBox::Show("Hello World!");
}
这里声明的方法没有.NET对象作为参数。那就是你可以拥有数据类型,如 char
, int
或 std :: string
作为参数,而不是 System :: String
。
之后,在EXE项目属性中添加对库项目的引用。然后添加保存头文件的路径。然后 #include
头文件。并且您可以使用ShowSomeMessage()方法,而不会将整个EXE项目源暴露给CLR。
注意:此技巧仅适用于Visual C ++。
Here the declaration doesn't of the method has no .NET objects as parameters. That is you can have data types like char
, int
or std::string
as the parameters and not System::String
for example.
After this, add reference to the library project in the EXE project properties. Then add the path where you've saved the header file. Then #include
the header file. And you can use ShowSomeMessage() method without exposing your whole EXE project source to CLR.
Note: This trick will only work in Visual C++.
我使用将C#/ VB.NET中的函数导出为本机代码 [ ^ ] for我需要与第三方C#API连接的项目。我创建了一个C#包装器DLL并插入了入口点。 DLL简单地称为C#API函数。
I used Exporting functions in C#/VB.NET to native code[^] for a project that I needed to interface with a third party C# API. I created a C# wrapper DLL and inserted entry points. The DLL simply called the C# API functions.
这篇关于不使用COM从VC ++ exe访问c#dll的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!