在C 中呼叫windows API函式

2022-08-03 07:18:09 字數 1417 閱讀 4230

在c#中呼叫windows api函式

對於windows 系統api函式的呼叫在程式設計中有時是必不可少的,各種程式語言都規範了呼叫的方法和介面,在c#語言中的呼叫方法如下(以下程式設計環境為visual studio .net):

1、 在工程專案中新增乙個類新項,開啟這個類檔案,在檔案頭部加入對以下命名空間的引用:

using system.runtime.interopservices; 

在類定義主體中,以靜態呼叫的方式加入對api的引用,本文以下的api呼叫為例:

/// 

/// 開啟和關閉cd托盤.

/// 

[dllimport("winmm.dll" , entrypoint="mcisendstring", charset=charset.auto)]

public static extern int mcisendstring (string lpstrcommand,string lpstrreturnstring ,int ureturnlength,int hwndcallback);

/// 

/// 顯示和隱藏滑鼠指標.

/// 

[dllimport("user32.dll", entrypoint="showcursor", charset=charset.auto)]

public static extern int showcursor(int bshow);

/// 

/// 清空**站.

/// 

[dllimport("shell32.dll", entrypoint="shemptyrecyclebin", charset=charset.auto)]

public static extern long shemptyrecyclebin(intptr hwnd, string pszrootpath, long dwflags); 

/// 

/// 開啟瀏覽器

/// 

[dllimport("shell32.dll", entrypoint="shellexecute", charset=charset.auto)]

public static extern int shellexecute(intptr hwnd,string lpoperation,string lpfile,string lpparameters,string lpdirectory,int nshowcmd);

/// 

/// 最大化視窗,最小化視窗,正常大小視窗;

/// 

[dllimport("user32.dll", entrypoint="showwindow", charset=charset.auto)]

public static extern int showwindow(intptr hwnd,int ncmdshow);

C 中呼叫Windows API的要點

在.net framework sdk文件中,關於呼叫windows api的指示比較零散,並且其中稍全面一點的是針對visual basic net講述的。本文將c 中呼叫api的要點匯集如下,希望給未在c 中使用過api的朋友一點幫助。另外如果安裝了visual studio net的話,在c ...

C 中呼叫Windows API的要點

c 中呼叫windows api的要點 在.net framework sdk文件中,關於呼叫windows api的指示比較零散,並且其中稍全面一點的是針對visual basic net講述的。本文將c 中呼叫api的要點匯集如下,希望給未在c 中使用過api的朋友一點幫助。另外如果安裝了vis...

C 中呼叫Windows API的要點

在 net framework sdk 文件中,關於呼叫 windows api 的指示比較零散,並且其中稍全面一點的是針對 visual basic net 講述的。本文將 c 中呼叫 api的要點匯集如下,希望給未在 c 中使用過 api的朋友一點幫助。另外如果安裝了 visual studio...