3
\$\begingroup\$

I have made a small and simple tool for Windows using C++ and the Win32 API to allow people to change their resolution in Fortnite, without having to search for the configuration file and manually edit it themselves. The program has a width and height input for the user to enter their desired resolution, and an apply button to save the changes to the configuration file. I have tested it on a few machines, and it seems to work as expected.

This is my first attempt at using C++ or the Win32 API, and I would love to release this project to the Fortnite community, but I want ensure there are no glaring issues with it.

My questions/concerns are:

  1. Have I deleted the system resources correctly? Any potential memory leaks? (brushes, fonts, etc)
  2. Do I need more error handling? Any improvements on what I do have?
  3. Should I separate the GetFortniteConfiguration(), SetFortniteConfiguration() and CenterWindow() functions into their own file or is the project small enough to keep it as is?

I will also be adding comments to the code shortly, but any other tips, suggestions or learning resources are welcome.

#include <stdio.h>
#include <sys/stat.h>
#include <windows.h>
#include <string>
#include "resources.h"
#include "simpleini/simpleini.h"
LRESULT CALLBACK WindowProc(
 HWND hwnd,
 UINT message,
 WPARAM wParam,
 LPARAM lParam);
void CenterWindow(HWND window, DWORD style, DWORD exStyle) {
 int screen_width = GetSystemMetrics(SM_CXSCREEN);
 int screen_height = GetSystemMetrics(SM_CYSCREEN);
 RECT client_rect;
 GetClientRect(window, &client_rect);
 AdjustWindowRectEx(&client_rect, style, FALSE, exStyle);
 int client_width = client_rect.right - client_rect.left;
 int client_height = client_rect.bottom - client_rect.top;
 SetWindowPos(window, NULL,
 screen_width / 2 - client_width / 2,
 screen_height / 2 - client_height / 2,
 client_width, client_height, 0);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
 const wchar_t CLASS_NAME[] = L"alphares";
 const wchar_t WINDOW_NAME[] = L"alphares";
 WNDCLASS wc = { };
 MSG message = { };
 wc.lpfnWndProc = WindowProc;
 wc.hInstance = hInstance;
 wc.hbrBackground = CreateSolidBrush(RGB(43, 45, 92));
 wc.hIcon = LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDR_ICON));
 wc.lpszClassName = CLASS_NAME;
 if (!RegisterClass(&wc)) {
 return 0;
 }
 HWND hwnd = CreateWindowEx(
 0,
 CLASS_NAME,
 WINDOW_NAME,
 WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX^WS_THICKFRAME,
 CW_USEDEFAULT, CW_USEDEFAULT, 250, 150,
 NULL,
 NULL,
 hInstance,
 NULL);
 CenterWindow(hwnd, WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX^WS_THICKFRAME, 0);
 if (hwnd == NULL) {
 return 0;
 }
 ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);
 while (GetMessage(&message, NULL, 0, 0)) {
 if (!IsDialogMessage(hwnd, &message)) {
 TranslateMessage(&message);
 DispatchMessage(&message);
 }
 }
 return 0;
}
LPCSTR GetFortniteConfiguration() {
 char *path;
 size_t length;
 _dupenv_s(&path, &length, "LOCALAPPDATA");
 std::string fortnite = "\\FortniteGame\\Saved\\Config\\WindowsClient\\GameUserSettings.ini";
 std::string fullpath = path + fortnite;
 free(path);
 return fullpath.c_str();
}
void SetFortniteConfiguration(LPCSTR file, int user_width, int user_height) {
 std::string width_string = std::to_string(user_width);
 std::string height_string = std::to_string(user_height);
 char const *width = width_string.c_str();
 char const *height = height_string.c_str();
 DWORD attributes = GetFileAttributesA(file);
 if (attributes & FILE_ATTRIBUTE_READONLY) {
 attributes &= ~FILE_ATTRIBUTE_READONLY;
 SetFileAttributesA(file, attributes);
 }
 const char *section = "/Script/FortniteGame.FortGameUserSettings";
 CSimpleIniA ini;
 ini.SetSpaces(false);
 ini.SetUnicode();
 ini.LoadFile(file);
 ini.SetValue(section, "ResolutionSizeX", width);
 ini.SetValue(section, "ResolutionSizeY", height);
 ini.SetValue(section, "LastUserConfirmedResolutionSizeX", width);
 ini.SetValue(section, "LastUserConfirmedResolutionSizeY", height);
 ini.SetValue(section, "DesiredScreenWidth", width);
 ini.SetValue(section, "DesiredScreenHeight", height);
 ini.SetValue(section, "LastUserConfirmedDesiredScreenWidth", width);
 ini.SetValue(section, "LastUserConfirmedDesiredScreenHeight", height);
 ini.SaveFile(file);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
 NONCLIENTMETRICS ncm;
 ncm.cbSize = sizeof(ncm);
 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
 HINSTANCE hInstance;
 static HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont);
 static HBRUSH hBrushStatic = CreateSolidBrush(RGB(43, 45, 92));
 static HBRUSH hBrushEdit = CreateSolidBrush(RGB(35, 35, 79));
 static HBRUSH hBrushButton = CreateSolidBrush(RGB(93, 107, 238));
 switch (message) {
 case WM_CREATE:
 hInstance = GetModuleHandle(nullptr);
 CreateWindowEx(
 NULL,
 TEXT("Static"),
 TEXT("Width"),
 WS_CHILD | WS_VISIBLE | ES_CENTER,
 50, 15, 60, 20,
 hwnd,
 (HMENU)IDC_WIDTH_LABEL,
 hInstance,
 NULL);
 SendMessage(
 GetDlgItem(hwnd, IDC_WIDTH_LABEL),
 WM_SETFONT,
 (WPARAM)hFont,
 TRUE);
 CreateWindowEx(
 NULL,
 TEXT("Static"),
 TEXT("Height"),
 WS_CHILD | WS_VISIBLE | ES_CENTER,
 125, 15, 60, 20,
 hwnd,
 (HMENU)IDC_HEIGHT_LABEL,
 hInstance,
 NULL);
 SendMessage(
 GetDlgItem(hwnd, IDC_HEIGHT_LABEL),
 WM_SETFONT,
 (WPARAM)hFont,
 TRUE);
 CreateWindowEx(
 NULL,
 TEXT("Edit"),
 TEXT("1920"),
 WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_CENTER | WS_TABSTOP,
 50, 35, 60, 15,
 hwnd,
 (HMENU)IDC_WIDTH_EDIT,
 hInstance,
 NULL);
 SendMessage(
 GetDlgItem(hwnd, IDC_WIDTH_EDIT),
 WM_SETFONT,
 (WPARAM)hFont,
 TRUE);
 SendMessage(
 GetDlgItem(hwnd, IDC_WIDTH_EDIT),
 EM_SETLIMITTEXT,
 4, 0);
 CreateWindowEx(
 NULL,
 TEXT("Edit"),
 TEXT("1080"),
 WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_CENTER | WS_TABSTOP,
 125, 35, 60, 15,
 hwnd,
 (HMENU)IDC_HEIGHT_EDIT,
 hInstance,
 NULL);
 SendMessage(
 GetDlgItem(hwnd, IDC_HEIGHT_EDIT),
 WM_SETFONT,
 (WPARAM)hFont,
 TRUE);
 SendMessage(
 GetDlgItem(hwnd, IDC_HEIGHT_EDIT),
 EM_SETLIMITTEXT,
 4, 0);
 CreateWindowEx(
 NULL,
 TEXT("Button"),
 TEXT("Apply"),
 WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
 50, 65, 135, 25,
 hwnd,
 (HMENU)IDC_APPLY_BUTTON,
 hInstance,
 NULL);
 SendMessage(
 GetDlgItem(hwnd, IDC_APPLY_BUTTON),
 WM_SETFONT,
 (WPARAM)hFont,
 TRUE);
 break;
 case WM_CTLCOLORSTATIC:
 {
 HDC hdcStatic = (HDC)wParam;
 SetTextColor(hdcStatic, RGB(93, 107, 238));
 SetBkColor(hdcStatic, RGB(43, 45, 92));
 return (INT_PTR)hBrushStatic;
 }
 case WM_CTLCOLOREDIT:
 {
 HDC hdcEdit = (HDC)wParam;
 SetTextColor(hdcEdit, RGB(255, 255, 255));
 SetBkColor(hdcEdit, RGB(35, 35, 79));
 return (INT_PTR)hBrushEdit;
 }
 case WM_CTLCOLORBTN:
 {
 HDC hdcButton = (HDC)wParam;
 SetTextColor(hdcButton, RGB(255, 255, 255));
 SetBkColor(hdcButton, RGB(93, 107, 238));
 return (INT_PTR)hBrushButton;
 }
 case WM_COMMAND:
 if (LOWORD(wParam) == IDC_APPLY_BUTTON) {
 LPCSTR file = GetFortniteConfiguration();
 struct stat buffer;
 if (stat(file, &buffer) == 0) {
 BOOL success;
 int width = GetDlgItemInt(
 hwnd,
 IDC_WIDTH_EDIT,
 &success,
 FALSE);
 int height = GetDlgItemInt(
 hwnd,
 IDC_HEIGHT_EDIT,
 &success,
 FALSE);
 if (success == TRUE) {
 SetFortniteConfiguration(file, width, height);
 MessageBoxA(
 hwnd,
 "Your resolution was successfully saved.",
 "Success",
 MB_OK);
 } else {
 MessageBoxA(
 hwnd,
 "Please enter a resolution.",
 "Warning",
 MB_OK | MB_ICONWARNING);
 }
 } else {
 MessageBoxA(
 hwnd,
 "There was an error finding your configuration file.",
 "Error",
 MB_OK | MB_ICONERROR);
 }
 }
 break;
 case WM_DRAWITEM:
 if (wParam == IDC_APPLY_BUTTON) {
 LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT)lParam;
 RECT rect = pdis->rcItem;
 DrawTextA(
 pdis->hDC,
 "Apply",
 5,
 &rect,
 DT_CENTER | DT_SINGLELINE | DT_VCENTER);
 return TRUE;
 }
 break;
 case WM_DESTROY:
 DeleteObject(hFont);
 DeleteObject(hBrushStatic);
 DeleteObject(hBrushEdit);
 DeleteObject(hBrushButton);
 PostQuitMessage(0);
 return 0;
 case WM_PAINT:
 {
 PAINTSTRUCT ps;
 HDC hdc = BeginPaint(hwnd, &ps);
 EndPaint(hwnd, &ps);
 break;
 }
 return 0;
 }
 return DefWindowProc(hwnd, message, wParam, lParam);
}
Emily L.
16.7k1 gold badge39 silver badges89 bronze badges
asked Dec 21, 2018 at 3:01
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I gave it a quick glance the one thing that stood out is:

LPCSTR GetFortniteConfiguration() {
 char *path;
 size_t length;
 _dupenv_s(&path, &length, "LOCALAPPDATA");
 std::string fortnite = "\\FortniteGame\\Saved\\Config\\WindowsClient\\GameUserSettings.ini";
 std::string fullpath = path + fortnite;
 free(path);
 return fullpath.c_str();
}

The variable fullpath is destroyed and the memory released when you leave the function, so the pointer you return is dangling and points to freed memory. It only works because that memory is not overwritten in the short time your program lives. Of course this is random and may result in "random crashes".

answered Dec 22, 2018 at 9:48
\$\endgroup\$
2
  • \$\begingroup\$ Thanks Emily. Is it better if I return fullpath as a std::string, and then use c_str() on the return value in SetFortniteConfiguration and the WM_COMMAND case? For example: std::string path = GetFortniteConfiguration(); and then LPCSTR file = path.c_str(); \$\endgroup\$ Commented Dec 22, 2018 at 18:50
  • \$\begingroup\$ Return std::string and make sure it stays alive for the whole time that the LPCSTR is used. \$\endgroup\$ Commented Dec 22, 2018 at 19:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.