'СОРС ЗА ФОРМАТА

 

'При натискане на бутона за откриване на Hwnd

Private Sub Command1_Click()

    Text2.Text = GetInstance(Text1.Text) 'В Text2 записваме върнатото от функцията, като подаваме съдържащото се в Text1

End Sub

 

'СОРС ЗА МОДУЛА

Public NamerenHwnd

Public strCache As String

 

Public Const GW_HWNDNEXT = 2

 

Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long

 

 

'Това е публичната функция на която подаваме името на процеса и получаваме Hwnd

Public Function GetInstance(NameProces As String)

On Error Resume Next

strCache = ""

ImeNaProces = UCase(NameProces)

Set objWMI = GetObject("winmgmts://.")

Set objProcesses = objWMI.execquery("Select * from win32_process")

 

For Each objProcess In objProcesses

bolTaskFound = False

If UCase(objProcess.Name) = ImeNaProces Then

strCache = objProcess.Handle

End If

Next

 

If strCache = "" Then

NamerenHwnd = 0

Else

ConvertToHandle

End If

Set objWMI = Nothing

Set objProcesses = Nothing

GetInstance = NamerenHwnd

End Function

 

Private Sub ConvertToHandle()

Dim hInst As Long

hInst = strCache

 

hWndApp = GetWinHandle(hInst)

 

If hWndApp <> 0 Then

NamerenHwnd = hWndApp

End If

 

End Sub

 

Function ProcIDFromWnd(ByVal hwnd As Long) As Long

 

GetWindowThreadProcessId hwnd, idProc

 

ProcIDFromWnd = idProc

End Function

 

Function GetWinHandle(hInstance As Long) As Long

 

tempHwnd = FindWindow(vbNullString, vbNullString)

 

Do Until tempHwnd = 0

 

If GetParent(tempHwnd) = 0 Then

If hInstance = ProcIDFromWnd(tempHwnd) Then

GetWinHandle = tempHwnd

Exit Do

End If

End If

 

tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)

Loop

 

End Function