Solved

System.Windows.Forms.SendKeys

Posted on 2003-10-26
3
1,187 Views
Last Modified: 2007-12-19
hi,
   Iam having a small problem in using the System.Windows.Forms.SendKeys API to send input to an active appication. I have an exe file for adding two numbers. I have to give input to that and get the result. My code ( which i have given below) works sometime and does not work someother time. It gives and unhandled exception error. can anyone tell me why?
 
   Dim myProcess As Process = New Process()
   myProcess.StartInfo.FileName = "Addition.exe"
   myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
   myProcess.EnableRaisingEvents = True
   myProcess.Start()
    Try
         If myProcess.Responding Then
           System.Windows.Forms.SendKeys.SendWait("2")
           System.Windows.Forms.SendKeys.SendWait("{ENTER}")
           System.Windows.Forms.SendKeys.SendWait("3")
           System.Windows.Forms.SendKeys.SendWait("{ENTER}")
           System.Windows.Forms.SendKeys.Send("{ENTER}")
           System.Windows.Forms.SendKeys.Flush()
         Else
            myProcess.Kill()
        End If

-sendhil

0
Comment
Question by:sendhil_an
3 Comments
 
LVL 4

Accepted Solution

by:
brother7 earned 50 total points
ID: 9624332
Lemme see if I understand this correctly...
1- The result is received back after the 2nd {ENTER}
2- The 3rd {ENTER}'s purpose is to try to end the process normally, ie in response to "Press any key to continue"

I would try the following:
   Add myProcess.CloseMainWindow() as the last statement of the If block, before the Else block.

Do you know the type of Exception that's occurring?
Add a Catch block, such as:
   Catch exceptionParameter as Exception
      MessageBox.Show(exceptionParameter.Message, "Caught Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
   End Try

Hope that helps some.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10224382
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: brother7 {http:#9624332}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

20 Experts available now in Live!

Get 1:1 Help Now