【发布时间】: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;
}
【问题讨论】: