Search This Blog

Showing posts with label Kernel. Show all posts
Showing posts with label Kernel. Show all posts

Tuesday, May 5, 2009

How to Enumerate all Logical Drives using Win32 VC++







Intro


You should have seen Logical drives in Windows. Most commonly they are called C: D: E: etc drives. There are different type of Logical drives- Hard disk drives, CD/DVD Drives, Removable Drive, RAM Disk etc..


So, is there a way to get all of these drives installed or present on a system programatically? the Answer is very yes! Because the Win32 API's helps you to enumerate every drives presently on a system, and also to determine which type of drive is it. Ie., Hard disk, CD/DVD Drive etc...


How to Do?


The Win32 API used to enumerate all drives present on a system is  GetLogicalDrives(). This function retrieves a bitmask representing the currently available disk drives. We need to process the BitMask returned by the API to convert it to actual Drive letters. Here i will show you with a code snippet, how to enumerate all drives using GetLogicalDrives() API and process the return value to convert BitMask to appropriate Driveletter (C:, D:. E: etc..).


Code Snippet





void GetDirs()
{
DWORD dwMask = 1; // Least significant bit is A: flag
DWORD dwDrives = GetLogicalDrives ();
char strDrive[4] = {'\0'};
char strDrivex[4] = {'\0'};
int  nresult, dir;

// 26 letters in [A..Z] range
for (int i=0; i < 26; i++ )

//Logically 'AND' the Bitmask with 0x1. we get zero, if its a drive
if (dwDrives & dwMask)  
{
  wsprintfA ((LPSTR)strDrive, "%c:"'A'+i);
  printf (" %s",strDrive ); //Print out the Drive Letter
  //Just Zero filling the buffer, to prevent overwriting or junks
  for(int j = 0; j < 4; j++)
  strDrive[j] = '\0';
}
// Shift one bit position to left
dwMask <<= 1; 
}
}


Now, we have got all Logical drives (Drive Letters) Printed on our console(You can use it to display on Comboobox or other controls too, as i'm printing to console to make ease of the tutorial). 


Then, is there any way to check the types of each drives? I mean whether C: is a Hard drive or CD/DVD Drive or Removable media disk etc??


Yes!.. here comes the  GetDriveType() API. This function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive. lol.. so, you've it.. then.., how to do it?


See this snippet of code, its pretty easy.. i dont think there is a need of high level commenting ;-)


Code

//The lpdrv is the driveletter.( eg : C: or D:,  E: etc )
void FindDriveType(LPSTR lpdrv) 
{
UINT drvtype;
char szMsg[150];
//The GetDriveType() return value specifies the type of drive.
drvtype = GetDriveType(lpdrv); 
//Switching the returnvalue to check drive types
switch (drvtype)
   {
     case DRIVE_UNKNOWN:
wsprintf( szMsg, "Drive %s is of unknown type", lpdrv );
     break;
     case DRIVE_NO_ROOT_DIR:
wsprintf( szMsg, "Drive %s is invalid", lpdrv );
     break;
     case DRIVE_REMOVABLE:
wsprintf( szMsg, "Drive %s is a removable drive", lpdrv );
     break;
     case DRIVE_FIXED:
wsprintf( szMsg, "Drive %s is a hard disk", lpdrv );
     break;
     case DRIVE_REMOTE:
wsprintf( szMsg, "Drive %s is a network drive", lpdrv );
     break;
     case DRIVE_CDROM:
wsprintf( szMsg, "Drive %s is a CD-ROM drive", lpdrv );
     break;
     case DRIVE_RAMDISK:
wsprintf( szMsg, "Drive %s is a RAM disk", lpdrv );
     break;
default:
wsprintf( szMsg, "Drive %s is an unknown type",lpdrv ); 
break;
  }
}


So, Howz it?? Pretty cool and easy?... you can use this in your application to list drives and to test/evaluate which type of drive is it.. :-)


PS : Dont forget to Post your valuable feedbacks :-)
















Monday, December 22, 2008

How to get the MAC address of your Network Adapter.

Description
MAC address stands for “Media Access Control“ address which is 6 bytes( 48 bits ) long. MAC address is the unique address which is used to identify network interface hardware. So how to get the MAC address of your network adapter?

How to Do?
You can use the function - GetAdaptersInfo(). The network adapter information will be populated and filled back in IP_ADAPTER_INFO structure. In that structure, you can get the network adapter name, MAC address and a couple of other information. See the sample code snippet.

Code:
#include "Iphlpapi.h"
...
// Get the buffer length required for IP_ADAPTER_INFO.
ULONG BufferLength = 0;
BYTE* pBuffer = 0;
if( ERROR_BUFFER_OVERFLOW == GetAdaptersInfo( 0, &BufferLength ))
{
// Now the BufferLength contain the required buffer length.
// Allocate necessary buffer.
pBuffer = new BYTE[ BufferLength ];
}
else
{
// Error occurred. handle it accordingly.
}
// Get the Adapter Information.
PIP_ADAPTER_INFO pAdapterInfo = reinterpret_cast(pBuffer);
GetAdaptersInfo( pAdapterInfo, &BufferLength );
// Iterate the network adapters and print their MAC address.
while( pAdapterInfo )
{
// Assuming pAdapterInfo->AddressLength is 6.
CString csMacAddress;
csMacAddress.Format(_T("%02x:%02x:%02x:%02x:%02x:%02x"),
pAdapterInfo->Address[0],
pAdapterInfo->Address[1],
pAdapterInfo->Address[2],
pAdapterInfo->Address[3],
pAdapterInfo->Address[4],
pAdapterInfo->Address[5]);
cout << "Adapter Name :" <<>AdapterName << " "<< "MAC :" << (LPCTSTR)csMacAddress << padapterinfo =" pAdapterInfo-">Next;
}
// deallocate the buffer.
delete[] pBuffer;


PS : Dont forget to add Iphlpapi.lib to project settings.