sending command line args to running app


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: sending command line args to running app

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    sending command line args to running app

    [Originally posted by david]

    I have an application set up so that only one instance is allowed.˙ This works by checking to see if one is already open, and if it is closing.˙ My problem, however, is that I would like to pass command line arguments to my program.˙ How can I get the command line arguments from the subsequent programs to the running program?˙ I thought of using DDE, but could not figure out how.˙ Any ideas would be helpful, and I'm happy to clarify.

    -David

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:sending command line args to running app

    [Originally posted by Jose Sanchez]

    Hi,
    at any point of your app you can see
    the valueof global predefined variable called
    command$

    sub form_Load()
    ˙ msgbox command$
    end sub

    sub Main()
    ˙ msgbox command$
    end sub

    if you call your app by passing
    params...

    c:\MyApp.exe˙ -say hello

    inside your app you will get
    ?command$
    "-say hello"

    ˙

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:sending command line args to runni

    [Originally posted by david]

    I know this, my problem is that I only want one instance of the program to be open, but to recieve the command line arguments from subsequent openings.˙ For instance, if I run app.exe, and then later i run app.exe args, I want args to be passed to the first time it was run.˙ This way I can have only one instance open, but still recieve information from other programs using the Shell command.˙ Does this make sense?˙

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:sending command line args to runni

    [Originally posted by Mikekay2]

    Don`t think you can do this, only way I can see easily is to create a registry entry or file from the second instance and regularly check from the first instance. You might be able to do something with a windows message API call sending a message to your current window depending how complex it is.

  5. #5
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:sending command line args to

    [Originally posted by david]

    I only need to send one line of text from one program to another.˙ How do I do this with the windows messaging API?

  6. #6
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:Re:sending command line args to

    [Originally posted by m.posseth@work]

    hmmmmmmm :-|


    if i were you i wouldn`t spend time on technology`s that isn`t supported annymore ( like DDE ) if i wre you i would convert my executable to an activex.exe˙ than make a public property and fill that with the value you want from the outside ,,,,,,, big advantage is that a complete new world would be lying on your feet if you understand this, because then you can even make external callable procedures , you can even pass complete recordsets from one app to the other ,,,, and the world of a multithreaded vb6 app is also comming closer and closer :-)

  7. #7
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Re:Re:Re:Re:sending command line args to

    [Originally posted by Marty]

    Hi David,

    Try this...

    Create a new project as type ActiveX EXE.˙ Call the project "SingleInstanceUtil".˙ It will have a default Class file in it - rename this to "GlobalClass".˙ Set the instancing property of GlobalClass to Global MultiUse.

    Paste this code into GlobalClass:
    Option Explicit

    Public Property Get AlreadyRunning() As Boolean
    ˙ ˙ AlreadyRunning = mbAlreadyRunning
    End Property

    Public Property Let AlreadyRunning(ByVal NewValue As Boolean)
    ˙ ˙ mbAlreadyRunning = NewValue
    End Property

    Public Property Get CommandData() As String
    ˙ ˙ CommandData = msCommand
    End Property

    Public Property Let CommandData(ByVal NewValue As String)
    ˙ ˙ msCommand = NewValue
    End Property

    Add a module to the project, and call it whatever you like.˙ Paste this code into the module:

    Option Explicit

    Public mbAlreadyRunning As Boolean
    Public msCommand As String

    Save your project and compile it.



    Create a new project, make this one a standard exe.˙ Go into Project -> References, and add the reference for "SingleInstanceUtil".˙ Add a timer to the form, set Enabled to False and Interval to 1000.˙ Then, copy the following code into the default form:

    Option Explicit
    Dim objSI As New SingleInstanceUtil.GlobalClass

    Private Sub Form_Load()
    ˙ ˙ Dim sCommand As String
    ˙ ˙ sCommand = Trim$(Command$)
    ˙ ˙
    ˙ ˙ If objSI.AlreadyRunning Then
    ˙ ˙ ˙ ˙ If sCommand = "" Then
    ˙ ˙ ˙ ˙ ˙ ˙ MsgBox "This program is already running...", vbExclamation, "Single Instance Only"
    ˙ ˙ ˙ ˙ ˙ ˙ End
    ˙ ˙ ˙ ˙ Else
    ˙ ˙ ˙ ˙ ˙ ˙ objSI.CommandData = sCommand
    ˙ ˙ ˙ ˙ ˙ ˙ MsgBox "Command line has been submitted...", vbInformation, "Data Sent"
    ˙ ˙ ˙ ˙ ˙ ˙ End
    ˙ ˙ ˙ ˙ End If
    ˙ ˙ Else
    ˙ ˙ ˙ ˙ objSI.AlreadyRunning = True
    ˙ ˙ ˙ ˙ objSI.CommandData = ""
    ˙ ˙ End If
    ˙ ˙
    ˙ ˙
    ˙ ˙ Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Timer()
    ˙ ˙ If objSI.CommandData <> "" Then
    ˙ ˙ ˙ ˙ MsgBox "Command has been received..." & vbCrLf & vbCrLf & objSI.CommandData, vbInformation, "Data Received"
    ˙ ˙ ˙ ˙ objSI.CommandData = ""
    ˙ ˙ End If
    End Sub


    Compile this project and test.˙ It should only allow a single instance.˙ If you try to run another instance by itself, it will alert you and exit.˙ If you run another instance with command line data, it will pass this data on to the first instance and exit.˙ The first instance will have a timer running (every 1 second) and will be looking for command line data.˙ When it sees it, it will pop up a message box.

    I hope this helps!

    Marty

    ps - my code doesn't show it, but you should do a "Set objSI = Nothing" before each end statement and in the form_unload() event...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center