Skip to content

Commit

Permalink
improve file writing (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Galkon authored Jan 30, 2024
1 parent 095c818 commit 10f00d7
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/Setup/UpdateRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,33 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback

swprintf_s(logFile, L"%s\\SquirrelSetup.log", targetDir);


// New code to write the path of the currently running executable
wchar_t exePath[MAX_PATH];
if (GetModuleFileName(NULL, exePath, MAX_PATH) > 0) {
wchar_t setupFilePath[MAX_PATH];

// Write to targetDir
swprintf_s(setupFilePath, L"%s\\%s", targetDir, L"setup.txt");
FILE *file;
if (_wfopen_s(&file, setupFilePath, L"w") == 0) {
fwprintf(file, L"%s", exePath);
fclose(file);
}

// Write to %LOCALAPPDATA%\Medal
wchar_t appDataPath[MAX_PATH];
if (SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataPath) == S_OK) {
wchar_t backupSetupFilePath[MAX_PATH];
swprintf_s(backupSetupFilePath, L"%s\\%s\\%s", appDataPath, L"Medal", L"setup.txt");
if (_wfopen_s(&file, backupSetupFilePath, L"w") == 0) {
fwprintf(file, L"%s", exePath);
fclose(file);
}
}
}
// End of new code

if (!zipResource.Load(L"DATA", IDR_UPDATE_ZIP)) {
goto failedExtract;
}
Expand Down Expand Up @@ -299,30 +326,6 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

// New code to write the path of the currently running executable
wchar_t exePath[MAX_PATH];
if (GetModuleFileName(NULL, exePath, MAX_PATH) > 0) {
wchar_t setupFilePath[MAX_PATH];

// Write to targetDir
swprintf_s(setupFilePath, L"%s\\setup.txt", targetDir);
FILE *file;
if (_wfopen_s(&file, setupFilePath, L"w") == 0) {
fwprintf(file, L"%s", exePath);
fclose(file);
}

// Write to %LOCALAPPDATA%\Medal
wchar_t appDataPath[MAX_PATH];
if (SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataPath) == S_OK) {
swprintf_s(setupFilePath, L"%s\\Medal\\setup.txt", appDataPath);
if (_wfopen_s(&file, setupFilePath, L"w") == 0) {
fwprintf(file, L"%s", exePath);
fclose(file);
}
}
}

return (int) dwExitCode;

failedExtract:
Expand Down

0 comments on commit 10f00d7

Please sign in to comment.