Solved

Passing parameters to an exe

Posted on 2008-10-01
10
1,141 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:meteorelec
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22615228
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
 
LVL 2

Author Comment

by:meteorelec
ID: 22615898
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22616555
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
 
LVL 2

Author Comment

by:meteorelec
ID: 22616837
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22616882
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Author Comment

by:meteorelec
ID: 22616958
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22617078
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
 
LVL 2

Author Comment

by:meteorelec
ID: 22617088
Yes that is correct.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22617201
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
 
LVL 2

Author Comment

by:meteorelec
ID: 22617279
Thanks a million, this has got me sorted.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now