Solved

VB.Net - Help With Passing Variable to EXE

Posted on 2013-12-26
16
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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