在进程之间移动数据

发布于 2024-09-17 09:50:08 字数 1249 浏览 2 评论 0原文

我问这个问题的原因是寡妇不支持进程间通信的好方法。所以我想为Windows进程之间的通信点创建一个DLL。线程由一个进程拥有,不能交给另一个进程。 每个线程都有自己的堆栈。 如果加载 DLL (loadlibray) 并调用 DLL 函数,该函数会向 Windows 请求内存。我写信是为了认为线程仍然由同一进程拥有并将内存分配到同一进程中。

所以我在想我可以转向汇编将一个小内存块重新分配给另一个进程。创建一个关键部分,将数据复制到另一个(已创建的)内存块,然后返回到原始块到其原始进程,而无需设置窗口。以前有人这样做过吗?或者是更好的方法。

此致, 莱克斯·迪恩.


我看到其他方法速度相当快,但我想要一种非常快速且开销很小的方法。管道和互联网显然可以工作,但不是最好的选择,但实施起来很简单(感谢提供此类建议的人)。有时我想以相当规律的间隔发送相当多的 500 字节块。我喜欢 WM_COPYDATA 因为它看起来很快,我在互联网上查找的最大问题是:- GetCurrentProcess 和 DuplicateHandle 来获取真正的句柄。寻找其他进程。并使用消息来设置内存然后使用WM_COPYDATA。我只需要两条消息a)指针和大小b)数据已被复制。 我的应用程序进程很容易“GetCurrentProcess”,除了它是一个伪句柄,它总是 $FFFFFFE。我需要真实的进程句柄,但互联网上没有人给出 DuplicateHandle 的示例。这就是让我难住的原因。你能给我看一个 DuplicateHandle 的例子吗?这就是让我困惑的地方?

我不喜欢通过表格来获取句柄,因为一个应用程序并不总是有当前的表格。 我不喜欢通过表格来获取句柄,因为一个应用程序并不总是有当前的表格。 在 Delphi 中,我看到使用 TSpeedButton 发送消息,以在应用程序之间建立简单的快速通信方法,我猜它很可能使用大约 80 条指令。所以我仍然在思考dll的问题。 Mads Elvheim 发送的示例与我已经知道的处于同一行。 我仍然愿意了解使用我自己的 *.Dll 的任何其他选项 因为对我来说很重要的应用程序可以简单地在 *.DLL 上注册/取消注册它自己的进程,而不是一直搜索以查看进程是否是当前的。 这就是我在进程之间使用 *.DLL 管理内存的方式,但我没有被告知。 对我来说,实现 DLL 并不难,因为我已经拥有自己的一个正在运行。

真正的底线是访问Windows来创建一个好的选择。因为我对想法非常开放。甚至是进程之间或 Windows 调用的汇编指令。但我不知道通过做非法的事情来让法庭撞毁窗户以太。 因此,请举例说明您所做的满足我的需要的事情。这很快,我很感兴趣,因为无论如何我很可能会使用它。

The reason I ask this is widows do not support a good method to communicate between processes. So I want to create a DLL for a communications point between windows processes. A thread is owned by a process and cannot be given to another process.
Each thread has a stack of its own.
If a DLL is loaded (loadlibray) and a DLL function is called that asks windows for memory. Am I write to think the thread is still being owned by the same process and allocates memory into that same process.

So I’m thinking can I turn to assembly to reallocate a small memory block to another process. Create a critical section, copy the data over to another (already created) memory block and return to the original block to its original process with out up setting windows. Has any one done that before. Or is thier a better way.

Best regards,
Lex Dean.


I see other methods that mite be quite fast but I would like a very fast method that has little over head. Pipes and internet will obviously work but are not the best option yet simple to implement (thanks to offer such suggestions guys). I want to send quite a few 500 byte blocks at quite regular intervals sometimes. I like WM_COPYDATA because it looks fast, my biggest question that I have been looking all over the internet is:- GetCurrentProcess and DuplicateHandle to get the real handle. Finding the other process. And using messages to set up memory and then use WM_COPYDATA. I only need two messages a) the pointer and size b) the data has been copied.
I get my application process easy ‘GetCurrentProcess’ except it’s a pseudo handle, that’s always $FFFFFFE. I need the real process handle and no body on the internet gives an example of DuplicateHandle. That’s what’s got me stumped. Can you show me an example of DuplicateHandle as that’s what’s got me stumped?

I do not like turning to a form to get a handle as one application dose not always have a current form.
I do not like turning to a form to get a handle as one application dose not always have a current form.
In Delphi I have seen message sending with TSpeedButton to set up a simple fast communication methods between applications that most probably uses about 80 instructions I guess. And so I still thinking to think dll’s. The example Mads Elvheim sent is on that same line as what I already know.
I'm still willing to understand any other options of using my own *.Dll
Because my applications important to me can simply register/unregister on the *.DLL its own process rather than searching all the time to see if a process is current.
It’s how I manage memory with a *.DLL between process but I’m not told about.
To me DLL’s are not hard to implement to me as I already have one of my own in operation.

The real bottom line is access to windows to create a good option. As I’m very open to idea’s. Even the assembly instructions for between processes or a windows call. But I do not what to get court crashing windows ether by doing things illegal.
So please show an example of what you have done that is to my needs. That is fast and I’m interested as I most probably will use it anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

故事和酒 2024-09-24 09:50:08

我有一个基于命名管道的非常快速的 IPC(进程间通信)解决方案。它非常快并且非常易于使用(它向您隐藏了实际的实现。您只需处理数据包)。也经过测试和证明。您可以在这里找到代码和演示。

