Reputation: 27
I have written script in Excel VBA and it is working fine, now I am trying to covert the same to QTP 11.0 - however facing some issue.
Updated Question based on suggestion by Xiaofu -
What is the VBA - shell function equivalent in QTP 11?
And below is QTP 11 Script:
Extern.Declare micHwnd, "OpenProcess", "kernel32", "OpenProcess", micLong, micLong, micLong
Extern.Declare micHwnd, "WaitForSingleObject", "kernel32", "WaitForSingleObject", micLong, micLong
Extern.Declare micHwnd, "CloseHandle", "kernel32", "CloseHandle", micLong
Const PROCESS_QUERY_INFORMATION = &H400
Const SYNCHRONIZE = &H100000
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const STATUS_PENDING = &H103
Const STILL_ACTIVE = &H103
Const WAIT_TIMEOUT = &H102
Const INFINITE = &HFFFFFFFF
Public Function test_exe()
Dim argStr
argStr = "cmd.exe /c " + Chr(34) & "plink.exe -load " + Chr(34) + session_name + Chr(34) + " -l " + Chr(34) + login_id + Chr(34) + " -pw " + Chr(34) + dns_pwd + Chr(34) + " -m " + Chr(34) + cmd_dir + "commands.txt" + Chr(34) + " >> " + Chr(34) + log_dir & log_filename + Chr(34) + Chr(34)
exeCount = Run_Test(argStr, log_dir + log_filename)
End Function
Public Function Run_Test(exeStr, ByVal logFile)
Dim pid, ExitEvent
Dim lineStr
Dim okFlg
Dim hProcess
exeCount = "0"
okFlg = 0
pid = shell(exeStr, vbHide)
hProcess = Extern.OpenProcess(PROCESS_QUERY_INFORMATION + SYNCHRONIZE, 0, pid)
ExitEvent = Extern.WaitForSingleObject(hProcess, 15000)
Extern.CloseHandle(hProcess)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(logFile, ForWriting, True)
Do
f.writeLine lineStr
If InStr(1, lineStr, "/home/") > 0 Then okFlg = okFlg + 1
exeCount = "1"
Loop Until EOF(3)
f.close
If okFlg >= 1 Then
Run_Test = okFlg
Else
Run_Test = -1
End If
End Function
Error that I am getting is on line "**pid = shell(exeStr, vbHide)**
" - "Object doesn't support this property or method: 'shell'"
Any suggestion on how to resolve this issue?
Upvotes: 0
Views: 479
Reputation: 15899
I would suggest you make use of the DotNetFactory to access all that .NET goodness:
Dim csProcess
Set csProcess = DotNetFactory.CreateInstance("System.Diagnostics.Process")
Dim myProcess
Set myProcess = csProcess.Start("notepad.exe")
MsgBox myProcess.Id
If you are not familiar with or don't have access to the MSDN documentation, here is an example with command-line arguments:
Dim SystemProcess
Set SystemProcess = DotNetFactory.CreateInstance("System.Diagnostics.Process")
Dim processStartInfo
Set processStartInfo = DotNetFactory.CreateInstance("System.Diagnostics.ProcessStartInfo")
processStartInfo.FileName = "cmd.exe"
processStartInfo.Arguments = "/c notepad.exe"
Dim myProcess
Set myProcess = SystemProcess.Start(processStartInfo)
msgbox myProcess.Id
Upvotes: 1