Jochen
Jochen

Reputation: 1776

Debug content of shared memory

I got two processes. In process A, application Alpha is prepared for using shared memory. In process B, my windows console application with a main method accesses this shared memory. It works, I can connect to the memory. Unfortunately if I store the memory content in a variable (here pBuf) and inspect the variable via MsVisualStudioDebugger, there is only one letter in the buffer - what went wrong? How am I able to look in the memory/file to get a clue, what objects are stored in the memory? Thanks in advance.

Here is the code of my console app:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
using namespace std;
#define BUF_SIZE 256 
TCHAR szName[] = TEXT("SIMITSHM"); // Name des SHMs;
int main(void){
    std::cout << "*** start SharedMemory  ***\n";
    HANDLE hMapFile;
    LPCTSTR pBuf;
    hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);              
    if (hMapFile == NULL){
        MessageBox(NULL, TEXT("Could not open file mapping object"), TEXT("FEHLER"), MB_OK);
        return 1;
    }
    pBuf = (LPTSTR) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    if (pBuf == NULL){
        MessageBox(NULL, TEXT("Could not map view of file"), TEXT("FEHLER"), MB_OK);
        CloseHandle(hMapFile);
        return 1;
    }
    MessageBox(NULL, pBuf, TEXT("SIMIT-SHM"), MB_OK); // content of memory
    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
    std::cout << "*** close app by typing a number. ***\n";
    int a = 0;
    cin >> a;
    return 0;
}

UPDATE: after doing more research on this issue, I guess the problem is the casting of the return value of MapViewOfFile()-function.

UPDATE-2: Thanks Hans! I was able to look the pBuf content as a hexdump, see: View Microsoft Visual Studio Arbeitsspeicher of pBuf

UPDATE-3: Thanks Hans! Due to your advice I was able to use fwrite() to put the shared-memory-content in a file on my local machine. Moreover I am able to see some familiar names, i.e. names like EingangMotor1 which I configured in application-Alpha and it seems this content was stored in shared memory. Now I hope to play around with application-Alpha, recognize the related changes in shared-memory and afterwards hopefully I will be able to change the values of the shared memory variable values, so application-Alpha will change its behaviour on the fly. write shared memory content to a file on local machine

UPDATE-4: current code - thanks in advance for feedback, which lines needs to be improved.

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <strsafe.h>
#include <fstream>
#include <sstream>
#include <vector>
#pragma comment(lib, "user32.lib")
using namespace std;
#define BUF_SIZE 256 
TCHAR szName[] = TEXT("SIMITSHM"); // Name des SHMs;
int main(void){
    std::cout << "*** Start SharedMemory  ***\n";
    HANDLE hMapFile;
    FILE * pBuf;
    hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);              
    if (hMapFile == NULL){
        MessageBox(NULL, TEXT("Could not open file mapping object"), TEXT("ERROR"), MB_OK);
        return 1;
    }
    // MapViewOfFile return a pointer to void, so you need to cast it to a suitable structure 
    pBuf = (FILE*) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    if (pBuf == NULL){
        MessageBox(NULL, TEXT("Could not map view of file"), TEXT("ERROR"), MB_OK);
        CloseHandle(hMapFile);
        return 1;
    }
    // write file 
    FILE * pFile;
    pFile = fopen ("shm-main-simit-content-fwrite.txt","w");
    fwrite(pBuf, 50, 20, pFile);
    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
    std::cout << "*** close app by typing a number. ***\n";
    int a = 0;
    cin >> a;
    return 0;
} 

Upvotes: 0

Views: 2201

Answers (1)

Hans Passant
Hans Passant

Reputation: 941307

Yes, you certainly have a type mismatch. That file is very unlikely to contain a properly zero-terminated C string. Using the TCHAR macros adds to the randomness, there is no point in using them anymore. The last non-Unicode version of Windows died 10 years ago.

Answering the question: use Debug + Windows + Memory + Memory 1 and put "pBuf" in the Address box. You'll see a raw hex dump of the shared memory content.

Upvotes: 2

Related Questions