如何让另一个进程创建的进程使用创建者进程的一部分内存?
我有两个控制台进程,第二个是由第一个进程使用下面的 API 创建的:
BOOL WINAPI CreateProcess(
__in_opt LPCTSTR lpApplicationName,
__inout_opt LPTSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCTSTR lpCurrentDirectory,
__in LPSTARTUPINFO lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
现在我想知道是否通过 IpCommandLine 将指针传递给第一个进程的部分内存到第二个进程由第一个进程调用,第二个进程读取该指针指向的内存肯定会导致访问冲突错误,还是受该 API 某些参数值的影响? 如果我无法单独使用此 API 来实现我的目的,您建议采用什么方法进行访问?
I got two console processes that second one is created by first one using the API below:
BOOL WINAPI CreateProcess(
__in_opt LPCTSTR lpApplicationName,
__inout_opt LPTSTR lpCommandLine,
__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in BOOL bInheritHandles,
__in DWORD dwCreationFlags,
__in_opt LPVOID lpEnvironment,
__in_opt LPCTSTR lpCurrentDirectory,
__in LPSTARTUPINFO lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInformation
);
Now I wonder that if I pass a pointer to a part of memory of the first process, via IpCommandLine to the second process which is called by first process, will reading the memory to which that pointer points, by the second process definitely cause an access violation error or is it subject to value of some parameter of that API?
If I can't use this API alone for my purpose, what method do you propose for the access ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 ReadProcessMemory/WriteProcessMemory API 访问此内存。另一个进程需要知道该进程的内存地址和句柄才能访问其内存。
You can access this memory using ReadProcessMemory/WriteProcessMemory API. Another process needs to know memory address and handle of the process to access its memory.
这是我的不知情的意见(没有在 Windows 上做过很多这样的事情):
单独的进程(几乎根据定义)通常无法直接访问彼此的内存空间。因此,您应该使用进程间通信之一(提示:这是您在谷歌上搜索的内容)方法。
我在这方面对Windows了解不多,但我想到了诸如套接字、管道、内存映射文件和各种形式的RPC之类的东西。当然,这取决于您的实际用例。
Here is my uninformed opinion (haven't done much of this thing on windows):
Separate processes (pretty much by definition) normally don't have the access to each other's memory space directly. Therefore you should use one of the interprocess communication (hint: that is something you google for) methods.
I don't know much about windows in this respect, but things like sockets, pipes, memory-mapped files and various forms of RPC come to mind. It, of course, depends on your actual use case.
亚历克斯·法伯的回答是正确的;您可以使用 Win32 API 调用来读取另一个进程的内存。然而,这可能是一个坏主意。如果您有要移动的数据,则可以将其作为命令行上的参数传递,也可以将句柄传递给其他进程并使用管道连接两个进程。
Alex Farber's answer is correct; you can use Win32 API calls to read another process's memory. However, this is probably a bad idea. If you have a data to move around, you can either pass it as a parameter on the command line, or you can pass a handle to the other process and connect the two processes using a pipe.
我以前用#pragma 做过这个。我目前手头没有实际的代码,但我认为类似的东西可能适用于 Visual Studio 2008 C++:
I've done this before with #pragma's. I don't have the actual code handy at the moment, but I think something similar to this might work with Visual Studio 2008 C++:
我相信使用 volatile 将允许其他进程访问内存。当然它需要一个指向它的指针
I believe the use of volatile will allow other processes access to memory. Of course it will need a pointer to it