从托管应用程序调用非托管代码 当调用用户定义的 DLL 中所包含的函数时,有必要将 extern "C" 添加在 DLL 函数声明之前,如下所示: The function declaration in SampleDLL.h file extern "C" SAMPLEDLL_API int fnSampleDLL(void);
比如 我这样: class Test { public int SampleMethod(int i) { return i*10; } } 把这编译成dll 然后,在下面的程序中引入 using System; using System.Runtime.InteropServices;class Test { [DllImport("Add.dll",EntryPoint="SampleMethod")] public static extern int SampleMethod(int i); static int Main() { return SampleMethod(5); } } 然后就报EntryPointNotFoundException异常
直接右键添加引用引用这个DLL。 在程序里:Test test=new Test(); int aaa= test.SampleMethod(1);
C#的DLL和windows的标准dll不是一样的东西,不能直接这么用。
dll调用有问题
你写的这个DLL也是C#环境下编译成的,不是C/C++环境下编译成的,没有必要用下面的方法. [DllImport("Add.dll",EntryPoint="SampleMethod")] public static extern int SampleMethod(int i);用下面的方法就可以了: 直接右键添加引用引用这个DLL。 using ClassLibrary1; namespace ConsoleApplication1 { class Program { static int Main(string[] args) { Test test = new Test(); return test.SampleMethod(5); } } }
不管在什么窗口,你不就是想要调用DLL吗,你可以动态调用DLLAssembly MyAssembly=Assembly.LoadFrom("example.dll"); //传入类库的文件名 Type t = MyAssembly.GetType("Example.Class1"); //传入"名空间.类名"object obj=Activator.CreateInstance(t); //生成Example.Class1类的实例
--------------------------------------------
估计没正确引入
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
当调用用户定义的 DLL 中所包含的函数时,有必要将 extern "C" 添加在 DLL 函数声明之前,如下所示:
The function declaration in SampleDLL.h file
extern "C" SAMPLEDLL_API int fnSampleDLL(void);
详见http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/vccore/html/vcwlksysimportattributetutorial.asp
无法在DLL中找到名SampleMethod(int i)为的入口点说明没有访问此方法的权限,private?/protect??
http://www.cnblogs.com/tallman/archive/2007/04/27/729048.html
C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)
http://www.cppblog.com/kerlw/archive/2007/06/27/27061.html
我这样:
class Test
{
public int SampleMethod(int i)
{
return i*10;
}
}
把这编译成dll
然后,在下面的程序中引入
using System;
using System.Runtime.InteropServices;class Test
{
[DllImport("Add.dll",EntryPoint="SampleMethod")]
public static extern int SampleMethod(int i);
static int Main()
{
return SampleMethod(5);
}
}
然后就报EntryPointNotFoundException异常
在程序里:Test test=new Test();
int aaa= test.SampleMethod(1);
[DllImport("Add.dll",EntryPoint="SampleMethod")]
public static extern int SampleMethod(int i);用下面的方法就可以了:
直接右键添加引用引用这个DLL。
using ClassLibrary1;
namespace ConsoleApplication1
{
class Program
{
static int Main(string[] args)
{
Test test = new Test();
return test.SampleMethod(5);
}
}
}
而且按出现的异常来看,按理已经引用到了dll,只是找不到入口点。是编译成dll的程序本身的问题吗?
第一,没有添加引用
第二,如果你是默认建的,要修改class为public,方法也是public,注意是不是修改好了
Type t = MyAssembly.GetType("Example.Class1"); //传入"名空间.类名"object obj=Activator.CreateInstance(t); //生成Example.Class1类的实例
MethodInfo m=t.GetMethod("Add"); //获得"Add"方法if (m!=null) //如果获得Add方法
{
object[] param=new object[2]; //Add方法有两个参数,如果没有参数则用new object[0];
param[0] = 1; //第一个参数传入1
param[1] = 2; //第二个参数传入2
int total = (int)m.Invoke(obj, param); //total的值为1+2=3
}
文件开头添加引用using System.Reflection;