无法在DLL'xxx.dll'异常中找到名为'xxx'的入口点? [英] Unable to find an entry point named 'xxx' in DLL 'xxx.dll' exception?

查看:1284
本文介绍了无法在DLL'xxx.dll'异常中找到名为'xxx'的入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我有一个问题要问。



我正试图用C ++创建一个库并在C#中使用它。



这是我用来实现的指导:

如何 [ ^ ]



我用C ++创建了文件(源代码和标题),如下所示:



  //   MathFuncsDll.h  

namespace MathFuncs
{
class MyMathFuncs
{
public
// 返回a + b
static __declspec(d llexport) double 添加( double a, double b);

// 返回a - b
static __declspec(dllexport) double 减去( double a,< span class =code-keyword> double b);

// 返回* b
static __declspec(dllexport) double 乘法( double a,< span class =code-keyword> double b);

// 返回a / b
// 如果b为0,则抛出DivideByZeroException
static __declspec(dllexport) double 除以( double a, double b);
};
}





  //   MathFuncsDll.cpp  
// compile with:/ EHsc / LD

#include MathFuncsDll.h

#include< stdexcept>

使用 命名空间标准;

命名空间 MathFuncs
{
double MyMathFuncs: :添加( double a, double b)
{
返回 a + b;
}

double MyMathFuncs :: Subtract( double a, double b)
{
return a - b;
}

double MyMathFuncs :: Multiply( double a, double b)
{
return a * b;
}

double MyMathFuncs :: Divide( double a, double b)
{
if (b == 0
{
throw new invalid_argument( b不能为零!);
}

return a / b;
}
}





我构建文件,创建一个dll。

之后我将dll文件放在我的C#解决方案中(与.exe文件相同的文件夹



然后,我在我的C#项目中导入dll:

 [DllImport(SimpleDll.dll,CallingConvention = CallingConvention.Cdecl)] 
public static extern double加(双a,双b);

static void Main(string [] args)
{
double a = Add(1.0,3.0));
}





当我执行该程序时,它给了我一个例外。



为什么会这样?有什么想法吗?



我最诚挚的问候......



注意:我试图添加 externC,但是发生了链接问题。

解决方案

你的C ++代码是在命名空间 MyMathFuncs 中,据我所知,C#将无法通过PInvoke机制解决这个问题。删除命名空间并使函数变为简单的C样式,并在定义上使用 externC;类似于:

  //   MathFuncsDll.h  

// 返回a + b
extern C __ declspec dllexport double 添加( double a, double b);





  //   MathFuncsDll.cpp  
#include MathFuncsDll。 h

double 添加( double a, d ouble b)
{
return a + b;
}


尝试使用依赖性walker打开您的DLL文件,您必须在那里看到您的函数名称。如果您看不到,那么您没有正确导出。但是如果你看到类似的东西?添加@@ MyMathFunc @@ ....然后你必须使用整个字符串作为你的功能名称这样的



[DLLImport( SimpleDLL.dll,Entrypoint =?添加@@ MyMathFunc @@ ....,CallingConvention = CallingConvention.Cdecl)]


对于这种类型的交互,你可以使用中介 - 另一个DLL(在CLI中写入)将您的函数暴露给C#。



您当然可以用C ++ / CLI编写第一个DLL,或者您可以编写中介。



查看此线程DaniWeb [ ^ ] for可能性。

Hey everyone.

I have a question to ask.

I'm trying to create a library in C++ and use it in C#.

Here's the guidance that I'm using to achieve:
How to[^]

I created the files in C++(source and header) as follows:

// MathFuncsDll.h

namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}



// MathFuncsDll.cpp
// compile with: /EHsc /LD

#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}



I build the files, create a dll.
After that I put the dll file in my C# solution(with the same folder of .exe file)

Then, I import the dll in my C# project:

[DllImport("SimpleDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern double Add(double a, double b);

static void Main(string[] args)
{
    double a = Add(1.0, 3.0));
}



When I excecute the program, it gives me an exception.

Why does it happen? Any ideas?

My best regards...

Note: I've tried to add extern "C", but linkage problems occured.

解决方案

Your C++ code is in a namespace MyMathFuncs, and C# will not be able to resolve that through the PInvoke mechanism as far as I am aware. Remove the namespace and make the functions simple C style, and also use extern "C" on the definitions; something like:

// MathFuncsDll.h

// Returns a + b
extern "C" __declspec(dllexport) double Add(double a, double b);


and

// MathFuncsDll.cpp
#include "MathFuncsDll.h"

double Add(double a, double b)
{
    return a + b;
}


Try to open your DLL file with dependency walker and you must see your function name there. If you cannot see then you have not exported correctly. But if you see something like ?Add@@MyMathFunc@@....then you must use that whole string as your fucntion name like this

[DLLImport("SimpleDLL.dll",Entrypoint="?Add@@MyMathFunc@@....",CallingConvention=CallingConvention.Cdecl)]


For that type of interaction, you CAN USE an intermediary -- another DLL (writtin in CLI) that will expose your functions to the C#.

You could of course write the first DLL in C++/CLI or you can make the intermediary.

Check out this thread on DaniWeb[^] for possibilities.


这篇关于无法在DLL'xxx.dll'异常中找到名为'xxx'的入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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