Solved

How to tell VB accept parameter ?

Posted on 2002-05-28
29
356 Views
Last Modified: 2010-08-05
Here is my way of openning the vb program :
C:\myVB.exe abc 123 This_is_a_test

At this time, myVB still open. When I issue
above cmd on MS-DOS, myVB should able to pick it
and do msgbox the parameter. Is that possible ?
0
Comment
Question by:dynomite082002
  • 11
  • 4
  • 4
  • +7
29 Comments
 
LVL 4

Expert Comment

by:Monchanger
ID: 7040156

Microsoft's example function (can be found at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctcommandx.asp)

Function GetCommandLine(Optional MaxArgs)
   'Declare variables.
   Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
   'See if MaxArgs was provided.
   If IsMissing(MaxArgs) Then MaxArgs = 10
   'Make array of the correct size.
   ReDim ArgArray(MaxArgs)
   NumArgs = 0: InArg = False
   'Get command line arguments.
   CmdLine = Command()
   CmdLnLen = Len(CmdLine)
   'Go thru command line one character
   'at a time.
   For I = 1 To CmdLnLen
      C = Mid(CmdLine, I, 1)
      'Test for space or tab.
      If (C <> " " And C <> vbTab) Then
         'Neither space nor tab.
         'Test if already in argument.
         If Not InArg Then
         'New argument begins.
         'Test for too many arguments.
            If NumArgs = MaxArgs Then Exit For
            NumArgs = NumArgs + 1
            InArg = True
         End If
         'Concatenate character to current argument.
         ArgArray(NumArgs) = ArgArray(NumArgs) & C
      Else
         'Found a space or tab.
         'Set InArg flag to False.
         InArg = False
      End If
   Next I
   'Resize array just enough to hold arguments.
   ReDim Preserve ArgArray(NumArgs)
   'Return Array in Function name.
   GetCommandLine = ArgArray()
End Function


Which returns an array of variants (watch out for index 0, which is Empty)

To use your arguments in design time (when you debug), enter them in Project->Project Properties, "Make" tab -> "Command Line Arguments" (do not include the name of your .exe)
0
 

Author Comment

by:dynomite082002
ID: 7040176
Monchanger,

The codes you pasted will only work as long as myVB
is not openning before I issue the cmd, right ?

What I want is - it will still able to get the parameter
while myVB is still exist on the desktop.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 7040179
:-/
0
 
LVL 4

Expert Comment

by:Monchanger
ID: 7040249
I'm sorry, but I didn't understand your last comment. Please explain what you mean. I'll venture a guess as to what you meant to hopefully save you some time.

Are you saying you want a shortcut on the desktop to contain the command line arguments ? If so, you can set the "Traget" property of the shortcut to "[EXE full path]" [arguments]

0
 

Author Comment

by:dynomite082002
ID: 7040256
Sorry ! My English. Let me make an example.

First I issue CMD like :
C:\myVB.exe 123 abc
then it will open myVB at this point to display
123 and abc on the screen or in msgbox.
I don't close myVB.exe

Second, I reissue CMD again :
C:\myVB.exe 234 bcd
Same thing, I will see 234 and bcd on the screen or msgbox.

Is that possible ? What I know is - I have to close myVB
before I issue another CMD, right ?
But I don't want to close myVB.exe.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7040287
It sounds like you want to display the parameters in a msgbox when the application starts?

In form_load:

if Command$ <> "" then
   msgbox "Command line: " & Command$
end if

You can also parse them using the code from Monchanger.

Let us know if we are off base...
0
 

Author Comment

by:dynomite082002
ID: 7040293
gencross,

if Command$ <> "" then
  msgbox "Command line: " & Command$
end if

Above code is what I am looking for, but it will only work
as long as myVB have not been openned yet.

myVB is openned all the time, and it cannot be closed.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7040311
It should not matter that an instance of your exe is already running.  If you start another instance of the exe with parameters it should display those.  

I have just tried this using the above code in a form load event and compiled an exe.  It works multiple times while leaving the EXE running.

Perhaps you could post some code, or give more info.
0
 

Author Comment

by:dynomite082002
ID: 7040329
Only one instance of myVB can run at a time.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7040357
The only way I can think of to accomplish what you want is when you run the EXE check to see if there is another instance running already.  If so then kill it and run the new one, which will run using the new parameters.  Off the top of my head I do not know how this (killing) can be done.

Does this sound like it will work for you?
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 7040359
Do you possibly have code that precludes a second instance of the program starting?

We do that on a database program we have to prevent the user from updating records from multiple instances.

mlmcc
0
 

Author Comment

by:dynomite082002
ID: 7040379
gencross,

I like your idea. What I have right now is preventing a new
myVB from running which is opposite of your idea.

Do you have code for your idea ?
0
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 7040607
Private Sub Form_Load()
    MsgBox Command
    If App.PrevInstance Then
        Unload Me
    End If
End Sub

Have a look at the code given above.

It works fine. However, in this case the latest instance is killed and the first one keeps running. Your requirement of just one instance running at a time is satisfied, but i don't know how exactly you want it to happen.

0
 
LVL 2

Expert Comment

