Click here to Skip to main content
15,892,005 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to open a logical partition and read all of its file using windows function ReadFile() ? Pin
krish_kumar12-Jun-09 22:53
krish_kumar12-Jun-09 22:53 
QuestionRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
Randor 13-Jun-09 5:38
professional Randor 13-Jun-09 5:38 
AnswerRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
krish_kumar13-Jun-09 6:27
krish_kumar13-Jun-09 6:27 
GeneralRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
Stuart Dootson13-Jun-09 12:59
professionalStuart Dootson13-Jun-09 12:59 
GeneralRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
Randor 14-Jun-09 6:14
professional Randor 14-Jun-09 6:14 
GeneralRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
krish_kumar14-Jun-09 18:32
krish_kumar14-Jun-09 18:32 
GeneralRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
Randor 15-Jun-09 7:37
professional Randor 15-Jun-09 7:37 
GeneralRe: How to open a logical partition and read all of its file using windows function ReadFile() ? [modified] Pin
krish_kumar15-Jun-09 15:19
krish_kumar15-Jun-09 15:19 
#include "WinIoCtl.h"
#include "stdio.h"
#include "ntddvol.h"
#include "stdafx.h"
#include "Cluster_Used_Logical_to_Phy_Ofst.h" 


 

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 

 

CWinApp theApp;
using namespace std;

 

   
   DWORD BytesReturned,dwBytesReturnedh; 
   LARGE_INTEGER li_add;
   int i=0;
   UINT32  *BitmapDetail;
   DWORD dwError;
   LPDWORD lpBytesReturned;
   int iCopy_Count=0;
   LONGLONG llCountUsed=0;
   LONGLONG llCountFree=0;
   LONGLONG llTotalCount=0; 

 

   
   LONGLONG llChunk_Size=512; // chunk size
   LONGLONG Rest_Size=llVolume_Size;
   LONGLONG dwclusterSizeInBytes=512;
 

   LPVOID bBuffer1 = VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE);    

    BOOL IsClusterUsed (UINT64 Cluster)
    {
return ((BitmapDetail[Cluster / 32] & (1<< (Cluster % 32))) ? TRUE : FALSE);     
    }

 

 

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])

