?
Solved

VB Application

Posted on 2001-06-21
6
Medium Priority
?
202 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 195 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

764 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