Thursday, July 20, 2006

SNTT - How to shell a Windows application from LotusScript and close it

A recent post on Notes.Net asked about shelling a Windows application and closing it. I suggested the poster look up a couple of Windows API calls that would help, then I went digging for them myself. It wasn't quite as simple as I expected to find the necessary information, so I thought I would post it here:
(Declarations)
Const SEE_MASK_NOCLOSEPROCESS = &H40
Const SEE_MASK_FLAG_NO_UI = &H400

Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type

Declare Function ShellExecuteEx Lib "shell32.dll"_
Alias "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long

Declare Function TerminateProcess Lib "kernel32"_
Alias "TerminateProcess" (Byval hProcess As Long, Byval uExitCode As Long) As Long

Sub Initialize
Dim SEI As SHELLEXECUTEINFO

SEI.cbSize = Len(SEI)
SEI.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_NO_UI
SEI.lpVerb = "open"
SEI.lpFile = "c:\sample.txt"
SEI.nShow = 1
SEI.hInstApp = 0
SEI.lpIDList = 0

Call ShellExecuteEx(SEI)

Messagebox "Application opened.", 0, "Success"
Call TerminateProcess(SEI.hProcess, 0)

End Sub

This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.


,

3 comments:

  1. Very nice. Several times, I've written apps or snippets that print attachements, without knowing which what application to use to do it. I've usually parsed out the extension then picked from a pre determined list of "known apps"

    Without doing any research myself, I suspect that your DLL call will let windows to the work. My guess is that SEI.lpVerb = "print" would do the trick. I don't have a need for it now, but, I've added this to my library.

    ReplyDelete
  2. You are correct, ShellExecuteEx looks up the file association and launches the file using the default application. There are a number of other verbs that can be used.

    ReplyDelete
  3. Great! Just what I needed, thanks a lot.

    ReplyDelete