Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
C#
DeviceIoControl(volumehandle, Consts.MoveFile, movefiledataPointer, movefiledataSize, IntPtr.Zero, 0, ref size, IntPtr.Zero)
i've been trying to get it to work... but i always get ERROR_INSUFFICIENT_BUFFER.
msdn[^] says it happens when 'the output buffer is too small to receive any data, the call fails, GetLastError returns ERROR_INSUFFICIENT_BUFFER, and lpBytesReturned is zero.' although in the FSCTL_MOVE_FILE[^] page it is said that lpOutBuffer must be NULL (IntPtr.Zero) and nOutBufferSize set to 0 !?!?!

tried in this order :
- rechecked types in movefiledata struct
C#
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    struct MoveFileData
    {
        /// <summary>
        /// handle to the file to be moved
        /// </summary>
        public IntPtr File;

        /// <summary>
        /// VCN (cluster number relative to the beginning of a file) of the first cluster to be moved.
        /// </summary>
        public Int64 StartingVcn;

        /// <summary>
        /// LCN (cluster number on a volume) to which the VCN is to be moved.
        /// </summary>
        public Int64 StartingLcn;

        /// <summary>
        /// The count of clusters to be moved.
        /// </summary>
        public UInt32 ClusterCount;
    }
// snippet 
        GCHandle handle = GCHandle.Alloc(movefiledata, GCHandleType.Pinned);
        IntPtr movefiledataPointer = handle.AddrOfPinnedObject();
        uint movefiledataSize = (uint)Marshal.SizeOf(movefiledata);


- re-targeted compilation to x86
- hacked code so as size would increment on error (desperate)
- re-checked value of Consts.MoveFile against WinIoCtl.h
- re-checked types in MoveFileData against WinIoCtl.h
- posted question on codeproject.com ^_-


any idea???
:: update ::
this code is working perfectly !!!!
C++
int _tmain(int argc, _TCHAR* argv[])
{
	LPCWSTR name = L"\\\\.\\e:";
	HANDLE volumehandle = CreateFileW(name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
	HANDLE filehandle = CreateFileW(L"e:\\Temp\\4caf4c2.tmp", FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_POSIX_SEMANTICS, NULL);


	MOVE_FILE_DATA move_data;
    move_data.FileHandle = filehandle;
    move_data.StartingVcn.QuadPart = 129;
    move_data.StartingLcn.QuadPart = 1167419;

	move_data.ClusterCount = (DWORD) 1;
    DWORD bytes_ret;
    BOOL ret = DeviceIoControl(volumehandle, FSCTL_MOVE_FILE, &move_data, sizeof(move_data), NULL, 0, &bytes_ret, NULL);
	if (ret == 0)
	{
		DWORD lasterr = GetLastError();
		std::printf("error hapened %u" , lasterr);

		if (lasterr == ERROR_HANDLE_EOF)
			return lasterr;
	}
	CloseHandle(filehandle);
	CloseHandle(volumehandle);

	return 0;
}
Posted
Updated 21-Nov-14 5:55am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900