Implementing a form with DDE prevents multiple instances from running and passes commands

Source: Internet
Author: User
Tags command line implement connect

Internet friends must have used the network ant (Net Ants), right? I wonder if you have noticed in the course of use that it is impossible for you to mobilize two "ants" to work for you--it always shuts down the new run. This is easy to achieve in VB:

Private Sub Form_Load ()

If app.previnstance Then

MsgBox "You've already run this application."

end note: Quit a newly running program

End If

End Sub

So if you run the program after running it, it pops up a message box and refuses to run again. It's very easy.

The beauty of the "ant" Program is that it not only refuses to run, but also activates the "ants" that are running, so there is nothing you can do with the above program. But there are several ways in which you can actually implement a program that refuses to run and activates a run:

1. Use the FindWindow function to get the handle of the form already running (HWND), and then activate it with API functions such as SetActiveWindow. The disadvantage is also obvious, that is, cannot pass the parameter.

2, using the FindWindow function to get the handle of the already running form, send a custom message (with parameters) to the form with SendMessage method, and then intercept and process it in the form, but do this to modify the standard message handler for the form, used in VC, BC or Delphi program is also good, but the workload in VB is too large, and prone to "General Protection line error" so that VB crashes, not very desirable (of course, if you have enough confidence and not afraid of the spirit of collapse, you can try ^_^).

3, the use of DDE technology.

The so-called DDE technology, is the Dynamic data exchange technology. Perhaps you are strange, what does this have to do with the content discussed in this article? And let me speak slowly.

In order to achieve a denial of operation and to activate and implement functions that have already been run, we can first use the method mentioned at the beginning of this article to detect whether the program has been run, if not, the normal operation, if it has been run, the DDE channel with it to pass it a (or some) data, Then by the running of the program to deal with the data, and then to achieve a variety of "unexpected" functions, then perhaps someone to your program shouted: "Cool, cool ..." ^_^

Well, listen to the false, seeing is real, let us move a bit seriously.

Open VB, create a new project, select the "Engineering-> Project 1" In the menu, and change the project name to "P1" (I love laziness, short ^_^), change the "LinkTopic" property of a form to "Formdde", and Change the "LinkMode" property to "1 -Source, add a PictureBox control as the DDE execution control, named Picdde. Then add a TextBox control named "Txtinfo" and set the "MultiLine" property to "True" to display multiple lines of text as a message display control.

Finally, enter the following code in the form code area:

Const COMMANDLINE = "commandline=" NOTE: Or for the sake of convenience, define a constant
Private Sub Form_linkexecute (cmdstr as String, Cancel as Integer)
Static Lngcount as Long
Dim Info as String
info = Txtinfo.text Note: Keep original information
Select Case CMDSTR Note: CMDSTR is a parameter that the DDE program transmits.
Case "Max."
Me.windowstate = 2
info = info + vbnewline + "form has been maximized"
Case "ShowTime"
info = info + vbnewline + "The last time you run this program is:" + Str (now)
Case "Count"
Lngcount = lngcount + 1
info = info + vbnewline + "You have the first" + STR (lngcount) + "Repeated calls to this program." " _
+ vbNewLine + "But afraid you do not pay more, so only run a ^_^"
End Select
If left (Cmdstr, Len (COMMANDLINE)) = COMMANDLINE Then
info = info + vbnewline + "The new program has run" + vbnewline + "command behavior on command line:" _
+ vbNewLine + Right (cmdstr, Len (CMDSTR)-Len (COMMANDLINE))
End If
Txtinfo.text = info Comment: Show the information
Cancel = False
End Sub
Private Sub linkandsendmessage (ByVal Msg as String)
Dim T as Long
Picdde.linkmode = 0 Note:--
Picdde.linktopic = "p1| Formdde Note: |______ connect the DDE program and send data/parameters
Picdde.linkmode = 2 Note: | | is the pipe character, which is the vertical bar next to the BACKSPACE key.
Picdde.linkexecute MSG Note:--not letters or numbers!
t = picdde.linktimeout Annotation:--
Picdde.linktimeout = 1 Note: |______ terminates the DDE channel. Of course, there are other ways
Picdde.linkmode = 0 Note: | This is a method for terminating the timeout.
Picdde.linktimeout = t annotation:--
End Sub
Private Sub Form_Load ()
If app.previnstance Then Note: Whether the program is already running
Me.linktopic = "" Note: These two lines are used to clear the DDE server properties of the newly running program.
Me.linkmode = 0 Note: Otherwise, there will be trouble connecting to the DDE program.
Linkandsendmessage "Max" Note:--
Linkandsendmessage "Count" NOTE: |-----Connect DDE Accept program and transfer data/parameters
Linkandsendmessage "ShowTime" Note:--
If command <> "" Then Note: If there is a command-line argument, pass the past
Linkandsendmessage COMMANDLINE + Command
End If
end note: Ending the running of a new program
End If
End Sub

Test:

The project "P1" compiled into an EXE file (set name for P1. EXE)

1, open "My Computer", find P1. EXE and execute. You can see the program running properly.

2, run again, the new program does not run successfully, but the original running program is maximized, and the text box has the following

Character:

Form has been maximized

You have called this program repeatedly for the 1th time.

But afraid you do not pay more, so only run a ^_^

The last time you run this program is: 00-2-6 7:11:01

3, open MS-DOS, with the command line to run the program again, such as "P1 How to Are you?"

The original running Program text box in a few more lines of words:

Form has been maximized

You have called this program repeatedly for the 2nd time.

But afraid you do not pay more, so only run a ^_^

The last time you run this program is: 00-2-6 7:14:32

The new program was run as a command line

Command behavior:

How Are?

OK, run exactly right, and then you can apply it to your program.

Of course, this is just some personal experience, if there are omissions, I also ask you to correct the shrimp.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.