全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1451
推到 Plurk!
推到 Facebook!

FileMapping無法在兩個應用程式傳遞!

答題得分者是:taishyang
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-11-01 19:05:28 IP:140.128.xxx.xxx 訂閱
請問下面程式有什麼錯嗎?
發送程式A按下WriteButt"red">
#include
#pragma hdrstop

#include "Unit1.h"
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CreateButt NULL;

hFileMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "SakurabaAoi");

if (hFileMapping != NULL)
{
if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageDlg("Mapping file already created!", mtError, TMsgDlgButtons() << mbOK, 0);
CreateButton->Enabled = false;
::CloseHandle(hFileMapping);
}
else
{
char *P = static_cast (::MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0));

if (P != NULL)
{
std::memset(P, 0x00, 4096);
::UnmapViewOfFile(P);
CreateButton->Enabled = false;
CloseButton->Enabled = true;
}
}
}
else
{
MessageDlg("失敗", mtError, TMsgDlgButtons() << mbOK, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CloseButt true;
CloseButton->Enabled = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WriteButt ::OpenFileMapping(FILE_MAP_WRITE, FALSE, "SakurabaAoi");

if (hFileMap != NULL)
{
char* P = static_cast (::MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0));
/*P[0] = 100;
P[1] = 200;
P[2] = 300;
P[3] = 400; /*/

std::strcpy(P, Edit1->Text.c_str());
::UnmapViewOfFile(P);

}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (CloseButton->Enabled)
{
::CloseHandle(hFileMapping);
}
}
//---------------------------------------------------------------------------
[/code]


接收程式B
[code cpp]
#include
#pragma hdrstop

#include
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::BitBtn1Click(TObject *Sender)
{
HANDLE hFileMap = ::OpenFileMapping(FILE_MAP_WRITE, FALSE, "SakurabaAoi");

if (hFileMap != NULL)
{
char *P = static_cast (::MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0));
Memo1->Lines->Add(P);
::UnmapViewOfFile(P);
}
}
[/code]

------
-謝謝大大熱心的回覆!謝謝!
taishyang
站務副站長


發表:377
回覆:5486
積分:4552
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-11-01 20:13:54 IP:122.116.xxx.xxx 訂閱
TForm1 *Form1;
//---------------------------------------------------------------------------
: TForm(Owner)
}

{
hFileMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0, 4096, "SakurabaAoi");
if (::GetLastError() == ERROR_ALREADY_EXISTS){
::CloseHandle(hFileMapping);
mapData = (char *)(::MapViewOfFile(hFileMapping,
FILE_MAP_WRITE, 0, 0, 0));
std::memset(mapData, 0x00, 4096);
}
MessageDlg("失敗", mtError, TMsgDlgButtons() << mbOK, 0);
}

{
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
::UnmapViewOfFile(mapData);
//---------------------------------------------------------------------------
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
{
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
HANDLE hFileMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,
FALSE, "SakurabaAoi");
char *P = (char *) (::MapViewOfFile(hFileMap,
FILE_MAP_READ, 0, 0, 0));
::UnmapViewOfFile(P);
}
//---------------------------------------------------------------------------

編輯記錄
taishyang 重新編輯於 2010-11-01 06:15:19, 註解 無‧
l90425
初階會員


發表:95
回覆:152
積分:49
註冊:2008-04-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2010-11-01 20:28:32 IP:114.33.xxx.xxx 訂閱
可以了!

不過這是為什麼?
我還是不太懂,能否請大大解釋一下!

非常感謝!!謝謝!

===================引 用 taishyang 文 章===================
TForm1 *Form1;
//---------------------------------------------------------------------------
: TForm(Owner)
}

{
hFileMapping = ::CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0, 4096, "SakurabaAoi");
if (::GetLastError() == ERROR_ALREADY_EXISTS){
::CloseHandle(hFileMapping);
mapData = (char *)(::MapViewOfFile(hFileMapping,
FILE_MAP_WRITE, 0, 0, 0));
std::memset(mapData, 0x00, 4096);
}
MessageDlg("失敗", mtError, TMsgDlgButtons() << mbOK, 0);
}

{
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
::UnmapViewOfFile(mapData);
//---------------------------------------------------------------------------
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
{
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
HANDLE hFileMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,
FALSE, "SakurabaAoi");
char *P = (char *) (::MapViewOfFile(hFileMap,
FILE_MAP_READ, 0, 0, 0));
::UnmapViewOfFile(P);
}
//---------------------------------------------------------------------------

------
-謝謝大大熱心的回覆!謝謝!
系統時間:2017-12-18 7:28:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!