Pass Parameters to C# WinForms App (1 Viewer)

sixHat

Registered User.
Local time
Yesterday, 19:12
Joined
Apr 28, 2012
Messages
46
I am opening a c# application through MS Access VBA... this part is done. I now need to pass the c# application a simple "user name" string.

My initial thought is that it wouldn't be much of a problem to pass a simple string value to a c# application... My research has set me straight. Really I still think it's an easy fix... but I've not seen anything solid posted on how to do this?

I did hear someone mention passing command line values... I did prep the c# app to read them... but I haven't found anything on how to pass command line values through VBA?

If I can't figure it out, I'll have to output the user name to a text document and just read it from the c# application... but I hate to go this route because it's pretty cheezy!

Help, ideas, and especially code examples would be greatly appreciated!
 

spikepl

Eledittingent Beliped
Local time
Today, 03:12
Joined
Nov 3, 2010
Messages
6,144
I am opening a c# application through MS Access VBA... this part is done.
How?


Command-line values are normally "passed" by starting a new Shell, where an almost normal commandline can be passed to the system (paths containing spaces need to be wrapped in quotes) - see Shell in the helpfile.
 

sixHat

Registered User.
Local time
Yesterday, 19:12
Joined
Apr 28, 2012
Messages
46
Sorry for the confusion in my explanation... I didn't use command line to open the application. I had just read a forum comment that said you could pass values through the command line... they seemed to imply that you could pass it through and something like an opening argument... but as they stated they are not VBA programmers. I used a custom function to open the c# application because I couldn't get the Shell function to open it for some crazy reason here is the code:

Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long




Private Sub Command0_Click()
Dim hProcess As Long
Dim myPath As String
Dim myFile As String
Dim varFile As Variant


Dim Dlg As FileDialog
Dim txtFilePath As String

myFile = Chr(34) & "C:\Users\agriggs\Desktop\ReportGen\Report Gen.application" & Chr(34)
hProcess = ShellExecute(Me.hwnd, "Open", myFile, "AGriggs", "D:\", SW_SHOWNORMAL)
Call PassUserName

End Sub
 

DJkarl

Registered User.
Local time
Yesterday, 21:12
Joined
Mar 16, 2007
Messages
1,028
The command line is where to go, I would modify your main method in the program.cs file to accept command line arguments. Then have a public property such as username on your form, set that in the main method before the Application.Run call is made.
 

sixHat

Registered User.
Local time
Yesterday, 19:12
Joined
Apr 28, 2012
Messages
46
Thanks for the reply...
I had already set the Program.cs file to except command line arguments as you stated... I've been experimenting with trying to pass values to it... my problem is that I need a VBA example in how to pass the string to the C# application from the VBA side.

Thanks Again
 

DJkarl

Registered User.
Local time
Yesterday, 21:12
Joined
Mar 16, 2007
Messages
1,028
Simplest way would probably be the shell command

Shell Chr(34) & "C:\Temp\MyTestForm.exe userNameHere" & Chr(34)
 

sixHat

Registered User.
Local time
Yesterday, 19:12
Joined
Apr 28, 2012
Messages
46
Thanks... Interestingly I battled with the Shell function first... for some reason I could not get it to locate the file. Yes I did use double quotes... so I did some research and came across the function I used above which looks like it has a similar ability to pass the parameters via the: ByVal lpDirectory As String variable. Which I've tried as you can see I set a test "AGriggs" into this but it is not picking up the value?
 

sixHat

Registered User.
Local time
Yesterday, 19:12
Joined
Apr 28, 2012
Messages
46
Simplest way would probably be the shell command

Shell Chr(34) & "C:\Temp\MyTestForm.exe userNameHere" & Chr(34)

I noticed you did not use brackets... i.e. () after the shell? Maybe this is why I couldn't get it to work because I always used brackets: Chr(34) & Shell( "C:\Temp\MyTestForm.exe userNameHere") & Chr(34)
 

Users who are viewing this thread

Top Bottom