问题描述:
第余念迹一次接触这个,手边又没相关资料.有没有热心的高手给详细的源码? 感激不尽!
解析:
CPEHelper::CPEHelper(PCSTR lpFileName)
{
hFile = INVALID_HANDLE_VALUE
hFile = CreateFileA( lpFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
}
CPEHelper::CPEHelper(PCWSTR lpFileName)
{
hFile = INVALID_HANDLE_VALUE
hFile = CreateFileW( lpFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
}
bool CPEHelper::operator !()
{
if (hFile == INVALID_HANDLE_VALUE)
return true
return false
}
CPEHelper::~CPEHelper(void)
{
if(hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile)
}
bool CPEHelper::MappingFile()
{
hMapFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0,NULL)
if (hMapFile == NULL)
return false
lpMapAddress = MapViewOfFile(hMapFile,FILE_MAP_READ,0,0,0)
if (lpMapAddress == NULL)
return false
return true
}
void CPEHelper::UnMappingFile()
{
UnmapViewOfFile(lpMapAddress)
CloseHandle(hMapFile)
}
这高孝是我的一个程序中的一段
希望对你有帮助
#include <stdio.h>#include <malloc.h>
#include <string.h>
char *memory, *currentPointer
void ShareMemory( char function, char *a )
{
switch (function)
{
case 'c':
memory = ( char * )malloc( 65 * sizeof( char ) )
memory[64] = 0
currentPointer = memory
break
case 'r':
printf("%s", memory)
printf("\n")
break
case 'w':
strcpy( currentPointer, a )
while( *currentPointer != '\0' )
{
currentPointer++
}
break
case 'd':
free(memory)
break
default:
printf("悔衫wrong parameter!")
}
}
int main()
{
char *a = "aaaaa"
ShareMemory('碧昌腔c', a)
ShareMemory('r'迅森, a)
ShareMemory('w', a)
a = "bbbbb"
ShareMemory('w', a)
ShareMemory('r', a)
ShareMemory('d', a)
return 0
}
共享内存竖和用于进程之间通信,如果是多线程,用全局变量+信号量互斥就可以了呀?非要用的话,一个进程用shmget创建共享内存区,然后用shmat连接共享内存进行访问
注意要好郑配合信号量互斥,共享内存本身是没有同步机制的友纤颂。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)