Driver Problems? Questions? Issues?
Put OSR's experience to work for you! Contact us for assistance with:
  • Creating the right design for your requirements
  • Reviewing your existing driver code
  • Analyzing driver reliability/performance issues
  • Custom training mixed with consulting and focused directly on your specific areas of interest/concern.
Check us out. OSR, the Windows driver experts.

Upcoming OSR Seminars:

Writing WDF Drivers I: Core Concepts, Nashua, NH 15-19 May, 2017
Writing WDF Drivers II: Advanced Implementation Tech., Nashua, NH 23-26 May, 2017
Kernel Debugging and Crash Analysis, Dulles, VA 26-30 June, 2017
Windows Internals & Software Driver Development, Nashua, NH 24-28 July, 2017


Go Back   OSR Online Lists > ntfsd
Welcome, Guest
You must login to post to this list
  Message 1 of 5  
11 Jan 17 07:38
saqib khanzada
xxxxxx@gmail.com
Join Date: 04 Jan 2017
Posts To This List: 3
Access Denied(Error code 5) when try to use FSCTL_MOVE_FILE

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?
  Message 2 of 5  
11 Jan 17 07:53
saqib khanzada
xxxxxx@gmail.com
Join Date: 04 Jan 2017
Posts To This List: 3
Access Denied(Error code 5) when try to use FSCTL_MOVE_FILE

I am running my program as Administrator .
  Message 3 of 5  
11 Jan 17 11:41
Scott Noone
xxxxxx@osr.com
Join Date: 10 Jul 2002
Posts To This List: 872
List Moderator
Access Denied(Error code 5) when try to use FSCTL_MOVE_FILE

Do you have the SE_MANAGE_VOLUME_NAME privilege when you open the volume? -scott OSR @OSRDrivers wrote in message news:102082@ntfsd... 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?
  Message 4 of 5  
12 Jan 17 03:19
saqib khanzada
xxxxxx@gmail.com
Join Date: 04 Jan 2017
Posts To This List: 3
Access Denied(Error code 5) when try to use FSCTL_MOVE_FILE

Yes i have added SE_MANAGE_VOLUME_NAME in my application before opening the volume. On Wed, Jan 11, 2017 at 9:40 PM, Scott Noone <xxxxx@osr.com> wrote: > Do you have the SE_MANAGE_VOLUME_NAME privilege when you open the volume? > > -scott > OSR > @OSRDrivers > > wrote in message news:102082@ntfsd... > > > Hi , <...excess quoted lines suppressed...> -- Regards Saqib Ahmed Khanzada --
  Message 5 of 5  
13 Jan 17 08:46
Scott Noone
xxxxxx@osr.com
Join Date: 10 Jul 2002
Posts To This List: 872
List Moderator
Access Denied(Error code 5) when try to use FSCTL_MOVE_FILE

Which file system are you doing this on? Does it work on FAT? -scott OSR @OSRDrivers "Saqib Khan" <xxxxx@gmail.com> wrote in message news:102089@ntfsd... Yes i have added SE_MANAGE_VOLUME_NAME in my application before opening the volume. On Wed, Jan 11, 2017 at 9:40 PM, Scott Noone <xxxxx@osr.com> wrote: Do you have the SE_MANAGE_VOLUME_NAME privilege when you open the volume? -scott OSR @OSRDrivers wrote in message news:102082@ntfsd... 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? --- NTFSD is sponsored by OSR MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at <http://www.osr.com/seminars> To unsubscribe, visit the List Server section of OSR Online at <http://www.osronline.com/page.cfm?name=ListServer> -- Regards Saqib Ahmed Khanzada
Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You must login to OSR Online AND be a member of the ntfsd list to be able to post.

All times are GMT -5. The time now is 20:09.


Copyright ©2015, OSR Open Systems Resources, Inc.
Based on vBulletin Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Modified under license