0

An application I'm writing needs to send a message to another application to make the cursor visible. The vast majority of my experience in programming is in C#, and I've managed to write a DLL Injector that allows me to inject managed code using CreateRemoteThread. Inside the DLL I'm injecting is a P/Invoked call to ShowCursor. This seems to have no effect however, and using a StreamWriter to check the value of what ShowCursor returns shows that while the function is successful and the display counter increments, at some other point the counter returns to 0. (Which should be displaying the cursor anyway, now that I think about it.)

What important piece of information am I missing? Do Windows Forms have some functionality that prevents me from changing the cursor like this? Is ShowCursor tied to the thread it's running in, so it reverts upon completion? Or is it something completely different?

4

1 に答える 1

2

もちろん。独自のスレッドで実行しても機能しないことが簡単にわかります。

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e) {
        System.Threading.ThreadPool.QueueUserWorkItem((o) => {
            int cnt = ShowCursor(false);
            System.Diagnostics.Debug.Print("Count = {0}", cnt);
        });
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern int ShowCursor(bool show);
}

カーソルが動かない。そのプロセスの UI スレッドにコードを挿入する必要があります。SetWindowsHookEx() で WH_CALLWNDPROC フックを設定し、SendMessage() でそれをトリガーするのは非常に困難です。おもう。

于 2012-10-06T00:30:06.267 に答える