0

I tried an example from MSDN that shows how to read and write using fileMapping functions. I am pasting the code here for you reference from MSDN. The link is http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx

#include"stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include<iostream>
#pragma comment(lib, "user32.lib")
using namespace std;
#define BUF_SIZE 256
TCHAR szName[]=TEXT("/Global/MyFileMappingObject");

int _tmain()
{
   HANDLE hMapFile;
   LPTSTR 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\n"),
             GetLastError());
        //cout<<"Could not create file mapping object"<<endl;
       _getche();

      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());
       //cout<<"Could not map view of file"<<endl;
       _getche();
      CloseHandle(hMapFile);

      return 1;
   }
   //_tprintf(Text("Message from process 1 is %s",&pBuf));

   //Convert LPTSTR to char
   cout<<"Pbuf is "<<*pBuf<<endl;
   size_t size = wcstombs(NULL,pBuf,0);
   const wchar_t* charStr = new wchar_t[size+1];
   //wcstombs(pBuf,charStr,size+1);

   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);

   _getche();
   return 0;
}

if you see there is this statement MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK); that takes in pBuf (LPCTSTR variable) and prints what was entered in the file. I want to retrieve what pBuf points to or can somebody guide how messagebox can read the value. I tried using *pBuf but it gives me some location. I am just stuck here. Please help.

1 Answers1

0
  1. You are using OpenFileMapping() without CreateFileMapping(). So, hMapFile is null.

  2. Even if, you use CreateFileMapping(), unless writing something to MapFile, your pBuf is always empty in your code.

Below code is just sample. I hope this will help you a little.

    int main(int argc, char *argv[])
    {
        HANDLE hFile, hMap;
        char *data;
        DWORD written_size;

        //create text file for test
        hFile = CreateFile(L"test.txt",GENERIC_READ|GENERIC_WRITE, 0, 
            NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        //and write "123" on file
        WriteFile(hFile, "123", 3,  &written_size, NULL);

        //create file map
        hMap = CreateFileMapping(
            hFile,                          //file handle
            NULL,
            PAGE_READWRITE,
            0,                           //file size
            0,                           //file size
            NULL);                    //map name
        if(hMap == NULL)
        {
            cout << "CreateFileMapping() fail";
            CloseHandle(hFile);
            return 1;
        }

        //file link to map
        data = (char *)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);

        //MessageBox prints "123"
        //Notice that using 'MessageBoxA' for output 'char *'.
        MessageBoxA(NULL, data, "Process2", MB_OK);

        UnmapViewOfFile(data);
        CloseHandle(hMap);
        CloseHandle(hFile);

        return 0;
    }
hyun
  • 2,135
  • 2
  • 18
  • 20
  • Hi, I have put a link about MSDN. I have created another program that writes to the file. In this code pasted above I need to read from pBuf or the LPCTSTR. I am able to read the 1st letter but am trying to find how do I read the whole number – Ravee S Gohiel Jul 17 '14 at 05:31
  • it is LPCSTR and not LPCTSTR. I am missing something. – Ravee S Gohiel Jul 17 '14 at 05:45