我有以下代码似乎适用于我的一个项目,但它不适用于另一个项目。两者的代码完全相同,但EnumWindow的方法永远不会得到 执行。
以下是代码:
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern Int32 GetClassName(IntPtr hWnd, StringBuilder StrPtrClassName, Int32 nMaxCount);
[System.Runtime.InteropServices.DllImport("user32")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
System.Runtime.InteropServices.GCHandle listHandle = System.Runtime.InteropServices.GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, System.Runtime.InteropServices.GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
System.Runtime.InteropServices.GCHandle gch = System.Runtime.InteropServices.GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);
return true;
}
我要做的是从iexplorer.exe进程获取所有孩子。这两个项目之间的唯一区别是,在其中一个项目中,我是手动打开IE实例的那个,另一个是从代码中启动IE。
在这两种情况下,我能够捕获正确的进程和正确的主窗口处理,但不知道为什么在第二个项目中我不能得到任何孩子。
有什么想法吗?
提前致谢:)