http://www.cromis.net/blog/downloads/cromis-ipc/< /a>

它也适用于同一 LAN 中的计算机。

I have a very fast IPC (interprocess communication) solution based on named pipes. It is very fast and very easy to use (It hides the actual implementation from you. You just work with data packets). Also tested and proven. You can find the code and the demo here.

http://www.cromis.net/blog/downloads/cromis-ipc/

It also works across computers in the same LAN.

娜些时光,永不杰束 2024-09-24 09:50:08

如果您的进程有消息循环(对于 Windows),您可以使用 WM_COPYDATA 消息发送/接收序列化数据http://msdn.microsoft.com/en-us/library/ms649011(VS.85).aspx

请记住,只有允许读取为 COPYDATASTRUCT::lpData 成员分配的内存。同样,您不能传递具有指针的结构。相反,数据必须被序列化。并且接收方只能读取该结构,而不能对其进行写入。示例:

/* Both are conceptual windows procedures. */

/* For sending : */
{
    ...
    TCHAR msg[] = _T("This is a test\r\n");
    HWND target;
    COPYDATASTRUCT cd = {0};
    cd.lpData = _tcsdup(msg); // We allocate and copy a string, which is fine.
    cd.cbData = _tcsclen(msg) + 1; //The size of our data. Windows needs to know this.
    target = FindWindow(..); //or EnumProcesses
    SendMessage(target, WM_COPYDATA, (LPARAM)hwnd, (WPARAM)&cd);
}

/* For receiving */
{
    ...
    case WM_COPYDATA:
    {
        TCHAR* msg;
        COPYDATASTRUCT* cb = (COPYDATASTRUCT*)wParam;
        sender = FindWindow(..); //or EnumProcesses

        //check if this message is sent from the window/process we want
        if(sender == (HWND)lParam){
            msg = _tcsdup(cb->ldData);
            ...
        }
        break;
    }
}

否则,请使用内存映射文件或网络套接字。

If your processes have message loops (with windows), you can send/receive serialized data with the WM_COPYDATA message: http://msdn.microsoft.com/en-us/library/ms649011(VS.85).aspx

Just remember that only the allocated memory for the COPYDATASTRUCT::lpData member is allowed to be read. Again, you can not pass a structure that has pointers. The data must be serialized instead. And the receiving side can only read this structure, it can not write to it. Example:

/* Both are conceptual windows procedures. */

/* For sending : */
{
    ...
    TCHAR msg[] = _T("This is a test\r\n");
    HWND target;
    COPYDATASTRUCT cd = {0};
    cd.lpData = _tcsdup(msg); // We allocate and copy a string, which is fine.
    cd.cbData = _tcsclen(msg) + 1; //The size of our data. Windows needs to know this.
    target = FindWindow(..); //or EnumProcesses
    SendMessage(target, WM_COPYDATA, (LPARAM)hwnd, (WPARAM)&cd);
}

/* For receiving */
{
    ...
    case WM_COPYDATA:
    {
        TCHAR* msg;
        COPYDATASTRUCT* cb = (COPYDATASTRUCT*)wParam;
        sender = FindWindow(..); //or EnumProcesses

        //check if this message is sent from the window/process we want
        if(sender == (HWND)lParam){
            msg = _tcsdup(cb->ldData);
            ...
        }
        break;
    }
}

Otherwise, use memory mapped files, or network sockets.

落在眉间の轻吻 2024-09-24 09:50:08

我目前使用 Delphi 中的 Mailslots 来完成此操作,效率非常高。

I currently use Mailslots in Delphi to do it and it is very efficient.

青瓷清茶倾城歌 2024-09-24 09:50:08

“Win32 DLL 被映射到调用进程的地址空间。默认情况下,每个使用 DLL 的进程都有自己的所有 DLL 全局变量和静态变量的实例。如果您的 DLL 需要与其他实例加载的数据共享,应用程序中,您可以使用以下任一方法:

• 使用data_seg pragma 创建命名数据段。
•使用内存映射文件。请参阅有关内存映射文件的 Win32 文档。"

http:/ /msdn.microsoft.com/en-us/library/h90dkhs0(VS.80).aspx

您不能在进程之间共享指针,它们只对您可能分配的进程有意义。遇到问题。

"Win32 DLLs are mapped into the address space of the calling process. By default, each process using a DLL has its own instance of all the DLLs global and static variables. If your DLL needs to share data with other instances of it loaded by other applications, you can use either of the following approaches:

•Create named data sections using the data_seg pragma.
•Use memory mapped files. See the Win32 documentation about memory mapped files."

http://msdn.microsoft.com/en-us/library/h90dkhs0(VS.80).aspx

You cannot share pointers between processes, they only make sense to the process that alloc'd it. You're likely to run into issues.

春夜浅 2024-09-24 09:50:08

Win32 在这方面与任何其他现代操作系统没有什么不同。 Windows 中有大量 IPC 服务可供您使用。

尝试描述您想要解决的任务 - 而不是“...那么我认为我需要在此处复制该内存块...”。这不是你的任务。您的客户没有对您说:“我想将线程从一个进程转移到另一个进程”。

Win32 is not different from any other modern OS in this aspect. There are plenty IPC services at your disposal in Windows.

Try to describe, which task you want to solve - not the "...then I think that I need to copy that block of memory here...". It's not your task. Your customer didn't say you: "I want to transfer thread from one process to another".

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