在C#应用程序中使用C ++ dll [英] Using a C++ dll in a C# application

查看:116
本文介绍了在C#应用程序中使用C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C ++作为类库编写一个dll,并在C#程序中调用它的方法.

请注意,在VS 2010 C ++ Express版本中,没有MFC DLL.我必须将我的方法放在C ++类中,然后使用类库"选项将其编译为DLL.

同样,如何在C#中调用这些C ++方法.代码片段,网站或在搜索引擎中键入的确切内容将不胜感激.

我正在将VS 2010 Express用于C ++和C#.

I want to write a dll in C++ as a class library and call it’s methods in a C# program.

Note, with the VS 2010 C++ Express edition there is no MFC DLL. I have to put my methods in a C++ class and compile it to a DLL using the Class Library option.

Again, how do I call those C++ methods in C#. A code snippet, a website, or exactly what to type into my search engine would be appreciated.

I''m using VS 2010 Express for both C++ and C#.

推荐答案

好的. "用C ++编写(但是如果您要使用VC ++,则将对其进行管理.)

现在,在创建DLL文件(并记得将其保存为.DLL)之后,您将使用.NET的System.Runtime.InteropServices(处理托管-非托管代码互操作性)命名空间来进行导入

所以首先..将System.Runtime.InteropServices命名空间添加到您的代码中.
然后按照下面的代码示例进行操作.该示例导入一个名为msvcrt的DLL,并访问其中名为puts(string _c)
int类型化方法.
-请记住,您要为称为的每个方法进行DLL导入-

ok first..C# has support for unmanaged code (which your case will be) since you''re writing it in C++ (but if you''ll be using VC++, It will be managed then).

Now after creating the DLL file (and pls remember to save it as a .DLL), you''ll use the System.Runtime.InteropServices (which handles managed-unmanaged code interoperability) namespace of .NET to do your import

so first..add the System.Runtime.InteropServices namespace to your code..

and then follow the code sample below..which imports a DLL named : msvcrt and access the int typed method in it named : puts(string _c)

--remember that you do the DLL imports for each method called--

// PInvokeTest.cs
using System;
using System.Runtime.InteropServices;

class PlatformInvokeTest
{
    /* DLL Import with 1st method */
    [DllImport("msvcrt.dll")]
    public static extern int puts(string c);

    /* DLL Import with 2nd method */
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();

/* Methods in DLL being used below  */
    public static void Main()
    {
        puts("Test");
        _flushall();
    }
}


但我必须补充一点,这取决于您要对DLL进行的操作,您可以做很多事情(可选),例如Marshalling,分配Entry Point等.因此,我建议您阅读上面有一些文章.在线有很多

如果您喜欢上述内容,请接受答案并投票..:)


but I must add that depending on what you want to do with the DLL, there are quite a number of things you can do (optional), such as Marshalling, assigning Entry Point, etc..So I would suggest you read some articles on it..There are loads of them online

If you like the above, Accept Answer and give a Vote.. :)




你是说MC ++吗?!
如果是这样,您可以轻松地在c#中引用它并使用它.
但是,如果您想用VC ++写一个dll(我的意思是非托管代码),那么您就必须按照Lantei所说的去做.
Hi,

do you mean MC++?!
If so you can easily reference it in c# and use it.
But if you want to write a dll in VC++ ( I mean unmanaged code), then you have to do what Lantei said.


下面是C ++部分.

Visual Studio 2010 C ++
新项目
Win32控制台应用程序
名称:计算器
应用程序类型:DLL


//Calculator.cpp:定义DLL应用程序的导出函数.


#include stdafx

静态double加(double x,double y)
{
返回(x + y);
}
上面的代码成功构建为"calculator.dll"

-------------------------------------------------- --------

以下是C#代码
//Form1.cs
使用System.Windows.Forms;
使用System.Runtime.InteropServices;

命名空间WindowsFormsApplication1
{
公共局部类Form1:Form
{

[DllImport("calculator.dll",EntryPoint ="Add")]
public static extern double Add(double x,double y);



公共Form1()
{
InitializeComponent();
}

private void button1_Click(对象发送者,EventArgs e)
{

label1.Text = Add(2.2,3.4).ToString();
}

}
}

C#程序已构建并运行,直到我单击了button1.生成了以下异常:
"EntryPointNotFoundException未处理"

在DLL‘calculator.dll''中找不到名为添加"的入口点.
Below is the C++ part.

Visual Studio 2010 C++
New project
Win32 Console Application
Name: calculator
Application type: DLL


// calculator.cpp : Defines the exported functions for the DLL application.


#include stdafx

static double Add (double x, double y)
{
return (x+y);
}
The above builds successfully as "calculator.dll"

----------------------------------------------------------

Below is the C# code
// Form1.cs
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

[DllImport("calculator.dll", EntryPoint = "Add")]
public static extern double Add(double x, double y);



public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

label1.Text = Add(2.2, 3.4).ToString();
}

}
}

The C# program built and ran till I clicked on button1. The following exception was generated:
"EntryPointNotFoundException was unhandled"

Unable to find an entry point named Add in DLL ‘calculator.dll''


这篇关于在C#应用程序中使用C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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