Hi ,
I am trying to move some cluster of a given file to other cluster(free) but i am getting access denied always
here is my code
bool MoveCluster()
{
HANDLE hVol;
STARTING_LCN_INPUT_BUFFER startingLcn;
VOLUME_BITMAP_BUFFER *volBitmap;
UINT32 bitmapSize;
DWORD bytesReturned;
LARGE_INTEGER startOfFreeSpace;
startOfFreeSpace.QuadPart = 0;
// Open the volume
hVol = CreateFile(L"\\.\C:",
GENERIC_READ | GENERIC_WRITE ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hVol == NULL)
{
cout << "can not open The volume ";
}
// Open a file
HANDLE hFile = ::CreateFile(L"C:\test.exe",
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hVol == NULL)
{
cout << "can not open The volume ";
}
startingLcn.StartingLcn.QuadPart = 0;
bitmapSize = sizeof(VOLUME_BITMAP_BUFFER) + 1024 * 1024 * 64;
//DWORD sizeofOutput = sizeof(VOLUME_BITMAP_BUFFER) + 1024 * 1024 * 64;
volBitmap = (VOLUME_BITMAP_BUFFER*)malloc(bitmapSize);
// Read the volume bitmap
BOOL result = DeviceIoControl(
hVol,
FSCTL_GET_VOLUME_BITMAP,
&startingLcn,
sizeof(STARTING_LCN_INPUT_BUFFER),
volBitmap,
bitmapSize,
&bytesReturned,
NULL);
if (!result)
{
cout<< GetLastError();
return true;
}
else
{
// Set a pointer to the start of the bitmap
BYTE* dataPtr = (BYTE*)volBitmap + sizeof(VOLUME_BITMAP_BUFFER);
LARGE_INTEGER i;
i.QuadPart = 0;
LARGE_INTEGER freeCount;
freeCount.QuadPart = 0;
// Walk the list and count the free clusters
for (; i.QuadPart < volBitmap->BitmapSize.QuadPart; ++(i.QuadPart))
{
// Check if the clust is allocated
if (!IsClusterUsed(dataPtr, i))
{
if (freeCount.QuadPart == 0)
{
// Note the start of free space
startOfFreeSpace = i;
// printf(" Start of Free Space %llu \n", startOfFreeSpace);
}
// Note the number of free clusters
++(freeCount.QuadPart);
if (freeCount.QuadPart > 20)
{
// If we have enough free clusters then stop the search
break;
}
}
else
{
// Reset the search
freeCount.QuadPart = 0;
}
}
// return true;
}
MOVE_FILE_DATA inBuffer;
//inBuffer = (PMOVE_FILE_DATA)malloc(sizeof(MOVE_FILE_DATA));
inBuffer.ClusterCount = 100; // Move all the clusters
inBuffer.FileHandle = hFile; // Set the file handle
inBuffer.StartingLcn = startOfFreeSpace;//Move to the free space we calculated above
inBuffer.StartingVcn.QuadPart = 0; // Move from the start of the file
// char inBuffer1 = { “dsdasd” };
DWORD size = 0;
DWORD inBufferSize = sizeof(inBuffer);
// Move the file
result = DeviceIoControl(
hVol,
FSCTL_MOVE_FILE,
&inBuffer,
sizeof(MOVE_FILE_DATA),
NULL,
0,
&size,
0);
if (!result)
{
//here i am always getting
cout<< GetLastError();
}
return true;
}
i have also tried using
https://blogs.msdn.microsoft.com/jeffrey_wall/2004/09/13/defrag-api-c-wrappers/
but this also results in access violation
i tried changing access flag file opening volume but still issue persist
Any suggestion or any thing wrong in code?