从另一个进程的内存中获取结构和unicode问题 [英] Get structure from memory in another process and unicode question

查看:59
本文介绍了从另一个进程的内存中获取结构和unicode问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我可以在另一个进程中从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.

推荐答案

8月12日上午6:05 * am,michelqa< miche ... @ yahoo.cawrote:


< snip>
On Aug 12, 6:05*am, michelqa <miche...@yahoo.cawrote:

<snip>

- Unicode问题,当显示列名时,看起来像

需要修剪的东西,因为最后一个 ]"

执行时缺少字符:

MessageBox.Show(" ColumnText = [" + Encoding.Unicode.GetString(ListViewItemBuffe r)

+"]< -Text这里忽略了为什么???;); *是否有人知道正确的方式

修剪字符串?
- 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?



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


感谢Jon努力修复我的unicode问题


我仍​​然急需帮助解决我的问题LV_COLUMN

结构。


看起来缺少一些东西来获取结构

ListViewItemPointer ...我尝试Marshal.PtrToStruct和其他不同的

事情没有成功
Thanks Jon it work to fix my unicode problem

I desperately still need help for my problem to retrive LV_COLUMN
structure.

Look like something is missing to get the struct with
ListViewItemPointer... I try Marshal.PtrToStruct and different other
things without success


8月12日,12:34 * pm ,michelqa< miche ... @ yahoo.cawrote:
On Aug 12, 12:34*pm, michelqa <miche...@yahoo.cawrote:

感谢Jon为修复我的unicode问题而努力


我拼命地仍然需要帮助解决我的问题LV_COLUMN

结构。


看起来缺少一些东西以获得结构

ListViewItemPointer ...我试试Marshal.PtrToS truct和其他不同的

没有成功的东西
Thanks Jon it work to fix my unicode problem

I desperately still need help for my problem to retrive LV_COLUMN
structure.

Look like something is missing to get the struct with
ListViewItemPointer... I try Marshal.PtrToStruct and different other
things without success



如果你正在使用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问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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