1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
static void* operator new(size_t size,const string x,HANDLE*File,HANDLE*Map){
*File=CreateFile(x.c_str(),GENERIC_ALL,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(*File==INVALID_HANDLE_VALUE)throw INVALID_HANDLE_VALUE;
*Map=CreateFileMapping(*File,NULL,PAGE_EXECUTE_READWRITE,0,0,NULL);
if(*Map==NULL){
CloseHandle(*File);
throw (void*)NULL;
}
void*t=MapViewOfFile(*Map,FILE_MAP_ALL_ACCESS,0,0,size);
if(t==NULL){
CloseHandle(*Map);
CloseHandle(*File);
throw t;
}
return t;
}
void operator delete(void*View,HANDLE File,HANDLE Map){
UnmapViewOfFile(View);
CloseHandle(Map);
CloseHandle(File);
}
int main(int argc,char*argv[]){
HANDLE x;
HANDLE y;
char*a=new("test.txt",&x,&y)char;
cout<<a;
cin>>a;
cout<<a;
delete (a,&x,&y);
cin.ignore();
cin.ignore();
return 0;
}
|