gpt4 book ai didi

c++ - 将指向局部变量的指针传递给另一个进程有时有效,但有时无效

转载 作者:可可西里 更新时间:2023-11-01 12:01:25 26 4
gpt4 key购买 nike

前阵子我写了一个程序,可以让你选择和修改窗口。它使用 WindowFromPoint() 获取鼠标光标下窗口的句柄,并调用 GetWindowText() 获取窗口的标题。这很好用。

然后我添加了获取列表控件列标题的功能。问题在于,与返回宽度的 GetColumnWidth() 不同,没有相应的函数来获取标题。相反,获取列标题的标题需要将缓冲区传递给 GetColumn() 以填充标题。因此,当我将 LVCOLUMN 结构的 pszText 成员分配给指向缓冲区的指针并将该结构传递给 GetColumn() 时,其他进程将指针解释为在其自己的内存空间内。显然这是行不通的。

我使用 CodeProject article 中的方法解决了这个问题.效果很好。但是,我仍然对为什么 GetWindowText() did 起作用感到困惑。

这很令人困惑,因为 GetWindowText()GetColumn() 的工作方式相同;它不返回窗口标题,它需要一个缓冲区/变量来放入标题。

那么,为什么将变量传递给另一个进程以进行填充在一种情况下有效,而在另一种情况下却无效?



这是获取窗口标题的片段:

// m_Wnd is a pointer to a window class, pointing to a window in another process
CWnd *m_Wnd=WindowFromPoint(point);

// t is a local variable within this program’s address space
CString t;

// passing a reference to a local variable to another process
m_Wnd->GetWindowText(t); //works correctly!


这是获取列标题的相应代码段:

// *lc points to a list-control in another process
int colwidth = lc->GetColumnWidth(col); //works correctly!

// local variables
CString colname = _T("");
LVCOLUMN col;
memset(&col, 0, sizeof(col));

col.mask=LVCF_TEXT;
col.cchTextMax=256;
col.pszText=colname.GetBuffer(256); // passing a pointer to local buffer
BOOL ret=lc.GetColumn(colnum, &col); // buffer is empty
colname.ReleaseBuffer();

最佳答案

GetWindowText 比较特殊。当您在属于另一个进程的窗口上调用它时,it doesn't actually call the other process to get the text .

CListCtrl::GetColumn 另一方面是调用 SendMessage 的内联函数(参见 afxcmn.inl),因此消息到另一个进程,然后在自己的内存空间中解释指针。

关于c++ - 将指向局部变量的指针传递给另一个进程有时有效,但有时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com