• <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;
            }

            亚洲人成伊人成综合网久久久| 久久久久久精品无码人妻| 国产精品久久毛片完整版| 一本色道久久综合亚洲精品| 久久久久久亚洲Av无码精品专口| 久久精品人人做人人爽97| 亚洲精品国产成人99久久| 久久免费视频6| 日产精品久久久久久久性色| 99精品国产在热久久| 久久综合伊人77777麻豆| 亚洲中文久久精品无码| 亚洲一区中文字幕久久| 国产精品久久久久久久久久影院| 亚洲精品无码成人片久久| 精品国产青草久久久久福利| 久久久久亚洲AV成人网人人网站| 香蕉久久一区二区不卡无毒影院| 思思久久99热免费精品6| 2021少妇久久久久久久久久| 一97日本道伊人久久综合影院| 久久这里只有精品18| 国产精品中文久久久久久久| 国内精品久久久久久99| 亚洲欧美日韩精品久久亚洲区 | 久久久久国产精品嫩草影院| 国产精品久久国产精麻豆99网站| 久久婷婷午色综合夜啪| 国产综合成人久久大片91| 国产精品久久久久AV福利动漫| 亚洲AV伊人久久青青草原| 久久国产精品-国产精品| 漂亮人妻被黑人久久精品| 色偷偷88888欧美精品久久久| 久久久无码精品亚洲日韩软件| 久久―日本道色综合久久| 久久国产精品77777| 精品久久久久久中文字幕人妻最新| 波多野结衣久久精品| 久久综合亚洲色一区二区三区| 亚洲国产日韩欧美综合久久|