Search code examples
c#wpfgoogle-chrome-extension

C# WPF How to convert native messaging --parent-window to IntPtr hwnd


I'm trying to convert the native messaging argument --parent-window=<decimal handle value> to a C# IntPtr hwnd value. The following code does not work:

[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);
foreach (string arg in Environment.GetCommandLineArgs())
{
    if (arg.IndexOf("--parent-window") != -1)
    {
        string[] subs = arg.Split('=');
        IntPtr hwnd = new IntPtr(Convert.ToInt32(subs[1], 16));
        uint pid;
        GetWindowThreadProcessId(hwnd, out pid);
        System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById((int)pid);
        Console.WriteLine(p.MainModule.FileName.ToString());            
    }
}

Can you help me to rewrite this so that it will display the filename of the --parent-window?


Solution

  • After reviewing comments, just try to convert to Int32 normally:

      IntPtr hwnd = new IntPtr(Convert.ToInt32(subs[1]));