Solved

VB.Net - Help With Passing Variable to EXE

Posted on 2013-12-26
16
316 Views
Last Modified: 2014-01-09
Good Day Experts!

I have my next requirement for my new project that I am unsure how to achieve...hopefully you can help.

New Project Deliverable:

Add ability to accept passing in of Variable to User.exe.

The User.exe was in VB6 and I just converted it to Vb.Net.  The calling program is in VB6 and has the following line of code to call the exe and will be modified to pass a Variable:

Shell("C:\Programs\User.exe")

I have to modifiy the User project to take in the newly passed in variable.

The User project Form load event has this following button click to initiate the activity:

Private Sub Form2_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

         Command1_Click(Command1, New System.EventArgs())

End Sub

What do I have to do to my Form2_Load event and Command1_Click to get this newly passed variable into the code?

Thanks for the help,
jimbo9999
0
Comment
Question by:Jimbo99999
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 3

Assisted Solution

by:13Shadow
13Shadow earned 167 total points
ID: 39740862
You can use Environment.CommandLine or  to retrieve any arguments. You can do it right in your load event. I am not sure what you are trying to do with the command1_click event.
http://msdn.microsoft.com/en-us/library/system.environment.commandline(v=vs.110).aspx
0
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 166 total points
ID: 39741289
You can pass in variables at run time using

Public Sub main(ByVal Args() As String)

where args is an array of command line arguments.

Have a look at this tutorial for more info along with an example
http://www.functionx.com/vbasic/topics/main.htm

Michael
0
 

Author Comment

by:Jimbo99999
ID: 39741717
Thanks everyone for your responses.

13Shadow:

This is a little exe written by an individual that is no longer here.  It appears that it use to be a console app with a button on it to initiate the processing.  Then, at some point, it was switched to be fired off by shelling to the exe with the button click put in the load event.  

So, that is what is throwing me off.  Cause now we want to pass a variable to it.  I will take a look at Michael74 link...I sure hope it helps!

Thanks,
jimbo9999
0
 

Author Comment

by:Jimbo99999
ID: 39741727
Om, Michael74...the link sounds good.  But when I went to the project I found no Sub Main()!!!

Can I add it after the fact now?

Thanks,
jimbo99999
0
 
LVL 40
ID: 39741918
You do not need a Main. The method specified by 13Shadow works very well. Just try the following, and you will see what it provides you with:

            MessageBox.Show(Environment.CommandLine)

It could be easier however to call GetCommandLineArgs, since it will isolate the parameter from the call to the application

            MessageBox.Show(Environment.GetCommandLineArgs(1))
0
 

Author Comment

by:Jimbo99999
ID: 39742019
Hello JamesBurger...thanks for responding.

Currently the calling program uses this:

Shell("C:\Programs\User.exe")

Another programmer will be modifying that code to pass the new variable.  I am not sure what that will look like.  Will it be something similar to?:

Shell("C:\Programs\User.exe testvalue")

So I would use Environment.GetCommandLineArgs(1) somewhere in the below code for the exe to get the value of the passed variable?

Private Sub Form2_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

         Command1_Click(Command1, New System.EventArgs())

End Sub

Thanks for the help,
jimbo99999
0
 
LVL 40
ID: 39742054
If your programmer change the Shell command as you show, you simply need a variable to retrieve the parameter:

Dim yourVariable As String = Environment.GetCommandLineArgs(1)

yourVariable will contain "testvalue"

What you do with it is then up to you.
0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39742118
In the program that calls the User.exe change the Shell line to contain the parameter you are passing like this:

Shell("""C:\Programs\User.exe""yourparameterhere")

Then in your load event:

Private Sub Form2_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
' if it is one parm only
  dim myparameter as string  =     Environment.CommandLine

End Sub
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40
ID: 39742186
@13Shadow

Environment.CommandLine gives the full command line, including the call to the program. You have to manipulate the resulting string in order to isolate the parameter(s).

GetCommandLineArgs is more useful because it splits the command into its different components, and getting the second element of the array (GetCommandLineArgs(1)) directly gives you the parameter.
0
 
LVL 23

Expert Comment

by:Michael74
ID: 39745140
@Jimbo99999

Yes you can change

Main()

to

Main(ByVal Args() As String)

without any issue and then add the code to handle the command line arguments.

Michael
0
 

Author Comment

by:Jimbo99999
ID: 39752097
Thanks all for the responses.  I will be getting back to this project the next two days.
0
 

Author Comment

by:Jimbo99999
ID: 39768411
I have to try and figure out how to test this now.  The calling program adjustments in test are not completed yet.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 167 total points
ID: 39768521
To test it, you go into the project's Properties window (Project menu).

In the Debug tab, you can specify commands line arguments and Visual Studio will send them to the application when starting it with F5.
0
 

Author Comment

by:Jimbo99999
ID: 39768587
Hello JamesBurger:

I have the Debug Tab open but I do not see where I would enter the value before hitting F5.
I tried just hitting F5 and received an error on this line that I have in the Form_Load:

AccountNumberPassed = Environment.GetCommandLineArgs(1)

I am probably just not understanding what you are telling me to try. What do you think?

Thanks,
jimbo99999
0
 
LVL 40
ID: 39768793
In the Command line arguments TextBox. See attached image.
Image1.png
0
 

Author Comment

by:Jimbo99999
ID: 39769550
Shoot...I was in the wrong Tab.  Thanks...learning a lot here.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

12 Experts available now in Live!

Get 1:1 Help Now