My C# program has a very strange behavior. I’m using four ”hacky” pinvoke methods in this program: GlobalKeyHooking, HotKey Registering, SetForegroundWindow/GetForegroundWindow, and SendKeys.Send/SendWait.
Here is where there is a behavior I don’t understand. I’m stealing the Ctrl+V in a program where the standard cut/copy&paste routine is replaced by an autocomplete list in a listbox that appears and disappears. On some computer with Windows 7, my programs works like a charm, on an other 50% of computers with Windows 7 (and sadly no VS2010 to debug it), a very weird loop appears —inside— a method. Since the Ctrl and the V themselves are hooked, I already prevented the method to be infinitely triggered. That’s ok. But another loop appears inside the method.
Briefly: myDebugValue increases until it reaches 23-24-25! So something is trying to execute a function a lot of times before deciding to stop.
Does anyone has already seen a similar undesired loop? Even though there is no try/catch block, it’s bouncing inside the function.
Can some P/Invokes function crash on some Windows 7 and not on other?
Are P/Invokes having their own invisible low-level assembler error handlers try/catch, stronger than my C# program execution?
Visually, when it’s doing it, I see my program UI quickly flashing a lot of times, 25 times, I guess.
private bool getOutOfHere = false;
private int myDebugValue = 0;
private void globalKeyHooking_KeyUp(object sender, KeyEventArgs e)
{
if (getOutOfHere) return;
myDebugValue = 0;
if (e.KeyCode == Keys.LControlKey)
{
getOutOfHere = true;
SendKeys.SendWait("^v");
getOutOfHere = false;
myDebugValue++;
}
}
I tried to compile with 2.0, 3.0 and 4.0, and on the same 4 computers, in all cases, it still the same rate: 50% crashes, 50% works.
[Edit]
I really think that SendKeys.Send acts differently on different computers with Windows 7.
Didn’t want to answer my own question, but there is always a first time.
I decided to use a method similar to InputSimulator to achieve my goal and to avoid using SendKeys. Now, everything works perfectly on all machines.
I saw that InputSimulator is compatible with all my machines using Windows 7 and is using SendInput instead of SendMessage or SendKeys, so I used SendInput in my app.