• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            若我的小家

            -編程,讀書,感悟,旅游,設計
            posts - 21, comments - 0, trackbacks - 0, articles - 0

            用DeviceIoControl獲取系統磁盤信息

            Posted on 2008-09-23 13:19 若我 閱讀(4771) 評論(0)  編輯 收藏 引用

            代碼:
            #include <iostream>
            #include <windows.h>
            using namespace std;

            int main()
            {
             SECURITY_ATTRIBUTES secStru;
             secStru.bInheritHandle=0;
             secStru.lpSecurityDescriptor=0;
             secStru.nLength=0; 
             HANDLE hDevice=CreateFile("\\\\.\\PhysicalDrive0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);

             if(hDevice==INVALID_HANDLE_VALUE)
             {
              return -1;
             }

             //DWORD outBuff[1000];
             GET_LENGTH_INFORMATION infoStruct;

             memset(&infoStruct,0,sizeof(infoStruct));
             DWORD bytesReturned;
             if(DeviceIoControl(hDevice,IOCTL_DISK_GET_LENGTH_INFO,NULL,0,&infoStruct,sizeof(infoStruct),&bytesReturned,NULL)==0)
             {
              cout<<"Failed to get disk information."<<endl;
              DWORD error;
              error=GetLastError();
              HRESULT hRe=HRESULT_FROM_WIN32(error);
              char errorData[10];
              sprintf(errorData,"%x",hRe);
              cout<<"Error code:"<</*hRe*/errorData<<endl;
              CloseHandle(hDevice);
              return -1;
             }

             DISK_GEOMETRY_EX geoStruct;
             memset(&geoStruct,0,sizeof(geoStruct));
             if(DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY_EX ,NULL,0,&geoStruct,sizeof(geoStruct),&bytesReturned,NULL)==0)
             {
              cout<<"Failed to get disk information."<<endl;
              DWORD error;
              error=GetLastError();
              HRESULT hRe=HRESULT_FROM_WIN32(error);
              char errorData[10];
              sprintf(errorData,"%x",hRe);
              cout<<"Error code:"<</*hRe*/errorData<<endl;
              CloseHandle(hDevice);
              return -1;
             }

             cout<<"The disk's size is:"<<infoStruct.Length.QuadPart/1024/1024/1024<<" G Bytes."<<endl;
             cout<<"The disk's cylinder number:"<<geoStruct.Geometry.Cylinders.QuadPart<<endl;
             cout<<"The disk's media type:"<<geoStruct.Geometry.MediaType<<endl;
             cout<<"Number of tracks per cylinder:"<<geoStruct.Geometry.TracksPerCylinder<<endl;
             cout<<"Number of sectors per track:"<<geoStruct.Geometry.SectorsPerTrack<<endl;
             cout<<"Number of bytes per sector:"<<geoStruct.Geometry.BytesPerSector<<endl;

             PDISK_PARTITION_INFO  partitionInfo=DiskGeometryGetPartition(&geoStruct);

             DRIVE_LAYOUT_INFORMATION_EX layOutInfo[20];

             memset(&layOutInfo,0,sizeof(DRIVE_LAYOUT_INFORMATION_EX)*20);

             //layOutInfo.PartitionEntry=*(new PARTITION_INFORMATION_EX[10]);

             if(DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_LAYOUT_EX,NULL,0,&layOutInfo,sizeof(DRIVE_LAYOUT_INFORMATION_EX)*20,&bytesReturned,NULL)==0)
             {
              cout<<"Failed to get disk information."<<endl;
              DWORD error;
              error=GetLastError();
              HRESULT hRe=HRESULT_FROM_WIN32(error);
              char errorData[10];
              sprintf(errorData,"%x",hRe);
              cout<<"Error code:"<</*hRe*/errorData<<endl;
              CloseHandle(hDevice);
              return -1;
             }

             int partitionCount=layOutInfo[0].PartitionCount;
             cout<<"Number of partitions:"<<layOutInfo[0].PartitionCount<<endl;
             cout<<"Partitions' information:"<<endl;
             for(int i=0;i<partitionCount;i++)
             {
              //PDISK_PARTITION_INFO pParInfo=partitionInfo+i*sizeof(DISK_PARTITION_INFO);
              if(layOutInfo[i].PartitionEntry[0].PartitionNumber!=0)
              {
               cout<<"Partition "<<layOutInfo[i].PartitionEntry[0].PartitionNumber<<",  partition size is "<<layOutInfo[i].PartitionEntry[0].PartitionLength.QuadPart/1024/1024/1024<<" G Bytes, partition style is "<<layOutInfo[i].PartitionEntry[0].PartitionStyle<<endl;
              }
             }

             //cout<<"The type of partition:"<<((partitionInfo.PartitionStyle==PARTITION_STYLE_MBR) ?"MBR":((partitionInfo.PartitionStyle==PARTITION_STYLE_GPT )?"GPT":((partitionInfo.PartitionStyle==PARTITION_STYLE_RAW)?"RAW":"")))<<endl;

             CloseHandle(hDevice);

             return 0;
            }

            色婷婷久久综合中文久久一本| 久久棈精品久久久久久噜噜| 日日狠狠久久偷偷色综合96蜜桃 | 国产99久久久久久免费看| 国产69精品久久久久99| 国产69精品久久久久9999APGF | 国产精品热久久毛片| 午夜精品久久久久| 精品国产热久久久福利| 亚洲精品乱码久久久久久蜜桃不卡| 狠狠狠色丁香婷婷综合久久俺| 久久丝袜精品中文字幕| 丁香狠狠色婷婷久久综合| 波多野结衣久久| 国产999精品久久久久久| 久久精品国产精品亚洲毛片| 久久久受www免费人成| 久久97精品久久久久久久不卡| 色狠狠久久综合网| 精品国产一区二区三区久久蜜臀| 久久精品无码专区免费青青 | 2021国内久久精品| 天天做夜夜做久久做狠狠| 91亚洲国产成人久久精品| 99久久久精品免费观看国产| 亚洲国产精品无码久久98| 久久精品国产2020| 久久久亚洲裙底偷窥综合| 国内精品免费久久影院| 久久国产成人| 久久亚洲中文字幕精品一区四| 国产高清国内精品福利99久久| 伊人久久综合热线大杳蕉下载| 蜜桃麻豆www久久| 午夜不卡888久久| 久久精品国产福利国产琪琪| 久久99精品久久久久久齐齐| 亚洲国产精品狼友中文久久久| 久久综合偷偷噜噜噜色| 97久久国产露脸精品国产| 久久亚洲AV成人无码电影|