mirror of
				https://github.com/velopack/velopack.git
				synced 2025-10-25 15:19:22 +00:00 
			
		
		
		
	Cleaning up C++ sample
This commit is contained in:
		| @@ -5,8 +5,10 @@ | |||||||
| #include <malloc.h> | #include <malloc.h> | ||||||
| #include <memory.h> | #include <memory.h> | ||||||
| #include <tchar.h> | #include <tchar.h> | ||||||
|  | #include <iostream> | ||||||
|  |  | ||||||
| #include "constants.h" | #include "constants.h" | ||||||
| #include "../../Velopack.hpp" | #include "Velopack.h" | ||||||
|  |  | ||||||
| #pragma comment(linker, \ | #pragma comment(linker, \ | ||||||
|   "\"/manifestdependency:type='Win32' "\ |   "\"/manifestdependency:type='Win32' "\ | ||||||
| @@ -17,13 +19,15 @@ | |||||||
|   "language='*'\"") |   "language='*'\"") | ||||||
| #pragma comment(lib, "ComCtl32.lib") | #pragma comment(lib, "ComCtl32.lib") | ||||||
|  |  | ||||||
|  | using namespace Velopack; | ||||||
|  |  | ||||||
| HINSTANCE hInst; | HINSTANCE hInst; | ||||||
| const WCHAR szTitle[] = L"Velopack C++ Sample App"; | const WCHAR szTitle[] = L"Velopack C++ Sample App"; | ||||||
| const WCHAR szWindowClass[] = L"VeloCppWinSample"; | const WCHAR szWindowClass[] = L"VeloCppWinSample"; | ||||||
| std::shared_ptr<Velopack::UpdateInfo> updInfo{}; |  | ||||||
|  | std::optional<UpdateManager> managerOpt = std::nullopt; | ||||||
|  | std::optional<UpdateInfo> updInfo = std::nullopt; | ||||||
| bool downloaded = false; | bool downloaded = false; | ||||||
| Velopack::UpdateManagerSync manager{}; |  | ||||||
| std::string currentVersion = ""; |  | ||||||
|  |  | ||||||
| // Forward declarations of functions included in this code module: | // Forward declarations of functions included in this code module: | ||||||
| int					MessageBoxCentered(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType); | int					MessageBoxCentered(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType); | ||||||
| @@ -31,70 +35,63 @@ ATOM                MyRegisterClass(HINSTANCE hInstance); | |||||||
| BOOL                InitInstance(HINSTANCE, int); | BOOL                InitInstance(HINSTANCE, int); | ||||||
| LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); | LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); | ||||||
| INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM); | INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM); | ||||||
| std::wstring utf8_to_wstring(std::string const& str); | std::wstring        Utf8ToWString(std::string const& str); | ||||||
| std::string wstring_to_utf8(std::wstring const& wstr); | std::string         WStringToUtf8(std::wstring const& wstr); | ||||||
|  |  | ||||||
| int APIENTRY wWinMain(_In_ HINSTANCE hInstance, | int APIENTRY wWinMain(_In_ HINSTANCE hInstance, | ||||||
| 	_In_opt_ HINSTANCE hPrevInstance, |     _In_opt_ HINSTANCE hPrevInstance, | ||||||
| 	_In_ LPWSTR    lpCmdLine, |     _In_ LPWSTR    lpCmdLine, | ||||||
| 	_In_ int       nCmdShow) |     _In_ int       nCmdShow) | ||||||
| { | { | ||||||
| 	UNREFERENCED_PARAMETER(hPrevInstance); |     std::cout << "Velopack C++ Sample App" << std::endl; | ||||||
|  |     UNREFERENCED_PARAMETER(hPrevInstance); | ||||||
|  |  | ||||||
| 	try |     // This should run as early as possible in the main method. | ||||||
| 	{ |     // Velopack may exit / restart the app at this point.  | ||||||
| 		// the first thing we need to do in our app is initialise the velopack sdk |     // See VelopackApp class for more options/configuration. | ||||||
| 		int pNumArgs = 0; |      VelopackApp::Build().Run(); | ||||||
| 		wchar_t** args = CommandLineToArgvW(lpCmdLine, &pNumArgs); |  | ||||||
| 		Velopack::startup(args, pNumArgs); |  | ||||||
| 		manager.setUrlOrPath(UPDATE_URL); |  | ||||||
| 		currentVersion = manager.getCurrentVersion(); |  | ||||||
| 	} |  | ||||||
| 	catch (std::exception& e) |  | ||||||
| 	{ |  | ||||||
| 		std::string what = e.what(); |  | ||||||
| 		std::wstring wideWhat(what.begin(), what.end()); |  | ||||||
| 		MessageBoxCentered(nullptr, wideWhat.c_str(), szTitle, MB_OK | MB_ICONERROR); |  | ||||||
| 		return 1; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	MyRegisterClass(hInstance); |     try { | ||||||
| 	if (!InitInstance(hInstance, nCmdShow)) |         // If the app is not installed, creating an UpdateManager will throw an exception. | ||||||
| 	{ |         managerOpt = UpdateManager(UPDATE_URL); | ||||||
| 		return FALSE; |     } | ||||||
| 	} |     catch (std::exception& e) { | ||||||
|  |         std::wstring what = Utf8ToWString(e.what()); | ||||||
|  |         MessageBoxCentered(nullptr, what.c_str(), szTitle, MB_OK | MB_ICONERROR); | ||||||
|  |         return 1; | ||||||
|  |     } | ||||||
|  |  | ||||||
| 	MSG msg; |     MyRegisterClass(hInstance); | ||||||
|  |     if (!InitInstance(hInstance, nCmdShow)) { | ||||||
|  |         return FALSE; | ||||||
|  |     } | ||||||
|  |  | ||||||
| 	// Main message loop: |     // Main message loop: | ||||||
| 	while (GetMessage(&msg, nullptr, 0, 0)) |     MSG msg; | ||||||
| 	{ |     while (GetMessage(&msg, nullptr, 0, 0)) { | ||||||
| 		TranslateMessage(&msg); |         TranslateMessage(&msg); | ||||||
| 		DispatchMessage(&msg); |         DispatchMessage(&msg); | ||||||
| 	} |     } | ||||||
|  |  | ||||||
| 	return (int)msg.wParam; |     return (int)msg.wParam; | ||||||
| } | } | ||||||
|  |  | ||||||
| ATOM MyRegisterClass(HINSTANCE hInstance) | ATOM MyRegisterClass(HINSTANCE hInstance) | ||||||
| { | { | ||||||
| 	WNDCLASSEXW wcex; |     WNDCLASSEXW wcex; | ||||||
|  |     wcex.cbSize = sizeof(WNDCLASSEX); | ||||||
| 	wcex.cbSize = sizeof(WNDCLASSEX); |     wcex.style = CS_HREDRAW | CS_VREDRAW; | ||||||
|  |     wcex.lpfnWndProc = WndProc; | ||||||
| 	wcex.style = CS_HREDRAW | CS_VREDRAW; |     wcex.cbClsExtra = 0; | ||||||
| 	wcex.lpfnWndProc = WndProc; |     wcex.cbWndExtra = 0; | ||||||
| 	wcex.cbClsExtra = 0; |     wcex.hInstance = hInstance; | ||||||
| 	wcex.cbWndExtra = 0; |     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); | ||||||
| 	wcex.hInstance = hInstance; |     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); | ||||||
| 	wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); |     wcex.lpszClassName = szWindowClass; | ||||||
| 	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); |     wcex.hIcon = 0; | ||||||
| 	wcex.lpszClassName = szWindowClass; |     wcex.hIconSm = 0; | ||||||
| 	wcex.hIcon = 0; |     wcex.lpszMenuName = 0; | ||||||
| 	wcex.hIconSm = 0; |     return RegisterClassExW(&wcex); | ||||||
| 	wcex.lpszMenuName = 0; |  | ||||||
|  |  | ||||||
| 	return RegisterClassExW(&wcex); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| HWND hCheckButton; | HWND hCheckButton; | ||||||
| @@ -103,163 +100,164 @@ HWND hRestartButton; | |||||||
|  |  | ||||||
| BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) | BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) | ||||||
| { | { | ||||||
| 	hInst = hInstance; // Store instance handle in our global variable |     hInst = hInstance; // Store instance handle in our global variable | ||||||
|  |  | ||||||
| 	HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, |     HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, | ||||||
| 		CW_USEDEFAULT, 0, 300, 260, nullptr, nullptr, hInstance, nullptr); |         CW_USEDEFAULT, 0, 300, 260, nullptr, nullptr, hInstance, nullptr); | ||||||
|  |  | ||||||
| 	hCheckButton = CreateWindowW(L"BUTTON", L"Check for updates", |     hCheckButton = CreateWindowW(L"BUTTON", L"Check for updates", | ||||||
| 		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, |         WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, | ||||||
| 		40, 50, 200, 40, |         40, 50, 200, 40, | ||||||
| 		hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); |         hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); | ||||||
|  |  | ||||||
| 	hDownloadButton = CreateWindowW(L"BUTTON", L"Download update", |     hDownloadButton = CreateWindowW(L"BUTTON", L"Download update", | ||||||
| 		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, |         WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, | ||||||
| 		40, 100, 200, 40, |         40, 100, 200, 40, | ||||||
| 		hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); |         hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); | ||||||
|  |  | ||||||
| 	hRestartButton = CreateWindowW(L"BUTTON", L"Apply / Restart", |     hRestartButton = CreateWindowW(L"BUTTON", L"Apply / Restart", | ||||||
| 		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, |         WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, | ||||||
| 		40, 150, 200, 40, |         40, 150, 200, 40, | ||||||
| 		hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); |         hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL); | ||||||
|  |  | ||||||
| 	if (!hWnd) |     if (!hWnd) { | ||||||
| 	{ |         return FALSE; | ||||||
| 		return FALSE; |     } | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	ShowWindow(hWnd, nCmdShow); |     ShowWindow(hWnd, nCmdShow); | ||||||
| 	UpdateWindow(hWnd); |     UpdateWindow(hWnd); | ||||||
|  |  | ||||||
| 	return TRUE; |     return TRUE; | ||||||
| } | } | ||||||
|  |  | ||||||
| LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) | ||||||
| { | { | ||||||
| 	switch (message) |     switch (message) { | ||||||
| 	{ |     case WM_COMMAND: | ||||||
| 	case WM_COMMAND: |     { | ||||||
| 	{ |         if (LOWORD(wParam) == BN_CLICKED) { | ||||||
| 		if (LOWORD(wParam) == BN_CLICKED) |             if (!managerOpt.has_value()) { | ||||||
| 		{ |                 break; // this should not happen because we construct UpdateManager in wWinMain | ||||||
| 			if ((HWND)lParam == hCheckButton) |             } | ||||||
| 			{ |             auto manager = managerOpt.value(); | ||||||
| 				try { |  | ||||||
| 					updInfo = manager.checkForUpdates(); |             if ((HWND)lParam == hCheckButton) { | ||||||
| 					if (updInfo != nullptr) { |                 try { | ||||||
| 						// this is a hack to convert ascii to wide string |                     updInfo = manager.CheckForUpdates(); | ||||||
| 						auto version = updInfo->targetFullRelease->version; |                     if (updInfo.has_value()) { | ||||||
| 						std::wstring message = L"Update available: " + utf8_to_wstring(version); |                         std::string version = updInfo.value().TargetFullRelease.Version; | ||||||
| 						MessageBoxCentered(hWnd, message.c_str(), szTitle, MB_OK); |                         std::wstring message = L"Update available: " + Utf8ToWString(version); | ||||||
| 					} |                         MessageBoxCentered(hWnd, message.c_str(), szTitle, MB_OK); | ||||||
| 					else { |                     } | ||||||
| 						MessageBoxCentered(hWnd, L"No updates available.", szTitle, MB_OK); |                     else { | ||||||
| 					} |                         MessageBoxCentered(hWnd, L"No updates available.", szTitle, MB_OK); | ||||||
| 				} |                     } | ||||||
| 				catch (std::exception& e) { |                 } | ||||||
| 					std::wstring wideWhat = utf8_to_wstring(e.what()); |                 catch (std::exception& e) { | ||||||
| 					MessageBoxCentered(hWnd, wideWhat.c_str(), szTitle, MB_OK | MB_ICONERROR); |                     std::wstring what = Utf8ToWString(e.what()); | ||||||
| 				} |                     MessageBoxCentered(hWnd, what.c_str(), szTitle, MB_OK | MB_ICONERROR); | ||||||
| 			} |                 } | ||||||
| 			else if ((HWND)lParam == hDownloadButton) |             } | ||||||
| 			{ |             else if ((HWND)lParam == hDownloadButton) { | ||||||
| 				if (updInfo != nullptr) { |                 if (updInfo.has_value()) { | ||||||
| 					try { |                     try { | ||||||
| 						manager.downloadUpdates(updInfo->targetFullRelease.get()); |                         manager.DownloadUpdates(updInfo.value()); | ||||||
| 						downloaded = true; |                         downloaded = true; | ||||||
| 						MessageBoxCentered(hWnd, L"Download completed successfully.", szTitle, MB_OK); |                         MessageBoxCentered(hWnd, L"Download completed successfully.", szTitle, MB_OK); | ||||||
| 					} |                     } | ||||||
| 					catch (std::exception& e) { |                     catch (std::exception& e) { | ||||||
| 						std::wstring wideWhat = utf8_to_wstring(e.what()); |                         std::wstring what = Utf8ToWString(e.what()); | ||||||
| 						MessageBoxCentered(hWnd, wideWhat.c_str(), szTitle, MB_OK | MB_ICONERROR); |                         MessageBoxCentered(hWnd, what.c_str(), szTitle, MB_OK | MB_ICONERROR); | ||||||
| 					} |                     } | ||||||
| 				} |                 } | ||||||
| 				else { |                 else { | ||||||
| 					MessageBoxCentered(hWnd, L"Check for updates first.", szTitle, MB_OK); |                     MessageBoxCentered(hWnd, L"Check for updates first.", szTitle, MB_OK); | ||||||
| 				} |                 } | ||||||
| 			} |             } | ||||||
| 			else if ((HWND)lParam == hRestartButton) |             else if ((HWND)lParam == hRestartButton) { | ||||||
| 			{ |                 if (!downloaded) { | ||||||
| 				if (!downloaded) { |                     MessageBoxCentered(hWnd, L"Download an update first.", szTitle, MB_OK); | ||||||
| 					MessageBoxCentered(hWnd, L"Download an update first.", szTitle, MB_OK); |                 } | ||||||
| 				} |                 else { | ||||||
| 				else { |                     manager.WaitExitThenApplyUpdate(updInfo.value()); | ||||||
| 					manager.applyUpdatesAndRestart(updInfo->targetFullRelease.get()); |                     exit(0); | ||||||
| 				} |                 } | ||||||
| 			} |             } | ||||||
| 		} |         } | ||||||
| 		break; |         break; | ||||||
| 	} |     } | ||||||
| 	case WM_PAINT: |     case WM_PAINT: | ||||||
| 	{ |     { | ||||||
| 		PAINTSTRUCT ps; |         PAINTSTRUCT ps; | ||||||
| 		HDC hdc = BeginPaint(hWnd, &ps); |         HDC hdc = BeginPaint(hWnd, &ps); | ||||||
| 		RECT r{ 0, 5, ps.rcPaint.right, ps.rcPaint.bottom }; |         RECT r{ 0, 5, ps.rcPaint.right, ps.rcPaint.bottom }; | ||||||
| 		auto ver = utf8_to_wstring(currentVersion); |  | ||||||
| 		std::wstring text = L"Welcome to v" + ver + L" of the\nVelopack C++ Sample App."; |         std::string currentVersion = "Not Installed"; | ||||||
| 		DrawText(hdc, text.c_str(), -1, &r, DT_BOTTOM | DT_CENTER); |         if (managerOpt.has_value()) { | ||||||
| 		EndPaint(hWnd, &ps); |             currentVersion = managerOpt.value().GetCurrentVersion(); | ||||||
| 		break; |         } | ||||||
| 	} |         auto ver = Utf8ToWString(currentVersion); | ||||||
| 	case WM_DESTROY: |         std::wstring text = L"Welcome to v" + ver + L" of the\nVelopack C++ Sample App."; | ||||||
| 		PostQuitMessage(0); |         DrawText(hdc, text.c_str(), -1, &r, DT_BOTTOM | DT_CENTER); | ||||||
| 		break; |         EndPaint(hWnd, &ps); | ||||||
| 	default: |         break; | ||||||
| 		return DefWindowProc(hWnd, message, wParam, lParam); |     } | ||||||
| 	} |     case WM_DESTROY: | ||||||
| 	return 0; |         PostQuitMessage(0); | ||||||
|  |         break; | ||||||
|  |     default: | ||||||
|  |         return DefWindowProc(hWnd, message, wParam, lParam); | ||||||
|  |     } | ||||||
|  |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| int MessageBoxCentered(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) | int MessageBoxCentered(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) | ||||||
| { | { | ||||||
| 	if (hWnd == nullptr) |     if (hWnd == nullptr) { | ||||||
| 	{ |         return MessageBox(hWnd, lpText, lpCaption, uType); | ||||||
| 		return MessageBox(hWnd, lpText, lpCaption, uType); |     } | ||||||
| 	} |     else { | ||||||
| 	else |         // Center message box at its parent window | ||||||
| 	{ |         static HHOOK hHookCBT{}; | ||||||
| 		// Center message box at its parent window |         hHookCBT = SetWindowsHookEx(WH_CBT, | ||||||
| 		static HHOOK hHookCBT{}; |             [](int nCode, WPARAM wParam, LPARAM lParam) -> LRESULT | ||||||
| 		hHookCBT = SetWindowsHookEx(WH_CBT, |             { | ||||||
| 			[](int nCode, WPARAM wParam, LPARAM lParam) -> LRESULT |                 if (nCode == HCBT_CREATEWND) { | ||||||
| 			{ |                     if (((LPCBT_CREATEWND)lParam)->lpcs->lpszClass == (LPWSTR)(ATOM)32770)  // #32770 = dialog box class | ||||||
| 				if (nCode == HCBT_CREATEWND) |                     { | ||||||
| 				{ |                         RECT rcParent{}; | ||||||
| 					if (((LPCBT_CREATEWND)lParam)->lpcs->lpszClass == (LPWSTR)(ATOM)32770)  // #32770 = dialog box class |                         GetWindowRect(((LPCBT_CREATEWND)lParam)->lpcs->hwndParent, &rcParent); | ||||||
| 					{ |                         ((LPCBT_CREATEWND)lParam)->lpcs->x = rcParent.left + ((rcParent.right - rcParent.left) - ((LPCBT_CREATEWND)lParam)->lpcs->cx) / 2; | ||||||
| 						RECT rcParent{}; |                         ((LPCBT_CREATEWND)lParam)->lpcs->y = rcParent.top + ((rcParent.bottom - rcParent.top) - ((LPCBT_CREATEWND)lParam)->lpcs->cy) / 2; | ||||||
| 						GetWindowRect(((LPCBT_CREATEWND)lParam)->lpcs->hwndParent, &rcParent); |                     } | ||||||
| 						((LPCBT_CREATEWND)lParam)->lpcs->x = rcParent.left + ((rcParent.right - rcParent.left) - ((LPCBT_CREATEWND)lParam)->lpcs->cx) / 2; |                 } | ||||||
| 						((LPCBT_CREATEWND)lParam)->lpcs->y = rcParent.top + ((rcParent.bottom - rcParent.top) - ((LPCBT_CREATEWND)lParam)->lpcs->cy) / 2; |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				return CallNextHookEx(hHookCBT, nCode, wParam, lParam); |                 return CallNextHookEx(hHookCBT, nCode, wParam, lParam); | ||||||
| 			}, |             }, | ||||||
| 			0, GetCurrentThreadId()); |             0, GetCurrentThreadId()); | ||||||
|  |  | ||||||
| 		int iRet{ MessageBox(hWnd, lpText, lpCaption, uType) }; |         int iRet{ MessageBox(hWnd, lpText, lpCaption, uType) }; | ||||||
|  |  | ||||||
| 		UnhookWindowsHookEx(hHookCBT); |         UnhookWindowsHookEx(hHookCBT); | ||||||
|  |  | ||||||
| 		return iRet; |         return iRet; | ||||||
| 	} |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| std::string wstring_to_utf8(std::wstring const& wstr) | std::string WStringToUtf8(std::wstring const& wstr) | ||||||
| { | { | ||||||
| 	if (wstr.empty()) return std::string(); |     if (wstr.empty()) return std::string(); | ||||||
| 	int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); |     int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); | ||||||
| 	std::string strTo(size_needed, 0); |     std::string strTo(size_needed, 0); | ||||||
| 	WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); |     WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); | ||||||
| 	return strTo; |     return strTo; | ||||||
| } | } | ||||||
|  |  | ||||||
| std::wstring utf8_to_wstring(std::string const& str) | std::wstring Utf8ToWString(std::string const& str) | ||||||
| { | { | ||||||
| 	if (str.empty()) return std::wstring(); |     if (str.empty()) return std::wstring(); | ||||||
| 	int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); |     int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); | ||||||
| 	std::wstring strTo(size_needed, 0); |     std::wstring strTo(size_needed, 0); | ||||||
| 	MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &strTo[0], size_needed); |     MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &strTo[0], size_needed); | ||||||
| 	return strTo; |     return strTo; | ||||||
| } | } | ||||||
| @@ -8,19 +8,13 @@ EndProject | |||||||
| Global | Global | ||||||
| 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||||
| 		Debug|x64 = Debug|x64 | 		Debug|x64 = Debug|x64 | ||||||
| 		Debug|x86 = Debug|x86 |  | ||||||
| 		Release|x64 = Release|x64 | 		Release|x64 = Release|x64 | ||||||
| 		Release|x86 = Release|x86 |  | ||||||
| 	EndGlobalSection | 	EndGlobalSection | ||||||
| 	GlobalSection(ProjectConfigurationPlatforms) = postSolution | 	GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Debug|x64.ActiveCfg = Debug|x64 | 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Debug|x64.ActiveCfg = Debug|x64 | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Debug|x64.Build.0 = Debug|x64 | 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Debug|x64.Build.0 = Debug|x64 | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Debug|x86.ActiveCfg = Debug|Win32 |  | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Debug|x86.Build.0 = Debug|Win32 |  | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Release|x64.ActiveCfg = Release|x64 | 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Release|x64.ActiveCfg = Release|x64 | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Release|x64.Build.0 = Release|x64 | 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Release|x64.Build.0 = Release|x64 | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Release|x86.ActiveCfg = Release|Win32 |  | ||||||
| 		{F9BB1F11-3827-4745-B11B-77FA5DBE1195}.Release|x86.Build.0 = Release|Win32 |  | ||||||
| 	EndGlobalSection | 	EndGlobalSection | ||||||
| 	GlobalSection(SolutionProperties) = preSolution | 	GlobalSection(SolutionProperties) = preSolution | ||||||
| 		HideSolutionNode = FALSE | 		HideSolutionNode = FALSE | ||||||
|   | |||||||
| @@ -1,14 +1,6 @@ | |||||||
| <?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||||||
| <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||||||
|   <ItemGroup Label="ProjectConfigurations"> |   <ItemGroup Label="ProjectConfigurations"> | ||||||
|     <ProjectConfiguration Include="Debug|Win32"> |  | ||||||
|       <Configuration>Debug</Configuration> |  | ||||||
|       <Platform>Win32</Platform> |  | ||||||
|     </ProjectConfiguration> |  | ||||||
|     <ProjectConfiguration Include="Release|Win32"> |  | ||||||
|       <Configuration>Release</Configuration> |  | ||||||
|       <Platform>Win32</Platform> |  | ||||||
|     </ProjectConfiguration> |  | ||||||
|     <ProjectConfiguration Include="Debug|x64"> |     <ProjectConfiguration Include="Debug|x64"> | ||||||
|       <Configuration>Debug</Configuration> |       <Configuration>Debug</Configuration> | ||||||
|       <Platform>x64</Platform> |       <Platform>x64</Platform> | ||||||
| @@ -26,19 +18,6 @@ | |||||||
|     <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> |     <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> |   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> |  | ||||||
|     <ConfigurationType>Application</ConfigurationType> |  | ||||||
|     <UseDebugLibraries>true</UseDebugLibraries> |  | ||||||
|     <PlatformToolset>v143</PlatformToolset> |  | ||||||
|     <CharacterSet>Unicode</CharacterSet> |  | ||||||
|   </PropertyGroup> |  | ||||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> |  | ||||||
|     <ConfigurationType>Application</ConfigurationType> |  | ||||||
|     <UseDebugLibraries>false</UseDebugLibraries> |  | ||||||
|     <PlatformToolset>v143</PlatformToolset> |  | ||||||
|     <WholeProgramOptimization>true</WholeProgramOptimization> |  | ||||||
|     <CharacterSet>Unicode</CharacterSet> |  | ||||||
|   </PropertyGroup> |  | ||||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> |   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||||||
|     <ConfigurationType>Application</ConfigurationType> |     <ConfigurationType>Application</ConfigurationType> | ||||||
|     <UseDebugLibraries>true</UseDebugLibraries> |     <UseDebugLibraries>true</UseDebugLibraries> | ||||||
| @@ -57,12 +36,6 @@ | |||||||
|   </ImportGroup> |   </ImportGroup> | ||||||
|   <ImportGroup Label="Shared"> |   <ImportGroup Label="Shared"> | ||||||
|   </ImportGroup> |   </ImportGroup> | ||||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |  | ||||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |  | ||||||
|   </ImportGroup> |  | ||||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |  | ||||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |  | ||||||
|   </ImportGroup> |  | ||||||
|   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||||||
|   </ImportGroup> |   </ImportGroup> | ||||||
| @@ -70,38 +43,12 @@ | |||||||
|     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> |     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||||||
|   </ImportGroup> |   </ImportGroup> | ||||||
|   <PropertyGroup Label="UserMacros" /> |   <PropertyGroup Label="UserMacros" /> | ||||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> |   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||||
|     <ClCompile> |     <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);../../src/lib-cpp/include</IncludePath> | ||||||
|       <WarningLevel>Level3</WarningLevel> |   </PropertyGroup> | ||||||
|       <SDLCheck>true</SDLCheck> |   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||||||
|       <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> |     <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);../../src/lib-cpp/include</IncludePath> | ||||||
|       <ConformanceMode>true</ConformanceMode> |   </PropertyGroup> | ||||||
|       <LanguageStandard>stdcpp20</LanguageStandard> |  | ||||||
|       <LanguageStandard_C>stdc17</LanguageStandard_C> |  | ||||||
|     </ClCompile> |  | ||||||
|     <Link> |  | ||||||
|       <SubSystem>Windows</SubSystem> |  | ||||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> |  | ||||||
|     </Link> |  | ||||||
|   </ItemDefinitionGroup> |  | ||||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> |  | ||||||
|     <ClCompile> |  | ||||||
|       <WarningLevel>Level3</WarningLevel> |  | ||||||
|       <FunctionLevelLinking>true</FunctionLevelLinking> |  | ||||||
|       <IntrinsicFunctions>true</IntrinsicFunctions> |  | ||||||
|       <SDLCheck>true</SDLCheck> |  | ||||||
|       <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions> |  | ||||||
|       <ConformanceMode>true</ConformanceMode> |  | ||||||
|       <LanguageStandard>stdcpp20</LanguageStandard> |  | ||||||
|       <LanguageStandard_C>stdc17</LanguageStandard_C> |  | ||||||
|     </ClCompile> |  | ||||||
|     <Link> |  | ||||||
|       <SubSystem>Windows</SubSystem> |  | ||||||
|       <EnableCOMDATFolding>true</EnableCOMDATFolding> |  | ||||||
|       <OptimizeReferences>true</OptimizeReferences> |  | ||||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> |  | ||||||
|     </Link> |  | ||||||
|   </ItemDefinitionGroup> |  | ||||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> |   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||||
|     <ClCompile> |     <ClCompile> | ||||||
|       <WarningLevel>Level3</WarningLevel> |       <WarningLevel>Level3</WarningLevel> | ||||||
| @@ -114,7 +61,11 @@ | |||||||
|     <Link> |     <Link> | ||||||
|       <SubSystem>Windows</SubSystem> |       <SubSystem>Windows</SubSystem> | ||||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> |       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||||
|  |       <AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);$(SolutionDir)../../target/debug/velopack_libc.dll.lib</AdditionalDependencies> | ||||||
|     </Link> |     </Link> | ||||||
|  |     <PostBuildEvent> | ||||||
|  |       <Command>xcopy /y "$(SolutionDir)..\..\target\debug\velopack_libc.dll" "$(OutDir)"</Command> | ||||||
|  |     </PostBuildEvent> | ||||||
|   </ItemDefinitionGroup> |   </ItemDefinitionGroup> | ||||||
|   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> |   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||||||
|     <ClCompile> |     <ClCompile> | ||||||
| @@ -132,7 +83,11 @@ | |||||||
|       <EnableCOMDATFolding>true</EnableCOMDATFolding> |       <EnableCOMDATFolding>true</EnableCOMDATFolding> | ||||||
|       <OptimizeReferences>true</OptimizeReferences> |       <OptimizeReferences>true</OptimizeReferences> | ||||||
|       <GenerateDebugInformation>true</GenerateDebugInformation> |       <GenerateDebugInformation>true</GenerateDebugInformation> | ||||||
|  |       <AdditionalDependencies>$(CoreLibraryDependencies);%(AdditionalDependencies);$(SolutionDir)../../target/release/velopack_libc.dll.lib</AdditionalDependencies> | ||||||
|     </Link> |     </Link> | ||||||
|  |     <PostBuildEvent> | ||||||
|  |       <Command>xcopy /y "$(SolutionDir)..\..\target\release\velopack_libc.dll" "$(OutDir)"</Command> | ||||||
|  |     </PostBuildEvent> | ||||||
|   </ItemDefinitionGroup> |   </ItemDefinitionGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ClCompile Include="VeloCppWinSample.cpp" /> |     <ClCompile Include="VeloCppWinSample.cpp" /> | ||||||
|   | |||||||
| @@ -14,16 +14,10 @@ | |||||||
|     <ClCompile Include="VeloCppWinSample.cpp"> |     <ClCompile Include="VeloCppWinSample.cpp"> | ||||||
|       <Filter>Source Files</Filter> |       <Filter>Source Files</Filter> | ||||||
|     </ClCompile> |     </ClCompile> | ||||||
|     <ClCompile Include="..\..\Velopack.cpp"> |  | ||||||
|       <Filter>Source Files</Filter> |  | ||||||
|     </ClCompile> |  | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ClInclude Include="constants.h"> |     <ClInclude Include="constants.h"> | ||||||
|       <Filter>Header Files</Filter> |       <Filter>Header Files</Filter> | ||||||
|     </ClInclude> |     </ClInclude> | ||||||
|     <ClInclude Include="..\..\Velopack.hpp"> |  | ||||||
|       <Filter>Header Files</Filter> |  | ||||||
|     </ClInclude> |  | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
| </Project> | </Project> | ||||||
| @@ -56,7 +56,7 @@ typedef void (*vpkc_hook_callback_t)(const char* pszAppVersion); | |||||||
| typedef enum { | typedef enum { | ||||||
|     UPDATE_AVAILABLE = 0, |     UPDATE_AVAILABLE = 0, | ||||||
|     NO_UPDATE_AVAILABLE = 1, |     NO_UPDATE_AVAILABLE = 1, | ||||||
|     ERROR = 2, |     UPDATE_ERROR = 2, | ||||||
| } vpkc_update_check_t; | } vpkc_update_check_t; | ||||||
|  |  | ||||||
| // !! AUTO-GENERATED-START C_TYPES | // !! AUTO-GENERATED-START C_TYPES | ||||||
| @@ -270,7 +270,7 @@ static inline UpdateOptions to_cpp(const vpkc_update_options_t& dto) { | |||||||
|  |  | ||||||
| class VelopackApp { | class VelopackApp { | ||||||
| private: | private: | ||||||
|     VelopackApp(); |     VelopackApp() {}; | ||||||
| public: | public: | ||||||
|     static VelopackApp Build() {  |     static VelopackApp Build() {  | ||||||
|         return VelopackApp();  |         return VelopackApp();  | ||||||
| @@ -380,7 +380,7 @@ public: | |||||||
|         vpkc_update_info_t update; |         vpkc_update_info_t update; | ||||||
|         vpkc_update_check_t result = vpkc_check_for_updates(&m_pManager, &update); |         vpkc_update_check_t result = vpkc_check_for_updates(&m_pManager, &update); | ||||||
|         switch (result) { |         switch (result) { | ||||||
|             case vpkc_update_check_t::ERROR: |             case vpkc_update_check_t::UPDATE_ERROR: | ||||||
|                 throw_last_error(); |                 throw_last_error(); | ||||||
|                 return std::nullopt; |                 return std::nullopt; | ||||||
|             case vpkc_update_check_t::NO_UPDATE_AVAILABLE: |             case vpkc_update_check_t::NO_UPDATE_AVAILABLE: | ||||||
| @@ -390,14 +390,15 @@ public: | |||||||
|                 vpkc_free_update_info(&update); |                 vpkc_free_update_info(&update); | ||||||
|                 return cpp_info; |                 return cpp_info; | ||||||
|         } |         } | ||||||
|  |         return std::nullopt; | ||||||
|     }; |     }; | ||||||
|     void DownloadUpdates(const UpdateInfo& update, vpkc_progress_callback_t progress) { |     void DownloadUpdates(const UpdateInfo& update, vpkc_progress_callback_t progress = nullptr) { | ||||||
|         vpkc_update_info_t vpkc_update = to_c(update); |         vpkc_update_info_t vpkc_update = to_c(update); | ||||||
|         if (!vpkc_download_updates(&m_pManager, &vpkc_update, progress)) { |         if (!vpkc_download_updates(&m_pManager, &vpkc_update, progress)) { | ||||||
|             throw_last_error(); |             throw_last_error(); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|     void WaitExitThenApplyUpdate(const VelopackAsset& asset, bool silent, bool restart, std::vector<std::string> restartArgs) { |     void WaitExitThenApplyUpdate(const VelopackAsset& asset, bool silent = false, bool restart = true, std::vector<std::string> restartArgs = {}) { | ||||||
|         char** pRestartArgs = new char*[restartArgs.size()]; |         char** pRestartArgs = new char*[restartArgs.size()]; | ||||||
|         for (size_t i = 0; i < restartArgs.size(); i++) { |         for (size_t i = 0; i < restartArgs.size(); i++) { | ||||||
|             pRestartArgs[i] = new char[restartArgs[i].size() + 1]; |             pRestartArgs[i] = new char[restartArgs[i].size() + 1]; | ||||||
| @@ -417,7 +418,7 @@ public: | |||||||
|             throw_last_error(); |             throw_last_error(); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|     void WaitExitThenApplyUpdate(const UpdateInfo& asset, bool silent, bool restart, std::vector<std::string> restartArgs) { |     void WaitExitThenApplyUpdate(const UpdateInfo& asset, bool silent = false, bool restart = true, std::vector<std::string> restartArgs = {}) { | ||||||
|         this->WaitExitThenApplyUpdate(asset.TargetFullRelease, silent, restart, restartArgs); |         this->WaitExitThenApplyUpdate(asset.TargetFullRelease, silent, restart, restartArgs); | ||||||
|     }; |     }; | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -287,7 +287,7 @@ VPKC_EXPORT vpkc_update_check_t VPKC_CALL vpkc_check_for_updates(vpkc_update_man | |||||||
|     } |     } | ||||||
|     catch (const std::exception& e) { |     catch (const std::exception& e) { | ||||||
|         set_last_error(e.what()); |         set_last_error(e.what()); | ||||||
|         return vpkc_update_check_t::ERROR; |         return vpkc_update_check_t::UPDATE_ERROR; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate, vpkc_progress_callback_t cbProgress) { | VPKC_EXPORT bool VPKC_CALL vpkc_download_updates(vpkc_update_manager_t* pManager, vpkc_update_info_t* pUpdate, vpkc_progress_callback_t cbProgress) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user