Click here to Skip to main content
15,508,644 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 0:09
arnold_w1-Aug-20 0:09 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 0:58
arnold_w1-Aug-20 0:58 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 2:44
professionalRichard Andrew x641-Aug-20 2:44 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 2:55
arnold_w1-Aug-20 2:55 
AnswerRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 2:47
professionalRichard Andrew x641-Aug-20 2:47 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 3:04
arnold_w1-Aug-20 3:04 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 3:07
professionalRichard Andrew x641-Aug-20 3:07 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 3:45
arnold_w1-Aug-20 3:45 
Ahhh, you mean this:
C#
const uint WM_GETTEXT = 0x000D;
StringBuilder message = new StringBuilder(1000);
SendMessage(handle, WM_GETTEXT, message.Capacity, message);
Yes, that works great!!! Thank you! This is the complete code:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        WindowWrapper parentForm = ProcessWindowsHelper.getHandleToAnotherProcessWindow("TortoiseGitProc", "Switch/Checkout");
        MessageBoxEx.Show(parentForm, "Hello on top of TortoiseGit Switch/Checkout dialog");
    }
}
public class ProcessWindowsHelper
{
    private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
    [DllImport("user32.dll")]
    private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
    delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);

    private const uint WM_GETTEXT = 0x000D;

    public static WindowWrapper getHandleToAnotherProcessWindow(string processName, string substringInAnotherProcessWindow)
    {
        Process myProcess = findProcessByName(processName);
        IEnumerable<IntPtr> allHandlesInMyProcess = EnumerateProcessWindowHandles(myProcess.Id);
        foreach (IntPtr handle in allHandlesInMyProcess)
        {
            StringBuilder message = new StringBuilder(1000);
            SendMessage(handle, WM_GETTEXT, message.Capacity, message);
            if (message.ToString().Contains(substringInAnotherProcessWindow))
            {
                Debug.WriteLine(message);
                return new WindowWrapper(handle);
            }
        }
        return null;
    }

    private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
    {
        List<IntPtr> handles = new List<IntPtr>();
        ProcessThreadCollection processThreadCollection = Process.GetProcessById(processId).Threads;
        for (int i = 0; i < processThreadCollection.Count; i++)
        {
            ProcessThread thread;
            thread = processThreadCollection[i];
            EnumThreadWindows(thread.Id, 
                delegate(IntPtr hWnd, IntPtr lParam)
                {
                    handles.Add(hWnd);
                    return true;
                }, 
                IntPtr.Zero);
        }
        return handles;
    }

    private static Process findProcessByName(string processName)
    {
        Process[] allProcesses = Process.GetProcesses();
        for (int j = 0; j < allProcesses.Length; j++)
        {
            if (allProcesses[j].ProcessName.Equals(processName))
            {
                return allProcesses[j];
            }
        }
        return null;
    }
}


public class WindowWrapper : IWin32Window
{
    public WindowWrapper(IntPtr handle)
    {
        _hwnd = handle;
    }

    public IntPtr Handle
    {
        get { return _hwnd; }
    }

    private IntPtr _hwnd;
}
The class MessageBoxEx can be found here: Parent centered MessageBox dialog in C# ยท GitHub[^]

modified 1-Aug-20 9:49am.

GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 4:24
professionalRichard Andrew x641-Aug-20 4:24 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 4:49
arnold_w1-Aug-20 4:49 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 5:31
professionalRichard Andrew x641-Aug-20 5:31 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 6:22
arnold_w1-Aug-20 6:22 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 6:36
professionalRichard Andrew x641-Aug-20 6:36 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 7:07
arnold_w1-Aug-20 7:07 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 7:10
professionalRichard Andrew x641-Aug-20 7:10 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 7:15
arnold_w1-Aug-20 7:15 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 7:21
professionalRichard Andrew x641-Aug-20 7:21 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 7:51
arnold_w1-Aug-20 7:51 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 8:05
professionalRichard Andrew x641-Aug-20 8:05 
QuestionAzure Active Directory question Pin
pkfox31-Jul-20 1:50
professionalpkfox31-Jul-20 1:50 
AnswerRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 1:01
mveRichard Deeming4-Aug-20 1:01 
GeneralRe: Azure Active Directory question Pin
pkfox4-Aug-20 4:36
professionalpkfox4-Aug-20 4:36 
GeneralRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 4:40
mveRichard Deeming4-Aug-20 4:40 
GeneralRe: Azure Active Directory question Pin
pkfox4-Aug-20 6:38
professionalpkfox4-Aug-20 6:38 
GeneralRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 6:48
mveRichard Deeming4-Aug-20 6:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.