dll导入(无法找到名为"methodname"的入口点) [英] dll import (Unable to find an entry point named "methodname" )
本文介绍了dll导入(无法找到名为"methodname"的入口点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将C ++ dll导入我的C#Windows项目.但是我收到一个错误无法在DLL"Test.dll"中找到名为添加"的入口点"
我的C ++源代码:
#pragma一次
使用 命名空间系统;
命名空间测试{
公共 参考 class Class1
{
公共: int 添加( int 我, int j)
{
返回(i + j);
}
};
}
我的C#Windows源代码:
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Text;
使用使用System.Windows.Forms;
使用使用System.Runtime.InteropServices;
使用测试; // 我的c ++ dll引用
命名空间 WindowsApplication3
{
公共 部分 class Form1:表单
{
公共 Form1()
{
InitializeComponent();
}
[DllImport(" )]
公共 静态 外部 span class ="code-keyword"> int a, int b);
私有 无效 button1_Click(对象发件人,EventArgs e)
{
int p =添加( 1 , 2 );
}
}
}
我将c ++ dll放置在调试文件夹的c#应用程序输出路径中.
请帮我找到我在c ++或c#代码中所犯的错误.您将其定义为public static
的代码.将其更改为不在任何名称空间或类中的独立函数,然后导出其名称,如下所示:
__ declspec ( dllexport ) int 添加( int i, int j)
{
返回(i + j);
}
您需要使用__declspec(dllexport)
从C ++类导出函数.
http://msdn.microsoft.com/en-us/library/a90k134d (v = vs.80).aspx [
查看此示例以获得DLLImport的示例:
http://www.pinvoke.net/default.aspx/user32.CascadeChildWindows [ ^ ]
I am trying to import a c++ dll into my C# windows project. But i am getting an error "Unable to find an entry point named ''Add'' in DLL ''Test.dll''"
My c++ source code:
#pragma once
using namespace System;
namespace Test {
public ref class Class1
{
public: int Add(int i, int j)
{
return(i + j);
}
};
}
My C# windows source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Test;// my c++ dll refernce
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("Test.dll")]
public static extern int Add(int a, int b);
private void button1_Click(object sender, EventArgs e)
{
int p = Add(1, 2);
}
}
}
I placed the c++ dll in c# application output path i e debug folder.
Please help to find me the error i made in the c++ or c# code.
解决方案
YourAdd()
function is a member ofTest::Class1
and yet in your C# code you define it aspublic static
. Change it to a stand-alone function not in any namespace or class and export its name, like this:
__declspec(dllexport) int Add(int i, int j) { return(i + j); }
You need to export your function from the C++ class using__declspec(dllexport)
http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx[^]
The default directory of DLLImport is the Windows/System32 directory, not your application. You need to specify the location of your dll in absolute terms like
[DllImport("Test.dll")]
Check this out for an example of DLLImport:
http://www.pinvoke.net/default.aspx/user32.CascadeChildWindows[^]
这篇关于dll导入(无法找到名为"methodname"的入口点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文