如何在C#.net代码中调用c ++ dll [英] How to call a c++ dll in C#.net code

查看:60
本文介绍了如何在C#.net代码中调用c ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我想在C#.Net应用程序中使用C ++ DLL。如何在C#.net代码中使用C ++ DLL。如果我可以使用多种方式,这是使用C ++ DLL的最佳方式。



先谢谢

HI ,
I want to use a C++ dll in C#.Net application.How can i use C++ dll in my C#.net code. if i can use more than one way which is the best way to use C++ dll .

Thanks in Advance

推荐答案

有几种方法可以做到这一点。



PInvoke

围绕C ++本机代码创建C ++ / CLI包装器(make static C ++本机代码库和C ++ / CLI生成的程序集可以很容易地在.net应用程序中使用。

COM,即使用interop(在所有选项中很难)

在我的建议中,最简单的方法是使用选项2,但是你需要注意正确的编组。
There are several ways of doing it.

PInvoke
Create C++/CLI wrapper around your C++ native code (make static library out of C++ native code) and C++/CLI generated assembly can be easily utilized in .net application.
COM, i.e using interop (which is difficult among all the options)
In my suggestion easiest way is to use option 2, but you need to take care of proper marshaling.


P / Invoke可以用来从本机DLL调用独立函数。但是如果你想使用整个课程,那么你应该采用另一种方法。最好的方法可能是在本机代码上编写C ++ / CLI包装器,并向托管应用程序公开所需的功能。另一种方法,虽然我觉得更难,但是在你的本机DLL周围写一个COM包装器。然后可以从托管应用程序中使用此COM对象。
P/Invoke can be used to call stand-alone functions from native DLLs. But if you want to use entire classes, when you should go with another approach. The best would probably be to write a C++/CLI wrapper over your native code and expose the desire functionality to the managed application. Another approach, though I find it harder, is to write a COM wrapper around your native DLL. This COM object can then be consumed from your managed application.


以下是要遵循的步骤。



1.打开Visual C ++ Win 32项目

2.在应用程序设置中选择Dll&选择空项目

3.在源文件夹中添加新项目(*。cpp)

4.选择cpp文件

5.记下cpp文件中的简单代码:



#include< stdio.h>



externC

{

__declspec(dllexport)int add(int a,int b)

{

返回+ b;

}

__declspec(dllexport)int subtract(int a,int b)

{

return ab;

}

}

6.编译完成后,您将看到Debug文件夹中包含example.dll和example.lib文件。 />
7.然后创建c#console应用程序

8.代码如下



[DllImport(example.dll ,CallingConvention = CallingConvention.Cdecl)]

public static extern int subtract(int a,int b);

private void button2_Click(object sender,EventArgs e)

{

int x = Convert.ToInt32(textBox1.Text);

int y = Convert.ToInt32(textBox2.Text);

int z = subtract(x,y);

MessageBox.Show(必填答案是+ Convert.ToString(z),答案,MessageBoxButtons.OK,MessageBoxIcon.Information);

}

9.如果.net代码的早期版本将是(3.5)

Below are the steps to follow.

1. Open Visual C++ Win 32 Project
2. In Application Setting Chose Dll & select Empty Project
3. Add New Item(*.cpp) in Source Folder
4. Select cpp File
5. Write Down the simple Code in cpp file:

#include <stdio.h>

extern "C"
{
__declspec(dllexport) int add(int a,int b)
{
return a+b;
}
__declspec(dllexport) int subtract(int a,int b)
{
return a-b;
}
}
6. After Compile you will see the Debug Folder have example.dll and example.lib files.
7. Then create c# console application
8. Code is as follows

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int subtract(int a, int b);
private void button2_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int y = Convert.ToInt32(textBox2.Text);
int z = subtract(x, y);
MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
9. In case of Previous Version of .net Code will be (3.5)
[DllImport("example.dll")]
        public static extern int subtract(int a, int b);
        private void button2_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(textBox1.Text);
            int y = Convert.ToInt32(textBox2.Text);
            int z = subtract(x, y);
            MessageBox.Show("Required Answer is " + Convert.ToString(z), "Answer", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }





10.对于.net Frame Work 4,代码将是,



[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]

public static extern int add(int a,int b);



注意:输出将是相同的两个



现在你可以从你的C#应用​​程序(.net框架)调用一个C ++ DLL工作4)



10. For .net Frame Work 4 the code will be,

[DllImport("example.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int add(int a,int b);

Note: Out put will be same for both

Now you can call an C++ dll from your C# application (.net frame work 4)


这篇关于如何在C#.net代码中调用c ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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