Click here to Skip to main content
       

C / C++ / MFC

 
GeneralRe: c++ win32, What's the best way to handle large fonts 120 dpi Pinmemberjkirkerx14-May-12 11:38 
QuestionVfw decompression - real sample code wanted PinmemberVaclav_Sal14-May-12 8:24 
QuestionDrawImage from 2 image buffer Pinmemberjohn563213-May-12 23:34 
AnswerRe: DrawImage from 2 image buffer PinmvpRichard MacCutchan14-May-12 0:17 
QuestionIPC using named pipes PinmemberForNow13-May-12 9:27 
GeneralRe: IPC using named pipes Pinmember«_Superman_»13-May-12 16:15 
AnswerRe: IPC using named pipes PinmemberThatsAlok13-May-12 20:06 
AnswerRe: IPC using named pipes PinmemberTopCoder2314-May-12 0:02 
Try this: Creating Named Shared Memory.
 
**First Process**
 
The first process creates the file mapping object by calling the `CreateFileMapping` function with `INVALID_HANDLE_VALUE` and a name for the object. By using the `PAGE_READWRITE` flag, the process has read/write permission to the memory through any file views that are created.
Then the process uses the file mapping object handle that `CreateFileMapping` returns in a call to `MapViewOfFile` to create a view of the file in the process address space. The `MapViewOfFile` function returns a pointer to the file view, `pBuf`. The process then uses the CopyMemory function to write a string to the view that can be accessed by other processes.
 

Process 1 code:
 
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <tchar.h>
    
    #define BUF_SIZE 256
    TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
    TCHAR szMsg[]=TEXT("Message from first process.");
    
    int _tmain()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;
    
       hMapFile = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    // use paging file
                     NULL,                    // default security
                     PAGE_READWRITE,          // read/write access
                     0,                       // maximum object size (high-order DWORD)
                     BUF_SIZE,                // maximum object size (low-order DWORD)
                     szName);                 // name of mapping object
    
       if (hMapFile == NULL)
       {
          _tprintf(TEXT("Could not create file mapping object (%d).\n"),
                 GetLastError());
          return 1;
       }
       pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                            FILE_MAP_ALL_ACCESS, // read/write permission
                            0,
                            0,
                            BUF_SIZE);
    
       if (pBuf == NULL)
       {
          _tprintf(TEXT("Could not map view of file (%d).\n"),
                 GetLastError());
    
           CloseHandle(hMapFile);
    
          return 1;
       }
    
    
       CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
        _getch();
    
       UnmapViewOfFile(pBuf);
    
       CloseHandle(hMapFile);
    
       return 0;
    }
 
**Second Process**
 
A second process can access the string written to the shared memory by the first process by calling the `OpenFileMapping` function specifying the same name for the mapping object as the first process. Then it can use the `MapViewOfFile` function to obtain a pointer to the file view, `pBuf`. The process can display this string as it would any other string. In this example, the message box displayed contains the message "Message from first process" that was written by the first process.
 

Process 2 code:
 
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <tchar.h>
    #pragma comment(lib, "user32.lib")
    
    #define BUF_SIZE 256
    TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
    
    int _tmain()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;
    
       hMapFile = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object
    
       if (hMapFile == NULL)
       {
          _tprintf(TEXT("Could not open file mapping object (%d).\n"),
                 GetLastError());
          return 1;
       }
    
       pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
                   FILE_MAP_ALL_ACCESS,  // read/write permission
                   0,
                   0,
                   BUF_SIZE);
    
       if (pBuf == NULL)
       {
          _tprintf(TEXT("Could not map view of file (%d).\n"),
                 GetLastError());
    
          CloseHandle(hMapFile);
    
          return 1;
       }
    
       MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
    
       UnmapViewOfFile(pBuf);
    
       CloseHandle(hMapFile);
    
       return 0;
    }
 

Source : [http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx]
Questionc++, CryptGetHashParam works in XP, Vista, but not Windows 7 Pinmemberjkirkerx13-May-12 8:49 
Answer[SOLVED] Re: c++, CryptGetHashParam works in XP, Vista, but not Windows 7 Pinmemberjkirkerx13-May-12 10:13 
Questionwhere should add the try-catch? Pingroupyu-jian13-May-12 5:55 
AnswerRe: where should add the try-catch? PinmvpRichard MacCutchan13-May-12 6:45 
GeneralRe: where should add the try-catch? Pingroupyu-jian13-May-12 18:39 
AnswerRe: where should add the try-catch? PinmemberThatsAlok13-May-12 20:08 
GeneralRe: where should add the try-catch? PinmvpRichard MacCutchan13-May-12 21:41 
AnswerRe: where should add the try-catch? PinmemberVivekuniq13-May-12 20:59 
GeneralRe: where should add the try-catch? Pingroupyu-jian13-May-12 22:24 
AnswerRe: where should add the try-catch? PinmemberAescleal14-May-12 3:15 
QuestionNeed Help with Audio Output in C/C++ PinmemberVeeshal Beotra13-May-12 4:12 
AnswerRe: Need Help with Audio Output in C/C++ PinmemberVaclav_Sal14-May-12 8:35 
QuestionChanging the background color of Text control or combo box when text got changed Pinmemberash mishra13-May-12 4:01 
AnswerRe: Changing the background color of Text control or combo box when text got changed Pinmember«_Superman_»13-May-12 4:12 
Question'Forward Class Declaration' Not Working PinmemberOverloaded_Name12-May-12 20:44 
QuestionRe: 'Forward Class Declaration' Not Working Pinmember«_Superman_»12-May-12 20:53 
AnswerRe: 'Forward Class Declaration' Not Working PinmemberOverloaded_Name12-May-12 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 24 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid