Solved

VB Application

Posted on 2001-06-21
6
193 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - French characters 6 59
wordmultiple challenge 12 121
drawing animated level bar based on numbers 3 88
MS SQL store procedure to calculate and return result 6 50
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

867 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

21 Experts available now in Live!

Get 1:1 Help Now