Solved

VB Application

Posted on 2001-06-21
6
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

707 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