19

osk.exe90%の確率で、の32ビットプロセスから起動できませんWin7 x64。元々、コードは次のものを使用していました。

Process.Launch("osk.exe");

これは、ディレクトリの仮想化のためにx64では機能しません。私が思った問題ではありません。仮想化を無効にしてアプリを起動し、再度有効にするだけです。これが正しい方法だと思いました。また、キーボードが最小化されている場合にキーボードを元に戻すためのコードも追加しました(これは正常に機能します)。コード(サンプルのWPFアプリ内)は次のようになります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;using System.Diagnostics;
using System.Runtime.InteropServices;

namespace KeyboardTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

        private const UInt32 WM_SYSCOMMAND = 0x112;
        private const UInt32 SC_RESTORE = 0xf120;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        private string OnScreenKeyboadApplication = "osk.exe";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void KeyboardButton_Click(object sender, RoutedEventArgs e)
        {
            // Get the name of the On screen keyboard
            string processName = System.IO.Path.GetFileNameWithoutExtension(OnScreenKeyboadApplication);

            // Check whether the application is not running 
            var query = from process in Process.GetProcesses()
                        where process.ProcessName == processName
                        select process;

            var keyboardProcess = query.FirstOrDefault();

            // launch it if it doesn't exist
            if (keyboardProcess == null)
            {
                IntPtr ptr = new IntPtr(); ;
                bool sucessfullyDisabledWow64Redirect = false;

                // Disable x64 directory virtualization if we're on x64,
                // otherwise keyboard launch will fail.
                if (System.Environment.Is64BitOperatingSystem)
                {
                    sucessfullyDisabledWow64Redirect = Wow64DisableWow64FsRedirection(ref ptr);
                }

                // osk.exe is in windows/system folder. So we can directky call it without path
                using (Process osk = new Process())
                {
                    osk.StartInfo.FileName = OnScreenKeyboadApplication;
                    osk.Start();
                    osk.WaitForInputIdle(2000);
                }

                // Re-enable directory virtualisation if it was disabled.
                if (System.Environment.Is64BitOperatingSystem)
                    if (sucessfullyDisabledWow64Redirect)
                        Wow64RevertWow64FsRedirection(ptr);
            }
            else
            {
                // Bring keyboard to the front if it's already running
                var windowHandle = keyboardProcess.MainWindowHandle;
                SendMessage(windowHandle, WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));
            }
        }
    }
}

しかし、このコードは、ほとんどの場合、次の例外をスローしますosk.Start()

指定されたプロシージャがSystem.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)で見つかりませんでした

競合状態ではないことを確認するために、osk.Start行の周りに長いThread.Sleepコマンドを配置しようとしましたが、同じ問題が解決しません。誰かが私が何か間違ったことをしているところを見つけたり、これに対する代替の解決策を提供したりできますか?メモ帳を起動しても問題なく動作するようですが、オンスクリーンキーボードではボールを再生できません。

4

6 に答える 6

5

あなたが得ている正確なエラーメッセージについて、私は非常にしっかりした説明を持っていません. しかし、リダイレクトを無効にすると、.NET フレームワークが台無しになります。デフォルトでは、Process.Start() P/ShellExecuteEx() API 関数を呼び出してプロセスを開始します。この関数は shell32.dll にあり、DLL をロードしていない場合はロードする必要があります。リダイレクトを無効にすると、間違ったものになります。

これを回避するには、ProcessStartInfo.UseShellExecute を false に設定します。ここでは必要ありません。

明らかに、リダイレクトを無効にすることは、実際には予測できない副作用を伴う危険なアプローチです。デマンド ロードされる DLLはたくさんあります。プラットフォーム ターゲットでコンパイルする非常に小さなヘルパー EXE = 任意の CPU で問題を解決できます。

于 2010-05-28T14:51:34.157 に答える
3

MTAスレッドからosk.exeを起動する必要がある特定のことが内部で行われています。Wow64DisableWow64FsRedirectionその理由は、への呼び出しが現在のスレッドにのみ影響するためと思われます。ただし、特定の条件下でProcess.Startは、別のスレッドから新しいプロセスが作成されます。たとえば、UseShellExecuteがfalseに設定されている場合や、STAスレッドから呼び出された場合などです。

以下のコードは、アパートの状態をチェックしてから、MTAスレッドからオンスクリーンキーボードを起動することを確認します。

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, 
        UInt32 Msg, 
        IntPtr wParam, 
        IntPtr lParam);
    private const UInt32 WM_SYSCOMMAND = 0x112;
    private const UInt32 SC_RESTORE = 0xf120;

    private const string OnScreenKeyboardExe = "osk.exe";

    [STAThread]
    static void Main(string[] args)
    {
        Process[] p = Process.GetProcessesByName(
            Path.GetFileNameWithoutExtension(OnScreenKeyboardExe));

        if (p.Length == 0)
        {
            // we must start osk from an MTA thread
            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
            {
                ThreadStart start = new ThreadStart(StartOsk);
                Thread thread = new Thread(start);
                thread.SetApartmentState(ApartmentState.MTA);
                thread.Start();
                thread.Join();
            }
            else
            {
                StartOsk();
            }
        }
        else
        {
            // there might be a race condition if the process terminated 
            // meanwhile -> proper exception handling should be added
            //
            SendMessage(p[0].MainWindowHandle, 
                WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));
        }
    }

    static void StartOsk()
    {
        IntPtr ptr = new IntPtr(); ;
        bool sucessfullyDisabledWow64Redirect = false;

        // Disable x64 directory virtualization if we're on x64,
        // otherwise keyboard launch will fail.
        if (System.Environment.Is64BitOperatingSystem)
        {
            sucessfullyDisabledWow64Redirect = 
                Wow64DisableWow64FsRedirection(ref ptr);
        }


        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = OnScreenKeyboardExe;
        // We must use ShellExecute to start osk from the current thread
        // with psi.UseShellExecute = false the CreateProcessWithLogon API 
        // would be used which handles process creation on a separate thread 
        // where the above call to Wow64DisableWow64FsRedirection would not 
        // have any effect.
        //
        psi.UseShellExecute = true;
        Process.Start(psi);

        // Re-enable directory virtualisation if it was disabled.
        if (System.Environment.Is64BitOperatingSystem)
            if (sucessfullyDisabledWow64Redirect)
                Wow64RevertWow64FsRedirection(ptr);
    }
}
于 2010-05-28T14:05:42.320 に答える
0

不器用な方法:

側でこのバッチ ファイルを実行します (64 ビット エクスプローラーから開始)。

:lab0
タイムアウト /T 1 >ヌル
存在する場合 oskstart.tmp goto lab2
lab0 に移動
:lab2
del oskstart.tmp
オスク
lab0 に移動

キーボードが必要な場合は、ファイル oskstart.tmp を作成します。

于 2015-02-23T13:24:15.040 に答える
-1

「スクリーン キーボードを起動できませんでした。」という問題に直面している場合は、プロジェクトのプラットフォーム ターゲットを任意の CPU に変更します。

于 2013-06-08T15:46:50.280 に答える