{

      int nRetCode = 0; 

      if(!AfxWinInit(::GetModuleHandle(NULL), NULL,::GetCommandLine(), 0))

      {    

      _tprintf(_T("Fatal Error: MFC initialization failed\n"));

      nRetCode = 1;

      }

      else

      {         

 

        HANDLE h_MyDrive_Src,h_MyDrive_Dest,hDevice;   

        DWORD dwRetBytes1,dwRetBytes2;  

 

 
       //Disk handle
       hDevice = CreateFile( _T("\\\\.\\PhysicalDrive0"),

                          GENERIC_READ | GENERIC_WRITE,

                          FILE_SHARE_READ | FILE_SHARE_WRITE,

                          NULL, OPEN_EXISTING,

                          FILE_ATTRIBUTE_NORMAL, NULL); 

       if (hDevice==INVALID_HANDLE_VALUE)
       {
       printf("\nUnable to Open the Device: Error Number is : %d\n",GetLastError());      
       }
 



 

     //Source partition  Drive handle...to get bitmap volume
        h_MyDrive_Src = CreateFile( L"\\\\.\\H:",
                                  GENERIC_READ|GENERIC_WRITE,
                                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                                  NULL,
                                  OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING, 
                                   NULL);                                
                                  

      if (h_MyDrive_Src==INVALID_HANDLE_VALUE)
      {
      printf("\nUnable to Open  Device: Error No: %d\n",GetLastError());  
      }      

      
   //Destination partition --  

   h_MyDrive_Dest=CreateFile( L"\\\\.\\I:",
                            GENERIC_WRITE,					                    FILE_SHARE_READ|FILE_SHARE_WRITE,
			    NULL,
			    OPEN_EXISTING,				                    FILE_ATTRIBUTE_NORMAL|FILE_FLAG_NO_BUFFERING,
			     NULL);

   if (h_MyDrive_Dest==INVALID_HANDLE_VALUE)
   {
   printf("\nUnable to Open  Device: Error Number:%d\n",GetLastError());
   }
       



   //Getting the  Bitmap of source Volume

    STARTING_LCN_INPUT_BUFFER StartingLCN;
    VOLUME_BITMAP_BUFFER *Bitmap = NULL;
    UINT32 BitmapSize;
    DWORD BytesReturned1;
    BOOL Result;
    ULONGLONG ClusterCount  = 0;

 

 

    StartingLCN.StartingLcn.QuadPart = 0;   
    BitmapSize = sizeof (VOLUME_BITMAP_BUFFER) + 4;
    Bitmap = (VOLUME_BITMAP_BUFFER *) malloc (BitmapSize); 

    Result = DeviceIoControl(h_MyDrive_Src,
		                 FSCTL_GET_VOLUME_BITMAP,
				     &StartingLCN,
				     sizeof (StartingLCN),
				     Bitmap,
				     BitmapSize,
				     &BytesReturned1,
				     NULL);

    if (Result == FALSE &&GetLastError()!= ERROR_MORE_DATA)
    {
    free (Bitmap);
    goto label12;
    }

                      

    ClusterCount = Bitmap->BitmapSize.QuadPart -
                   Bitmap->StartingLcn.QuadPart;  //TOT noof clusters

    cout<<"\n Tot no of clusters :"<<ClusterCount<<"\n";
    BitmapSize = sizeof (VOLUME_BITMAP_BUFFER) + (Bitmap->;BitmapSize.QuadPart / 8) + 1;

    Bitmap = (VOLUME_BITMAP_BUFFER *) realloc (Bitmap, BitmapSize);

 

    Result = DeviceIoControl(h_MyDrive_Src,
		                 FSCTL_GET_VOLUME_BITMAP,
				     &StartingLCN,
				     sizeof (StartingLCN),
				     Bitmap,
				     BitmapSize,
				     &BytesReturned,
				     NULL);   

    DWORD LastError = GetLastError ();

    if (Result == FALSE)

    {

    printf ("\nCouldn't properly read volume bitmap\n");

    free (Bitmap);

    goto label12;

    }

    BitmapDetail = (UINT32 *) malloc (sizeof(UINT32) * (1 + (ClusterCount / 32)));

    memcpy (BitmapDetail, Bitmap->Buffer, sizeof(UINT32) * (1 + (ClusterCount / 32)));

 

 

       UINT64 Max;

       UINT64 cc;

       Max=ClusterCount;

       UINT64 usedcluster=0; 

       LONGLONG llReturnValue = 0;
   
  /*    
  BOOL lbb = DeviceIoControl (h_MyDrive_Dest,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);

		if(!lbb)
		{
		printf("Lock Volume Failed %d \n",GetLastError());
		}
  */
 

 

    for (cc = 0; cc< Max; cc++)

    {
 /*
if(!ReadFile(h_MyDrive_Src,bBuffer1,llChunk_Size,&dwRetBytes1,0))
{
printf("\nUnable to Read the DriveError:%d\n",GetLastError));             

}
*/       

    

            if ( IsClusterUsed (cc)) // return 1 from buffer

            {   

             llCountUsed++;
             printf("\n used %I64d",llCountUsed);
             llTotalCount++;   

 

//----conversion starts here  ( Logical offset to Physical)

            

            LARGE_INTEGER li_Lcn;
            li_Lcn.QuadPart=(LONGLONG)cc;
            VOLUME_LOGICAL_OFFSET logicalOffset;
            logicalOffset.LogicalOffset= li_Lcn.QuadPart *dwclusterSizeInBytes;

 

            VOLUME_PHYSICAL_OFFSETS physicalOffsets;
            DWORD dw_Bytes_Returned;

 

            if (!DeviceIoControl(h_MyDrive_Src,

                                 IOCTL_VOLUME_LOGICAL_TO_PHYSICAL,

                                 &logicalOffset,

                                 sizeof(VOLUME_LOGICAL_OFFSET),

                                 &physicalOffsets,

                                 sizeof(VOLUME_PHYSICAL_OFFSETS),

                                 &dw_Bytes_Returned,

                                  NULL) )

 

   {
    printf("\n Error occoured :%d",GetLastError)); 
    goto label12;
   }

                  

   llReturnValue += physicalOffsets.PhysicalOffset [0].Offset;
   printf("\n physical offsets = %I64d",llReturnValue); 




 // Let's start to read &  write data
     
if(!SetFilePointer(hDevice,llReturnValue,NULL,0))  //hDevice :disk handle
{
printf("\nSetFilePointer Failed: Error Number : %d\n",GetLastError());
}

                  

if(!ReadFile(hDevice,bBuffer1,llChunk_Size,&dwRetBytes1,0))//hDevice :disk handle
{
printf("\nUnable to Read  ( src _ handle disk) the Drive Error: %d\n",GetLastError());          
}

 
 

if(!WriteFile(h_MyDrive_Dest,bBuffer1,llChunk_Size,&dwRetBytes2,0))/*to desti volume handle  */

{

printf("\nUnable towrite the Drive Error:%d\n",GetLastError());           

}
                                    
}
else
{   llCountFree++;
    llTotalCount++;
     // printf("\n free %I64d",llCountFree);

}
}

 

 

cout<<"\n Total no of clusters :"<<llTotalCount<<"\n";   
cout<<"\n Used no of clusters :"<<llCountUsed<<"\n";
cout<<"\n Free no of clusters :"<<llCountFree<<"\n";

 
 

/*
 BOOL lbbb = DeviceIoControl(h_MyDrive_Dest,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);

if(!lbbb)
{
printf("Lock Volume Failed %d \n",GetLastError());
}
*/

                  

 

CloseHandle(h_MyDrive_Src); //close source

CloseHandle(h_MyDrive_Dest);

 

BOOL bupdate= DeviceIoControl(hDevice,
		              IOCTL_DISK_UPDATE_PROPERTIES,
				 NULL,
				 NULL,
				 NULL,
				 NULL,				
                                 &dwBytesReturnedh,
				 NULL);

       CloseHandle(hDevice); 

label12:

       cout<<"\n Press any key to continue...........\n\n";
       char c=getchar();
      }

      return nRetCode;

}





















