| |

VerySource

 Forgot password?
 Register
Search
View: 1494|Reply: 8

Ask: Two applications communicate

[Copy link]

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-17 15:00:01
| Show all posts |Read mode
Will my two applications communicate

What is the fastest connection between them?

You only need to pass pointers when passing data
Reply

Use magic Report

0

Threads

15

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-12-17 21:30:01
| Show all posts
0 Memory mapping file

1 Kernel shared area
Reply

Use magic Report

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-12-17 22:00:01
| Show all posts
Thank you

Explain in more details
Reply

Use magic Report

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-12-17 22:45:01
| Show all posts
The party I received the message is a dll
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-18 03:30:02
| Show all posts
Inter-process communication and data sharing under Windows
There are many ways to implement inter-process communication under Windows, such as using socket, pipe (Pipe), mailbox (Mailslot), and so on. But the most basic and direct is to use memory sharing. Other methods will eventually detour here.

It is conceivable that if there is only one copy of physical memory, and this memory is mapped to their respective virtual address spaces in different processes, each process can read the same data, which is the most efficient data exchange. method. Below we discuss how to implement it.

Shared memory is implemented by FileMapping in Windows. We can use CreateFileMapping to create a memory file mapping object. CreateFileMapping API will create a kernel object for mapping files to memory. Here, we don't need an actual file, so there is no need to call CreateFile to create a file. The parameter of hFile can be filled in INVALID_HANDLE_VALUE. However, the file length needs to be filled. Windows supports files up to 64bit, but here, our demand must not exceed 4G, dwMaximumSizeHigh must be 0, and the length should be filled in dwMaximumSizeLow. Then call MapViewOfFile to map to the virtual address of the current process. Once the shared memory is used up, call UnmapViewOfFile to reclaim the memory address space.

It makes sense for Windows to separate the CreateFileMapping and MapViewOfFile APIs. This is because it is allowed to map a file exceeding 4G, and the address space is only 4G at most (in fact, the general user's program can only use 2G), MapViewOfFile can specify the offset of the file and only map a part.

Fill in a name in the last parameter pszName of CreateFileMapping, then other processes can use this name to call OpenFileMapping to open the FileMapping object and map it in the new process. However, the method of passing agreed strings seems not very elegant.

An elegant method is to use DuplicateHandle to copy a FileMapping object in the new process, and then find a way to notify the new process of the Handle, such as passing it in a message.

If the two processes that need to share memory have a parent-child relationship, then we can notify the Handle of FileMapping without message passing. The parent process can directly pass the Handle of FileMapping to the child process by inheriting Handle. Of course, properties that can be inherited should be set when CreateFileMapping.

It looks like this:

SECURITY_ATTRIBUTES sa;
sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=NULL;
sa.bInheritHandle=TRUE;
handle=CreateFileMapping(INVALID_HANDLE_VALUE,&sa,PAGE_READWRITE,0,size,NULL);

In this way, during CreateProcess, if the bInheritHandles parameter is TRUE, all kernel objects with inheritable properties will be copied to the child process.

Note: The inheritance of kernel objects is to create a child process in CreateProcess, but before the main thread of the child process is not active, the kernel scans all kernel objects in the current process, checks out those with inheritable attributes, and then uses DuplicateHandle to copy to the child process . Since it is a kernel object, there is essentially only one copy in the kernel, all is just the reference count plus one, the Handle of the same kernel object of the parent process and the child process must be the same.

The process of copying the kernel object is done internally by CreateProcess, and we can safely pass the object Handle (same as the child process) to the child process through the command line. Or, it can be passed with environment variables.

It is worth noting that after the child process runs out of this FileMapping object, it also needs CloseHandle minus the reference count.

Remarks:
When CreateProcess is called, pszCommandLine cannot be directly filled with an unmodifiable string. E.g:

CreateProcess("test.exe","test argument",...);

This is wrong, because the "test argument" will be compiled by the compiler into the unmodifiable data segment. The correct way is:

char cmdline[]="test argument";
CreateProcess("test.exe",cmdline,...);

In this way, the string of the command line is placed on the stack and can be read and written.

The penultimate parameter of CreateProcess needs to be filled in a STARTUPINFOW structure, which is very complicated and usually very troublesome to fill. We can copy the structure of the parent process and modify it as appropriate. the way is:

STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;
GetStartupInfo(&si);
CreateProcess(...,&si,&pi);

Here, the first length information of the STARTUPINFO structure should usually be filled in to ensure the correct execution of GetStartupInfo(&si);.
Reply

Use magic Report

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-12-18 15:15:01
| Show all posts
For large blocks of data, memory mapped files are better
Reply

Use magic Report

0

Threads

14

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-12-19 14:45:02
| Show all posts
Memory mapped file.
Reply

Use magic Report

0

Threads

14

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-12-19 15:15:01
| Show all posts
If you want to pass a string, it is recommended to use the WM_SETTEXT message directly, and Windows will copy the string for you.
Reply

Use magic Report

0

Threads

45

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-12-19 15:45:01
| Show all posts
If it is not a very complicated application, try WM_COPYDATA

Personally feel that memory mapping is relatively efficient and can handle complex applications
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list