【发布时间】:2014-04-07 22:56:11
【问题描述】:
我在尝试运行以下代码时遇到问题:
#include "header.h"
int main()
{
id = GetCurrentProcessId();
EnumWindows(hEnumWindows, NULL);
Sleep(5000);
//MoveWindow(hThis, 450, 450, 100, 100, TRUE);
system("pause");
return 0;
}
//header.h
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <Windows.h>
using namespace std;
DWORD id = 0;
HWND hThis = NULL;
BOOL CALLBACK hEnumWindows(HWND hwnd, LPARAM lParam)
{
DWORD pid = 0;
pid = GetWindowThreadProcessId(hwnd, NULL);
if (pid == id)
{
hThis = GetWindow(hwnd, GW_OWNER);
if (!hThis)
{
cout << "Error getting window!" << endl;
}
else
{
char *buffer = nullptr;
int size = GetWindowTextLength(hThis);
buffer = (char*)malloc(size+1);
if (buffer != nullptr)
{
GetWindowText(hThis, buffer, size);
cout << pid << ":" << buffer << endl;
free(buffer);
}
}
}
return TRUE;
}
当我运行这段代码时,几乎没有任何东西输出到屏幕上,就像没有附加程序一样。我尝试在 VS2013 的控制台和 Windows 子系统下运行它。
【问题讨论】:
-
@RobertHarvey 我看到了 C 风格的演员表,所以希望不是 C++ ;)
-
@Cyber:我看到像
cout << pid <<这样的东西让我相信它是 C++。 -
这是在 VS2013 上使用 C++ 编译器编译的,我更喜欢 malloc 而不是 new,这就是使用它的原因。