Editing a Fortnite configuration file using C++ and Win32
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:
- Have I deleted the system resources correctly? Any potential memory leaks? (brushes, fonts, etc)
- Do I need more error handling? Any improvements on what I do have?
- Should I separate the
GetFortniteConfiguration()
,SetFortniteConfiguration()
andCenterWindow()
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 "alphares.h"
#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_HEIGHT_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_WIDTH_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);
}
c++ performance beginner configuration
add a comment |
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:
- Have I deleted the system resources correctly? Any potential memory leaks? (brushes, fonts, etc)
- Do I need more error handling? Any improvements on what I do have?
- Should I separate the
GetFortniteConfiguration()
,SetFortniteConfiguration()
andCenterWindow()
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 "alphares.h"
#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_HEIGHT_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_WIDTH_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);
}
c++ performance beginner configuration
add a comment |
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:
- Have I deleted the system resources correctly? Any potential memory leaks? (brushes, fonts, etc)
- Do I need more error handling? Any improvements on what I do have?
- Should I separate the
GetFortniteConfiguration()
,SetFortniteConfiguration()
andCenterWindow()
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 "alphares.h"
#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_HEIGHT_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_WIDTH_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);
}
c++ performance beginner configuration
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:
- Have I deleted the system resources correctly? Any potential memory leaks? (brushes, fonts, etc)
- Do I need more error handling? Any improvements on what I do have?
- Should I separate the
GetFortniteConfiguration()
,SetFortniteConfiguration()
andCenterWindow()
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 "alphares.h"
#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_HEIGHT_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_WIDTH_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);
}
c++ performance beginner configuration
c++ performance beginner configuration
edited 10 mins ago
Jamal♦
30.2k11116226
30.2k11116226
asked 13 mins ago
braycarlson
237
237
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210093%2fediting-a-fortnite-configuration-file-using-c-and-win32%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210093%2fediting-a-fortnite-configuration-file-using-c-and-win32%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown