0
1

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-04-13
動作環境
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でないといけないようだ。

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);
}
//---------------------------------------------------------------------------
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1