Passing parameters to an exe

Hi I'm trying to pass parameteres to an exe which will set focus to an applicaiton and perform a series of keystrokes.  I can pass the parameter to the exe and I can see it in the form that it displays when loading.  I can then perform the keystrokes but what I can't do is when the application is minimised (Saleslogix) is to make it active nor can I pass the parameter that I have captured as a series of keystrokes.  Any help would be much appreciated as I'm fresh to the VB environment.
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        MsgBox(Command)
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        AppActivate("Saleslogix")
        SendKeys.Send("%LAC")
        SendKeys.Send(MsgBox)
 
    End Sub
 
End Class

Open in new window

LVL 2
meteorelecAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
OK...so the whole thing might look like:
Public Class Form1
 
    Private AccountName As String = ""
 
    Private Const SW_SHOWNORMAL As Integer = 1
 
    Private Declare Function IsIconic Lib "user32" (ByVal handle As IntPtr) As Integer
    Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Application.CommandLineArgs.Count > 0 Then
            Dim params() As String = Nothing
            ReDim params(My.Application.CommandLineArgs.Count - 1)
            My.Application.CommandLineArgs.CopyTo(params, 0)
            AccountName = String.Join(" ", params)
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const delayInMilliseconds As Integer = 750
 
        Dim targetAppExeName As String = "NotePad" ' <-- NO path, the EXE name WITHOUT the ".exe" on the end
        Dim ps() As Process = Process.GetProcessesByName(targetAppExeName)
        If ps.Length > 0 Then
            Dim P As Process = ps(0)
 
            ' Restore the window if it is Minimized...
            If IsIconic(P.MainWindowHandle) <> 0 Then
                ShowWindow(P.MainWindowHandle, SW_SHOWNORMAL)
            End If
 
            ' Make it the ForegroundWindow...
            SetForegroundWindow(P.MainWindowHandle)
 
            System.Threading.Thread.Sleep(delayInMilliseconds) ' OPTIONAL WAIT 
 
            ' ...now send your keystrokes...
            SendKeys.Send("%LAC")
            System.Threading.Thread.Sleep(delayInMilliseconds) ' OPTIONAL WAIT 
            SendKeys.Send(AccountName)
        End If
    End Sub
 
End Class

Open in new window

0
 
Gyanendra SinghArchitectCommented:
use this
Imports System.Diagnostics
..
..
..
Dim psi As New ProcessStartInfo()
With psi
.FileName = "Saleslogix.exe"
.Arguments = ...
End With
Dim p As Process = Process.Start(psi)
p.WaitForExit()
MsgBox(CStr(p.ExitCode))
///
0
 
meteorelecAuthor Commented:
Sorry could you just add a few comments to the code so I can get an quick understanding of what it is doing, I really am a complete beginner here with VB.  Thanks.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I think BondinASP just read the title of your question and didn't actually look at what you were trying to do in code...  What he has posted does not apply to your situation.  =\

If the application is minimized then you need to restore it first.

It might look like the code below.  Note that I am not using AppActivate() anymore.  Instead, I am getting a Process instance via Process.GetProcessesByName().  Pass the name of the Executable WITHOUT any Path and WITHOUT the ".exe" extension.

The example uses "Notepad"...so you need to replace "Notepad" with the name of YOUR target application.  Remember, this is the name of the executable (without path or extension)...NOT the title of the apps main window.
Public Class Form1
 
    Private Const SW_SHOWNORMAL As Integer = 1
 
    Private Declare Function IsIconic Lib "user32" (ByVal handle As IntPtr) As Integer
    Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ps() As Process = Process.GetProcessesByName("NotePad") ' <-- NO path, the EXE name WITHOUT the ".exe" on the end
        If ps.Length > 0 Then
            Dim P As Process = ps(0)
 
            ' Restore the window if it is Minimized...
            If IsIconic(P.MainWindowHandle) <> 0 Then
                ShowWindow(P.MainWindowHandle, SW_SHOWNORMAL)
            End If
 
            ' Make it the ForegroundWindow...
            SetForegroundWindow(P.MainWindowHandle)
 
            ' now send your keystrokes...
            SendKeys.Send("Hello World!")
        End If
    End Sub
 
End Class

Open in new window

0
 
meteorelecAuthor Commented:
Hi Idle Mind, that works really well for me just one more thing I have placed the command that I am going to run below, how would I use the parameter AccountName and get it to display as keystrokes so instead of the key strokes displaying Hello World! it would be the account name say Joe Bloggs.  Thanks.

"C:\Documents and Settings\Administrator.METEORELEC\My Documents\Visual Studio 2008\Projects\Keystrokes\Keystrokes\bin\Debug\Keystrokes.exe" AccountName
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So is AccountName being passed into YOUR executable (via command line arguments) and then you want to turn around and send it to the Saleslogix window?

...if not, please explain the "flow" of the app.  Where does AccountName come from and where does it need to go...
0
 
meteorelecAuthor Commented:
You are absoultely corrent in that we want to pass it to the Saleslogix window.  What I had intended to to do was to use the keystrokes to fire the account lookup and then pass the AccountName parameter to the lookup window.  The Key sequence to get the lookup window is "Alt,L,A,C".  Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...so YOUR app will be run with:

    "C:\Documents and Settings\Administrator.METEORELEC\My Documents\Visual Studio 2008\Projects\Keystrokes\Keystrokes\bin\Debug\Keystrokes.exe" AccountName

And you need help getting the AccountName value from the command line into a useable variable in your application? (so it can be sent to Saleslogix)
0
 
meteorelecAuthor Commented:
Yes that is correct.
0
 
meteorelecAuthor Commented:
Thanks a million, this has got me sorted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.