我们的很多exe的动态加载B.dll。B.dll 在临时文件中复制自身并重新加载 %TMP%\B-.dll。我正在尝试使用此代码将 B-.dll 标记为删除,但由于“访问被拒绝”而失败,这无疑是因为 LoadLibrary 调用:
char ourDllPath[MAX_PATH];
// ... set ourDllPath to absolute path...
char tempPath[MAX_PATH];
DWORD dwRetVal = GetTempPath(MAX_PATH, tempPath);
char shadowPath[MAX_PATH];
UINT uRetVal = GetTempFileNameA(tempPath, "FOO_", 0, shadowPath);
BOOL ok = CopyFileA(ourDllPath, shadowPath, false);
HMODULE hShadowDll = LoadLibraryA(shadowPath);
typedef int (WINAPI *PRESUMEFOO)();
PRESUMEFOO onload2 = (PRESUMEFOO)GetProcAddress(hShadowDll,"_Resume_Foo@0");
BOOL ok2 = DeleteFileA(shadowPath); // Fails with "Access is denied"
但是,根据此处(http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx),这应该是可能的:
“DeleteFile 函数将文件标记为在关闭时删除。因此,在文件的最后一个句柄关闭之前不会删除文件。”
关于如何在 Windows 服务器操作系统上将当前打开的文件标记为关闭时删除的任何想法?