Description
This is a small sample on using DDE to check for a running previous instance (w/o using App.PrevInstance) and how to pass current command-line to this already running instance.
Usage
In an Std-EXE project's default Form1 form place a TextBox and a ListBox and set form's LinkMode property at design-time (cannot set at run-time) to 1 - Source and then paste this code:
The code is based on Using DDE on Your VB Application article but is refactored into a pvNotifyPreviousInstance function which returns True if a previous instance is notified/running.
Download
The whole project zipped: DDETest.zip
cheers,
</wqw>
This is a small sample on using DDE to check for a running previous instance (w/o using App.PrevInstance) and how to pass current command-line to this already running instance.
Usage
In an Std-EXE project's default Form1 form place a TextBox and a ListBox and set form's LinkMode property at design-time (cannot set at run-time) to 1 - Source and then paste this code:
Code:
Option Explicit
Private Sub Form_Load()
On Error GoTo EH
If pvNotifyPreviousInstance(Command$) Then
Unload Me
Exit Sub
End If
pvOpenDocument Command$
Exit Sub
EH:
MsgBox Err.Description, vbCritical, "Form_Load"
End Sub
Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
On Error GoTo EH
If WindowState = vbMinimized Then
WindowState = vbNormal
End If
SetFocus
pvOpenDocument CmdStr
Cancel = 0
Exit Sub
EH:
MsgBox Err.Description, vbCritical, "Form_LinkExecute"
End Sub
Private Sub pvOpenDocument(sFileName As String)
If LenB(sFileName) <> 0 Then
List1.AddItem sFileName
End If
End Sub
Private Function pvNotifyPreviousInstance(sMessage As String) As Boolean
On Error GoTo EH
txtHidden.LinkTopic = App.Title & "|" & Me.LinkTopic
txtHidden.LinkMode = vbLinkManual
txtHidden.LinkExecute sMessage
txtHidden.LinkMode = vbLinkNone
'--- success
pvNotifyPreviousInstance = True
Exit Function
EH:
End Function
Download
The whole project zipped: DDETest.zip
cheers,
</wqw>