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를 사용해 보았지만 위치 정보를 제공합니다. 나는 단지 여기 붙어있다. 도와주세요.
'cout << "Pbuf는"<< (LPCTSTR) pBuf << endl;은 무엇입니까? –
나는 cout << "Pbuf is"<< (LPCSTR) pBuf << endl을 사용했다. 출력은 전체 숫자의 첫 번째 문자입니다. 예. 내가 630을 저장했다면 "Pbuf is 6"이고 152를 저장할 때 "Pbuf is 1"이라는 것을 보았습니다. –