by:ventond
ID: 7040751
This is probably way to hard to bother with, and I can't help you with the code but...

in the example:
If app.PrevInstance then
  Unload Me
end if

In front of unload do a :
SendMessage Special Message,to previnstance, Command

and have your program listen to the messages looking for Special Message, and acting appropriately.

Sorry I'm not more specific on how, I haven't had reason to write a app that talks to other apps.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:dynomite082002
ID: 7040854
ventond,

You have mentioned about SendMessage. But
what API should I use to tell myVB listen to the message ?

p/s: winsock is not an option.
0
 
LVL 3

Expert Comment

by:MCummings111400
ID: 7040879
Use DDE to link to your previous application and send the commandline parameters.
0
 
LVL 1

Expert Comment

by:zzconsumer
ID: 7041140
Have a look at the question TimCottee answered for me just a few days ago. This is surely what you need. I'm sorry the question is already PAQ, but copy and paste would not be fair, I think.

This is a solution using DDE. It worked for me.

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20303531

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7041168
zzconsumer, you can repost it if you like. As you say this is the solution for this problem as well.
0
 
LVL 1

Expert Comment

by:zzconsumer
ID: 7041191
Thanks. Here it is:

Change TDDE to the name of your executable, add two textboxes and set the form's property 'LinkMode' to 'source'.

Private Sub Form_Load()
 If App.PrevInstance Then
     Text2.LinkTopic = "TDDE|Form1"
     Text2.LinkItem = "Text1"
     Text2.LinkMode = vbLinkAutomatic
     Text2.Text = Command()
     Text2.LinkPoke
     End
 End If
End Sub

Private Sub Text1_Change()
 MsgBox Text1.Text
 ' In Text1.Text, the passed parameter is now in.
End Sub

You may do it with one TextBox as I finally did, but this way you avoid any possible confusion.

0
 
LVL 4

Expert Comment

by:gencross
ID: 7041635
dynomite, sounds like you have a good solution now.  

I hope it works for you.
0
 

Author Comment

by:dynomite082002
ID: 7041738
MCummings and zzconsumer,

I have thought of DDE, but will work because dde server and client is the same EXE ?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7041772
It will indeed work because they are not the "Same" executable, merely seperate instances of it. The situation zzconsumer was in was in respect of selecting multiple files in explorer and opening them all with his application. In this case there are potentially hundreds of instances of the application all open for a short time. Even in this case this works happily as the DDE connection is always to the first registered instance of the application. You could test this using DDE-SPY to determine that subsequent registrations as a DDE server by the subsequent instances actually fails because a DDE server already exists.
0
 

Author Comment

by:dynomite082002
ID: 7041785
Problem !

This is what I have :

Private Sub Form_Load()
Text1.Text = "1"   'Initialize
If App.PrevInstance Then
    Text2.LinkTopic = "project1|Form1"
    Text2.LinkItem = "Text1"
    Text2.LinkMode = vbLinkAutomatic
    Text2.Text = Command()
    Text2.LinkPoke
    End
Else
  Text1.Text = Command()
End If
End Sub


Private Sub Text1_Change()
Print Text1.Text
End Sub


when I called C:\project1.exe 123 for the first time, I saw
123in text1.text, but it did not print 123 on the form.

when I called it for the 2nd time C:\project1.exe abc, I saw abc in text1.text and printed on the form.

How do I make it printed for the 1st time ?
0
 

Author Comment

by:dynomite082002
ID: 7041788
when can I get DDE-Spy ?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7041793
Make sure that the form is clear the first time, in your form_load do Me.Cls to reset the current print position.

DDE-spy comes with MS Visual Studio 6 full install, if you have this then it is in the tools folder. It will show you all the DDE conversations and messages that are being used. You may find that there is a lot of stuff there that is not entirely clear (I did to start with). But you can get the sense of what is going on by studying these messages.
0
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 7041806
I found out something else about App.PrevInstance property, which i didn't know before.

As long as the ONLY instance of an APP is running the property returns FALSE. However, when another instance starts executing, property value in BOTH instances changes to TRUE.

In light of this, are you ready to consider some sort of loop which keeps checking for change in this value and then terminates the APP?

If yes, please let me  know. I might spend some time writing such a loop.

Drawback of this approach is Performance!
0
 

Author Comment

by:dynomite082002
ID: 7041810
TimCotte,

I put Me.Cls before Text1.Text = "1"   'Initialize.
I still cannot see it for the 1st time.
0
 

Author Comment

by:dynomite082002
ID: 7041824
I do initialize the text1.text from the beginning, and
Private Sub Text1_Change() should pick it up, right ?
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 120 total points
ID: 7041846
Silly me, the reason you don't see it is that the change occurs before the form is actually displayed so it gets cleared.

If you set the Form's .AutoRedraw property to True then you will see it the first time!

Nitin, you may well be right but unless you are running an extremely slow machine the first instance will have passed the .PrevInstance check before any subsquent instance is created so the problem never occurs.

If this is ever a problem there are a number of other ways to determine whether a previous instance exists using API calls and the like.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

23 Experts available now in Live!

Get 1:1 Help Now