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