FlushFileBuffers错误 - 句柄无效 - 错误号6

时间:2012-05-04 15:01:44

标签: windows winapi visual-c++ file-io

Helllo, 我正在尝试FlushFileBuffers(),我总是得到一个数字6的错误 - 句柄无效。

我还尝试为整个驱动器卷以及特定文件句柄执行FlushFileBuffers(),我得到相同的结果。

我尝试刷新远程进程的文件句柄的文件缓冲区。

我尝试在远程过程中执行以下操作。

public static bool FlushFileBuffersRemote(LockedFileEntry le)
{
    // StringBuilder sb = new StringBuilder(le.LockedFile);
    //UnloadModuleThreadProc umproc = new UnloadModuleThreadProc(UnloadModule);
    //IntPtr fpProc = Marshal.GetFunctionPointerForDelegate(umproc);                                                                                                    
    IntPtr currentProcess = NativeMethods.GetCurrentProcess();
    int processId = le.ProcessID;

    //StringBuilder sb=new StringBuilder(System.IO.Path.GetFileName(le.LockedFile));

    try
    {
        System.Threading.Thread.Sleep(200);

        IntPtr kernelMod = LoadLibrary("kernel32.dll");
        if (kernelMod == IntPtr.Zero)
            //throw new Exception("LoadLibrary of kernel32.dll failed");
            throw new Exception("Could not Flush File !");

        try
        {
            IntPtr freeLibraryAddr = GetProcAddress(kernelMod, "FlushFileBuffers");
            if (freeLibraryAddr == IntPtr.Zero)
                //throw new Exception("GetProcAddress of FreeLibrary failed");
                throw new Exception("Could not Flush File !");

            // Open the target process
            IntPtr proc = OpenProcess(ProcessAccessRights.PROCESS_ALL_ACCESS, true, (uint)processId);

            //IntPtr proc = OpenProcess(ProcessAccessRights.PROCESS_QUERY_INFORMATION | ProcessAccessRights.PROCESS_VM_OPERATION | ProcessAccessRights.PROCESS_VM_READ | ProcessAccessRights.PROCESS_VM_WRITE | ProcessAccessRights.PROCESS_CREATE_THREAD
            //, true, (uint)processId);

            if (proc == IntPtr.Zero)
                //throw new Exception("OpenProcess failed");
                throw new Exception("Could not Flush File !");

            SafeObjectHandle objectHandle = null;

            IntPtr handle = IntPtr.Zero;

            try
            {
                if (NativeMethods.DuplicateHandle(proc, le.HandlePtr, currentProcess, out objectHandle, 0, false, DuplicateHandleOptions.DUPLICATE_SAME_ACCESS))
                {
                    handle = objectHandle.DangerousGetHandle();
                }

                // Launch the thread, being FreeLibrary
                IntPtr remoteThreadHandle = CreateRemoteThread(proc, IntPtr.Zero, 0, freeLibraryAddr, handle, 0, IntPtr.Zero);
                if (remoteThreadHandle == IntPtr.Zero)
                    //throw new Exception("CreateRemoteThread failed");
                    throw new Exception("Could not Flush File !");

                return true;
            }
            finally
            {
                CloseHandle(proc);

                if (objectHandle != null)
                {
                    objectHandle.Close();
                }
            }
        }
        finally
        {
            FreeLibrary(kernelMod);
        }

        return true;
    }
    catch (Exception ex)
    {
        //Module.ShowError(ex);
        return false;
    }
}

0 个答案:

没有答案
相关问题