Code : Tout sélectionner
Opt("MustDeclareVars", 1)
WinWaitActive("CATIA")
Local $ret = _GetActiveWinEXEPath("cnext.exe", "CATIA V5")
Local $version = FilegetVersion($ret,"ProductVersion")
WinSetTitle("CATIA", "", "CATIA V" & $version)
Func _GetActiveWinEXEPath($filename, $title = ""); filename and title substring(optional)
; Returns the executable path of the active program window
; where more than one instance of program with same exe name is running.
; Parameters:
; $filename = Executable name, i.e "notepad.exe" without path
; $title = substring of window title, i.e. "Notepad" or "Untitled"
; Return:
; On Success: Exe path of active window and @error set to 0
; On Error: "" and @error set to 1
; large chunk of code in this script from post by PerryRaptor
; code from this post nicely replaces my WMI code in first version
; http://www.autoitscript.com/forum/index.php?showtopic=56280
Local $hWnd, $aWinList, $aProcList, $mode, $pid, $hProc, $stHMod, $stCB, $resEnum, $resPath, $err
If Not ProcessExists($filename) Then
SetError(1)
Return ""
EndIf
$mode = Opt("WinTitleMatchMode", 2) ; Match any substring in the title
If $title <> "" Then
$aWinList = WinList($title, "")
Else
$aWinList = WinList()
EndIf
Opt("WinTitleMatchMode", $mode)
$aProcList = ProcessList($filename)
For $i1 = 1 To $aWinList[0][0]
If $aWinList[$i1][0] <> "" Then
For $i2 = 1 To $aProcList[0][0]
$pid = $aProcList[$i2][1]
$hProc = DllCall("kernel32.dll", "int", "OpenProcess", "int", _
0x0410, "int", False, "int", $pid)
$err = @error
If $err Then
If $hProc[0] Then DllCall("kernel32.dll", 'int', 'CloseHandle', 'int', $hProc[0])
SetError($err, @extended, 0)
Return ""
EndIf
If $hProc[0] Then
$stHMod = DllStructCreate("int hMod")
$stCB = DllStructCreate("dword cbNeeded")
$resEnum = DllCall("psapi.dll", "int", "EnumProcessModules", _
"int", $hProc[0], "ptr", DllStructGetPtr($stHMod), "dword", _
DllStructGetSize($stHMod), "ptr", DllStructGetPtr($stCB, 1))
$err = @error
If $err Then
DllCall("kernel32.dll", 'int', 'CloseHandle', 'int', $hProc[0])
SetError($err, @extended, 0)
Return ""
EndIf
If $resEnum[0] Then
$resPath = DllCall("psapi.dll", "int", "GetModuleFileNameEx", _
"int", $hProc[0], "int", DllStructGetData($stHMod, 1), _
"str", "", "dword", 32768)
$err = @error
DllCall("kernel32.dll", 'int', 'CloseHandle', 'int', $hProc[0])
If $err Then
SetError($err, @extended, 0)
Return ""
EndIf
If WinGetProcess($aWinList[$i1][1]) = $pid Then
$hWnd = $aWinList[$i1][1]
If BitAND(WinGetState($hWnd, ""), 8) Then
If FileExists($resPath[3]) Then
SetError(0)
Return $resPath[3]
Else
SetError(1)
Return ""
EndIf
EndIf
EndIf
EndIf
$stHMod = 0
$stCB = 0
If $hProc[0] Then DllCall("kernel32.dll", 'int', 'CloseHandle', 'int', $hProc[0])
If @error Then
SetError(@error, @extended, 0)
Return ""
EndIf
EndIf
Next
EndIf
Next
SetError(1)
Return ""
EndFunc ;==>_GetActiveWinEXEPath