Click here to Skip to main content
15,300,338 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi!

I'm trying to send some keystrokes to an application in c#, i found some usefull information on forums and such, but I couldn't make it work, can you tell me what i did wrong ? this is my code:
C#
[DllImport("User32.dll")]
       static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
       [DllImport("User32.dll")]
       static extern int SetForegroundWindow(IntPtr hWnd);
       public void Procese()
       {
           Process[] processlist = Process.GetProcessesByName("winrar");

           foreach (Process theprocess in processlist)
           {
                   IntPtr ptrWIRAR = theprocess.Handle;
                   SetForegroundWindow(ptrWIRAR);
                   SendKeys.Send("{F1}");
           }
       }


this is a test, if i can make this work, then it will work fore every process.

pleas help!
Posted

Saying "I couldn't make it work" doesn't tell us squat. Have you run it under the debugger to make sure it's actually FINDING the desired process, and that the foreach code is working?
   
v2
Comments
Mitran Stefanita Alexandru 14-Nov-11 7:45am
   
I ran the debugger and found that the "theprocess" has the value "WinRAR" and "ptrWIRAR" has "1076" and "proceslist" has "1";
#realJSOP 14-Nov-11 15:07pm
   
Then that tells me the flaw is somehow connected to the SendKeys class, wheich you haven't deigned to reveal to us.
Sergey Alexandrovich Kryukov 14-Nov-11 14:24pm
   
My 5. How many time we have to repeat that?
--SA
This Codeproject article will help you:

Sending Keystrokes to another Application in C#[^]
   
The application of SendKeys is very limited. The most reliable way to send keyboard and mouse input is using low-level Windows API SendInput, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^]. This is practically what keyboard and mouse drivers ultimately do.

I need to warn you though: using this for collaboration between different processed is bad thing.
Using it for UI development is even worse. This is just a hack, no more, not reliable, not supportable. I would advise to avoid communication between processes via simulation of user input by all means.

—SA
   
I am agree with SAKryukov.

Very much agree with you, I used SendMessage Win API with different messages. Only thing you required Handle of that application which you can obtain from Process.

I think SendMessage is better way as it is not working with that process thread and allowing both application to work independently.

By the way, why you want this functionality, any specific requirements.

Regards
Rushi
   
Comments
Mitran Stefanita Alexandru 15-Nov-11 3:49am
   
I'm trying to simulate a user input, to simplify my work, I have to a repetetive thing every time and I thout it would help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900