c++ builder / Windowsソフト間通信 > Named File Mapping > 書出し側コード

```txt:動作環境
C++ Builder XE4 on Windows 7 pro (32bit)
```

http://qiita.com/7of9/items/0166b705d73559386f67
にて見つけたcode exampleをC++ Builderで動かしてみた。

## v0.1

```Sender.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Sender.h"
#include <tchar.h>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

#define BUF_SIZE (256)
#if 1
TCHAR szName[] = TEXT("MyfileMappingObject");
#else
TCHAR szName[] = TEXT("Global\\MyfileMappingObject");
#endif
TCHAR szMsg[] = TEXT("Message from first process");

void __fastcall TForm2::Button1Click(TObject *Sender)
{
	HANDLE hMapFile;
	LPCTSTR pBuf;

	hMapFile = CreateFileMapping(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		BUF_SIZE,
		szName);

	if (hMapFile == NULL) {
		String msg = String().sprintf(L"Could not create file mapping object (%d)\n", GetLastError());
		ShowMessage(msg);
		return;
	}

	pBuf = (LPTSTR) MapViewOfFile(hMapFile,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		BUF_SIZE);

	if (pBuf == NULL) {
		String msg = String().sprintf(L"Could not map view of file (%d)\n", GetLastError());
		ShowMessage(msg);
		return;
	}

	CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));

	UnmapViewOfFile(pBuf);

	CloseHandle(hMapFile);
}
//---------------------------------------------------------------------------
```


szName[]の定義において`Global\\`をつけたものでは`Could not create file mapping object (5)`となった。

`#if 1`で定義した方にするとCloseHandle()まで実行された。

Global\\付きで実行する場合は administratorでないといけないようだ。

http://stackoverflow.com/questions/2647051/error-using-createfilemapping-c

> There is a note about this in the MSDN article you referenced above, saying that you must be an Administrator to create global shared memory objects in Windows Vista/7.

`Global\\`以外にも`Local\\`という定義もある
https://msdn.microsoft.com/ja-jp/library/cc430039.aspx

上記のコードを`Local\\`で実行するとエラーが出ずに最後まで実行された。


## v0.2

CloseHandle()を別のボタン処理とした。

```Sender.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Sender.h"
#include <tchar.h>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

#define BUF_SIZE (256)
#if 1
TCHAR szName[] = TEXT("GlobalMyfileMappingObject");
#else
TCHAR szName[] = TEXT("Global\\MyfileMappingObject");
#endif
TCHAR szMsg[] = TEXT("Message from first process");

HANDLE hMapFile;

void __fastcall TForm2::B_sendClick(TObject *Sender)
{
	LPCTSTR pBuf;

	hMapFile = CreateFileMapping(
		INVALID_HANDLE_VALUE,
		NULL,
		PAGE_READWRITE,
		0,
		BUF_SIZE,
		szName);

	if (hMapFile == NULL) {
		String msg = String().sprintf(L"Could not create file mapping object (%d)\n", GetLastError());
		ShowMessage(msg);
		return;
	}

	pBuf = (LPTSTR) MapViewOfFile(hMapFile,
		FILE_MAP_ALL_ACCESS,
		0,
		0,
		BUF_SIZE);

	if (pBuf == NULL) {
		String msg = String().sprintf(L"Could not map view of file (%d)\n", GetLastError());
		ShowMessage(msg);
		return;
	}

	CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));

	UnmapViewOfFile(pBuf);

}
//---------------------------------------------------------------------------
void __fastcall TForm2::B_closeClick(TObject *Sender)
{
	CloseHandle(hMapFile);
}
//---------------------------------------------------------------------------
```