DLL共享内存

进程间共享内存
//1.共享数据段
class CTest{
public:
    int  m_1;
    char m_2[10];
};
#pragma data_seg("SharedData")
HHOOK g_Hook=NULL; //一定要初始化
CTest   g_Test;  //可以共享
CTest* g_Test1; //动态分配的内存不能共享
#pragma data_seg()
__declspec(allocate("SharedData"))
#pragma comment(linker,"/SECTION:SharedData,rws")

//2.内存映射文件
//2.1 创建内存映射文件,写入内存数据
HANDLE hFileMap = OpenFileMapping(NULL,0,"SEHOOK");
if( hFileMap == NULL )
 hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT,0,1000000,"SEHOOK");
LPVOID FileMapView = MapViewOfFile(hFileMap,FILE_MAP_WRITE,NULL,NULL,NULL);
CTest* test = new CTest;
test->m_1 = 1001;
memcpy(FileMapView,test,sizeof(CTest));
UnmapViewOfFile(FileMapView);
...
用完后
CloseHandle(hFileMap);

//2.2 访问内存映射文件,读入内存数据
FILE* f = fopen("Log.txt","a+");
HANDLE hFileMap = OpenFileMapping(FILE_MAP_READ,FALSE,"SEHOOK");
if( hFileMap ){
 LPVOID lpFileMapView = MapViewOfFile(hFileMap,FILE_MAP_READ,NULL,NULL,NULL);
 CTest test;
 memcpy(&test,lpFileMapView,sizeof(CTest));
 CloseHandle(hFileMap);
}


//2.3 注意事项
在应用程序间共享一个类的实例,其静态成员,虚拟成员不能共享。 

你可能感兴趣的:(File,null,Class,dll,hook,linker)