Solved

VB.Net - Help With Passing Variable to EXE

Posted on 2013-12-26
16
320 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:Michael Fowler
Michael Fowler 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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:Michael Fowler
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

630 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