C 呼叫WINDOWS API 示例

2022-09-12 03:42:12 字數 4000 閱讀 3263

一、呼叫windowsapi。

c#下呼叫windows api方法如下:

1、引入命名空間:using system.runtime.interopservices;

2、引用需要使用的方法,格式:[dllimport("dll檔案")]方法的宣告;

[dllimport("user32.dll")]private static extern bool showwindow(intptr hwnd, int ncmdshow);

[dllimport("user32.dll")]private static extern bool setforegroundwindow(intptr hwnd);

[dllimport("user32.dll")]private static extern intptr findwindow(string lpclassname,string lpwindowname);

[dllimport("user32.dll")]private static extern int sendmessage(intptr hwnd,int msg,int wparam,int lparam);

[dllimport("user32.dll")]private static extern bool setcursorpos(int x, int y);

[dllimport("user32.dll")]private static extern void mouse_event(int dwflags, int dx, int dy, int dwdata, int dwextrainfo);

[dllimport("user32.dll")]private static extern void keybd_event(byte bvk, byte bscan, uint dwflags, uint dwextrainfo);

[dllimport("user32.dll")]private static extern bool setwindowpos(intptr hwnd, intptr hwndlnsertafter, int x, int y, int cx, int cy, uint flags);

//showwindow引數

private const int sw_shownormal = 1;

private const int sw_restore = 9;

private const int sw_shownoactivate = 4;

//sendmessage引數

private const int wm_keydown = 0x100;

private const int wm_keyup = 0x101;

private const int wm_syschar = 0x106;

private const int wm_syskeyup = 0x105;

private const int wm_syskeydown = 0x104;

private const int wm_char = 0x102;

二、找到目標視窗

1)、根據視窗的標題得到控制代碼

intptr myintptr = findwindow(null,"視窗名"); //null為類名,可以用spy++得到,也可以為空

showwindow(myintptr, sw_restore); //將視窗還原

setforegroundwindow(myintptr); //如果沒有showwindow,此方法不能設定最小化的視窗

2)、遍歷所有視窗得到控制代碼

1 定義委託方法callback,列舉視窗api(enumwindows),得到視窗名api(getwindowtextw)和得到視窗類名api(getclassnamew)

public delegate bool callback(int hwnd, int lparam);

[dllimport("user32")]public static extern int enumwindows(callback x, int y);

[dllimport("user32.dll")]private static extern int getwindowtextw(intptr hwnd, [marshalas(unmanagedtype.lpwstr)]stringbuilder lpstring, int nmaxcount);

[dllimport("user32.dll")]private static extern int getclassnamew(intptr hwnd, [marshalas(unmanagedtype.lpwstr)]stringbuilder lpstring, int nmaxcount);

2 呼叫enumwindows遍歷視窗

callback mycallback = new callback(recall);

enumwindows(mycallback, 0);

3 **方法recall

public bool recall(int hwnd, int lparam)

else

}3)、開啟視窗得到控制代碼

1 定義設定活動視窗api(setactivewindow),設定前台視窗api(setforegroundwindow)

[dllimport("user32.dll")]static extern intptr setactivewindow(intptr hwnd);

[dllimport("user32.dll")][return: marshalas(unmanagedtype.bool)]static extern bool setforegroundwindow(intptr hwnd);

2 開啟視窗

process proc = process.start(@"目標程式路徑");

setactivewindow(proc.mainwindowhandle);

setforegroundwindow(proc.mainwindowhandle);

三、向指定的視窗輸入資料

1 利用傳送訊息api(sendmessage)向視窗傳送資料

inputstr(myintptr, _gameid); //輸入遊戲id

sendmessage(myintptr, wm_syskeydown, 0x09, 0); //輸入tab(0x09)

sendmessage(myintptr, wm_syskeyup, 0x09, 0);

inputstr(myintptr, _gamepass); //輸入遊戲密碼

sendmessage(myintptr, wm_syskeydown, 0x0d, 0); //輸入enter(0x0d)

sendmessage(myintptr, wm_syskeyup, 0x0d, 0);

///

/// 傳送乙個字串

///

/// 視窗控制代碼

/// 字串

public void inputstr(intptr myintptr, string input)

}2 利用滑鼠和鍵盤模擬向視窗傳送資料

setwindowpos(pw, (intptr)(-1), 0, 0, 0, 0, 0x0040 | 0x0001); //設定視窗位置

setcursorpos(476, 177); //設定滑鼠位置

mouse_event(0x0002, 0, 0, 0, 0); //模擬滑鼠按下操作

mouse_event(0x0004, 0, 0, 0, 0); //模擬滑鼠放開操作

sendkeys.send(_gameid);   //模擬鍵盤輸入遊戲id

sendkeys.send(""); //模擬鍵盤輸入tab

sendkeys.send(_gamepass); //模擬鍵盤輸入遊戲密碼

sendkeys.send(""); //模擬鍵盤輸入enter

另:上面還提到了keybd_event方法,用法和mouse_event方法類似,作用和sendkeys.send一樣。

C 呼叫windows api示例

這是執行結果 api函式是構築windws應用程式的基石,每一種windows應用程式開發工具,它提 供的底層函式都間接或直接地呼叫了windows api函式,同時為了實現功能擴 展,一般也都提供了呼叫windowsapi函式的介面,也就是說具備呼叫動態連線 庫的能力。visual c 和其它開發...

Windows API 呼叫示例

簡介 本文主要記錄windows api 的呼叫示例,因為這項技術並不常用,屬於c 中比較孤僻或接觸底層的技術,並不常用。但是有時候也可以借助他完成一些c 本身不能完成的功能,例如 通過控制代碼獲取其他程式資料,或者向作業系統發出指定的訊息等等。1.滑鼠事件api 1 函式原型 void mouse...

C 呼叫windowsAPI函式

一 呼叫格式 c 在呼叫windowsapi函式介面的時候有一套專門的呼叫流程 首先我們在呼叫api函式的時候必須引用命名空間interopservices using system.runtime.interopservices 例如我們想呼叫windows的kernel32.dll動態庫中的介面...