わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

IOCTL_DISK_GET_PARTITION_INFO_EX

パーティションの情報を取得します。

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <windows.h>
//#include <Mountmgr.h>	// --> WindowsDDKです。



int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコードを表示できるようにする
	setlocale( LC_ALL, "Japanese" );

	// "C:"は論理ドライブ名称です。
	HANDLE hDevice = CreateFile(L"¥¥¥¥.¥¥C:", 0,
                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                    NULL, OPEN_EXISTING, 0, NULL);

	if ( hDevice != INVALID_HANDLE_VALUE ) {

		/*
			IOCTL_MOUNTDEV_QUERY_DEVICE_NAME
				(DDKのMountmgr.hのインクルードが必要です。
		*/
		{
			PARTITION_INFORMATION_EX tPartitionInformationEx;

			DWORD dwResult = 0;

			/*
				IOCTL_DISK_GET_PARTITION_INFO_EX
			*/
			BOOL bResult = ::DeviceIoControl(
				(HANDLE) hDevice							// handle to device
			  , IOCTL_DISK_GET_PARTITION_INFO_EX			// dwIoControlCode
			  , NULL										// lpInBuffer
			  , 0											// nInBufferSize
			  , (LPVOID)&tPartitionInformationEx			// output buffer
			  , (DWORD)sizeof( tPartitionInformationEx )	// size of output buffer
			  , (LPDWORD)&dwResult							// number of bytes returned
			  , (LPOVERLAPPED)NULL							// OVERLAPPED structure
			);

			if ( 0 == bResult ) {

				// 取得失敗
				std::wcout << L"IOCTL_DISK_GET_PARTITION_INFO_EX失敗(" << ::GetLastError() << L")" << std::endl;
				::CloseHandle( hDevice );
				return( -1 );
			}


			/*
				結果を出力
			*/
			std::wcout << L"IOCTL_DISK_GET_PARTITION_INFO_EX" << std::endl;
			std::wcout << L"¥t" << L"PartitionNumber         : " << tPartitionInformationEx.PartitionNumber           << std::endl;
			std::wcout << L"¥t" << L"PartitionStyle          : " << tPartitionInformationEx.PartitionStyle            << std::endl;
			std::wcout << L"¥t" << L"StartingOffset          : " << tPartitionInformationEx.StartingOffset.QuadPart   << std::endl;
			std::wcout << L"¥t" << L"PartitionLength         : " << tPartitionInformationEx.PartitionLength.QuadPart  << std::endl;
			std::wcout << L"¥t" << L"RewritePartition        : " << tPartitionInformationEx.RewritePartition          << std::endl;
			switch( tPartitionInformationEx.PartitionStyle ) {
			case PARTITION_STYLE_MBR:
				{
					std::wcout << L"¥t" << L"Mbr.BootIndicator       : " << tPartitionInformationEx.Mbr.BootIndicator       << std::endl;
					std::wcout << L"¥t" << L"Mbr.HiddenSectors       : " << tPartitionInformationEx.Mbr.HiddenSectors       << std::endl;
					std::wcout << L"¥t" << L"Mbr.PartitionType       : " << tPartitionInformationEx.Mbr.PartitionType       << std::endl;
					std::wcout << L"¥t" << L"Mbr.RecognizedPartition : " << tPartitionInformationEx.Mbr.RecognizedPartition << std::endl;
				}
				break;

			case PARTITION_STYLE_GPT:
				{
					std::wcout << L"¥t" << L"Mbr.Attributes          : " << tPartitionInformationEx.Gpt.Attributes          << std::endl;
					std::wcout << L"¥t" << L"Mbr.Name                : " << tPartitionInformationEx.Gpt.Name                << std::endl;
				}
				break;

			case PARTITION_STYLE_RAW:
				{
				}
				break;

			}
		}
		::CloseHandle( hDevice );
	}

	// 正常終了
	return 0;
}

実行結果

IOCTL_DISK_GET_PARTITION_INFO_EX
        PartitionNumber         : 4
        PartitionStyle          : 0
        StartingOffset          : 168532377600
        PartitionLength         : 331574411264
        RewritePartition        : 0
        Mbr.BootIndicator       : 0
        Mbr.HiddenSectors       : 329164800
        Mbr.PartitionType       : 7
        Mbr.RecognizedPartition : 1






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。