Get PID from Window Handle in Excel (Visual Basic)

Rate Guide Rating_0_0 (0)
624728168

Example of GetWindowThreadProcessId: how to get the process Id from of a specific running process using the parent hwnd and Windows API.

Note about Text Formatting

Due to the text markup language in use on Guidespot, some of the characters namely quotes, double dashes, and apostrophies, are changed to a different character encoding which may be incompatable with your vb compiler. As such, all source code in a form which can be compiled is provided in the link below.

Download Sample Code

Special Thanks to Datafilehost.com for providing a service that allows me to make clean code available for download. You Rock!

Visual Basic Declaration

Private Declare Function GetWindowThreadProcessId Lib “user32” _
(ByVal hwnd As Long, _
PID As Long) As Long

 

GetPIDFromHWND Function Example

’Insert function

Public Function _
GetPID(Byval hwnd as Long) as Long

        Dim Result

        Result = _
        GetWindowThreadProcessId(hwnd, GetPID)

End Function

Usage in Excel

Sub Test()
    Dim XLPID as Long
    XLPID = GetPID(Application.hwnd)
    Msgbox XLPID
End Sub

Share on StumbleUpon Share on Facebook Tweet this Guide! Share on Digg Share on Reddit Add to del.icio.us

Discussions