有个 dll文件,我想调用其中的函数,比如有个dll的文件名为diaoyong.dll
用delphi语言调用这个dll中的函数的写法为cici('user32.dll',@a(),@b());其中cici是被调用的dll中的函数,
a(),b()都是主程序中自己定义的函数,@a(),@b()应该是表示这个函数的地址吧~(我自己这样理解)
那在c#中应该怎样表示自己定义的函数的地址呢?
用delphi语言调用这个dll中的函数的写法为cici('user32.dll',@a(),@b());其中cici是被调用的dll中的函数,
a(),b()都是主程序中自己定义的函数,@a(),@b()应该是表示这个函数的地址吧~(我自己这样理解)
那在c#中应该怎样表示自己定义的函数的地址呢?
在调用时,实际上.Net内部传入了另外一个函数指针(stub),由stub来调用Delegates,所以可以传入静态函数或类成员函数。
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
cici(A, B);
} static void A()
{
MessageBox.Show("static A()");
} void B()
{
MessageBox.Show("instance B()");
} [DllImport("diaoyong.dll")]
static extern void cici(MethodInvoker a, MethodInvoker b);
}
}
[DllImport("kernel32.DLL", EntryPoint = "SetFileAttributes")] //如果dll文件不在系统目录,需要绝对定位
private static extern bool SetFileOrDirAttrib(string DFname, int attrib); //名字可以随便起,入口要保持一至(参数等)