Imports?System.Runtime.InteropServices
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)烏恰免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
Module?winapi
DllImport("User32.dll",?CallingConvention:=CallingConvention.StdCall,?EntryPoint:="GetWindowThreadProcessId")?_
Function?GetWindowThreadProcessId(ByVal?hwnd?As?IntPtr,?ByRef?procId?As?UInt32)?As?UInt32
End?Function
DllImport("kernel32.dll",?CallingConvention:=CallingConvention.StdCall,?EntryPoint:="OpenProcess")?_
Function?OpenProcess(ByVal?access?As?UInt32,?ByVal?inherit?As?Boolean,?ByVal?procid?As?UInt32)?As?IntPtr
End?Function
DllImport("kernel32.dll",?CallingConvention:=CallingConvention.StdCall,?EntryPoint:="CloseHandle")?_
Function?CloseHandle(ByVal?handle?As?IntPtr)?As?Boolean
End?Function
DllImport("psapi.dll",?CallingConvention:=CallingConvention.StdCall,?EntryPoint:="GetModuleFileNameExW",?Charset:=CharSet.Unicode)?_
Function?GetModuleFileNameExW(ByVal?hProc?As?IntPtr,?ByVal?hMod?As?IntPtr,?ByVal?arrName()?As?Char,?ByVal?arrSize?As?UInt32)?As?UInt32
End?Function
End?Module
Public?Class?Form1
Private?Sub?Button1_Click(sender?As?Object,?e?As?EventArgs)?Handles?Button1.Click
Dim?procid?As?UInt32
GetWindowThreadProcessId(Me.Handle,?procid)
Dim?handle?As?IntPtr
handle?=?OpenProcess(1040,?False,?procid)
Dim?name(65536)?As?Char
Dim?nameSize?As?UInt32?=?GetModuleFileNameExW(handle,?IntPtr.Zero,?name,?65536)
Dim?strName?As?String?=?New?String(name,?0,?nameSize)
CloseHandle(handle)
MsgBox(strName)
End?Sub
End?Class
可惡……我不會(huì)vb……臨時(shí)去七拼八湊查了點(diǎn)語法……盡力了
vb.net 根本就訪問不了內(nèi)存,只能通過調(diào)用api函數(shù)方式,不光是vb.net ,C#也是,主要是為了安全,微軟在net里面不容許直接訪問內(nèi)存。
Imports?System.Text
Imports?System.Runtime.InteropServices
Public?Class?Form1
'?相關(guān)API函數(shù)聲明,注釋掉的這里沒用到,但是也比較常用吧,這些函數(shù)的功能都能搜到。
Private?Declare?Function?FindWindow?Lib?"user32"?Alias?"FindWindowA"?(ByVal?lpClassName?As?String,?ByVal?lpWindowName?As?String)?As?IntPtr
Private?Declare?Function?FindWindowEx?Lib?"user32"?Alias?"FindWindowExA"?(ByVal?hWnd1?As?IntPtr,?ByVal?hWnd2?As?IntPtr,?ByVal?lpsz1?As?String,?ByVal?lpsz2?As?String)?As?IntPtr
Private?Delegate?Function?EnumChildProc(ByVal?hWnd?As?IntPtr,?ByVal?lParam?As?Integer)?As?Boolean
Private?Declare?Function?EnumChildWindows?Lib?"user32.dll"?(ByVal?hWndParent?As?IntPtr,?ByVal?lpEnumFunc?As?EnumChildProc,?ByVal?lParam?As?Integer)?As?Boolean
Private?Declare?Auto?Function?SendMessage?Lib?"User32.dll"?(ByVal?hWnd?As?IntPtr,?ByVal?Msg?As?Integer,?ByVal?wParam?As?Integer,?ByVal?lParam?As?String)?As?Integer
'Private?Declare?Function?CheckDlgButton?Lib?"user32"?Alias?"CheckDLGButtonA"?(ByVal?hDlg?As?IntPtr,?ByVal?nIDButton?As?IntPtr,?ByVal?wCheck?As?Integer)?As?Integer
Private?Declare?Function?GetClassName?Lib?"user32"?Alias?"GetClassNameA"?(ByVal?hWnd?As?IntPtr,?ByVal?lpClassName?As?StringBuilder,?ByVal?nMaxCount?As?Integer)?As?Integer
'Private?Declare?Function?GetWindowThreadProcessId?Lib?"user32"?Alias?"GetWindowThreadProcessId"?(ByVal?hwnd?As?IntPtr,?ByVal?lpdwProcessId?As?Long)?As?Integer
Private?Declare?Auto?Function?GetWindowTextLength?Lib?"user32"?Alias?"GetWindowTextLength"?(ByVal?hwnd?As?IntPtr)?As?Integer
Private?Declare?Function?GetWindowText?Lib?"user32"?Alias?"GetWindowTextA"?(ByVal?hwnd?As?IntPtr,?ByVal?lpString?As?StringBuilder,?ByVal?cch?As?Integer)?As?Integer
'?相關(guān)消息定義,也有沒用到的
Const?WM_SETTEXT?=?HC
Const?WM_GETTEXT?=?HD
'Const?WM_SETFOCUS?=?H7
'Const?WM_KILLFOCUS?=?H8
'Const?WM_CLOSE?=?H10
'Const?WM_SYSCOMMAND?=?H112
'Const?SC_CLOSE?=?HF060
'Const?SC_MINIMIZE?=?HF020
Const?BM_GETCHECK?=?HF0
Const?BM_SETCHECK?=?HF1
Const?BM_GETSTATE?=?HF2
Const?BM_SETSTATE?=?HF3
Const?BM_SETSTYLE?=?HF4
Const?BM_CLICK?=?HF5
'Const?BM_GETIMAGE?=?HF6
'Const?BM_SETIMAGE?=?HF7
Const?BST_UNCHECKED?=?O0
Const?BST_CHECKED?=?O1
Const?BST_INDETERMINATE?=?O2
'?儲存窗口句柄
Dim?WindowHandle?As?IntPtr
'?儲存兩個(gè)(或者多個(gè))編輯框句柄
Dim?EditHandle?As?New?List(Of?IntPtr)
Dim?EditWindowsText?As?List(Of?String)
'?儲存復(fù)選框句柄
Dim?CheckHandle?As?IntPtr?=?0
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
Button1_Click(sender,?e)
End?Sub
'?EnumChildWindows?回調(diào)函數(shù),該函數(shù)名作為API函數(shù)EnumChildWindows?的一個(gè)參數(shù)
'?該函數(shù)實(shí)現(xiàn)了枚舉各個(gè)子窗口,找出編輯框?qū)傩缘墓δ?/p>
Public?Function?EnumChildProcC(ByVal?hwnd?As?IntPtr,?ByVal?lParam?As?Integer)?As?Boolean
Dim?dwWindowClass?As?StringBuilder?=?New?StringBuilder(100)
'?獲得某一個(gè)句柄的類名
GetClassName(hwnd,?dwWindowClass,?100)
If?dwWindowClass.ToString.Contains("EDIT")?Or?dwWindowClass.ToString.Contains("Edit")?Then?????'?類名包含EDIT的為編輯框
EditHandle.Add(hwnd)????????????????????????'?存儲該句柄
End?If
'?返回?True?一直枚舉完
Return?True
End?Function
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
WindowHandle?=?FindWindow(vbNullString,?"登陸")
If?WindowHandle.ToInt32?=?0?Then
MsgBox("未捕獲到窗口"?+?"登陸")
Return
End?If
'?枚舉所有主窗口的子窗口(控件),枚舉時(shí)自動(dòng)調(diào)用回調(diào)函數(shù),完成編輯框句柄的獲取
EnumChildWindows(WindowHandle,?AddressOf?EnumChildProcC,?0)
'?尋找復(fù)選框
CheckHandle?=?FindWindowEx(WindowHandle,?IntPtr.Zero,?vbNullString,?"記住密碼")
Dim?str?As?New?StringBuilder
Dim?j?As?Integer?=?0
'?對編輯框文本賦值
For?j?=?0?To?EditHandle.Count?-?1
SendMessage(EditHandle(j),?WM_SETTEXT,?0,?"Text")
'GetWindowText(EditHandle(j),?str,?20)
'EditWindowsText.Add(Str.ToString)
'Str.Clear()
Next
If?EditHandle.Count?=?0?Then
MsgBox("未找到輸入框!")
End?If????????
If?CheckHandle.ToInt32??0?Then
'CheckDlgButton(WindowHandle,?id,?1)
'?對復(fù)選框進(jìn)行鼠標(biāo)單擊操作
SendMessage(CheckHandle,?BM_CLICK,?0,?0)
'SendMessage(CheckHandle,?BM_SETCHECK,?True,?0)
End?If
End?Sub
End?Class
Form1: TForm1;
implementation
{$R *.dfm}
uses PsAPI; {GetModuleFileNameEx 函數(shù)需要它}
{根據(jù)窗口句柄獲取所在程序路徑的函數(shù)}
function GetProcessExePath(h: HWND): string;
var
pid: Cardinal;
pHandle: THandle;
buf: array[0..MAX_PATH] of Char;
begin
{先獲取進(jìn)程 ID}
GetWindowThreadProcessId(h, @pid);
{再獲取進(jìn)程句柄}
pHandle := OpenProcess(PROCESS_ALL_ACCESS, False, pid);
{獲取進(jìn)程路徑}
GetModuleFileNameEx(pHandle, 0, buf, Length(buf));
CloseHandle(pHandle);
Result := buf;
end;
{測試當(dāng)前程序}
procedure TForm1.Button1Click(Sender: TObject);
var
path: string;
begin
path := GetProcessExePath(Handle);
ShowMessage(path);
end;
{測試記事本 - 需要隨便打開一個(gè)記事本}
procedure TForm1.Button2Click(Sender: TObject);
var
wh: HWND;
path: string;
begin
wh := FindWindow('Notepad', nil);
path := GetProcessExePath(wh);
ShowMessage(path);
end;
end.