Question

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?

Was it helpful?

Solution

Of course. You can easily see that it doesn't work even when you run it on your own thread:

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);
}

The cursor doesn't budge. You have to inject the code on the UI thread of that process. Very hard to do, SetWindowsHookEx() to set, say, a WH_CALLWNDPROC hook and SendMessage() to trigger it. I think.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top