Call another interface button through Win32API

Function description

There are two independent winform interfaces created by two teams. One team operates the button of the other team's interface under the premise of unknown source code.

Exhibition

The main interface is as follows:

The sub interface is as follows:

There is a button (test) in the sub interface. Click this button to pop up the prompt box of "prompt content of word window".
When you click the button (button1) on the parent form (Form1), you can operate the button (test) of the child form (form of the child form), and a prompt box will appear

Code

Code for subform:

namespace TestForm
{
    public partial class MyTestForm : Form
    {
        public MyTestForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Prompt content of subform!");
        }
    }
}

Code of the parent form

    public partial class Form1 : Form
    {
        [DllImport("user32.dll", EntryPoint = "FindWindowA", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
        private static extern void SetForegroundWindow(IntPtr hwnd);  

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Process.Start("TestForm.exe");

                const int WM_CLICK = 0x00F5;//Click the message with the mouse, and refer to MSDN for the values of various messages  

                string lpszName_TestChild = "Form of subform";//Title of subform (Text property value)  
                string lpszName_btnYes = "test";//Title of button on subform (Text property value)  

                IntPtr hwndTestChild = new IntPtr();//Handle to subform  
                IntPtr hwndbtnYes = new IntPtr();//Handle to button on subform  

                hwndTestChild = FindWindow(null, lpszName_TestChild);//Get handle to subform  
                hwndbtnYes = FindWindowEx(hwndTestChild, 0, null, lpszName_btnYes);//Get handle to button on subform  

                if (hwndTestChild != IntPtr.Zero)
                {
                    SendMessage(hwndbtnYes, WM_CLICK, 0, 0);//Send a mouse click message to the button on the subform,  
                }
                else
                {
                    MessageBox.Show("Not found!");
                }


            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }


    }

Other instructions

1. In the method description:

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

Either EntryPoint = "FindWindow" or EntryPoint = "FindWindowA" can be used here.
My environment VS2010, Win7 flagship. The return result of FindWindow has been 0 before. But somehow it's all right again.

2. About session0:

1,On the failure of some Win32 APIs under WIN 7
2,Session 0 Isolation
3,FindWindow always returns 0

3. References

Interprocess communication

Tags: Session

Posted on Sat, 04 Apr 2020 05:36:49 -0700 by holladb