c#、C++等調用Dll方法(未完)

來源:互聯網
上載者:User

一、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;

}

 

(未完待續)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.