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.
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?