根据窗口句柄获取应用程序实例句柄失败

狐帝 狐帝 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题,调用GetWindowLongPtr(hWnd, GWLP_HINSTANCE)时返回0,通用GetLastError获取错误代码,竟然也是0。完整代码如下:

#include <windows.h>
#include <iostream>

using namespace std;

void testGetInstance()
{
const char *className = "TaskSwitcherWnd";
const char *windName = "任务切换";
HWND hWnd = FindWindow(className, windName);
cout << "window handle: " << hWnd << endl;
uint64_t tempHandle = GetWindowLongPtr(hWnd, GWLP_HWNDPARENT);
HWND hpWnd = *(HWND *)(&tempHandle);
cout << "parent window handle(from GetWindowLongPtr): " << hpWnd << endl;
hpWnd = GetParent(hWnd);
cout << "parent window handle(from GetParent): " << hpWnd << endl;
tempHandle = GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
if (tempHandle == 0)
{
DWORD errorCode = GetLastError();
cout << "get instance handle error: " << errorCode << endl;
}
else
{
HINSTANCE hInst = *(HINSTANCE *)(&tempHandle);
cout << "instance handle: " << hInst << endl;
}
}

int main(int argc, char **argv)
{
testGetInstance();
}

这里还有个问题,就是调用GetParent(hWnd);和GetWindowLongPtr(hWnd, GWLP_HWNDPARENT);获得的父窗口句柄与Visual Studio 2017所带的Spy++工具看到的不一样。
请大神们指点迷津。谢谢!
...全文

相关推荐

win32+sdk教程与基础

这个资料是从最基础的window程序写的。然后,还有sdk的一些资料。;希望这个可以帮助大家打下基础

xxyoung

windows窗口创建代码

用vc编写创建窗口的代码,先对txt控件建立变量,对应变量设置为字符串型:m_strPassword,然后updatedata()一下,将用户输入的文字赋予m_strPassword变量,然后对字符串进行验证就可以了

caohui19920104

Delphi下用WindowsAPI创建窗体.net-电脑资料.pdf

。。。

苦茶子12138

Win32窗口事例.txt

Win32下简单窗口实例 外加部分注解 希望大家喜欢

孤独虾

VC++常用函数

VC++常用函数一应俱全 内容举例: 在状态栏中添加时间 1)在String Table中添加New String,在此定义为ID_INDICATOR_CLOCK, 将其Caption设为00:00:00(由于状态栏根据Caption确定时间窗格的缺 省宽度,使用此值将为时间的显示预留空间)。注 2)在MainFrm.cpp中indicators声明处添加ID_INDICATOR_CLOCK,代 码如下:   这一步中ID_INDICATOR_CLOCK的插入位置将影响时间窗格在状态栏中 的显示位置。   3)安装定时器:在MainFrm.cpp中OnCreate函数处添加代码如下:   int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)   { ……   SetTimer(1,1000,NULL);//安装定时器,并将其时间间隔设为1000毫秒   return 0;   }   4)编写时间处理函数:利用ClassWizard为CMainFrame类加入WM_TIMER 的消息处理函数OnTimer,并添加代码如下:   void CMainFrame::OnTimer(UINT nIDEvent)   {// TODO: Add your message handler code here and/or call default   CTime time;   time=CTime::GetCurrentTime();//得到当前时间   CString s=time.Format(″%H:%M:%S″);//转换时间格式,这里格式一定要大写   m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIn-   dex(ID_INDICATOR_CLOCK),s); //显示时钟   CFrameWnd::OnTimer(nIDEvent);}   5)销毁定时器:利用ClassWizard为CMainFrame类加入WM_CLOSE的消息处理   函数OnClose,并添加代码如下:   void CMainFrame::OnClose()   {// TODO: Add your message handler code here and/or call default   KillTimer(1);//销毁定时器

yeye1984
评论 11
收藏 已收藏
分享
返回顶部
狐帝
其它技术问题 3,881 成员 9,044 内容