如何在另一个进程的Windows Mobile分配内存 [英] How to allocate memory in another process for windows mobile

查看:132
本文介绍了如何在另一个进程的Windows Mobile分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取Windows Mobile的另一个进程中ListView控件的内容。要做到这一点,我需要为了把这些值有(然后从我的程序读取它们)指向一些空闲内存的进程。这可以在正常的Windows或Win32的完成与 VirtualAllocEx的功能

I'd like to read the contents of another process listview control in windows mobile. To do this, I need a pointer to some free memory to that process in order to put the values there (and then read them from my process). This can be done in normal Windows or Win32 with the VirtualAllocEx function.

但是,此功能不支持Windows Mobile的!你能推荐我的方式来分配内存?

However, this function is not supported in windows mobile ! Can you recommend me a way to allocate that memory?

推荐答案

好了,很多搜索后,我认为我找到了一个可行的解决方案。我不是说,该解决方案可以完美运行或工作时间的100%,但我认为这是一个可以与Windows Mobile的为我们提供了记忆工具来完成最好的。

Well, after a lot of search, I believe that I found a working solution. I am not saying that the solution works perfect or works 100% of the time, however I believe that it is the best that can be done with the memory tools that Windows Mobile provides us.

下面就是该方法的一个粗略的说明(如果人民需要,我可以提供完整的源$ C ​​$ C): a)使用CreateToolhelp32Snapshot来获取所有正在运行的进程的信息

Here's a rough description of the method (if people needed, I can provide full source code): a) Use CreateToolhelp32Snapshot to get information from all running processes


CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS |  TH32CS_SNAPNOHEAPS , 0 );

二)通过这些流程走,直到你找到一个与ListView。您将有一个PROCESSENTRY32结构为proccess,姑且称之为PE32。

b) Walk through these processes until you find the one with the listview. You'll have a PROCESSENTRY32 structure for that proccess, let's call it pe32.


PROCESSENTRY32 pe32;
if( !Process32First( hProcessSnap, &pe32 ) ) 
...
do { ... }
while( Process32Next( hProcessSnap, &pe32 ) );

c)利用调用OpenProcess得到一个句柄这个过程中,我们称之为hProcess。

c) Use OpenProcess to get a handle for that process, let's call it hProcess.


HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );

D)从pe32.th32MemoryBase + 512开始,直到pe32.th32MemoryBase + 0x02000000(processses Windows Mobile 6中有32 MB的内存空间)使用VirtualQuery来的方法来获得有关该地区的信息。你会得到一个MEMORY_BASIC_INFORMATION结构,让我们MBI调用它。内存区域将被mbi.RegionSize递增

d) Starting from pe32.th32MemoryBase+512 until pe32.th32MemoryBase + 0x02000000 (processses in windows mobile 6 have 32 mb memory space) use the VirtualQuery method to receive information about that region. You'll get a MEMORY_BASIC_INFORMATION structure, let's call it mbi. Memory regions will be incremented by mbi.RegionSize


DWORD dwAddress = pe32.th32MemoryBase + 512;
DWORD dwStopAddress = pe32.th32MemoryBase + 0x02000000; 
while (VirtualQuery((LPVOID)dwAddress, &mbi, sizeof(mbi))) {
 ...
  dwAddress += mbi.RegionSize;
  if (dwAddress >= dwStopAddress) break;
}

e)检查,看看是否mbi.State == MEM_COMMIT和mbi.Protect == PAGE_READWRITE。如果两个都是真的,那么你可以写信给该区域。此外,一起来看看mbi.RegionSize,看看内存区域足以让您的数据。如果条件得不到满足,就会到下一个区域。警告:你不知道什么你会写。您可能会破坏列表视图的应用程序。更多关于这一点。

e) Check to see if mbi.State == MEM_COMMIT and mbi.Protect == PAGE_READWRITE. If both are true, then you can write to that area. Also, take a look at mbi.RegionSize to see if the memory region is enough for your data. If condition are not met, will to next region. WARNING: You do not know on what you will write. You may break the listview application. More on this later.


if(mbi.State == MEM_COMMIT && mbi.Protect == PAGE_READWRITE ) { ... }

F)里面的previous如果(所有条件满足):声明指针将指向mbi.BaseAddress - pe32.th32MemoryBase:

f) Inside the previous if (all conditions met): Declare a pointer that will point to mbi.BaseAddress - pe32.th32MemoryBase:

char * membase2 = (char *)mbi.BaseAddress - pe32.th32MemoryBase ;

G)现在,你可以读取或写入使用ReadProcessMemory和WriteProcessMemory的另一个进程的内存!举例来说,这里是我的code读取列表视图中的内容:

g) Now you may read or write the memory of the other process using ReadProcessMemory and WriteProcessMemory! For instance, here's my code for reading the contents of the listview:


LVITEM lvi, *_lvi;
LPWSTR _item;
TCHAR item[128];

_lvi = (LVITEM *) membase2;
_item = (LPWSTR)membase2;
_item += 128;

lvi.iSubItem=1;
lvi.pszText=_item;
lvi.iItem = 0;
lvi.cchTextMax = 64;

WriteProcessMemory(hProcess, _lvi, &lvi, sizeof(LVITEM), NULL);
SendMessage(listHWND, LVM_GETITEMTEXT, (WPARAM)0, (LPARAM)_lvi);
ReadProcessMemory(hProcess, _item, item, 128, NULL);
wprintf(TEXT("%s\n"), item);

h)最后的想法:该方法不工作。已经由我测试。但是,你必须时刻考虑到我之前提到的警告:你不知道你要去哪里写的:存储您将使用COMMITED,你可以写,但您将无法确定你正在写什么。当然,这是其他应用程序的内存,所以你只有打破它。如果这不要紧,多了,你可以重新启动,并继续阅读它的内存!当我测试了它,其他的应用程序没有美眉甚至一度,但它只是关闭一次或两次。另外,如果你不能得到它的工作,然后尝试另一个存储区(步骤d)。

h) Final thoughts: The method does work. It has been tested by me. However, you must always take into account the warning I mentioned before: You do not know where you are going to write: The memory you will use commited and you can write to it, however you won't be sure on what you are writing. Of course, this is the memory of the other application so you'll only break it. If that doesn't matter that much, you can the restart it and continue reading its memory! When I tested it, the other application did not crush even once, however it was just closed one or two times. Also, if you cannot get it to work, then try another memory region (step d).

这篇关于如何在另一个进程的Windows Mobile分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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