3

特定のアクティブ ウィンドウに「Enter」キーを送信するプログラムを作成しました。タイマーを使用して、その時点でアクティブなウィンドウのタイトルを取得し、それに応じて行動しました。すべてのエラーを追跡できるように、エラー ログ ファイルを作成しました。エラーを作成しているコードは次のとおりです。

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        try
        {
            ttl = GetActiveWindowTitle();
            if (ttl != null)
            {
                if (ttl.ToLower() == "xxxxxxxxxxx")
                {
                    SendKeys.SendWait("{ENTER}");
                }
            }
        }
        catch (Exception err)
        {
            Write2ErrLog(err, "OnTimedEvent");
        }
    }

これが GetActiveWindowTitle() メソッドです。

    static private string GetActiveWindowTitle()
    {
        try
        {
            const int nChars = 256;
            IntPtr handle = IntPtr.Zero;
            StringBuilder Buff = new StringBuilder(nChars);
            handle = GetForegroundWindow();

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                return Buff.ToString();
            }
            return null;
        }
        catch (Exception e)
        {
            Write2ErrLog(e, "GetActiveWindowTitle");
            return null;
        }
    }

私が受け取っているエラーは次のとおりです。

2011 年 4 月 19 日 12:57:16 PM: System.InvalidOperationException: キューが空です。
   System.Collections.Queue.Dequeue() で
   System.Windows.Forms.SendKeys.SendInput (Byte[] oldKeyboardState、キューの previousEvents) で
   System.Windows.Forms.SendKeys.Send (文字列キー、コントロール コントロール、ブール待機) で
   System.Windows.Forms.SendKeys.SendWait (文字列キー) で
   DataViews_SendKeys.Form1.OnTimedEvent (オブジェクト ソース、ElapsedEventArgs e) OnTimedEvent で

2011 年 4 月 19 日 1:03:11 PM: System.ArgumentException: 宛先配列の長さが十分ではありませんでした。destIndex と長さ、および配列の下限を確認してください。
   System.Array.Copy で (配列 sourceArray、Int32 sourceIndex、配列 destinationArray、Int32 destinationIndex、Int32 長さ、信頼できるブール値)
   System.Collections.Queue.Clone() で
   System.Windows.Forms.SendKeys.Send (文字列キー、コントロール コントロール、ブール待機) で
   System.Windows.Forms.SendKeys.SendWait (文字列キー) で
   DataViews_SendKeys.Form1.OnTimedEvent (オブジェクト ソース、ElapsedEventArgs e) OnTimedEvent で

2011 年 4 月 19 日 1:04:00 PM: System.AccessViolationException: 保護されたメモリの読み取りまたは書き込みを試みました。これは多くの場合、他のメモリが破損していることを示しています。
   System.Windows.Forms.UnsafeNativeMethods.PeekMessage (MSG& msg、HandleRef hwnd、Int32 msgMin、Int32 msgMax、Int32 削除) で
   System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Int32 dwComponentID、Int32 理由、Int32 pvLoopData) で
   System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 理由、ApplicationContext コンテキスト) で
   System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 理由、ApplicationContext コンテキスト) で
   System.Windows.Forms.SendKeys.Flush() で
   System.Windows.Forms.SendKeys.Send (文字列キー、コントロール コントロール、ブール待機) で
   System.Windows.Forms.SendKeys.SendWait (文字列キー) で
   DataViews_SendKeys.Form1.OnTimedEvent (オブジェクト ソース、ElapsedEventArgs e) OnTimedEvent で

このプログラムを実行しているコンピューターは、Intel Xeon プロセッサを搭載した HP サーバー コンピューターであり、OS は Windows XP です。プログラムはラップトップではエラーなしで実行されますが、サーバー コンピューターでは実行されません。何かご意見は?

4

1 に答える 1

0

これは、デスクトップではなくサーバーで UAC が有効になっていることが原因である可能性があります。SendKeys.Send() を使用したときに同じ動作が発生しました

チェックするもう 1 つのことは、ビルド対象の .NET バージョンです。SendKeys 送信がブロックされているを参照してください。

于 2014-10-22T14:36:18.997 に答える