0

我尝试了一个来自 MSDN 的示例,该示例展示了如何使用 fileMapping 函数进行读写。我在此处粘贴代码供您从 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

0
  1. 您正在使用OpenFileMapping()没有CreateFileMapping(). 所以,hMapFile为空。

  2. 即使您使用CreateFileMapping(), 除非向 MapFile 写入内容,否则pBuf您的代码中始终为空。

下面的代码只是示例。我希望这会对你有所帮助。

    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;
    }
于 2014-07-17T04:56:41.190 回答