Click here to Skip to main content
11,578,006 members (64,741 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: c/c++
Hi Everyone,

Can anyone helps me to write a code in C/C++ - Language to print number of harddisk partitions and there names??

Pavit
Posted 11-Feb-13 2:17am
pavit1434

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

FROM: http://msdn.microsoft.com/en-us/library/cc542456%28VS.85%29.aspx

SOURCE:
----------------------------------------------------------------------------------------
#include
#include

void DisplayVolumePaths(
__in PWCHAR VolumeName
)
{
DWORD CharCount = MAX_PATH + 1;
PWCHAR Names = NULL;
PWCHAR NameIdx = NULL;
BOOL Success = FALSE;

for (;;)
{
//
// Allocate a buffer to hold the paths.
Names = (PWCHAR) new BYTE [CharCount * sizeof(WCHAR)];

if ( !Names )
{
//
// If memory can't be allocated, return.
return;
}

//
// Obtain all of the paths
// for this volume.
Success = GetVolumePathNamesForVolumeNameW(
VolumeName, Names, CharCount, &CharCount
);

if ( Success )
{
break;
}

if ( GetLastError() != ERROR_MORE_DATA )
{
break;
}

//
// Try again with the
// new suggested size.
delete [] Names;
Names = NULL;
}

if ( Success )
{
//
// Display the various paths.
for ( NameIdx = Names;
NameIdx[0] != L'\0';
NameIdx += wcslen(NameIdx) + 1 )
{
wprintf(L" %s", NameIdx);
}
wprintf(L"\n");
}

if ( Names != NULL )
{
delete [] Names;
Names = NULL;
}

return;
}

void __cdecl wmain(void)
{
DWORD CharCount = 0;
WCHAR DeviceName[MAX_PATH] = L"";
DWORD Error = ERROR_SUCCESS;
HANDLE FindHandle = INVALID_HANDLE_VALUE;
BOOL Found = FALSE;
size_t Index = 0;
BOOL Success = FALSE;
WCHAR VolumeName[MAX_PATH] = L"";

//
// Enumerate all volumes in the system.
FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));

if (FindHandle == INVALID_HANDLE_VALUE)
{
Error = GetLastError();
wprintf(L"FindFirstVolumeW failed with error code %d\n", Error);
return;
}

for (;;)
{
//
// Skip the \\?\ prefix and remove the trailing backslash.
Index = wcslen(VolumeName) - 1;

if (VolumeName[0] != L'\\' ||
VolumeName[1] != L'\\' ||
VolumeName[2] != L'?' ||
VolumeName[3] != L'\\' ||
VolumeName[Index] != L'\\')
{
Error = ERROR_BAD_PATHNAME;
wprintf(L"FindFirstVolumeW/FindNextVolumeW returned a bad path: %s\n", VolumeName);
break;
}

//
// QueryDosDeviceW does not allow a trailing backslash,
// so temporarily remove it.
VolumeName[Index] = L'\0';

CharCount = QueryDosDeviceW(&VolumeName[4], DeviceName, ARRAYSIZE(DeviceName));

VolumeName[Index] = L'\\';

if ( CharCount == 0 )
{
Error = GetLastError();
wprintf(L"QueryDosDeviceW failed with error code %d\n", Error);
break;
}

wprintf(L"\nFound a device:\n %s", DeviceName);
wprintf(L"\nVolume name: %s", VolumeName);
wprintf(L"\nPaths:");
DisplayVolumePaths(VolumeName);

//
// Move on to the next volume.
Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName));

if ( !Success )
{
Error = GetLastError();

if (Error != ERROR_NO_MORE_FILES)
{
wprintf(L"FindNextVolumeW failed with error code %d\n", Error);
break;
}

//
// Finished iterating
// through all the volumes.
Error = ERROR_SUCCESS;
break;
}
}

FindVolumeClose(FindHandle);
FindHandle = INVALID_HANDLE_VALUE;

return;
}
----------------------------------------------------------------------------------------

OUTPUT:

Found a device:
 \Device\HarddiskVolume2
Volume name: \\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}\
Paths:  C:\
 
Found a device:
 \Device\CdRom0
Volume name: \\?\Volume{4c1b02c4-d990-11dc-99ae-806e6f6e6963}\
Paths:  D:\
  Permalink  
Comments
Richard MacCutchan at 11-Feb-13 8:55am
   
Please format your code properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Richard MacCutchan 165
1 Peter Leow 150
2 OriginalGriff 120
3 Afzaal Ahmad Zeeshan 100
4 Frankie-C 85
0 OriginalGriff 880
1 Sergey Alexandrovich Kryukov 685
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 393


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100