C++windows Kernel Programming note day12 hard disk logical partition management, file management, memory management

Source: Internet
Author: User
Tags readfile

Windows system disk File storage:

Partition format: Ntfs/fat32

GetSystemDirectory ();//Get system path
GetWindowsDirectory ();//Get Windows Path
GetCurrentDirectory ();//Get Current working folder
SetCurrentDirectory ();//Change the current working folder
Createfile/readfile/writefile/getfilesize/setfilepointer
Returns the lower 32 bits of the file size
DWORD getfilesize (HANDLE hfile,//HANDLE to File
Lpdword lpFileSizeHigh//Output parameters: Returns high 32 bits of file size
);
Create a file and write a text code demo sample
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;//Returns the length of the actual write
WriteFile (Hf,txt,strlen (TXT), &len,null);
CloseHandle (HF);
Example of a reading substitution code demo
HANDLE hf=createfile ("file.txt", Generic_read,file_share_read,
Null,open_always,file_attribute_normal,null);
CHAR txt[1000]={0};
DWORD len=0;//The actual length of the read
ReadFile (hf,txt,sizeof (TXT), &len,null);
printf ("string read:%s\n", TXT);
CloseHandle (HF);


day75 am over!
Create folder: CreateDirectory
Move folders (cannot move across partitions), move files (can cross partitions):
BOOL MoveFile (lpctstr lpexistingfilename,//file name
LPCTSTR lpnewfilename//New file name
);
Delete empty folder: RemoveDirectory
Copy file: CopyFile
Delete file: DeleteFile
Locate the file. Return lookup handle
HANDLE FindFirstFile (LPCTSTR lpfilename,//file name
Lpwin32_find_data lpfindfiledata//output parameter data buffer
);
Find the next
BOOL FindNextFile (HANDLE hfindfile,//Search HANDLE
Lpwin32_find_data lpfindfiledata//DATA buffer
);
Close the Find handle
BOOL FindClose (HANDLE hfindfile//File Search HANDLE
);
Using the Demo sample
Char *path= "C:/*.*";
Win32_find_data wfd={0};
HANDLE Hf=findfirstfile (PATH,&WFD);
while (1)
{
if (wfd.dwfileattributes&file_attribute_directory)
{
printf ("folder: [%s]\n", wfd.cfilename);
}
Else
{
printf ("File:%s\n", wfd.cfilename);
}
if (FindNextFile (HF,&WFD) ==0) break;
}

if (HF) FindClose (HF);

Demo Sample code:

Winlogic.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <stdio. H> #include <windows. H>void getlogical () {UINT logic=getlogicaldrives ();p rintf ("%d\n", logic); void GetPath () {char txt[200]={0}; GetSystemDirectory (txt,sizeof (TXT));p rintf ("System path%s\n", txt), memset (txt,0,sizeof (TXT)); GetWindowsDirectory (txt,sizeof (TXT));p rintf ("Windows path%s\n", txt), memset (txt,0,sizeof (TXT)); GetCurrentDirectory (sizeof (TXT), txt);p rintf ("program working path%s\n", TXT); SetCurrentDirectory ("c:/"); memset (txt,0,sizeof (TXT)); GetCurrentDirectory (sizeof (TXT), txt);p rintf ("Post-change program work path%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";D word len=0;//returns the actual write length 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};D the length that word len=0;//actually readReadFile (hf,txt,sizeof (TXT), &len,null);p rintf ("string read:%s\n", TXT); CloseHandle (HF);} void Createdir () {createdirectory ("abc/", NULL);} void Find () {char *path= "c:/*.*"; Win32_find_data wfd={0}; HANDLE Hf=findfirstfile (PATH,&AMP;WFD); while (1) {if (wfd.dwfileattributes&file_attribute_directory) {printf (" folder: [%s]\n ", wfd.cFileName);} else{printf ("File:%s\n", wfd.cFileName);} if (FindNextFile (HF,&AMP;WFD) ==0) break;} if (HF) FindClose (HF);} int main (int argc, char* argv[]) {//getlogical ();//getpath ();//createwrite ();//read ();//createdir (); Find (); Char c=0;printf ("Press the random key to end!

\ n "); scanf ("%c ", &c); return 0;}


Windows Memory: 1, Zone (for memory concepts):
is usually a multiple of 64K.
Spare: Not used
Private: Booked
Images: Storing code
Mapping: Storing data
2. Physical memory
The system can use the actual memory, the CPU can directly access the memory
3. Virtual memory
Virtual memory data must be placed into physical memory when the CPU assumes access to virtual memory data by virtualizing the hard disk files into intrinsic use (Pagafile.sys).


4. Memory page (for address concept)
The smallest unit of system management memory. The memory page size is 4K, and each memory page has its own permissions.
5. List of pages.


Pointer address (32-bit):
31~22 21~12 11~0
Page (1K) page table (1K) page offset address (4K)
6. The process of acquiring data from memory
1, first in the physical memory to find.
2, can not find in the virtual internal search.
3, if found in virtual memory, the physical memory and Virtual memory page data exchange.
4, the use of physical memory data;
5, the last possible physical memory data and virtual memory data exchange restore.
7. Memory allocation
7.1 Virtual memory allocation-suitable for large memory allocations, typically memory above 1M.
7.2 Heap memory allocation-suitable for small memory allocations, usually under 1M of memory.


7.3 Stack memory allocation-suitable for small memory allocations, typically 1M of memory below.
Virtual memory allocation function, return address after allocation succeeds
LPVOID VirtualAlloc (LPVOID lpaddress,//null or submit address
size_t dwsize,//allocation size
DWORD Flallocationtype,//allocation method
DWORD flprotect//Memory Interview method
);
Flallocationtype:
Return address and memory space after mem_commit allocation
Mem_reserve reserved address. Only return address after allocation, memory space is not generated
Flprotect: General Page_readwrite




To view memory usage
VOID GlobalMemoryStatus (Lpmemorystatus lpbuffer
);
typedef struct _MEMORYSTATUS {
DWORD dwlength;
DWORD dwMemoryLoad;
DWORD dwtotalphys;//Total Physical memory size
DWORD dwavailphys;//physical memory available size
DWORD dwtotalpagefile;//Total Virtual memory size
DWORD dwavailpagefile;//virtual memory available size
DWORD dwtotalvirtual;//Address Total
DWORD dwavailvirtual;//number of available addresses
} memorystatus, *lpmemorystatus;
BOOL VirtualFree (LPVOID lpaddress,//release address
size_t dwsize,//release size
DWORD dwFreeType//Release method
);
dwFreeType:
mem_decommit-just frees up memory
Mem_release-free memory and address
Sample Code Demo:
void Showmemstatus ()
{
Memorystatus mm;
Mm.dwlength=sizeof (mm);
GlobalMemoryStatus (&AMP;MM);
printf ("Memory Utilization:%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);//1g Memory and address
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 ()
{
Application address. Do not allocate memory
char* p = (char*) VirtualAlloc (null,1024*1024,mem_reserve,page_readwrite);
char* p1= (char*) VirtualAlloc (p,4096,mem_commit,page_readwrite);//commit memory to a memory page 4k for the smallest unit
strcpy (P, "HELLO MEMORY");
printf ("%s\n", p);
VirtualFree (p,0,mem_release);

}

Heap Memory Heap
Getprocessheap-gets the default first heap of the program
getprocessheaps-Get program All default heap
Demo Sample:
HANDLE h1=getprocessheap ();
HANDLE hs[256]={0};
DWORD count=getprocessheaps (256,HS);
HeapCreate-Create Heap
HeapAlloc-allocating memory from the heap
-Return the memory to the heap
HeapDestroy-Destroy Heap
Demo Sample:
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);
Suppose to be used, suggested by: Malloc/new and Free/delete


The operation of the memory-mapped file. Fast speed:
1, first create a hard disk file CreateFile ()
2. Create a memory-mapped file
HANDLE createfilemapping (HANDLE hfile,//Handle to hard disk file
Lpsecurity_attributes lpattributes,//Pass 0
DWORD Flprotect,//protection
DWORD Dwmaximumsizehigh,//high 32 bit size. Less than 4G pass 0
DWORD Dwmaximumsizelow,//Low 32-bit size
LPCTSTR lpname//name, can be null
);
3. Get the address of a part of the mapping file
LPVOID MapViewOfFile (
HANDLE Hfilemappingobject,//Map file handle
DWORD dwdesiredaccess,//access permissions
DWORD Dwfileoffsethigh,//high 32 bit size. Less than 4G pass 0
DWORD Dwfileoffsetlow,//Low 32-bit size
size_t Dwnumberofbytestomap//number of bytes mapped
);
The synthesis of dwfileoffsethigh and Dwfileoffsetlow must be an integer multiple of 64K.
4. Disconnect the address and mapping files in part
BOOL UnmapViewOfFile (lpcvoid lpbaseaddress//
);
5. Close the mapping file
CloseHandle (HANDLE)
6. Close hard disk files
CloseHandle (HANDLE);
Demo Sample:
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);//Shut down, assuming other processes are to be used, it cannot be closed.


CloseHandle (file);//closed still on the hard drive




Other processes get the mapped file that the process has created and cannot close the mapping file prematurely
HANDLE openfilemapping (DWORD dwdesiredaccess,//access mode
BOOL bInheritHandle,//return value can be used by quilt process
LPCTSTR lpname//Map name
);
Demo Sample:
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);


Demo Sample code:

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);p rintf ("Memory Utilization:%d/100\n", Mm.dwmemoryload);p rintf ("totalphys:%u mb\n", mm.dwtotalphys/ 1024/1024);p rintf ("avaliphys:%u mb\n", mm.dwavailphys/1024/1024);p rintf ("totalvirtual:%u mb\n", Mm.dwTotalPageFile /1024/1024);p rintf ("avalivirtual:%u mb\n", mm.dwavailpagefile/1024/1024);p rintf ("totaladdr:%u MB\n", mm.dwtotalvirtual/1024/1024);p rintf ("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 () {//request address, do not allocate memory char* p = (char*) VirtuAlalloc (null,1024*1024,mem_reserve,page_readwrite); char* p1= (char*) VirtualAlloc (P,4096,mem_commit,page_ READWRITE);//commit memory to a memory page 4k for the smallest unit strcpy (p, "HELLO memory");p rintf ("%s\n", p); VirtualFree (p,0,mem_release);} void Heapinfo () {HANDLE h1=getprocessheap ();p rintf ("h1-%d\n", H1); HANDLE hs[256]={0};D Word 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");p rintf ("%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");p rintf ("%s\n", p); UnmapViewOfFile (P); GetChar (); CloseHandle (map);//Shut it down. Assuming that other processes areUse. It cannot be closed.

CloseHandle (file);//closed still on hard disk}void Readfilemap () {HANDLE map=openfilemapping (file_map_all_access,false, "PKM"); char * p= (char*) mapviewoffile (map,file_map_all_access,0,64*1024,0);p rintf ("%s\n", p); UnmapViewOfFile (P); CloseHandle (map);//close}int main (int argc, char* argv[]) {//virtual_info ();//virtual_commit ();//virtual_reserve () ;//heapinfo ();//myhead (); Filemap (); Readfilemap (); return 0;}







C++windows Kernel Programming note day12 hard disk logical partition management, file management, memory management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.