如何使用C#加载DLL?
我有一个小DLL,它有3个函数:Init、Load和run。 我是c#新手,所以在阅读这里的问题时,我打开了一个控制台项目,希望加载DLL并使用它的函数。 不幸的是,它不起作用。谁能告诉我哪里出了问题 这是我得到的错误:如何使用C#加载DLL?,c#,dll,pinvoke,C#,Dll,Pinvoke,我有一个小DLL,它有3个函数:Init、Load和run。 我是c#新手,所以在阅读这里的问题时,我打开了一个控制台项目,希望加载DLL并使用它的函数。 不幸的是,它不起作用。谁能告诉我哪里出了问题 这是我得到的错误: Unable to find an entry point named 'Init' in DLL '....path to my DLL....'. 代码如下: using System; using System.Collections.Generic; using S
Unable to find an entry point named 'Init' in DLL
'....path to my DLL....'.
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("C:\\Desktop\\DLLTest.dll", CharSet = CharSet.Unicode)]
public static extern bool Init();
[DllImport("C:\\Desktop\\DLLTest.dll", CharSet = CharSet.Unicode)]
public static extern bool Load(string file);
[DllImport("C:\\Desktop\\DLLTest.dll", CharSet = CharSet.Unicode)]
public static extern bool Play();
static void Main()
{
Console.WriteLine("got till here!!!");
Init();
Load("C:\\Desktop\\the_thing_about_dogs_480x270.mp4");
Play();
}
}
}
我唯一能怀疑的可能是我没有创建实例?
除此之外,没有线索:(
*编辑:
这是DLL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DLLTest
{
public class DLLTestApi
{
myInfo localPlay;
public bool Init()
{
localPlay = new myInfo();
return true;
}
public bool Load(string file)
{
localPlay.Load(file);
return true;
}
public bool Play()
{
localPlay.StartNewThread();
return true;
}
public bool Stop()
{
localPlay.DxStopWMp();
return true;
}
public bool Pause()
{
localPlay.DxPause();
return true;
}
public bool Resume()
{
localPlay.DxResume();
return true;
}
public bool Close()
{
localPlay.DxClose();
return true;
}
}
}
错误消息清楚地告诉您问题所在。DLL没有导出名为
Init
的函数。可能的原因包括:
从编辑到问题,很明显第1项是原因。您的DLL是托管DLL,尝试使用p/invoke访问它是不正确的。只需将其作为控制台应用程序项目的引用添加即可。要动态加载DLL,不确定它是否适用于托管DLL,请使用Assembly.LoadFrom()从制度上反思
要创建实例,请使用Activator.CreateInstance()从System.Activator;是您的DLL吗?您是否正确导出了
Init
函数?是DLLTest.DLL
a.Net程序集?什么类型的DLL?COM?Native?.Net?运行时似乎发现DLLTest.DLL
很好。错误似乎是导出了Init
函数。您可以发布本机数据吗这些函数的定义?这可能是由于缺少信息而导致错误的地方。我已经编辑了它-添加了DLL。它也是用c#编写的。是的,第1项是解释。David是正确的,托管DLL不会像非托管DLL那样导出符号。请在项目中引用DLL,并像使用其他托管DLL一样使用它很好。