Tuesday, June 10, 2008

How to interrupt GAMS from VBA

> how can I interrupt GAMS cleanly from VBA (I call GAMS
> from VBA using CreateProcess).

You need to send a Ctrl-C to the GAMS child-process. It turns out that stuffing Ctrl-C in the keyboard buffer is unreliable. The easiest is to use a small DLL available from me that can be called as follows:

Private Declare Function GamsInterrupt Lib "gamsinterrupt.dll" _  
(ByVal pid As Long, ByVal InterruptType As Long) As Long
Private Sub SendInterrupt(InterruptType As Long)
Dim rc As Long
Dim pid As Long
pid = getpid()
rc = GamsInterrupt(ByVal pid, ByVal InterruptType)
End Sub
Private Sub InterruptButton_Click()
' try to interrupt
Call SendInterrupt(0)
End Sub
Private Sub KillButton_Click()
' try to kill
Call SendInterrupt(1)
End Sub

The subroutine InterruptButton_Click() mimics the Interrupt button in the GAMS/IDE and KillButton_Click() works like the Stop button. The DLL performas the low-level code-injection and remote thread managament. See also