In the above code I have not touch MFT ,simply copied ; I think that is my problem.And you suggested that the logical offsets must represent the corresponding physical offsets; And I have converted the logical offset to physical.That physical offests returning are giving correct values.

Result

I:\ is not accessible
The file or directory is corrupted and unreadble.

modified on Tuesday, June 16, 2009 9:10 AM

GeneralRe: How to open a logical partition and read all of its file using windows function ReadFile() ? Pin
krish_kumar19-Jun-09 20:01
krish_kumar19-Jun-09 20:01 
Questionvirtual constructor Pin
siva45512-Jun-09 22:11
siva45512-Jun-09 22:11 
AnswerRe: virtual constructor Pin
Rajesh R Subramanian12-Jun-09 22:16
professionalRajesh R Subramanian12-Jun-09 22:16 
AnswerRe: virtual constructor Pin
Cedric Moonen12-Jun-09 23:09
Cedric Moonen12-Jun-09 23:09 
GeneralRe: virtual constructor Pin
Arun Singh K13-Jun-09 0:57
Arun Singh K13-Jun-09 0:57 
QuestionC++ dll for Excel -&gt; Is it possible to return strings with &gt; 255 characters in Excel? [modified] Pin
T210212-Jun-09 22:00
T210212-Jun-09 22:00 
AnswerRe: Possible to return strings with &gt;255 characters in Excel? Pin
Rajesh R Subramanian12-Jun-09 22:10
professionalRajesh R Subramanian12-Jun-09 22:10 
GeneralRe: Possible to return strings with &gt;255 characters in Excel? Pin
T210212-Jun-09 23:13
T210212-Jun-09 23:13 
GeneralRe: Possible to return strings with &gt;255 characters in Excel? Pin
Rajesh R Subramanian12-Jun-09 23:26
professionalRajesh R Subramanian12-Jun-09 23:26 
GeneralRe: Possible to return strings with &gt;255 characters in Excel? Pin
T210212-Jun-09 23:47
T210212-Jun-09 23:47 
GeneralRe: Possible to return strings with &gt;255 characters in Excel? Pin
Rajesh R Subramanian12-Jun-09 23:49
professionalRajesh R Subramanian12-Jun-09 23:49 
GeneralRe: Possible to return strings with &gt;255 characters in Excel? Pin
AlexesDad30-Jun-09 11:40
AlexesDad30-Jun-09 11:40 
QuestionErrors occurs while compiling Smart device MFC application using VS2005 Pin
saksp12-Jun-09 20:42
saksp12-Jun-09 20:42 
GeneralRe: Errors occurs while compiling Smart device MFC application using VS2005 Pin
Arun Singh K13-Jun-09 1:00
Arun Singh K13-Jun-09 1:00 
Questionweight and bias chage problem in neural network AND gate Pin
salahuddincse12-Jun-09 17:04
salahuddincse12-Jun-09 17:04 
QuestionI'm hoping some C# Guru out there can help me... (IndexOf and Multidimension Arrays) Pin
Paul Calabro12-Jun-09 14:48
Paul Calabro12-Jun-09 14:48 
AnswerRe: I'm hoping some C# Guru out there can help me... (IndexOf and Multidimension Arrays) Pin
Luc Pattyn12-Jun-09 14:58
sitebuilderLuc Pattyn12-Jun-09 14:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.