【问题标题】:Printf gets program stuck with no responsePrintf 让程序卡住而没有响应
【发布时间】:2019-10-06 21:23:48
【问题描述】:

此代码获取并打印所有窗口标题和 hwnd。但是,这行代码有问题:

printf("----%s\n",hwndTitlePtrCollection[idx-1]);

如果我删除这一行,应用程序运行正常,否则会卡住。

#include <stdio.h>
#include <stdlib.h>
#define WINVER 0x0600
#define _WIN32_IE 0x0500
#include <windows.h>
#include <stdint.h>
uint64_t* hwndCollection;
char**    hwndTitlePtrCollection;
uint64_t  idx;
BOOL CALLBACK WindowFoundCB(HWND hwnd, char* param) {
    char *strIn2 = (char*) param;
    char *strIte;
    GetWindowText(hwnd, strIte, 256);
    if (IsWindowVisible(hwnd)){
       idx++;

       hwndCollection = (uint64_t *)realloc(hwndCollection,idx*sizeof(uint64_t));
       hwndCollection[idx-1] =  hwnd;
       printf("**** get a window's number ****\n");
       printf("----%d----%d\n",hwnd,hwndCollection[idx-1]);

       hwndTitlePtrCollection = (char**)realloc(hwndTitlePtrCollection,idx*sizeof(char*));
       hwndTitlePtrCollection[idx-1] = strIte;
       printf("**** get a window's Title ****\n");
       // this line if delete, runs OK. If exist, got stuck here
       printf("----%s\n",hwndTitlePtrCollection[idx-1]);
    }
    return TRUE;
}
int main()
{
    printf("Hello world!\n");
    idx = 0;
    char* aStr = "good";
    hwndCollection = (uint64_t*)malloc(sizeof(uint64_t)*1);
    hwndTitlePtrCollection = (char**)malloc(sizeof(char*)*1);
    EnumWindows(WindowFoundCB,&aStr);
    printf("total query recorded is: %d\n",idx);
    return 0;
}

【问题讨论】:

    标签: c printf


    【解决方案1】:

    我很惊讶代码能走到你提到的那一行——也许只是运气——因为你在其他地方有一些重大错误。这些将导致未定义的行为。请参阅下面代码中的 cmets 和修复:

    BOOL CALLBACK WindowFoundCB(HWND hwnd, char* param) {
        char *strIn2 = (char*) param;
    //  char *strIte; // You are not actually allocating any memory here!
        char strIte[256]; // This way, you have allocated 256 bytes for the Window name.
        GetWindowText(hwnd, strIte, 256); // In the uncorrected version, this is UB!
        if (IsWindowVisible(hwnd)){
            idx++;
    
            hwndCollection = (uint64_t *)realloc(hwndCollection,idx*sizeof(uint64_t));
            hwndCollection[idx-1] =  hwnd;
            printf("**** get a window's number ****\n");
            printf("----%d----%d\n",hwnd,hwndCollection[idx-1]);
    
            // EDIT (1a)!
            // The following line allocates pointers to strings, but no memory for the strings...
            hwndTitlePtrCollection = (char**)realloc(hwndTitlePtrCollection,idx*sizeof(char*));
            // Here, we allocate memory for the new string …
            // EDIT (1b)!
            hwndTitlePtrCollection[idx-1] = malloc(256 * sizeof(char));
        //  hwndTitlePtrCollection[idx-1] = strIte; // Wrong! This just copies the pointer...
            strcpy(hwndTitlePtrCollection[idx-1], strIte); // ...this copies the contents.
            printf("**** get a window's Title ****\n");
            // this line if delete, runs OK. If exist, got stuck here
            printf("----%s\n",hwndTitlePtrCollection[idx-1]);
        }
        return TRUE;
    }
    

    试试这个(也可能有其他错误,但让我们努力解决)!随时要求进一步解释和/或澄清。

    【讨论】:

    • 你好,我试了你的版本,发现这次一开始没有卡住,但是打印3次后又卡住了。下面是比我第一次更好的输出:
    • 世界你好! **** 获取窗口编号 **** ----66594----66594 **** 获取窗口标题 **** ---- **** 获取窗口编号 **** ----196738----196738 **** 获取窗口的标题 **** ---- **** 获取窗口的编号 **** ----4790020----4790020 进程返回255 (0xFF) 执行时间:4.928 s 按任意键继续。
    • 我已将输出粘贴到上面的答案中。谢谢。
    • 知道了。打勾
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多