Solved

System.Windows.Forms.SendKeys

Posted on 2003-10-26
3
1,195 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 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

10 Experts available now in Live!

Get 1:1 Help Now