将指向局部变量的指针传递给另一个进程有时工作,而不是其他进程 [英] Passing a pointer to a local variable to another process works sometimes and not others

查看:136
本文介绍了将指向局部变量的指针传递给另一个进程有时工作,而不是其他进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A while back I wrote a program that lets you pick and modify windows. It uses WindowFromPoint() to get a handle to the window under the mouse cursor and calls GetWindowText() on that to get the window’s title. This works just fine.

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

I then added the ability to get the title of columns of a list-control. The problem is that unlike GetColumnWidth() which returns the width, there is no corresponding function to get the title. Instead, getting the title of the column header requires passing a buffer to GetColumn() to fill with the title. As such, when I assign the pszText member of an LVCOLUMN structure to a pointer to a buffer and pass the struct to GetColumn(), the other process interprets the pointer as within its own memory space. Obviously this won’t work.

我通过使用 CodeProject文章。它工作得很好。但我仍然困惑为什么 GetWindowText() 有效。

I worked around that by using a method from a CodeProject article. It worked great. However I am still confused about why GetWindowText() did work.

因为 GetWindowText()的工作原理与 GetColumn()相同;它不返回窗口标题,它需要一个缓冲区/变量来放置标题。

It is confusing because GetWindowText() works the same as GetColumn(); it does not return the window title, it takes a buffer/variable to put the title in.

那么为什么将一个变量传递给另一个进程填充工作在一个

So why does passing a variable to another process to be filled work in one scenario but not another?




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



Here is a snippet that gets the window title:

// 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!



下面是一个相应的代码片段,


Here is a corresponding snippet to get the title of a column:

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

GetWindowText is special. When you call it on a window that belongs to another process, it doesn't actually call the other process to get the text.

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

CListCtrl::GetColumn on the other hand is an inline function (see afxcmn.inl) that calls SendMessage, so the message goes to the other process, which then interprets the pointer in its own memory space.

这篇关于将指向局部变量的指针传递给另一个进程有时工作,而不是其他进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