Solved

VB Application

Posted on 2001-06-21
6
191 Views
Last Modified: 2013-11-13
Suppose I have an MDI application written in VB. This application can be executed with command line parameter which is the name of the document to open.
The question is if the application is already running and I execute "MyApp.exe /Mydocument.xxx" how can I make the new instance detect that there is an already running instance (The easy part of the question "App.PrevInstance") and force the already running instance to open the document, pointed to in the command line?
0
Comment
Question by:Herzalah
  • 3
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 65 total points
ID: 6214863
You are right about the first part, the second bit is up to you. You need to begin some form of conversation between the two instances. There are a number of ways that you could do this, Windows Sockets (winsock), DDE or API messaging. The simplest is perhaps DDE. If you set the form's LinkMode property to True you can then add a textbox somewhere which can act as a DDE source/destination. In the second instance of the application, use a different textbox with the dde link information of the first to send the filename to the original instance. The Chande event of this textbox can be used to run the appropriate procedure to open the file as you would normally.
0
 
LVL 1

Expert Comment

by:CArnold
ID: 6399857
Herzalah,
   I think I may have a solution to your problem.  I encountered a need similar to what you are asking for when App.PrevInstance proved futile in NT Terminal Server.
   What I did was create a DLL with 2 parameters.  The 1st parameter is App.EXEName.  The 2nd was the text that would appear in the application's title bar (usually, App.Caption would work here).
   You would declare an instance of this DLL in the startup Form or .bas module.  Reference it in the Form_Load (or Sub Main(), if starting from a .bas module).

   The code requires a lot of API calls.  The DLL I mentioned is responsible for acquiring information of a possible previous instance of the application running.  IT does this by:
   1) Grabbing the user's Logon ID and the app's App.EXEName
   2) Creating a text file (AS RANDOM) and writing something to it (Date, Additional parameter values, etc.)
   3) Close and open the text file as READ ONLY under a new file #.  LOCK the text file!
   4) Include and ON ERROR condition at the beginning of the procedure.  When another instance of the application tries to run, it will try to write to the textfile that is locked.  This will create an error.  The ON ERROR procedure should recognizes this error returns a value indicating that another instance of the application is running.  If the application with locked control of the text file is prematurely terminated, the text file becomes unlocked, eliminating possibility of your application falsely reporting an previous instance of an app.

If the application exists, the DLL passes the 2nd parameter to an .exe that is responsible for maximizing and bringing focus to the app.
   I chose to make an .exe file responsible for the maximizing and focusing of the app because an out-of process component was required.  When I tried incorporating this feature into the original DLL, sometimes the app I was trying to prevent from being fully executed was being maximized before terminating.

   If you post your e-mail address, I could e-mail you (and anyone else interested) the source code for the procedures I've mentioned.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6450967
Herzalah, a response from you would be appreciated.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Herzalah
ID: 6451383
Sorry guys for staying far away from this questions. AT the same time I appreciate the efforts you are putting in this.
0
 
LVL 1

Expert Comment

by:CArnold
ID: 6451409
?????!
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6452468
What can we do for you Herzalah?

To give you some code to achieve this:

Here is is in its simplest form:

Private Sub Form_Load()
  If App.PrevInstance Then
      Text2.LinkTopic = "TDDE|Form1"
      Text2.LinkItem = "Text1"
      Text2.LinkMode = vbLinkAutomatic
      Text2.Text = InputBox("Send Message", "To Previous Instance", "A Message")
      Text2.LinkPoke
      End
  End If
End Sub

Private Sub Text1_Change()
  MsgBox Text1.Text
End Sub

Where TDDE is the name of the executable.  

Another method which is significantly more complex but you wish to consider:

http://vbaccelerator.com/codelib/ssubtmr/startup.htm
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now