将一个指向局部variables的指针传递给另一个进程有时候并不是其他的

后来我写了一个程序,让你select和修改窗口。 它使用WindowFromPoint()来获取鼠标光标下的窗口的句柄,并调用GetWindowText()来获得窗口的标题。 这工作得很好。

然后我添加了获得列表控件列的标题的能力。 问题是,不像GetColumnWidth()返回宽度,没有相应的函数来获得标题。 相反,获取列标题的标题需要将缓冲区传递给GetColumn()来填充标题。 因此,当我将LVCOLUMN结构的pszText成员赋值给一个指向缓冲区的指针,并将该结构传递给GetColumn() ,另一个进程将指针解释为它自己的内存空间。 显然这是行不通的。

我通过使用CodeProject文章中的方法解决了这个问题。 它运作良好。 但是,我仍然困惑为什么GetWindowText() 确实工作。

这很令人困惑,因为GetWindowText()GetColumn() 。 它不会返回窗口标题,它需要一个缓冲区/variables来放置标题。

那么为什么将一个variables传递给另一个进程需要填充,而不是另一个呢?

这里是一个获取窗口标题的代码片段:

 // 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是特殊的。 当您在属于另一个进程的窗口上调用它时, 实际上并不会调用其他进程来获取文本 。

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

这不是一个答案,而是一个建议…可能是其他窗口是Unicode。 在这种情况下,您可能必须使用wice字符版本才能使其工作。 有一个API函数IsWindowUnicode()会告诉你一个给定的窗口是否是本机的Unicode。