-1

I am trying to make an application for routine projects;

It is more like autogui,macro but when macro is running i should be able to do another things so i need two mouse and two keyboards at the sametime running one of for my use otherone should run in the application

I can successfully post message to notepad with enter it to textbox but other applications not work like calculator,notepad++,discord,.. so on

I need to send keyboard,mouse to current form application

In both methods

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

First method

    [DllImport("User32.Dll", EntryPoint = "PostMessageA", SetLastError = true)]
    public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    public void control()
    {
        Process[] processes = Process.GetProcessesByName(textBox1.Text);

        foreach (Process p in processes)
        {
            textBox2.Text += p.ProcessName + System.Environment.NewLine;
            IntPtr windowHandle = p.MainWindowHandle;
            const int WM_KEYDOWN = 0x0100;
            const int VM_KEYUP = 0x0101;
            IntPtr editx = FindWindowEx(windowHandle, IntPtr.Zero, "edit", null);
            PostMessage(editx, WM_KEYDOWN, (IntPtr)66, IntPtr.Zero);
            PostMessage(editx, VM_KEYUP, (IntPtr)66, IntPtr.Zero);
        }

    }

Other tried method

    [DllImport("user32.dll")]
    private static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, Int32 lParam);

    public void control()
    {
            IntPtr handle = this.Handle;
            IntPtr handle2 = Handle;
            const int WM_KEYDOWN = 0x0100;
            const int VM_KEYUP = 0x0101;
            const int WM_MOUSEMOVE = 0x0200;
            const int WM_LBUTTONDOWN = 0x0201;
            const int WM_LBUTTONUP = 0x0202;
            IntPtr editx = FindWindowEx(handle , IntPtr.Zero, "edit", null);
            IntPtr editx2 = FindWindowEx(handle2, IntPtr.Zero, "edit", null);

            PostMessage(editx, WM_KEYDOWN, (Int32)Keys.S, 1);
            PostMessage(editx, VM_KEYUP, (Int32)Keys.S, 1);
            PostMessage(editx, WM_LBUTTONDOWN, 1, (280 << 16) | 280);
            PostMessage(editx, WM_LBUTTONUP, 0, (280 << 16) | 280);
            PostMessage(editx2, WM_KEYDOWN, (Int32)Keys.S, 1);
            PostMessage(editx2, VM_KEYUP, (Int32)Keys.S, 1);
            PostMessage(editx2, WM_LBUTTONDOWN, 1, (280 << 16) | 280);
            PostMessage(editx2, WM_LBUTTONUP, 0, (280 << 16) | 280);
    }

It can be about:

IntPtr editx = FindWindowEx(windowHandle, IntPtr.Zero, "edit", null);

What can i write instead edit and how can i find it ?

I tried to trace what i giving to GetProcessesByName("") with textBox2.Text , i am sure about process names are true

Even to make sure about used method i send to all related names with for

So what is wrong about this method ?

Why it is not working on other applications ?

San Diego
  • 75
  • 1
  • 12
  • Ever since Windows Vista, it inadvisable to use `PostMessage` to remote control apps. You may want to consider using _Microsoft UI Automation_. No need for fiddly p-invoke :) –  Aug 03 '19 at 15:54
  • 1
    Possible duplicate of [Simulating key press with PostMessage only works in some applications?](https://stackoverflow.com/questions/11890972/simulating-key-press-with-postmessage-only-works-in-some-applications) – GSerg Aug 03 '19 at 15:55
  • Can i use mouse when application running ? @MickyD – San Diego Aug 03 '19 at 15:56
  • It's not even close i think GSerg.. i have no error, just not working as expected.. Also tags are different. @GSerg – San Diego Aug 03 '19 at 15:58
  • @SanDiego yes.. –  Aug 03 '19 at 16:11

1 Answers1

0

You're just sending to the wrong windows. In the case of notepad.exe the window to send to is the main window so that works nicely.

In the case of Notepad++ there are a bunch of sub-windows (for the button-bars etc), you can't send to the main Notepad++ window, it doesn't know what to do with the WM_KEYDOWN messages.

This works for Notepad++:

IntPtr editx = FindWindowEx(windowHandle, IntPtr.Zero, "Scintilla", null);
CodeOrElse
  • 328
  • 2
  • 16