ACE中ACE_MMAP_Memory_Pool、ACE_Lite_MMAP_Memory_Pool、ACE_Pagefile_Memory_Pool都封装了操作系统的内存映射文件的功能.因此可以实现进程间的通信。闲来无聊,练练自己的笨手...
进程1代码示例(写入值):
//
定义内存映射的名称,以供进程间通信
ACE_TCHAR
*
pszPoolName
=
ACE_TEXT(
"
SelfPoolName
"
);
//
指定ACE_PI_Control_Block参数,表示分配的内存与位置无关
typedef ACE_Malloc_T
<
ACE_MMAP_Memory_Pool, ACE_Process_Mutex, ACE_PI_Control_Block
>
SharedMalloc;
SharedMalloc myMalloc( pszPoolName );
ACE_TCHAR szName[]
=
ACE_TEXT(
"
fangkm
"
);
ACE_TCHAR szSchool[]
=
ACE_TEXT(
"
whut
"
);
//
分配内存
ACE_TCHAR
*
pszName
=
(ACE_TCHAR
*
)myMalloc.malloc(
sizeof
( szName ) );
ACE_OS::strcpy( pszName, szName );
//
为该内存地址关联一个名称
if
(
-
1
==
myMalloc.bind(
"
Name
"
, pszName ) )
{
AtlMessageBox( NULL, _T(
"
failed
"
) );
}
ACE_TCHAR
*
pszSchool
=
(ACE_TCHAR
*
)myMalloc.malloc(
sizeof
( szSchool ) );
ACE_OS::strcpy( pszSchool, szSchool );
if
(
-
1
==
myMalloc.bind(
"
School
"
, pszSchool ) )
{
AtlMessageBox( NULL, _T(
"
failed
"
) );
}
ACE_OS::sleep(
10
);
//
释放对象相关的资源
myMalloc.remove();
进程2代码示例(读取值):
ACE_TCHAR
*
pszPoolName
=
ACE_TEXT(
"
SelfPoolName
"
);
typedef ACE_Malloc_T
<
ACE_MMAP_Memory_Pool, ACE_Process_Mutex, ACE_PI_Control_Block
>
SharedMalloc;
SharedMalloc myMalloc( pszPoolName );
ACE_TCHAR
*
pszName
=
NULL;
ACE_TCHAR
*
pszSchool
=
NULL;
//
查找Name关联的内存值
if
( myMalloc.find(
"
Name
"
, (
void
*&
)pszName )
!=
-
1
)
{
AtlMessageBox( NULL, _U_STRINGorID(CA2T(pszName)) );
}
if
( myMalloc.find(
"
School
"
, (
void
*&
)pszSchool )
!=
-
1
)
{
AtlMessageBox( NULL, _U_STRINGorID(CA2T(pszSchool)) );
}