tags:

views:

250

answers:

8

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.

A: 

"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.

James
Yes I have realised using a file system approch in memory but that is very processor hungry for 500 bytes and very slow. If I knew how to do what a vertaul file system does I'm sure In can get speed.
lexdean
+3  A: 

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.

Mads Elvheim
Thanks I have not found this beforeThis may be quite fast I need to try it
lexdean
+5  A: 

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.

Runner
A: 

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".

Alexander
+1  A: 

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

philnext
A: 

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.

lexdean
A: 

I have decided a lazy write/read with the registry is an easy way to do what I want but this does not help me identify processes easly.

Lex Dean.

lexdean
A: 

May I ask has any one used:- DllMain Callback Function I understand threads cannot be relocated to another process leaving only one option to comunicate to a process. Thats using a message and thats ok.

 What I understand is when accessing a file, the internet scoket, and all thouse things. The DOS 640k memory block is used with the processors DS register. 

And the dll is allicated a memory space in every process that calls loadlibray with the dll's name. So what I'm thinking is:- as memory space is allicated for a dll can that space be extended and accessed by all processes.

lexdean