Win32控制台程序如何得到其窗口类的详细信息?

狐帝 2018-12-29 08:47:16
用了下面的代码来获取Win32控制台程序的窗口类的详细信息,但是运行时一直报告“类不存在”:
#include "stdafx.h"
#include <iostream>
#include <windows.h>

using namespace std;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

WINBASEAPI HWND WINAPI GetConsoleWindow();

#ifdef __cplusplus
}
#endif // __cplusplus

int main(int argc, char *argv[])
{
cout << "Hello, " << argv[0] << "." << endl;
HWND hWnd = GetConsoleWindow();
char windowClassName[256];
GetClassNameA(hWnd, windowClassName, sizeof(windowClassName) - 1);
cout << "class name: " << windowClassName << endl;

WNDCLASS windowClass;
HINSTANCE hInst;
hInst = (HINSTANCE)GetModuleHandle(NULL);
cout << "instance handle: " << hInst << endl;
BOOL success = GetClassInfo(hInst, (LPWSTR)windowClassName, &windowClass);
if (!success)
{
HRESULT errCode = GetLastError(); // the error code will be 0x583 that means "ERROR_CLASS_DOES_NOT_EXIST"
cout << "error when getting class information: " << errCode << endl;
void *errMsgBuf;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)(&errMsgBuf), 0, NULL);
cout << (LPSTR)(errMsgBuf) << endl;
LocalFree(errMsgBuf);

system("pause");
return -1;
}
cout << "class name: " << windowClass.lpszClassName << endl;
cout << "menu name: " << windowClass.lpszMenuName << endl;

return 0;
}

第22行已经获得了控制台程序的窗口类的类名,第27行也获得了控制台程序的实例句柄,但是调用GetClassInfoA的时候却出错,错误代码的含义是“类不存在”。究竟是哪里出了问题呢?

请各位高人不吝赐教。谢谢~~~
...全文
493 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2019-01-02
  • 打赏
  • 举报
回复
参考这个里面的spy源代码?:
MSDN98_1.ISO http://pan.baidu.com/s/1dDF41ix, MSDN98_2.ISO http://pan.baidu.com/s/1bnGo0Vl

MSDN98\SAMPLES\VC98\SDK\SDKTOOLS\SPY\*.*
狐帝 2018-12-31
  • 打赏
  • 举报
回复
引用 1 楼 赵4老师 的回复:
将你程序获取的各变量值和用spy++程序获取的各变量值对比一下。

诡异的事情出现了:用GetWindowLong(hWnd, GWL_HINSTANCE)获得的实例句柄(值为0)与GetModuleHandle(NULL)获得的实例句柄(值为非零)竟然不同?!这两种方法难道获得的不是同一种句柄?
spy++比较了一下我的程序的数据,发现窗口句柄相同,窗口类相同。spy++“常规”页面中的“实例句柄”是0,但“类”页面中的实例句柄是非零(与我的程序输出的实例句柄也不相同)。spy++在“类”页面中获得的“窗口进程地址”与我的程序(用GetWindowLong获得)也不同。详见下图:
赵4老师 2018-12-31
  • 打赏
  • 举报
回复
将你程序获取的各变量值和用spy++程序获取的各变量值对比一下。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