问题描述
你好,
我可以在另一个进程中从ListView中检索列文本但是我不知道如何访问结构元素(LVCOLUMN)
< code>
//句柄变量是一个有效的ListView句柄
LV_COLUMN ListViewItem = new LV_COLUMN ();
IntPtr ListViewItemPointer = IntPtr.Zero;
byte [] ListViewItemBuffer = new byte [512];
IntPtr ListViewPointer_item = IntPtr。零;
IntPtr ListViewProcessPointer = IntPtr.Zero;
//打开流程
int ProcessID;
Win32.GetWindowThreadProcessId(Handle,out ProcessID);
ListViewProcessPointer = Win32.OpenProcess(Win32.PROCESS_VM_OPERATION
| Win32.PROCESS_VM_READ | Win32.PROCESS_VM_WRITE |
Win32.PROCESS_QUERY_INFORMATION,false,(int)ProcessID);
//分配内存
ListViewItemPointer = Win32.VirtualAllocEx(ListViewProcessPointer,
我ntPtr.Zero,Marshal.SizeOf(typeof(LV_COLUMN)),Win32.MEM_COMMIT,
Win32.PAGE_READWRITE);
ListViewPointer_item = Win32.VirtualAllocEx(ListViewProcessPointer,
IntPtr.Zero,512,Win32.MEM_COMMIT,Win32.PAGE_READWRITE);
//获取列文本(pszText)和宽度(cx)
int ColumnCnt = 0;
bool GetColumnResult = true;
while(GetColumnResult)
{
ListViewItem.mask =(int)Win32.ListViewConstants.LVC F_TEXT |
(int)Win32.ListViewConstants.LVCF_WIDTH;
ListViewItem.cchTextMax = 512;
ListViewItem.pszText = ListViewPointer_item;
Win32.WriteProcessMemory(ListViewProcessPointer,
ListViewItemPointer,
ref ListViewItem,Marshal.SizeOf(typeof( LV_COLUMN)),
IntPtr.Zero);
GetColumnResult = Convert.ToBoolean((int)Win32.SendM essage(
Handle,( int)Win32.WindowsMessages.LVM_GETCOLUMN,
(IntPtr)ColumnCnt,ListViewItemP ointer));
if(GetColumnResult)
{
IntPtr bytesReaded;
IntPtr buff = IntPtr.Zero ;
Win32.ReadProcessMemory(ListViewProcessPointer,
MessageBox.Show(" ; ColumnText = [" + Encoding.Unicode.GetString(ListViewItemBuffe r)
+"]");
MessageBox.Show(" ColumnWidth(not
working)=" + ListViewItem.cx.ToString());
}
ColumnCnt ++;
}
Win32.VirtualFreeEx(ListViewProcessPointer,ListViewItemPointer,0,
Win32.MEM_RELEASE);
Win32.VirtualFreeEx(ListViewProcessPointer,ListViewPointer_item,0,
Win32.MEM_RELEASE);
< / code:
问题
******** *******
- 如何从内存中获取我的LVCOLUMN结构来读取cv
成员以获得列宽?
- Unicode问题,当displ对列名称感兴趣,看起来有一些要修剪的东西,因为最后一个]
执行时缺少字符:
MessageBox.Show(" ColumnText = [" + Encoding.Unicode.GetString(ListViewItemBuffe r)
+"]< -Text这里忽略了为什么???;);是否有人知道正确的方法
修剪字符串?
我找不到unicode问题的丑陋办法,但我真的需要
来访问LVCOLUMN元素....宽度示例
LVCOLUMN.cx假设包含使用
掩码时列的宽度LVCF_WIDTH。
我很长时间以来一直在努力....如果可以,请帮助我。
谢谢。
Hello,
I can retrieve column text from a ListView in another process but I
cant figure out how to access to structure elements (LVCOLUMN)
<code>
//Handle variable is a valid ListView handle
LV_COLUMN ListViewItem = new LV_COLUMN();
IntPtr ListViewItemPointer = IntPtr.Zero;
byte[] ListViewItemBuffer = new byte[512];
IntPtr ListViewPointer_item = IntPtr.Zero;
IntPtr ListViewProcessPointer = IntPtr.Zero;
//open the process
int ProcessID;
Win32.GetWindowThreadProcessId(Handle, out ProcessID);
ListViewProcessPointer = Win32.OpenProcess(Win32.PROCESS_VM_OPERATION
| Win32.PROCESS_VM_READ | Win32.PROCESS_VM_WRITE |
Win32.PROCESS_QUERY_INFORMATION, false, (int)ProcessID);
//allocate memory
ListViewItemPointer = Win32.VirtualAllocEx(ListViewProcessPointer,
IntPtr.Zero, Marshal.SizeOf(typeof(LV_COLUMN)), Win32.MEM_COMMIT,
Win32.PAGE_READWRITE);
ListViewPointer_item = Win32.VirtualAllocEx(ListViewProcessPointer,
IntPtr.Zero, 512, Win32.MEM_COMMIT, Win32.PAGE_READWRITE);
//Get column Text (pszText) and width (cx)
int ColumnCnt=0;
bool GetColumnResult=true;
while (GetColumnResult)
{
ListViewItem.mask=(int)Win32.ListViewConstants.LVC F_TEXT|
(int)Win32.ListViewConstants.LVCF_WIDTH;
ListViewItem.cchTextMax = 512;
ListViewItem.pszText = ListViewPointer_item;
Win32.WriteProcessMemory(ListViewProcessPointer,
ListViewItemPointer,
ref ListViewItem, Marshal.SizeOf(typeof(LV_COLUMN)),
IntPtr.Zero);
GetColumnResult=Convert.ToBoolean((int)Win32.SendM essage(
Handle, (int)Win32.WindowsMessages.LVM_GETCOLUMN,
(IntPtr)ColumnCnt, ListViewItemPointer));
if (GetColumnResult)
{
IntPtr bytesReaded;
IntPtr buff = IntPtr.Zero;
Win32.ReadProcessMemory(ListViewProcessPointer,
ListViewPointer_item, ListViewItemBuffer, 512, out bytesReaded);
MessageBox.Show("ColumnText=["+Encoding.Unicode.GetString(ListViewItemBuffe r)
+"]");
MessageBox.Show("ColumnWidth (not
working)="+ListViewItem.cx.ToString());
}
ColumnCnt++;
}
Win32.VirtualFreeEx(ListViewProcessPointer, ListViewItemPointer, 0,
Win32.MEM_RELEASE);
Win32.VirtualFreeEx(ListViewProcessPointer, ListViewPointer_item, 0,
Win32.MEM_RELEASE);
</code:
QUESTIONS
***************
- How can I get my LVCOLUMN structure from memory to read the cv
member to get the column Width ?
- Unicode issue, When displaying the column name, look like there is
something to trim because the last "]" character is missing when
executing :
MessageBox.Show("ColumnText=["+Encoding.Unicode.GetString(ListViewItemBuffe r)
+"]<-Text here is ignored why???"); Is anybody know the correct way
to trim the string?
I cant find an ugly workaround for the unicode issue but I really need
to get access to the LVCOLUMN elements....the width by example
LVCOLUMN.cx is suppose to contain the width of the column when using
the mask LVCF_WIDTH.
I''m working on this since a long time....please help me if you can.
Thanks.
推荐答案
Encoding.GetString正在转换* all *数据(因为这就是你要求它做的
) 。这意味着你最终会在最后加载一个
unicode字符0 - 而MesageBox.Show将会停止它
看到字符0,认为它'这是字符串的结尾。你应该
实际上只解码正确的字节数,但是另一种方法是使用TrimEnd修改Encoding.GetString结果的结果(''\ 0 '')
Jon
Encoding.GetString is converting *all* the data (because that''s what
you''ve asked it to do). That means you''ll end up with a load of
unicode character 0s at the end - and MesageBox.Show will stop when it
sees character 0, thinking it''s the end of the string. You should
really only decode the correct number of bytes, but an alternative is
to trim the end of the result of Encoding.GetString with TrimEnd(''\0'')
Jon
如果你正在使用PtrToStructure的重载需要
destination Object作为第二个参数(而不是Type),保留在
中,如果你传递一个值类型它就不会工作。所以,如果你将
声明为ListViewItem作为结构,你应该使用
PtrToStructure(IntPtr,Type)重载 - 也许这就是问题?
如果没有,那么你可以更具体地说明究竟出了什么问题
给你?
If you''re using the overload of PtrToStructure which takes a
destination Object as a second argument (rather than a Type), keep in
mind that it won''t work if you pass it a value type. So, if you''ve
declared ListViewItem as a struct , you should rather use the
PtrToStructure(IntPtr, Type) overload - perhaps this is the problem?
If not, then can you be more specific as to what exactly goes wrong
for you?
这篇关于从另一个进程的内存中获取结构和unicode问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!