From ef39b4d85f38620b335e5b9137a6e64f387b8949 Mon Sep 17 00:00:00 2001 From: programmer2514 <43104632+programmer2514@users.noreply.github.com> Date: Tue, 10 Jan 2023 19:04:13 -0500 Subject: [PATCH] Use wcout everywhere to avoid undefined behavior --- DPEdit/DPEdit/DPEdit.cpp | 72 ++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/DPEdit/DPEdit/DPEdit.cpp b/DPEdit/DPEdit/DPEdit.cpp index 6ec90d5..fca0735 100644 --- a/DPEdit/DPEdit/DPEdit.cpp +++ b/DPEdit/DPEdit/DPEdit.cpp @@ -1,5 +1,5 @@ /*-------------------------------------------------------------- - | Display Position Editor v1.3.0 | + | Display Position Editor v1.3.1 | | By Benjamin J. Pryor | |--------------------------------------------------------------| | A simple command line utility to accurately set the relative | @@ -49,7 +49,7 @@ void set_display_pos(int argc, char** argv) { if (is_valid_int(argv[(3 * i) + 1]) && is_valid_int(argv[(3 * i) + 2]) && is_valid_int(argv[(3 * i) + 3])) { - cout << endl << "Applying position {" << argv[(3 * i) + 2] << ", " << argv[(3 * i) + 3] << "} to Display #" << argv[(3 * i) + 1] << "..." << endl; + wcout << endl << "Applying position {" << argv[(3 * i) + 2] << ", " << argv[(3 * i) + 3] << "} to Display #" << argv[(3 * i) + 1] << "..." << endl; if (EnumDisplayDevicesW(NULL, stoi(argv[(3 * i) + 1]) - 1, &dmInfo, EDD_GET_DEVICE_INTERFACE_NAME) != 0) { @@ -60,18 +60,18 @@ void set_display_pos(int argc, char** argv) { dmMode.dmPosition.y = stoi(argv[(3 * i) + 3]); if (ChangeDisplaySettingsExW(dmInfo.DeviceName, &dmMode, NULL, CDS_GLOBAL | CDS_UPDATEREGISTRY, NULL) == DISP_CHANGE_SUCCESSFUL) - cout << "Done!" << endl << endl; - else cout << "Operation failed! Unable to write to display settings." << endl << "Skipping..." << endl << endl; + wcout << "Done!" << endl << endl; + else wcout << "Operation failed! Unable to write to display settings." << endl << "Skipping..." << endl << endl; } - else cout << "Operation failed! Unable to read display settings." << endl << "Skipping..." << endl << endl; + else wcout << "Operation failed! Unable to read display settings." << endl << "Skipping..." << endl << endl; } - else cout << "Operation failed! Unable to connect to display." << endl << "Skipping..." << endl << endl; + else wcout << "Operation failed! Unable to connect to display." << endl << "Skipping..." << endl << endl; } else { - cout << "Invalid argument(s) detected!" << endl; - cout << "Use 'dpedit.exe /?' for more info" << endl << endl; + wcout << "Invalid argument(s) detected!" << endl; + wcout << "Use 'dpedit.exe /?' for more info" << endl << endl; } } } @@ -80,45 +80,45 @@ void list_displays(void) { DISPLAY_DEVICE displayDevice{ sizeof displayDevice, }; for (int i = 0; EnumDisplayDevices(nullptr, i, &displayDevice, EDD_GET_DEVICE_INTERFACE_NAME); i++) { - cout << endl; - cout << "Display #" << i + 1 << endl; + wcout << endl; + wcout << "Display #" << i + 1 << endl; wcout << "Device name: " << displayDevice.DeviceName << endl; wcout << "Device string: " << displayDevice.DeviceString << endl; - cout << "Active: " << (displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE) << endl; - cout << "Mirroring: " << (displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) << endl; - cout << "Modes pruned: " << (displayDevice.StateFlags & DISPLAY_DEVICE_MODESPRUNED) << endl; - cout << "Primary: " << (displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) << endl; - cout << "Removable: " << (displayDevice.StateFlags & DISPLAY_DEVICE_REMOVABLE) << endl; - cout << "VGA compatible: " << (displayDevice.StateFlags & DISPLAY_DEVICE_VGA_COMPATIBLE) << endl; + wcout << "Active: " << (displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE) << endl; + wcout << "Mirroring: " << (displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) << endl; + wcout << "Modes pruned: " << (displayDevice.StateFlags & DISPLAY_DEVICE_MODESPRUNED) << endl; + wcout << "Primary: " << (displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) << endl; + wcout << "Removable: " << (displayDevice.StateFlags & DISPLAY_DEVICE_REMOVABLE) << endl; + wcout << "VGA compatible: " << (displayDevice.StateFlags & DISPLAY_DEVICE_VGA_COMPATIBLE) << endl; DEVMODE devMode{ {}, {}, {}, sizeof devMode, 0, }; if (EnumDisplaySettings(displayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &devMode)) { - cout << "Dimensions: {" << devMode.dmPelsWidth << ", " << devMode.dmPelsHeight << "}" << endl; - cout << "Position: {" << devMode.dmPosition.x << ", " << devMode.dmPosition.y << "}" << endl; + wcout << "Dimensions: {" << devMode.dmPelsWidth << ", " << devMode.dmPelsHeight << "}" << endl; + wcout << "Position: {" << devMode.dmPosition.x << ", " << devMode.dmPosition.y << "}" << endl; } } } void show_help(void) { - cout << endl << "DPEdit 1.3.0" << endl; - cout << "A command line utility to accurately position displays in a multi-monitor setup." << endl << endl; - cout << "Usage: dpedit.exe [/H] [/?]" << endl; - cout << " dpedit.exe /L" << endl; - cout << " dpedit.exe [ ] ..." << endl << endl; - cout << " Options:" << endl << endl; - cout << " /H, /? Shows this help page" << endl; - cout << " /L Lists all displays and their indices" << endl; - cout << " The index of the display to position" << endl; - cout << " The X (horizontal) position of the top-left corner of display ." << endl; - cout << " The Y (vertical) position of the top-left corner of display ." << endl << endl; - cout << "Example: dpedit.exe 1 0 0 2 -1920 21" << endl; - cout << " Moves Display #1 to coords {0, 0} and positions Display #2 to the left of" << endl; - cout << " and 21 pixels lower than Display #1 (coords {-1920, 21}). This example assumes" << endl; - cout << " Display #2 to be 1080p." << endl << endl; - cout << "Notes: This utility should work for any number and any size(s) of monitors." << endl; - cout << " The display numbers do not need to be in order." << endl << endl; - cout << "THIS UTILITY MODIFIES THE REGISTRY! USE AT YOUR OWN RISK!" << endl; + wcout << endl << "DPEdit 1.3.1" << endl; + wcout << "A command line utility to accurately position displays in a multi-monitor setup." << endl << endl; + wcout << "Usage: dpedit.exe [/H] [/?]" << endl; + wcout << " dpedit.exe /L" << endl; + wcout << " dpedit.exe [ ] ..." << endl << endl; + wcout << " Options:" << endl << endl; + wcout << " /H, /? Shows this help page" << endl; + wcout << " /L Lists all displays and their indices" << endl; + wcout << " The index of the display to position" << endl; + wcout << " The X (horizontal) position of the top-left corner of display ." << endl; + wcout << " The Y (vertical) position of the top-left corner of display ." << endl << endl; + wcout << "Example: dpedit.exe 1 0 0 2 -1920 21" << endl; + wcout << " Moves Display #1 to coords {0, 0} and positions Display #2 to the left of" << endl; + wcout << " and 21 pixels lower than Display #1 (coords {-1920, 21}). This example assumes" << endl; + wcout << " Display #2 to be 1080p." << endl << endl; + wcout << "Notes: This utility should work for any number and any size(s) of monitors." << endl; + wcout << " The display numbers do not need to be in order." << endl << endl; + wcout << "THIS UTILITY MODIFIES THE REGISTRY! USE AT YOUR OWN RISK!" << endl; } bool is_valid_int(string s) {