Solved

Passing parameters to an exe

Posted on 2008-10-01
10
1,148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 86

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 86

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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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