一、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;
}
(未完待續)