应用程序激活不适用于进程 ID


Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
Declare Function GetWindowThreadProcessId Lib "user32.dll" (
    ByVal hwnd As Int32,
    ByRef lpdwProcessId As Int32) As Int32
Public Function RetCurTitle() As Integer
    Dim processID As Int32
    Return GetWindowThreadProcessId(GetForegroundWindow(), processID)
End Function

我使用上面的函数来获取活动窗口的进程id。它返回4060。

之后我调用

AppActivate(4060)

程序崩溃。我尝试使用整数变量代替4060,但同样的问题。

您的RetCurTitle()函数返回GetWindowThreadProcessId()的返回值,这是线程ID,而不是进程ID。将函数更改为:

Public Function RetCurTitle() As Int32
    Dim processID As Int32
    GetWindowThreadProcessId(GetForegroundWindow(), processID)
    Return processID
End Function

最新更新