VB.Net - Help With Passing Variable to EXE

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
Jimbo99999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

13ShadowCommented:
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
Michael FowlerSolutions ConsultantCommented:
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
Jimbo99999Author Commented:
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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Jimbo99999Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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))
Jimbo99999Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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.
13ShadowCommented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
@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.
Michael FowlerSolutions ConsultantCommented:
@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
Jimbo99999Author Commented:
Thanks all for the responses.  I will be getting back to this project the next two days.
Jimbo99999Author Commented:
I have to try and figure out how to test this now.  The calling program adjustments in test are not completed yet.
Jacques Bourgeois (James Burger)PresidentCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jimbo99999Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
In the Command line arguments TextBox. See attached image.
Image1.png
Jimbo99999Author Commented:
Shoot...I was in the wrong Tab.  Thanks...learning a lot here.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.