Solved

Passing parameters to an exe

Posted on 2008-10-01
10
1,146 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

11 Experts available now in Live!

Get 1:1 Help Now