Fixing memory violations

This commit is contained in:
Caelan Sayler
2024-10-20 00:06:38 +01:00
committed by Caelan
parent 49c2cc265e
commit b2e58e6142
7 changed files with 76 additions and 49 deletions

View File

@@ -25,7 +25,7 @@ HINSTANCE hInst;
const WCHAR szTitle[] = L"Velopack C++ Sample App";
const WCHAR szWindowClass[] = L"VeloCppWinSample";
std::optional<UpdateManager> managerOpt = std::nullopt;
std::unique_ptr<UpdateManager> manager;
std::optional<UpdateInfo> updInfo = std::nullopt;
bool downloaded = false;
@@ -38,22 +38,36 @@ INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
std::wstring Utf8ToWString(std::string const& str);
std::string WStringToUtf8(std::wstring const& wstr);
void handle_vpkc_log(const char* pszLevel, const char* pszMessage)
{
std::cout << pszLevel << ": " << pszMessage << std::endl;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
// Redirect StdOut to console window
AllocConsole();
FILE* fp;
_wfreopen_s(&fp, L"CONOUT$", L"w", stdout);
_wfreopen_s(&fp, L"CONOUT$", L"w", stderr);
_wfreopen_s(&fp, L"CONIN$", L"r", stdin);
// Initialize Velopack log capture
std::cout << "Velopack C++ Sample App" << std::endl;
UNREFERENCED_PARAMETER(hPrevInstance);
vpkc_set_log(handle_vpkc_log);
// This should run as early as possible in the main method.
// Velopack may exit / restart the app at this point.
// See VelopackApp class for more options/configuration.
VelopackApp::Build().Run();
VelopackApp::Build()
.Run();
try {
// If the app is not installed, creating an UpdateManager will throw an exception.
managerOpt = UpdateManager(UPDATE_URL);
manager = std::make_unique<UpdateManager>(UPDATE_URL);
}
catch (std::exception& e) {
std::wstring what = Utf8ToWString(e.what());
@@ -136,14 +150,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_COMMAND:
{
if (LOWORD(wParam) == BN_CLICKED) {
if (!managerOpt.has_value()) {
break; // this should not happen because we construct UpdateManager in wWinMain
}
auto manager = managerOpt.value();
if ((HWND)lParam == hCheckButton) {
try {
updInfo = manager.CheckForUpdates();
updInfo = manager->CheckForUpdates();
if (updInfo.has_value()) {
std::string version = updInfo.value().TargetFullRelease.Version;
std::wstring message = L"Update available: " + Utf8ToWString(version);
@@ -161,7 +170,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
else if ((HWND)lParam == hDownloadButton) {
if (updInfo.has_value()) {
try {
manager.DownloadUpdates(updInfo.value());
manager->DownloadUpdates(updInfo.value());
downloaded = true;
MessageBoxCentered(hWnd, L"Download completed successfully.", szTitle, MB_OK);
}
@@ -179,7 +188,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
MessageBoxCentered(hWnd, L"Download an update first.", szTitle, MB_OK);
}
else {
manager.WaitExitThenApplyUpdate(updInfo.value());
manager->WaitExitThenApplyUpdate(updInfo.value());
exit(0);
}
}
@@ -192,10 +201,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
HDC hdc = BeginPaint(hWnd, &ps);
RECT r{ 0, 5, ps.rcPaint.right, ps.rcPaint.bottom };
std::string currentVersion = "Not Installed";
if (managerOpt.has_value()) {
currentVersion = managerOpt.value().GetCurrentVersion();
}
std::string currentVersion = manager->GetCurrentVersion();
auto ver = Utf8ToWString(currentVersion);
std::wstring text = L"Welcome to v" + ver + L" of the\nVelopack C++ Sample App.";
DrawText(hdc, text.c_str(), -1, &r, DT_BOTTOM | DT_CENTER);

View File

@@ -5,6 +5,12 @@ VisualStudioVersion = 17.8.34525.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VeloCppWinSample", "VeloCppWinSample.vcxproj", "{F9BB1F11-3827-4745-B11B-77FA5DBE1195}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{25F8ED9F-9255-4CEC-AE44-7F2EAA392C18}"
ProjectSection(SolutionItems) = preProject
build.bat = build.bat
readme.md = readme.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64

View File

@@ -13,9 +13,8 @@ if "%~1"=="" (
)
echo.
echo Building Velopack Rust
cd %~dp0..\..\..\for-rust
cargo build --features cli -r
echo Building Velopack C Lib with Cargo
cargo build -p velopack_libc
cd %~dp0
@@ -43,8 +42,6 @@ cd %~dp0
echo #define UPDATE_URL "REPLACE_ME" > constants.h
copy %~dp0..\..\..\for-rust\target\release\vfusion.exe x64\Debug
echo.
echo Building Velopack Release v%~1
vpk pack -u VeloCppWinSample -o releases -p x64\Debug -v %*
vpk pack -u VelopackCppWin32Sample -o releases -p x64\Debug -v %* -e VeloCppWinSample.exe

View File

@@ -1,5 +1,5 @@
# VeloCppWinSample
_Prerequisites: Rust/Cargo, Dotnet, Msbuild_
# VelopackCppWin32Sample
_Prerequisites: Rust/Cargo, Msbuild_
This app is purely a proof of concept at this time, `velopack.hpp` currently only works on windows and needs testing / fixes for other operating systems, and probably also needs fixing for unicode/strings. This sample is made up of a simple Win32 desktop app, generated via a Visual Studio template, and it includes `velopack.hpp` and [`subprocess.h`](https://github.com/sheredom/subprocess.h).