Data instances are Shared between C++ processes

  • 2020-04-02 02:51:49
  • OfStack

In this paper, the example of C++ process to share data between the implementation of the method to share for your reference. The specific implementation method is as follows:

int main(int argc, char *argv[])  

    //RecursiveDelete("C:\20_128\"); 
    //SelfRun("runModel"); 
    //Memory sharing between processes & NBSP; < br / >     LPCTSTR lpName= "hello"; 
    LPCTSTR lpContent = "123456"; 
    HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, lpName); 
    if (NULL == hMap) //Failure to open & NBSP; < br / >     { 
        //Create & have spent < br / >         hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, strlen(lpContent), lpName); 
        if (hMap == NULL) 
        { 
            return -1; 
        } 
        //Write & have spent < br / >         LPVOID lpBase = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
        strcpy((char*)lpBase, lpContent); 
        printf("write %sn", lpContent); 
    } 
    else 
    { 
        //Open successfully just read & NBSP; < br / >         LPVOID lpBase = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0); 
        if (lpBase == NULL) 
        { 
            return -1; 
        } 
        printf("read %sn",(char*)lpBase); 
    } 
     
    getchar(); 
    return 0; 
}

Hope that the article described in the C++ programming to help you.


Related articles: