Logical drive copying using CreateFile

Archived from groups: microsoft.public.win2000.general (More info?)

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);
}

------------------------------------------------------------------------------
1 answer Last reply
More about logical drive copying createfile
  1. Archived from groups: microsoft.public.win2000.general (More info?)

    I recommend that you either use a single-line batch
    file for this task, or repost in a C++ newsgroup.


    "Software Engineer" <SoftwareEngineer@discussions.microsoft.com> wrote in
    message news:0EEAFFA9-612B-432D-A07A-EAD4DF9785F9@microsoft.com...
    > 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);
    > }
    >
    > --------------------------------------------------------------------------
    ----
Ask a new question

Read More

Windows