0

MSDN에서 fileMapping 함수를 사용하여 읽고 쓰는 방법을 보여주는 예제를 시도했습니다. MSDN에서 참조 용으로 여기 코드를 붙여 넣습니다. 링크는이 문장의 메시지 박스가 표시되는 경우 http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspxVisual C++에서 LPTSTR 변수를 매개 변수로 사용하여 메시지를 읽는 방법은 무엇입니까?

#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; 
} 

입니다 (NULL, pBuf, TEXT ("Process2"), MB_OK); 은 pBuf (LPCTSTR 변수)를 받아 들여 파일에 입력 된 내용을 인쇄합니다. pBuf가 가리키는 것을 검색하고 싶거나 누군가 messagebox가 값을 읽을 수있는 방법을 안내 할 수 있습니다. * pBuf를 사용해 보았지만 위치 정보를 제공합니다. 나는 단지 여기 붙어있다. 도와주세요.

+0

'cout << "Pbuf는"<< (LPCTSTR) pBuf << endl;은 무엇입니까? –

+0

나는 cout << "Pbuf is"<< (LPCSTR) pBuf << endl을 사용했다. 출력은 전체 숫자의 첫 번째 문자입니다. 예. 내가 630을 저장했다면 "Pbuf is 6"이고 152를 저장할 때 "Pbuf is 1"이라는 것을 보았습니다. –

답변

0
  1. 당신은 CreateFileMapping()없이 OpenFileMapping()을 사용하고 있습니다. 따라서 hMapFile은 null입니다.

  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; 
    } 
+0

안녕하세요, MSDN에 대한 링크를 넣었습니다. 파일에 쓰는 다른 프로그램을 만들었습니다. 위에 붙여진이 코드에서 pBuf 또는 LPCTSTR을 읽을 필요가 있습니다. 나는 첫 번째 글자를 읽을 수 있지만, 전체 숫자 –

+0

을 읽는 방법을 찾으려고 노력 중입니다. 그것은 LPCSTR이지 LPCTSTR이 아닙니다. 나는 뭔가를 놓치고있다. –