标签:des style 使用 os 文件 数据 io for
分区格式:NTFS / FAT32
GetSystemDirectory();//获取系统路径if(hf) FindClose(hf);
示例代码:
// winlogic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<STDIO.H> #include<WINDOWS.H> void GetLogical() { UINT logic=GetLogicalDrives(); printf("%d\n",logic); } void GetPath() { char txt[200]={0}; GetSystemDirectory(txt,sizeof(txt)); printf("系统路径%s\n",txt); memset(txt,0,sizeof(txt)); GetWindowsDirectory(txt,sizeof(txt)); printf("windows路径%s\n",txt); memset(txt,0,sizeof(txt)); GetCurrentDirectory(sizeof(txt),txt); printf("程序工作路径%s\n",txt); SetCurrentDirectory("c:/"); memset(txt,0,sizeof(txt)); GetCurrentDirectory(sizeof(txt),txt); printf("更改后程序工作路径%s\n",txt); } void CreateWrite() { HANDLE hf=CreateFile("file.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); char txt[]="hello file to write"; DWORD len=0;//返回实际写入的长度 WriteFile(hf,txt,strlen(txt),&len,NULL); CloseHandle(hf); } void Read() { HANDLE hf=CreateFile("file.txt",GENERIC_READ,FILE_SHARE_READ, NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); CHAR txt[1000]={0}; DWORD len=0;//实际读到的长度 ReadFile(hf,txt,sizeof(txt),&len,NULL); printf("读取到的字符串:%s\n",txt); CloseHandle(hf); } void CreateDir() { CreateDirectory("abc/",NULL); } void Find() { char *path="c:/*.*"; WIN32_FIND_DATA wfd={0}; HANDLE hf=FindFirstFile(path,&wfd); while(1) { if(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { printf("目录:[%s]\n",wfd.cFileName); } else { printf("文件:%s\n",wfd.cFileName); } if(FindNextFile(hf,&wfd)==0) break; } if(hf) FindClose(hf); } int main(int argc, char* argv[]) { // GetLogical(); // GetPath(); //CreateWrite(); //Read(); //CreateDir(); Find(); char c=0; printf("按任意键结束!\n"); scanf("%c",&c); return 0; }
}
堆内存Heap
GetProcessHeap-获取程序默认第一个堆
GetProcessHeaps-获取程序所有默认堆
示例:
HANDLE h1=GetProcessHeap();
HANDLE hs[256]={0};
DWORD count=GetProcessHeaps(256,hs);
HeapCreate -创建堆
HeapAlloc -从堆分配内存
-把内存还给堆
HeapDestroy -销毁堆
示例:
HANDLE h= HeapCreate(HEAP_NO_SERIALIZE,1024*1024,0);
char* p= (char*) HeapAlloc(h,HEAP_ZERO_MEMORY,2*1024*1024);
strcpy(p,"123456");
printf("%s\n",p);
HeapFree(h,0,p);
HeapDestroy(h);
如果要使用,建议用:malloc/new 和 free/delete
内存映射文件的操作,速度快:
1、先创建一个硬盘文件 CreateFile()
2、创建内存映射文件
HANDLE CreateFileMapping( HANDLE hFile, // 硬盘文件的句柄
LPSECURITY_ATTRIBUTES lpAttributes, // 传0就行
DWORD flProtect, // protection
DWORD dwMaximumSizeHigh, // 高32位大小,小于4G传0
DWORD dwMaximumSizeLow, // 低32位大小
LPCTSTR lpName // 命名,可以为NULL
);
3、获取映射文件中某部分的地址
LPVOID MapViewOfFile(
HANDLE hFileMappingObject, // 映射文件句柄
DWORD dwDesiredAccess, // 访问权限
DWORD dwFileOffsetHigh, // 高32位大小,小于4G传0
DWORD dwFileOffsetLow, // 低32位大小
SIZE_T dwNumberOfBytesToMap // 映射的字节数量
);
dwFileOffsetHigh和dwFileOffsetLow 的合成必须是64K的整数倍。
4、断开地址和映射文件某部分
BOOL UnmapViewOfFile( LPCVOID lpBaseAddress//
);
5、关闭映射文件
CloseHandle(HANDLE)
6、关闭硬盘文件
CloseHandle(HANDLE);
示例:
HANDLE file=CreateFile("file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,
0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
HANDLE map=CreateFileMapping(file,0,PAGE_READWRITE,0,1024*1024,"pkm");
//
char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0);
strcpy(p,"adsadsad asd as d\n as d a\n s d as d a s aaaaa bbbbb ccccc\n");
printf("%s\n",p);
UnmapViewOfFile(p);
getchar();
CloseHandle(map);//关闭就没了,如果其他进程要使用,则不能关。
CloseHandle(file);//关闭了还在硬盘上
其他进程获取本进程已经创建好的映射文件,不能提前关闭映射文件
HANDLE OpenFileMapping( DWORD dwDesiredAccess, // access mode
BOOL bInheritHandle, // 返回值能否被子进程使用
LPCTSTR lpName // 映射名称
);
示例:
HANDLE map=OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"pkm");
char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0);
printf("%s\n",p);
UnmapViewOfFile(p);
示例代码:
// winVirtual.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<STDIO.H> #include <WINDOWS.H> void ShowMemStatus() { MEMORYSTATUS mm; mm.dwLength=sizeof(mm); GlobalMemoryStatus(&mm); printf("内存使用率:%d/100\n",mm.dwMemoryLoad); printf("TotalPhys:%u MB\n",mm.dwTotalPhys/1024/1024); printf("AvaliPhys:%u MB\n",mm.dwAvailPhys/1024/1024); printf("TotalVirtual:%u MB\n",mm.dwTotalPageFile/1024/1024); printf("AvaliVirtual:%u MB\n",mm.dwAvailPageFile/1024/1024); printf("TotalAddr:%u MB\n",mm.dwTotalVirtual/1024/1024); printf("AvaliAddr:%u MB\n",mm.dwAvailVirtual/1024/1024); } void Virtual_info() { ShowMemStatus(); char* p=(char*)VirtualAlloc(NULL,1024*1024*1024,MEM_COMMIT,PAGE_READWRITE); ShowMemStatus(); VirtualFree(p,0,MEM_RELEASE); ShowMemStatus(); } void Virtual_commit() { char* p=(char*) VirtualAlloc(NULL,4096,MEM_COMMIT,PAGE_READWRITE); strcpy(p,"HELLO MEMORY"); printf("%s\n",p); VirtualFree(p,0,MEM_RELEASE); } void Virtual_reserve() { //申请地址,不分配内存 char* p =(char*) VirtualAlloc(NULL,1024*1024,MEM_RESERVE,PAGE_READWRITE); char* p1=(char*)VirtualAlloc(p,4096,MEM_COMMIT,PAGE_READWRITE);//提交内存,以一个内存页4k为最小单位 strcpy(p,"HELLO MEMORY"); printf("%s\n",p); VirtualFree(p,0,MEM_RELEASE); } void HeapInfo() { HANDLE h1=GetProcessHeap(); printf("h1-%d\n",h1); HANDLE hs[256]={0}; DWORD count=GetProcessHeaps(256,hs); for(int i=0;i<count;i++) { printf("%d-%d\n",i,hs[i]); } } void MyHead() { HANDLE h= HeapCreate(HEAP_NO_SERIALIZE,1024*1024,0); char* p= (char*) HeapAlloc(h,HEAP_ZERO_MEMORY,2*1024*1024); strcpy(p,"123456"); printf("%s\n",p); HeapFree(h,0,p); HeapDestroy(h); } void FileMap() { HANDLE file=CreateFile("file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ, 0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); HANDLE map=CreateFileMapping(file,0,PAGE_READWRITE,0,1024*1024,"pkm"); // char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0); strcpy(p,"adsadsad asd as d\n as d a\n s d as d a s aaaaa bbbbb ccccc\n"); printf("%s\n",p); UnmapViewOfFile(p); getchar(); CloseHandle(map);//关闭就没了,如果其他进程要使用,则不能关。 CloseHandle(file);//关闭了还在硬盘上 } void ReadFileMap() { HANDLE map=OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"pkm"); char* p=(char*)MapViewOfFile(map,FILE_MAP_ALL_ACCESS,0,64*1024,0); printf("%s\n",p); UnmapViewOfFile(p); CloseHandle(map);//关闭就没了 } int main(int argc, char* argv[]) { //Virtual_info(); //Virtual_commit(); //Virtual_reserve(); //HeapInfo(); //MyHead(); FileMap(); //ReadFileMap(); return 0; }
c++windows内核编程笔记day12 硬盘逻辑分区管理、文件管理、内存管理,布布扣,bubuko.com
c++windows内核编程笔记day12 硬盘逻辑分区管理、文件管理、内存管理
标签:des style 使用 os 文件 数据 io for
原文地址:http://blog.csdn.net/pukuimin1226/article/details/38227843