Option Explicit Private Const GWL_HINSTANCE As Long = -6Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" ( _ ByVal hModule As Long, _ ByVal lpFileName As String, _ ByVal nSize As Long) As LongPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long) As Long Private Sub Form_Load() Dim hInst As Long, strBuf As String, nLen As Long hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE) Debug.Assert hInst <> 0 strBuf = String(255, vbNullChar) GetModuleFileName hInst, strBuf, 255 MsgBox strBuf End Sub
GetWindowThreadProcessId Lib "kernel32" (byval hWnd as long,byref processID as long) as long
我把它改成一个函数,你可以看看是怎么回事,枚举在这里实在没有必要 Private Const GWL_HINSTANCE As Long = -6 Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" ( _ ByVal hModule As Long, _ ByVal lpFileName As String, _ ByVal nSize As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long) As Long' hWnd 是目标窗口的句柄 Private Sub AppFromWindow(byval hWnd as long)as string Dim hInst As Long, strBuf As String, nLen As Long hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE) if hInst <> 0 then strBuf = String(255, vbNullChar) GetModuleFileName hInst, strBuf, 255 AppFromWindow=replace(strBuf,vbnullchar,"") endif End Sub
Private Const GWL_HINSTANCE As Long = -6Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" ( _
ByVal hModule As Long, _
ByVal lpFileName As String, _
ByVal nSize As Long) As LongPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Sub Form_Load()
Dim hInst As Long, strBuf As String, nLen As Long
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Debug.Assert hInst <> 0
strBuf = String(255, vbNullChar)
GetModuleFileName hInst, strBuf, 255
MsgBox strBuf
End Sub
我打开了一个【写字板】,
我知道了写【字板窗口】的句柄,
我怎么知道它的exe是 "c:\winnt\notepad.exe"
判断processid和你需要的是否一致
主要使用到的api: CreateToolhelp32Snapshot,EnumWindows,GetWindowThreadProcessId
我是用1->OpenProcess 取得句柄进程
2->EnumProcessModules
3->GetModuleFileNameExA 返回指定进程的文件名。我是看msdn的,里面的CreateToolhelp32Snapshot用于win9x。msdn我太爱你了!不过英文的,看起来眼睛痛~如果有人需要msdn,可以联系我Q20032567,注明:vb,msdn
那用枚举呀?得到进程句柄,getmodulename就成了
getmodulenameex是为了照顾导入的动态连接库的
先谢谢你喇,,,,!!!!!我是在msdn上找到的。
你把代码里me.hwnd替换成你得到的句柄才行
否则,这个程序也不用这么写了,因为vb里的app对象本来就有hinstance属性
我想你还是没有真弄明白这里的原理
Private Const GWL_HINSTANCE As Long = -6
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" ( _
ByVal hModule As Long, _
ByVal lpFileName As String, _
ByVal nSize As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long' hWnd 是目标窗口的句柄
Private Sub AppFromWindow(byval hWnd as long)as string
Dim hInst As Long, strBuf As String, nLen As Long
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
if hInst <> 0 then
strBuf = String(255, vbNullChar)
GetModuleFileName hInst, strBuf, 255
AppFromWindow=replace(strBuf,vbnullchar,"")
endif
End Sub
为什么是MSVBVM60.DLL
我终于写出来喇,大家来看看:刚为自己写的鼠标增强小工具,方便自己,希望也能给你带来方便。
http://top2u.net/forum/forum_posts.asp?TID=137&PN=1&TPN=1是用上面的方法写得~谢谢大家喇。