Parameters in an executable

itms

Member
Joined
Nov 5, 2018
Messages
10
Programming Experience
10+
Hi,

My company has an MS Access application, and in the VBA a of this, they have a form with data in the fields. They had me make a C# application that needs to use the data from these fields on the MS Access form.
I could easily send this data to a table and write SQL to access this in the C# application. However, they would like me to make an executable of the C# application and pass the fields in parameters to the executable.
I cannot find anything on this is it possible? if so can someone give me an idea of how to do this?

Thanks
 
How much data are we talking about? C# apps can easily accept commandline parameters and you can access them in a variety of way within your C# code. That's what you should be researching. The best way to access the parameters depends on the type of application. If it's a WinForms app, you probably want to call Environment.GetCommandLineArgs. If it's a Console app, you can use a string array parameter in the Main method.
 
How much data are we talking about? C# apps can easily accept commandline parameters and you can access them in a variety of way within your C# code. That's what you should be researching. The best way to access the parameters depends on the type of application. If it's a WinForms app, you probably want to call Environment.GetCommandLineArgs. If it's a Console app, you can use a string array parameter in the Main method.
Hi, thanks for the reply. So, this will be coming from the VBA application, and it will be some 20 to 30 fields. I am not familiar with the command line but am looking into it. However, first, this is a form project and not a counsel project. Also, with each run, the data in the field will change, so I do not think a command line option will work. Can you call an executable and pass parameters to it as I need here? I personally do not think you can do that. Can you tell me if that is possible?
Thanks
 
As long as the VBA application calls the C# WinForms program everytime there is a change, passing via the command line should work. A new instance of your WinForms programs will just be launched.

If the plan is for there to only be one instance of your WinForms application, then you'll need to play some "single-instance" games. It'll get more complicated passing the command line that one instance gets to the master running instance, but it's doable.

I'm not familiar enough with how VBA launches other executables. Depending on how it launches the other executables, you maybe limits to a low as 2048 characters. More detailed readings in link below:

For you on the C# WinForms side, all you need to do is parse the command line arguments passed to you by examining Environment.GetCommandLineArgs() as mentioned above.
 
As long as the VBA application calls the C# WinForms program everytime there is a change, passing via the command line should work. A new instance of your WinForms programs will just be launched.

If the plan is for there to only be one instance of your WinForms application, then you'll need to play some "single-instance" games. It'll get more complicated passing the command line that one instance gets to the master running instance, but it's doable.

I'm not familiar enough with how VBA launches other executables. Depending on how it launches the other executables, you maybe limits to a low as 2048 characters. More detailed readings in link below:

For you on the C# WinForms side, all you need to do is parse the command line arguments passed to you by examining Environment.GetCommandLineArgs() as mentioned above.
Okay, I know how to call an executable in VBA, just not sure how to pass the parameters in the call. Also, I was not sure you could use this with a Form project. However, I will read these and see what I can do. Thank you
 
If the plan is for there to only be one instance of your WinForms application, then you'll need to play some "single-instance" games. It'll get more complicated passing the command line that one instance gets to the master running instance, but it's doable.
If a single instance is desired, it may actually be better to do it in VB than C#, as single-instance support is baked into VB projects. That applies to .NET Framework projects, at least. I'm not sure whether that feature made the journey to .NET Core or not.
 
So, all the things that I have found so far tell how to put the command line in but does not give an example how to use it.
For example, the following gives some instructions as follows :
To run your project with command line arguments within Visual Studio:

  1. Right-click the default project (the one to be run) in Visual Studio and select "Properties".​
  2. Click on the "Debug" tab on the left.​
  3. Enter your command line arguments in the textbox labeled "Command line arguments".​
  4. Save the updated properties and run the project.​
In the vba to run the calculator exe I would do the following
Dim RetVal
' Run Calculator.
RetVal = Shell("C:\Windows\System32\CALC.EXE", 1)
If my exe would have parameters, how would I include them here?
Do you know of some site that would explain how to use it with the executable?
Thank you
 
Last edited:
Code:
Dim RetVal
RetVal = Shell("C:\Windows\System32\Notepad.exe abc.txt", 1)

The "abc.txt" is the parameter being passed to notepad.exe to tell it to try to open (or create) a file named "abc.txt".
 
Hi,
I am sorry, I do not think I am making my issue clear. Sorry about that.
So let me try here. I can run an executable in VBA with no problem.
What I cannot do is put the command line in as sites have suggested and call the exe with parameters.
So, in my C#, in the debug command line I put the following:
Coomanline:
static void Main(string[] args)
        {
            if(args == null || args.Length == 0)
            {
                Console.WriteLine("Please specify arguments!");
            }
            else
            {
                Console.WriteLine(args[0]);     // First
                Console.WriteLine(args[1]);     // Second Argument
            }
        }
In my VBA code I have:
VBA Code:
Dim RetVal
' Run Calculator.
RetVal = Shell("C:\Import\CmdTest.exe 2 3", 1)

I made an simple form app here that takes two numbers and adds them see below
And while this will open the application, which adds two numbers it will not fill in the 2 and 3 and add them.
It just opens it.

C# Code:
 private void btnEnter_Click(object sender, EventArgs e)
        {
            int x = int.Parse(txtInput1.Text);
            
            int y = int.Parse(txtInput2.Text);
            int Total;
          
            txtdisplay.Text = (x + y).ToString();
          
                  
        }


Now I personally cannot see how this would work, but it is what every place I go and what everyone is telling me to do.

If anyone has any idea what I am doing wrong, please let me know.


Thank you
 
Last edited:
It won't fill in your textboxes automatically. You need to add code to put the values into the text boxes. Good luck with 30-40 parameters to fill in.

Or just these lines:
C#:
int x = int.Parse(txtInput1.Text);
int y = int.Parse(txtInput2.Text);
to:
C#:
char [] args = Environment.GetCommandLineArgs();
int x = int.Parse(args[0]);
int y = int.Parse(args[1]);
 
It won't fill in your textboxes automatically. You need to add code to put the values into the text boxes. Good luck with 30-40 parameters to fill in.

Or just these lines:
C#:
int x = int.Parse(txtInput1.Text);
int y = int.Parse(txtInput2.Text);
to:
C#:
char [] args = Environment.GetCommandLineArgs();
int x = int.Parse(args[0]);
int y = int.Parse(args[1]);

Hay thanks, I will try that. if this works I will not mind doing it for every this at this point.
Thanks
 
Hay thanks, I will try that. if this works I will not mind doing it for every this at this point.
Thanks

Hi,
I am trying what you suggested here and I think it will work here, but I am not sure how to call it inthe VBA.
I am trying something like this:
Code in VBA:
Dim RetVal
' Run my exe.


RetVal = Shell("C:\Import\CmdTest.exe 2 , 3", 1)

but this is just opening the form?

Any ideas on how I can add the parameters?
Thanks
 
It is for the C# program to retrieve the parameters and do something with them

C#:
static void Main(string[] args){

    if(args.Length > 0){
        //work with args here. it will be an array of {"2", ",", "3" } if called as you put in post #12
    } else {
       //no parameters supplied, show the form
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ChargerForm());
    }
}
 
Back
Top Bottom