Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表计数第一次为0后,C Pinvoke找不到控件的Hwnd_C#_List_Pinvoke_User32_Hwnd - Fatal编程技术网

C# 在列表计数第一次为0后,C Pinvoke找不到控件的Hwnd

C# 在列表计数第一次为0后,C Pinvoke找不到控件的Hwnd,c#,list,pinvoke,user32,hwnd,C#,List,Pinvoke,User32,Hwnd,我正在尝试单击另一个应用程序中的按钮,该应用程序是从我的程序Process.Start启动的 问题:我需要等到加载屏幕消失,GUI弹出 我的想法是读取所有HwndControls,直到找到一个特定的控件按钮:Kill Client from GUI=GUI Opened 但这只有在我手动等待GUI并按下搜索控制按钮时才起作用 如果我按下搜索按钮,如果加载屏幕是活动的,我也会得到一个Hwnd=0的列表计数 这是我的代码: public class WndSearcher { [DllI

我正在尝试单击另一个应用程序中的按钮,该应用程序是从我的程序Process.Start启动的

问题:我需要等到加载屏幕消失,GUI弹出

我的想法是读取所有HwndControls,直到找到一个特定的控件按钮:Kill Client from GUI=GUI Opened

但这只有在我手动等待GUI并按下搜索控制按钮时才起作用

如果我按下搜索按钮,如果加载屏幕是活动的,我也会得到一个Hwnd=0的列表计数

这是我的代码:

 public class WndSearcher
 {
    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }

    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = 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;
    }
}

到目前为止还没有找到解决办法

所以我决定将AutoHotKey与C结合使用

在C语言中,我启动自动热键脚本并等待脚本完成。然后外部程序完全启动

起始参数:1.Processid 2.NewExternalProgramName

这里是我的自动热键脚本:

counter := 0
Loop, %0%  ; For each parameter:
{

    param := %A_Index%

    if (counter = 0) ; do sth with parameter 1
        winwait, ahk_pid %param% ; Not logged in ;wait until the text "Not logged in" can be read (Program started completely)

    if (counter = 1) ; do sth with parameter 2
        WinSetTitle, %param%

    counter += 1
}

Thread.Sleep是一种明显的等待方式。当hwnd=0时,您可以使用while循环,先休眠几毫秒,然后再执行所需的代码。@Scott我尝试过这种方式,但如果列表在第一次计数=0,则每次都是0。。。这就是我尝试使用按钮的原因,但如果我搜索控件,如果加载屏幕是活动的,则会出现相同的问题。@Hans我知道,但我想要另一种方法:您需要使用自动化来完成此操作
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

public static string GetWindowTextRaw(IntPtr hwnd)
    {
        // Allocate correct string length first
        int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null);
        StringBuilder sb = new StringBuilder(length + 1);
        SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
        return sb.ToString();
    }
counter := 0
Loop, %0%  ; For each parameter:
{

    param := %A_Index%

    if (counter = 0) ; do sth with parameter 1
        winwait, ahk_pid %param% ; Not logged in ;wait until the text "Not logged in" can be read (Program started completely)

    if (counter = 1) ; do sth with parameter 2
        WinSetTitle, %param%

    counter += 1
}