Logical drive copying using CreateFile

G

Guest

Hi,

In the following code i am copying the contents of logical drive F to G
and then after that, from G to H.

After the program execution completes, the contents of the drives g and
h are not updated in windows explorer, even after pressing F5.

If I executed chkdsk on G and H drives the content sync up with that of F.
This problem is reproducable on NTFS but is not there on FAT32 partitions.

Please suggest the reason and a possible solution to fix this problem.

I have tried this program on 400 MB drives but the problem is reproducable
on any sizes.

Thanks in Anticipation.

---------------------------------------------------------------------------
#define _WIN32_WINNT 0x0500
#include<windows.h>
#include<iostream.h>
#include<conio.h>

void main()
{
char buffer[1024];
unsigned long bytesread;

HANDLE
h=INVALID_HANDLE_VALUE,h2=INVALID_HANDLE_VALUE,h3=INVALID_HANDLE_VALUE;
h = CreateFile(
"\\\\.\\f:", // file name
GENERIC_READ, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
NULL, // SD
OPEN_EXISTING, // how to create
0, // file attributes
NULL // handle to template file
);

if(h==INVALID_HANDLE_VALUE)
{
cout<<"first open failed"<<GetLastError()<<endl;
getch();
exit(1);
}
else
{
cout<<"first open successful"<<endl;
}
h2 = CreateFile(
"\\\\.\\g:", // file name
GENERIC_READ|GENERIC_WRITE, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
NULL, // SD
OPEN_EXISTING, // how to create
FILE_FLAG_WRITE_THROUGH, // file attributes
FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING
NULL // handle to template file
);

if(h2==INVALID_HANDLE_VALUE)
{
cout<<"second open failed"<<endl;
getch();
exit(1);
}
else
{
cout<<"second open successful"<<endl;
}

while(ReadFile(h,&buffer,sizeof(buffer),&bytesread,NULL) && bytesread)
WriteFile(h2,buffer,bytesread,&bytesread,NULL);

FlushFileBuffers(h2);

CloseHandle(h);
CloseHandle(h2);

h2=INVALID_HANDLE_VALUE;

cout<<"First write done"<<endl;

h2 = CreateFile(
"\\\\.\\g:", // file name
GENERIC_READ, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
NULL, // SD
OPEN_EXISTING, // how to create
0, // file attributes
FILE_FLAG_WRITE_THROUGH,
NULL // handle to template file
);

if(h2==INVALID_HANDLE_VALUE)
{
cout<<"second open failed"<<endl;
getch();
exit(1);
}
else
{
cout<<"second open successful"<<endl;
}

h3 = CreateFile(
"\\\\.\\h:", // file name
GENERIC_WRITE, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
NULL, // SD
OPEN_EXISTING, // how to create
FILE_FLAG_WRITE_THROUGH, // file attributes
FILE_FLAG_WRITE_THROUGH ,
NULL // handle to template file
);

if(h3==INVALID_HANDLE_VALUE)
{
cout<<"third open failed"<<endl;
getch();
exit(1);
}
else
{
cout<<"third open successful"<<endl;
}

while(ReadFile(h2,&buffer,sizeof(buffer),&bytesread,NULL) &&
bytesread)
WriteFile(h3,buffer,bytesread,&bytesread,NULL);

FlushFileBuffers(h3);

CloseHandle(h2);
CloseHandle(h3);
}

------------------------------------------------------------------------------
 
Joined
Jun 3, 2009
Messages
1
Reaction score
0
hiii


I'm new comer to this website...

This is the solution to your problem..Here I created 2 NTFS partitions mannually.(2 gb )size..Source is filled almost fully and destination is empty...And hare I atke the buffer as 50 mb ,because I want high sppedd.In your program i found it as 1024bytes.

Ok work it out and reply to me...


And my problem I want high speed....And Presently I'm copying all clusters secotrss...Not checking whether they are used or nott....

And somebody said that use "FSCTL_GET_VOLUME_BITMAP" that will returns the cluster usage details of a particular volumee...


Here I wanna get the used cluser only for copy process... If u know how to do that one please help mee...


And the source code for your problrm is here

vc++ win 32 mfc console based application (visual studio 2005)
Source code:


// sector_by_sector.cpp : Defines the entry point for the console application.
//



#include "stdafx.h"
#include "sector_by_sector.h"
#include


#ifdef _DEBUG
#define new DEBUG_NEW
#endif



CWinApp theApp;
using namespace std;


DWORD BytesReturned;
LONGLONG chunk_size=52428800; //26214400 52428800 =50 mb buffer
LONGLONG chunk_size1=52428800;//52428800;
LONGLONG volume_size=2147483648;//2147483648bytes =2gb partition

LONGLONG rest_size=volume_size;
int i=0;

LPVOID bMBR2 = VirtualAlloc(NULL,52428800,MEM_COMMIT,PAGE_READWRITE);
//buffer allocated (buffer size=50 MB)







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_MyDevice_src2,h_MyDevice_dest2;
BOOL bResult,bResult1,bResult2,bResult_unlock,bResult_unlock1;
DWORD dwRetBytes1,dwRetBytes2;




h_MyDevice_src2 = CreateFile( _T("\\\\.\\G:"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL/*FILE_FLAG_OVERLAPPED*/, NULL);
if (h_MyDevice_src2==INVALID_HANDLE_VALUE)
{
printf("\nUnable to Open the Device: Error Number is : %d\n",GetLastError());
}




h_MyDevice_dest2=CreateFile(/*L"\\Device\\HarddiskVolume11"*/ L"\\\\.\\K:",GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL/*|FILE_FLAG_OVERLAPPED*/, NULL);
if (h_MyDevice_dest2==INVALID_HANDLE_VALUE)
{
printf("\nUnable to Open the Device: Error Number is : %d\n",GetLastError());
}



//lock source
bResult = DeviceIoControl(h_MyDevice_src2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult)
{
printf("Lock Volume Failed %d \n",GetLastError());
}


//Lock desti
bResult1 = DeviceIoControl(h_MyDevice_dest2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
if(!bResult1)
{
printf("Lock Volume Failed %d \n",GetLastError());
}






while(rest_size>=0)
{
if(rest_size
{
LPVOID bMBR2 = VirtualAlloc(NULL,rest_size,MEM_COMMIT,PAGE_READWRITE);
chunk_size=rest_size;
goto label11;
}


label11:
if(!ReadFile(h_MyDevice_src2, bMBR2,52428800, &dwRetBytes1,0)) //50mb
{
printf("\nUnable to Read the Drive with Error Number is : %d\n",GetLastError());
}

if(!WriteFile(h_MyDevice_dest2,bMBR2,52428800,&dwRetBytes2,0))
{
printf("\nUnable to write the Drive with Error Number is : %d\n",GetLastError());
}

cout<<"\n \n"<< i++ <<" restored ";
rest_size=rest_size-chunk_size1;
}





//unlock source
bResult_unlock = DeviceIoControl(h_MyDevice_src2,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);

if(!bResult_unlock)
{
printf("UnLock Volume Failed %d \n",GetLastError());
}

//unlock desti
bResult_unlock1 = DeviceIoControl(h_MyDevice_dest2,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);

if(!bResult_unlock1)
{
printf("UnLock Volume Failed %d \n",GetLastError());
}



CloseHandle(h_MyDevice_src2); //close source
CloseHandle(h_MyDevice_dest2);



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

=============================
with regards Krish
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top