如何使用在C#程序中用Visual C ++编写的DLL? [英] How to use a DLL written in Visual C++ in a C# program?

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

问题描述


可能重复:

C#P\Invoke DLL没有进入C ++的入口点?


我在对SO和google进行了一次彻底的冲浪之后提出这个问题,大多数的答案让我大约80%的方式,但它仍然有点混乱,



我有一些Visual C ++函数定义如下:






MyDLL.h

  #ifdef FUNCTIONS_EXPORTS 
#define FUNCTIONS_API __declspec(dllexport)
#else
#define FUNCTIONS_API __declspec(dllimport)
#endif

命名空间函数{
class MyFunctions {
public:
static FUNCTIONS_API int Add(int a,int b);
static FUNCTIONS_API int Factorial(int a);
};
}






MyDLL。 cpp

 命名空间函数{
int MyFunctions :: Add(int a,int b)
{
return a + b;
}
int MyFunctions :: Factorial(int a)
{
if(a< 0)
return -1;
else if(a == 0 || a == 1)
return 1;
else
return a * MyFunctions :: Factorial(a-1);
}
}






现在,我想把这个生成的DLL导入我的C#程序,如下:



Program.cs
$ b

 使用System; 
using System.Collections.Generic;
using System.Runtime.InteropServices;

命名空间DLLTester
{
class Program
{
[DllImport(path\\to\\the\dll\ \myDLL.dll)]
public static extern int Factorial(int a);
static void Main(string [] args){
int num;
num = int.Parse(Console.ReadLine());
Console.WriteLine(factorial is+ Factorial(num));
}
}
}






我试着编写没有类的函数(定义时没有 static 关键字),但即使这样也不行,

我在哪里出错?

解决方案

是你试图p /调用类方法。由于 C ++名称调整,您提供的入口点不存在在你编译的DLL。您应该能够在您的DLL上运行 dumpbin.exe 并自己查看。



使用C ++类时总是遵循在C ++方面创建管理器方法的模式来处理C ++类的创建。创建方法创建一个对象(在C ++端),将其存储在数组中,并返回一个整数Id,用于使用该实例进一步调用。 本文概述了类似的方法,也覆盖使用类实例直接(此方法依赖于导入已修改的名称,这在使用单个编译器时应该是确定性的)。



我建议删除名称mangling article以及如何阻止 DllImport 的目的,并阅读上一段中链接的大多数CodeProject文章。它写得很好,涵盖了很多p / invoked minutiae。


Possible Duplicate:
C# P\Invoke DLL no entry point into C++?

I'm asking this question after doing a rather thorough surfing on SO and google, and most of the answers get me about 80% of the way, but its still a bit confusing, so kindly show me the way out.

I have some Visual C++ functions defined as follows:


MyDLL.h

#ifdef FUNCTIONS_EXPORTS
#define FUNCTIONS_API __declspec(dllexport) 
#else
#define FUNCTIONS_API __declspec(dllimport) 
#endif

namespace Functions {
    class MyFunctions {
    public:
        static FUNCTIONS_API int Add(int a, int b);
        static FUNCTIONS_API int Factorial(int a);
    };
}


MyDLL.cpp

namespace Functions {
    int MyFunctions::Add (int a, int b)
    {
        return a+b;
    }
    int MyFunctions::Factorial (int a)
    {
        if(a<0)
            return -1;
        else if(a==0 || a==1)
            return 1;
        else
            return a*MyFunctions::Factorial(a-1);
    }
}


Now, I want to import the DLL generated by this build into my C# program as such:

Program.cs

using System;
using System.Collections.Generic;    
using System.Runtime.InteropServices;

namespace DLLTester
{
    class Program
    {
        [DllImport("path\\to\\the\dll\\myDLL.dll")]
        public static extern int Factorial(int a);
        static void Main(string[] args) {
            int num;
            num = int.Parse(Console.ReadLine());
            Console.WriteLine("The factorial is " + Factorial(num));
        }
    }
}


I've tried writing the functions without a class (no static keyword while defining), but even that does not work and gives errors.

Where am I going wrong in all this?

解决方案

The biggest problem I see is that you're trying to p/invoke class methods. Because of C++ name mangling, the entry point you supplied isn't present in your compiled DLL. You should be able to run dumpbin.exe on your DLL and see for yourself.

When using C++ classes I've always followed the pattern of creating "manager" methods on the C++ side that handle creation of C++ classes. The creation method creates an object (on the C++ side), stores it in an array, and returns an integer Id that I use to make further calls with that instance. This article outlines an approach similar to this, and also covers using the class instance directly (this method relies on importing the mangled name which should be deterministic when using a single compiler).

I'd suggest skimming the name mangling article and how to prevent it for DllImport purposes, and read most of the CodeProject article linked in the previous paragraph. It's pretty well written and covers a lot of p/invoke minutiae.

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

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