Use shared memory communication windows api

主要涉及CreateFile,CreateFileMapping,GetLastError,MapViewOfFile,sprintf,OpenFileMapping,CreateProcess

CreateFile get files Handle

CreateFileMapping create Handle mapping

MapViewOfFile virtual address space in the process, showing the top part of the map (may be part of the largest mapping is complete, will not be large)

Create a process CreateProcess

reference:

sprintf Baidu Encyclopedia. MapOfview

Detailed CreateFile function (really detailed) - findumars - blog Park   CreateFileMapping

MapViewOfFile - Jane books     between Windows interprocess communication - shared memory-mapped files (FileMapping) - send and receive under VS2012 - Lala wind - blog Park

C ++ integer input data is read from a file - charlsonzhao's blog - CSDN blog

Create a process Reference: CreateProcess () function Detailed - the night sky's brightest star - CSDN blog  windows created under the process, CreateProcess () Detailed and usage - System Architect - CSDN blog  CreateProcess () function Detailed - the night sky's brightest star - CSDN blog  CreateProcess example - hisin Wang's column - CSDN blog

main content:

Win32 API programming using two communication using shared memory. Catalan producer process to generate sequence and write shared memory object. Consumers read and process the output sequence from the shared memory.

Catalan number generated by the number of command line specifies.

The number of Catalan nature:

 h (n) = h (n-1) * (4 * n-2) / (n + 1); h (n) = C (2n, n) / (n + 1) (n = 0,1,2, ...) h (n) = c (2n, n) c (2n, n-1) (n = 0,1,2, ...)

h(0)=1,h(1)=1  h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2)

Seeking simple: If the order f (2) = f (3) = 1, then f (n + 1) = f (n) * (4 * n-6) / n. But here 4 * n-6 / n will not divide.

May catalan [i] * (4 * j-6) / j

 

Mainly related to CreateFile, CreateFileMapping, GetLastError, MapViewOfFile, sprintf, OpenFileMapping, CreateProcess function.

The role of the function:

CreateFile get files Handle

CreateFileMapping create Handle mapping

MapViewOfFile virtual address space in the process, showing the top part of the map (may be part of the largest mapping is complete, will not be large)

CreateProcess creation process.

The role of parameters and functions:

HANDLE CreateFile (LPCTSTR lpFileName, // pointer to the file name of the DWORD dwDesiredAccess, // access mode (read / write) DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to point to the security attributes DWORD dwCreationDisposition, // how to create DWORD dwFlagsAndAttributes, // file attributes hANDLE hTemplateFile // used to copy a file handle);

HANDLE WINAPI CreateFileMapping(

  _In_     HANDLE                hFile,

  _In_opt_ LPSECURITY_ATTRIBUTES lpAttributes,

  _In_     DWORD                 flProtect,

  _In_     DWORD                 dwMaximumSizeHigh,

  _In_     DWORD                 dwMaximumSizeLow,

  _In_opt_ LPCTSTR               lpName

);

HANDLE CreateFileMapping(

  HANDLE hFile, // handle the physical file

  LPSECURITY_ATTRIBUTES lpAttributes, // Security Settings

  DWORD flProtect, // Protected Setup

  DWORD dwMaximumSizeHigh, // high file size (because our game is 32, so here is set to 0)

  DWORD dwMaximumSizeLow, // low file size

  LPCTSTR lpName // shared memory name

);

BOOL CreateProcess(

 LPCTSTR lpApplicationName, // application name

 LPTSTR lpCommandLine, // command line string

 Security attributes LPSECURITY_ATTRIBUTES lpProcessAttributes, // process

 Security attributes LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread

 BOOL bInheritHandles, // inherit the property of the parent process

 DWORD dwCreationFlags, // create a logo

 LPVOID lpEnvironment, // pointer to point to the new environment block

 LPCTSTR lpCurrentDirectory, // pointer to the current directory name

 LPSTARTUPINFO lpStartupInfo, // information passed to the new process

 LPPROCESS_INFORMATION lpProcessInformation // return information about the new process

);

 

Problems encountered:

1. CreateFileMapping error

Solve: GetLastError to get the error message, the parameters found to be wrong file size should not be resolved after adjustment 0. (0,0 is because if the book file already exists, it will map the real size)

2. How has encountered a problem press enter to view

solve:

for(int i=0;i<sizek;i++)

{

Int;

t=sprintf((char*)lpMapAddress+j,"%d\r\n",catalan[i]); j+=t; }

Question 3. How output

Solution: Direct output using printf% s

Reference is "Operating System Concepts, Seventh Edition" P301 example, wrote two programs. (Although I think the book is indeed a problem, it fucked me one hour)

The biggest problem encountered is the beginning of the middle forget to do error checking, resulting in the CreateFileMapping on the wrong, but I find silly question sprintf, and wasted an hour.

There is time to write the output of the program, wanted to show off a little point c ++ stuff, the results turned out to be wasting time. So programming aimed at solving problems, rather than the show itself.

There are profound experience of one thing: we must carefully analyze the function parameters, clear role. CreateFileMapping such as image size parameters specified in there, if set to 0 can cause problems (and the compiler not being given). (GetLastError 1006)

Source:

 


 

 


 

 


 

 


 

Renderings:

 


 

 


 

 

Guess you like

Origin www.cnblogs.com/lqerio/p/11117642.html