c#、C++等调用Dll方法(未完)
一、C#中
1、直接装载dll某个并使用之
使用DllImport引入dll。
然后引入dll中的某个方法。
如:调用系统的Beep方法。(此方法在kernel.dll中)
[DllImport("kernel32.dll")] //引入Dll
public static extern bool Beep(int frequency, int duration); //引入Dll中某方法
然后在程序中就可以直接使用Beep方法了。
private void button1_Click(object sender, EventArgs e)
{
Beep(1000,1000);
}
2、通过装载系统dll,使用其中的系统装载dll方法装载其他dll
[DllImport("kernel32")]
public static extern int LoadLibrary(string libname);
[DllImport("idcarddll.dll", CallingConvention = CallingConvention.Winapi)]
public static extern int LoadIdcardLibrary();
使用如下:
int hdll;
hdll = mydll.LoadLibrary("idcarddll.dll");
使用的是系统的LoadLibrary()方法。但是由于此方法C#不能直接支持。
二、C++中
使用的头文件#include <windows.h>
示例代码
#include "stdafx.h"
#include <windows.h>
int (WINAPI *pBeep)(int,int); //声明一个函数
int _tmain(int argc, _TCHAR* argv[])
{
HMODULE hMyDll = LoadLibrary(_T("kernel32.dll")); //装载dll
pBeep = (int (WINAPI *)(int,int))GetProcAddress(hMyDll,"Beep");
pBeep(1000,1000); //调用dll中函数
FreeLibrary(hMyDll); //释放dll
return 0;
}
(未完待续)