-
Feb 1st, 2023, 08:41 AM
#1
[VB6] Using DDE to check for previous instance
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 be set at run-time) to 1 - Source and then paste this code:
Code:
Option Explicit
Private Declare Function RtlGenRandom Lib "advapi32" Alias "SystemFunction036" (RandomBuffer As Any, ByVal RandomBufferLength As Long) As Long
Private Const STR_LINK_TOPIC As String = "DDELink"
Private Sub Form_Load()
Const MAX_CONCURRENCY As Long = 5
Dim lIdx As Long
On Error GoTo EH
LinkTopic = vbNullString
If pvNotifyPreviousInstance(STR_LINK_TOPIC, Command$) Then
Unload Me
Exit Sub
Else
LinkTopic = STR_LINK_TOPIC & (pvGetRandom Mod MAX_CONCURRENCY)
For lIdx = 0 To MAX_CONCURRENCY - 1
DoEvents
If pvNotifyPreviousInstance(STR_LINK_TOPIC & lIdx, vbNullString) Then
LinkTopic = vbNullString
Do
If pvNotifyPreviousInstance(STR_LINK_TOPIC, Command$) Then
Unload Me
Exit Sub
End If
Loop
End If
Next
End If
LinkTopic = STR_LINK_TOPIC
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 LinkTopic = STR_LINK_TOPIC Then
If WindowState = vbMinimized Then
WindowState = vbNormal
End If
If Visible Then
SetFocus
End If
pvOpenDocument CmdStr
End If
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 Timer & vbTab & sFileName
End If
End Sub
Private Function pvNotifyPreviousInstance(sTopic As String, sMessage As String) As Boolean
On Error GoTo EH
txtHidden.LinkTopic = App.Title & "|" & sTopic
txtHidden.LinkMode = vbLinkManual
txtHidden.LinkExecute sMessage
txtHidden.LinkMode = vbLinkNone
'--- success
pvNotifyPreviousInstance = True
EH:
End Function
Private Function pvGetRandom() As Long
Call RtlGenRandom(pvGetRandom, 2)
End Function
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: DDETest2.zip
cheers,
</wqw>
Last edited by wqweto; Feb 2nd, 2023 at 06:29 AM.
-
Feb 1st, 2023, 08:46 AM
#2
Re: [VB6] Using DDE to check for previous instance
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Jul 7th, 2023, 10:57 PM
#3
New Member
Re: [VB6] Using DDE to check for previous instance
Much simpler i think using like this:
just paste to Form1, add listbox and textbox, don't forget to set form property LinkMode = 1 - Source, and LinkTopic = "DDELink"
Code:
Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
Me.SetFocus
List1.AddItem CmdStr
Cancel = 0
End Sub
Private Sub Form_Load()
'Run Twice
If App.PrevInstance Then
'Previous instance of App found
If Command$ <> vbNullString Then
'New commandLine parameters to send
Text1.Text = Command$
'Text1.LinkTopic = "ProjectName|LinkTopic_of_Form_Property"
Text1.LinkTopic = "Project1|DDELink"
'Set the link topic
Text1.LinkMode = vbLinkManual
'Initiate the DDE link
Text1.LinkExecute Text1.Text
'Send data
Text1.LinkMode = vbLinkNone
'Close Link
End If
Unload Me
'Close the copy of the app
Exit Sub
End If
'Run First
If Command$ <> vbNullString Then
List1.AddItem Command$
End If
End Sub
Dont forget to compile, and voila....
-
Jul 8th, 2023, 03:46 PM
#4
Re: [VB6] Using DDE to check for previous instance
Did you read the "using DDE to check for a running previous instance (w/o using App.PrevInstance)" part in the first sentence? Why are you using App.PrevInstance in your code then?
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|