Laden...

Win API liefert leeres Objekt (User32.dll)

Erstellt von err_MSG vor 14 Jahren Letzter Beitrag vor 14 Jahren 980 Views
E
err_MSG Themenstarter:in
8 Beiträge seit 2009
vor 14 Jahren
Win API liefert leeres Objekt (User32.dll)

So meine zweite Woche hier und die zweite Frage 😉

Ich habe die User32.dll für mich herangezogen weil mir keine Methode bekannt ist wie ich es ohne sie machen soll. Wenn es auch ohne geht bin ich bestimmt nicht böse.

Also nun zum Problem!

Hier erstmal die Klasse mit den Verweisen auf die User32.dll



    [DllImport("user32.dll")]
    internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool GetGUIThreadInfo(uint idThread, out GUIThreadInfo lpgui);

    [StructLayout(LayoutKind.Sequential)]
    public struct GUIThreadInfo
    {
        public int cbSize;
        public uint flags;
        public IntPtr hwndActive;
        public IntPtr hwndFocus;
        public IntPtr hwndCapture;
        public IntPtr hwndMenuOwner;
        public IntPtr hwndMoveSize;
        public IntPtr hwndCaret;
        public rect rcCaret;
    }

    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct rect
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

und hier kommt das eigentliche Problem:

Der Process wird richtig erkannt und er läuft auch sauber bis zum Ende durch,
wirft also auch keine Exception aber o_TargetWindowInfo bleibt leer.
Ich hoffe es ist nur eine kleinigkeit aber ihr werdet mir sicher helfen 😉

             string str_process = "OUTLOOK";
            System.Diagnostics.Process proc;
            proc = System.Diagnostics.Process.GetProcessesByName(str_process)[0];
            IntPtr hnd_TargetWindow = proc.MainWindowHandle;

            var threadId = WinAPI.GetWindowThreadProcessId(hnd_TargetWindow, IntPtr.Zero);
            var o_TargetWindowInfo = new WinAPI.GUIThreadInfo();
            o_TargetWindowInfo.cbSize = Marshal.SizeOf(o_TargetWindowInfo);
            if (!WinAPI.GetGUIThreadInfo(threadId, out o_TargetWindowInfo))
                            throw new Win32Exception();

Frage am Rande: Warum bekomme ich mit

WinAPI.GetWindowThreadProcessId(hnd_TargetWindow, IntPtr.Zero);

eine andere ID als mit

System.Diagnostics.Process.GetProcessesByName(str_process)[0].Id

Besten dank schon mal.

R
234 Beiträge seit 2007
vor 14 Jahren

GetWindowThreadProcessId gibt die Thread-ID zurück. Process.ID hingegen die Process-ID. Steht auch alles in der Doku.

E
err_MSG Themenstarter:in
8 Beiträge seit 2009
vor 14 Jahren

Ok, ja klar ich steh immer noch neben mir weil ich noch keine Lösung gefunden hab! 😉 Aber danke für den Fingerzeig.

Edit:

Hat keiner eine Idee?

E
err_MSG Themenstarter:in
8 Beiträge seit 2009
vor 14 Jahren

Schade das keinem was dazu einfällt! Ist der Titel falsch?