就是说,知道某个窗体的hwnd,怎么知道它是属于那个exe的?

解决方案 »

  1.   

    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
      

  2.   

    GetWindowThreadProcessId Lib "kernel32" (byval hWnd as long,byref processID as long) as long
      

  3.   

    就是例如,
    我打开了一个【写字板】,
    我知道了写【字板窗口】的句柄,
    我怎么知道它的exe是  "c:\winnt\notepad.exe"
      

  4.   

    枚举进程,通过进程得到它的exe名称和processid.
    判断processid和你需要的是否一致
    主要使用到的api: CreateToolhelp32Snapshot,EnumWindows,GetWindowThreadProcessId
      

  5.   

    ok,问题解决了!谢谢大家!
    我是用1->OpenProcess 取得句柄进程
    2->EnumProcessModules
    3->GetModuleFileNameExA 返回指定进程的文件名。我是看msdn的,里面的CreateToolhelp32Snapshot用于win9x。msdn我太爱你了!不过英文的,看起来眼睛痛~如果有人需要msdn,可以联系我Q20032567,注明:vb,msdn
      

  6.   

    哎呀,,忘记用CloseHandle关闭打开的东西了 先关上。:)
      

  7.   

    我给你代码贴出来,你都不理我
    那用枚举呀?得到进程句柄,getmodulename就成了
    getmodulenameex是为了照顾导入的动态连接库的
      

  8.   

    但是我试过只有VB的窗体才可以,不知道为什么。
    先谢谢你喇,,,,!!!!!我是在msdn上找到的。
      

  9.   

    那是个例子,函数说明当然应该在msdn找啦
    你把代码里me.hwnd替换成你得到的句柄才行
    否则,这个程序也不用这么写了,因为vb里的app对象本来就有hinstance属性
    我想你还是没有真弄明白这里的原理
      

  10.   

    我不明白的是,你既然得到了窗口句柄,怎么能利用OpenProcess来得到进程句柄,这个函数一般用于通过进程id获取进程句柄呀?
      

  11.   

    我把它改成一个函数,你可以看看是怎么回事,枚举在这里实在没有必要
    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
      

  12.   

    我试了一下
    为什么是MSVBVM60.DLL
      

  13.   

    ok!!!!!
    我终于写出来喇,大家来看看:刚为自己写的鼠标增强小工具,方便自己,希望也能给你带来方便。 
    http://top2u.net/forum/forum_posts.asp?TID=137&PN=1&TPN=1是用上面的方法写得~谢谢大家喇。