如何让另一个进程创建的进程使用创建者进程的一部分内存?

发布于 2024-09-15 22:10:23 字数 713 浏览 7 评论 0原文

我有两个控制台进程,第二个是由第一个进程使用下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自由如风 2024-09-22 22:10:23

您可以使用 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.

南冥有猫 2024-09-22 22:10:23

这是我的不知情的意见(没有在 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.

旧情别恋 2024-09-22 22:10:23

亚历克斯·法伯的回答是正确的;您可以使用 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.

開玄 2024-09-22 22:10:23

我以前用#pragma 做过这个。我目前手头没有实际的代码,但我认为类似的东西可能适用于 Visual Studio 2008 C++:

//put this code in a dll that both processes link to 
#pragma section("shared",read,write,shared)

__declspec(allocate("shared"))
int i = 0;  // in theory, both processes should be able to access and modify i

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++:

//put this code in a dll that both processes link to 
#pragma section("shared",read,write,shared)

__declspec(allocate("shared"))
int i = 0;  // in theory, both processes should be able to access and modify i
来世叙缘 2024-09-22 22:10:23

我相信使用 volatile 将允许其他进程访问内存。当然它需要一个指向它的指针

I believe the use of volatile will allow other processes access to memory. Of course it will need a pointer to it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文