Skip to content

Commit

Permalink
Use wcout everywhere to avoid undefined behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer2514 committed Jan 11, 2023
1 parent 10185b2 commit ef39b4d
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions DPEdit/DPEdit/DPEdit.cpp
Original file line number Diff line number Diff line change
@@ -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 |
Expand Down Expand Up @@ -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) {

Expand All @@ -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;
}
}
}
Expand All @@ -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 <displayNum> <xPos> <yPos> [<displayNum2> <xPos2> <yPos2>] ..." << endl << endl;
cout << " Options:" << endl << endl;
cout << " /H, /? Shows this help page" << endl;
cout << " /L Lists all displays and their indices" << endl;
cout << " <displayNum> The index of the display to position" << endl;
cout << " <xPos> The X (horizontal) position of the top-left corner of display <displayNum>." << endl;
cout << " <YPos> The Y (vertical) position of the top-left corner of display <displayNum>." << 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 <displayNum> <xPos> <yPos> [<displayNum2> <xPos2> <yPos2>] ..." << endl << endl;
wcout << " Options:" << endl << endl;
wcout << " /H, /? Shows this help page" << endl;
wcout << " /L Lists all displays and their indices" << endl;
wcout << " <displayNum> The index of the display to position" << endl;
wcout << " <xPos> The X (horizontal) position of the top-left corner of display <displayNum>." << endl;
wcout << " <YPos> The Y (vertical) position of the top-left corner of display <displayNum>." << 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) {
Expand Down

0 comments on commit ef39b4d

Please sign in to comment.