Visual Studio 2015 MFC项目中删除.jpg/.dpx文件解决重生成时间戳问题
Hey there, let's work through this problem together. You're building an MFC C++ GUI project in VS2015, using ImageMagick to generate .jpg and .dpx files. The first run works fine, but when you regenerate, the files appear to update but their timestamps don't—you tried deleting files upfront with remove() but it's not solving the issue. Here's what's likely going wrong and how to fix it:
Common Root Causes
- Mismatched String Encoding: MFC projects default to Unicode, so using narrow-string
remove()with wide-string paths (the_T()macro) can lead to path resolution failures—yourremove()calls might not actually be finding the files to delete. - Unchecked Delete Errors: You're not verifying if
remove()succeeded. If files are locked (e.g., by a previewer or previous ImageMagick process) or you lack write permissions inProgram Files, the delete will silently fail, leaving old files in place. - Asynchronous ShellExecute Calls:
ShellExecute()runs processes in the background. You might be launching ImageMagick before the delete operations finish, or starting a new regeneration before the previous ImageMagick tasks complete—leading to file locks or stale outputs.
Step-by-Step Fixes
1. Use Unicode-Compatible File Deletion
Replace remove() with _tremove() (the TCHAR-compatible version) to match your wide-string paths. Always check the return value to catch failures:
// Define your paths consistently with TCHAR const TCHAR* imgDir = _T("C:\\Program Files\\ImageMagick-7.0.8-Q16\\images\\"); const TCHAR* jpgLeft0 = _T("C:\\Program Files\\ImageMagick-7.0.8-Q16\\images\\splitleftImage_4K00.jpg"); const TCHAR* jpgLeft1 = _T("C:\\Program Files\\ImageMagick-7.0.8-Q16\\images\\splitleftImage_4K01.jpg"); const TCHAR* dpxFile = _T("C:\\Program Files\\ImageMagick-7.0.8-Q16\\images\\Image_4K.dpx"); // Helper lambda to delete files and report errors auto deleteFile = [](const TCHAR* path) { if (_tremove(path) != 0) { DWORD errCode = GetLastError(); CString errMsg; errMsg.Format(_T("Failed to delete %s. Error code: %lu"), path, errCode); AfxMessageBox(errMsg); // Use MFC's message box for consistency } }; deleteFile(jpgLeft0); deleteFile(jpgLeft1); deleteFile(dpxFile);
2. Run ImageMagick Commands Synchronously
Instead of ShellExecute(), use CreateProcess() to wait for each ImageMagick task to finish before proceeding. This ensures no race conditions between delete/regenerate steps. Add this helper function to your code:
BOOL RunCommandSync(const TCHAR* exePath, TCHAR* args, const TCHAR* workingDir) { STARTUPINFO si = { sizeof(STARTUPINFO) }; PROCESS_INFORMATION pi; // Launch the process if (!CreateProcess(exePath, args, NULL, NULL, FALSE, 0, NULL, workingDir, &si, &pi)) { return FALSE; } // Wait for the process to complete fully WaitForSingleObject(pi.hProcess, INFINITE); // Clean up system handles CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return TRUE; }
Then use it to run your ImageMagick commands:
// Run magick crop command TCHAR magickArgs[] = _T(" Image_4K.jpg -crop 50%x100% +repage splitleftImage_4K\%02d.jpg"); if (!RunCommandSync(_T("C:\\Program Files\\ImageMagick-7.0.8-Q16\\images\\magick.exe"), magickArgs, imgDir)) { AfxMessageBox(_T("Magick crop command failed!")); } // Run convert to DPX command TCHAR convertArgs[] = _T(" Image_4K.jpg Image_4K.dpx"); if (!RunCommandSync(_T("C:\\Program Files\\ImageMagick-7.0.8-Q16\\images\\convert.exe"), convertArgs, imgDir)) { AfxMessageBox(_T("Convert to DPX command failed!")); }
3. Fix Permissions (Critical!)
The Program Files directory is protected by Windows UAC. Unless your app runs as administrator, you might not have write/delete permissions there. To avoid this:
- Move your output directory to a user-writable location (e.g.,
C:\\Users\\YourName\\Documents\\ImageMagickOutput\\) - Or, enable "Run as administrator" for your VS2015 project (right-click project > Properties > Linker > Manifest File > UAC Execution Level > set to
requireAdministrator)
Final Checks
After making these changes, test regeneration multiple times—you should see fresh timestamps on all output files. If you still get file lock errors, check if any apps (like image viewers) are holding open the output files.
内容的提问来源于stack exchange,提问作者Divya Sriramulu

