I occasionally need to call out to a batch file or an executable in my Lotusscript. The problem is the code has no native way of knowing when the external process has completed. Hence Execute and wait.
Using the windows API you can create a process and wait for it to complete in you lotusscript code before continuing.
an example would be where you need to run a command-line tool, with arguments, from your code. The output is a csv file that you then need to import. The size of the data is not guaranteed so you need to wait for the windows process to complete before importing.
Example code below
'this is the win32 api information
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&
Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Const STARTF_USESHOWWINDOW = &H1
Const SW_MINIMIZE = 6
Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long,ByVal dwMilliseconds As Long) As Long
Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long,ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVallpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO,lpProcessInformation As PROCESS_INFORMATION) As Long
Sub Initialize()
Dim cmdline As String ' this it the command you wish to run
cmdline = "application.exe -r -p -l output.csv"
Call ExecuteAndWait(cmdline)
msgbox "process finished"
end sub
' this creates the windows process and waits for it to finish before continuing on
Sub ExecuteAndWait(cmdline)
Dim NameOfProc As PROCESS_INFORMATION
Dim NameStart As STARTUPINFO
Dim X As Long
NameStart.wShowWindow = SW_MINIMIZE
NameStart.dwFlags = STARTF_USESHOWWINDOW
NameStart.cb = Len(NameStart)
X = CreateProcessA(0&, cmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS,0&, 0&, NameStart, NameOfProc)
X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
X = CloseHandle(NameOfProc.hProcess)
End Sub
There you have it pretty simple and straightforward. As always let me know if you have questions
Showing posts with label Advanced Lotusscript. Show all posts
Showing posts with label Advanced Lotusscript. Show all posts
Thursday, March 25, 2010
Wednesday, February 18, 2009
Windows explore in your notes forms or even the notes 8 sidebar?
Chris Toohey touched on this a while back. I have been doing this for years and helped him a bit.
Since release 6 I have used this technique extensively to handle file attachments.
The concept is embedding a windows explore object in a form and passing it parameters.
First issue I encountered was anytime I tried to right click on a file or folder I got an error stating running a script on this maybe unsafe. So a little registry code to add all my mapped and local drives to the trusted sites list fixed this.
Next I wanted to lock down the target network share I was storing the files on.
Some win32api code provided a way for me to pass credentials to map and log into the share on form open, and disconnect it on form close. The users never knew where the files were.
I have attached a db here with a form that has the embedded object and the code to add logical and mapped drives.
If you are in Notes 8.x you can make a sidebar widget from the existing document in the db.
Contact me if you need help with this
Since release 6 I have used this technique extensively to handle file attachments.
The concept is embedding a windows explore object in a form and passing it parameters.
First issue I encountered was anytime I tried to right click on a file or folder I got an error stating running a script on this maybe unsafe. So a little registry code to add all my mapped and local drives to the trusted sites list fixed this.
Next I wanted to lock down the target network share I was storing the files on.
Some win32api code provided a way for me to pass credentials to map and log into the share on form open, and disconnect it on form close. The users never knew where the files were.
I have attached a db here with a form that has the embedded object and the code to add logical and mapped drives.
If you are in Notes 8.x you can make a sidebar widget from the existing document in the db.
Contact me if you need help with this
Subscribe to:
Posts (Atom)