0

fileMapping 関数を使用して読み書きする方法を示す MSDN の例を試しました。MSDN から参照できるように、ここにコードを貼り付けます。リンクは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;
}

このステートメントがある場合 MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK); これは pBuf (LPCTSTR 変数) を取り込み、ファイルに入力された内容を出力します。pBuf が指すものを取得したい、または誰かがメッセージボックスが値を読み取る方法を案内できます。*pBuf を使用してみましたが、いくつかの場所が表示されます。私はここで立ち往生しています。助けてください。

4

1 に答える 1