DDE programming VB (this was previously seen when the csdn-VB community was mixed. It was clearly written and its principles were clearly explained)

Source: Internet
Author: User

Please use shellor shellexecuteto call an external. exe program. Then, the returned value of this. EXE is obtained.

You can use DDE for implementation. Here is an example.
Open VB, create a project, select "Project-> Project 1 property" in the menu, and change the project name to "p1" (I love
Lazy, short to short), change the "linktopic" attribute of an existing form to "formdde", and set "linkmode"
Change the property to "1-source" and add a picturebox control as the DDE execution control named picdde. Then add
Textbox Control, named "txtinfo", and set the "multiline" attribute to "true" to display multiple lines of text
Message Display Control.

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

Const CommandLine = "CommandLine =" Note: To save time, define a constant

Private sub form_linkexecute (reverse STR as string, cancel as integer)
Static lngcount as long
Dim info as string

Info = txtinfo. Text Note: retain original information

Select case when STR Note: When STR is a parameter transmitted by the DDE Program
Case "Max"
Me. windowstate = 2
Info = info + vbnewline + "The form has been maximized"
Case "Showtime"
Info = info + vbnewline + "the last time this program was run:" + STR (now)
Case "count"
Lngcount = lngcount + 1
Info = info + vbnewline + "you have already called this program again" + STR (lngcount) +. "_
+ Vbnewline + "I am afraid that you will not pay much, so I only run one ^_^"
End select

If left (comment STR, Len (CommandLine) = CommandLine then
Info = info + vbnewline + "the new program was run as a command line" + vbnewline + "Command Behavior :"_
+ Vbnewline + right (reverse STR, Len (reverse Str)-len (CommandLine ))
End if

Txtinfo. Text = info Note: display 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 to the DDE Program and send data/parameters
Picdde. linkmode = 2 Note: | "|" is the pipe operator, which is the vertical line next to the "backspace key,
Picdde. linkexecute MSG Note: -- not a letter or number!

T = picdde. linktimeout Note :--
Picdde. linktimeout = 1 Note: | ______ terminate the DDE channel. Of course, you can also use other methods.
Picdde. linkmode = 0 Note: | the force termination method is used here.
Picdde. linktimeout = t Note :--
End sub

Private sub form_load ()
If app. previnstance then Note: whether the program is running

Me. linktopic = "" NOTE: These two lines are used to clear the DDE Server attributes of the new program,
Me. linkmode = 0 Note: otherwise, when the DDE Program is connected, there will be errors.

Linkandsendmessage "Max" NOTE :--
Linkandsendmessage "count" NOTE: | ----- connect to the DDE receiver and transmit data/parameters
Linkandsendmessage "Showtime" NOTE :--

If command <> "" Then Note: If a command line parameter exists, it is passed
Linkandsendmessage CommandLine + command
End if
End note: end the running of the new program
End if
End sub

Test:
Compile the project "p1" into an EXE file (named p1.exe)
1. Open "my computer", find p1.exe and execute it. You can see that the program is running normally.
2. Run the program again. This time the new program was not successfully run, but the original program was maximized, and the text box contains the following
Character:

The form has been maximized.
You have already called this program 1st times.
But if you don't pay much, you only run one slave _^
The last time this program was run: 00-2-6 7:11:01

3. Open the MS-DOS method, run the program again with the command line method, such as "p1 how are you ?"
At this time, the original program text box contains a few more lines:

The form has been maximized.
You have already called this program 2nd times.
But if you don't pay much, you only run one slave _^
The last time this program was run: 00-2-6 7:14:32
The new program was run as a command line
Command Behavior:
How are you?

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.