To modify your existing application so that the success message is displayed within the current window instead of using a pop-up message box, you will need to change how you present messages to the user. Instead of using MessageBox, you can create a static text control that will display the status of the optimization.
Here's a revised version of your code where I've added a static text control to the existing window for displaying messages:
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <vector>
#include <string>
#define ID_START_BUTTON 1
#define ID_STATUS_TEXT 2 // Identifier for the status text control
// Function prototypes
DWORD_PTR GetAffinityMaskForAllCoresExceptCore0();
bool CheckForRunningProcesses(const std::vector<std::wstring>& processNames);
void CheckAndModifyProcesses(const std::vector<std::wstring>& processNames, HWND statusTextHandle);
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnStartButtonClick(HWND hwnd, HWND statusTextHandle);
// Main entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Remove console window
FreeConsole();
std::vector<std::wstring> processNames = {
L"MuMuPlayer.exe",
L"MuMuVMMHeadless.exe",
L"Ld9BoxHeadless.exe",
L"Ld9BoxSVC.exe",
L"dnplayer.exe",
L"HD-Player.exe"
};
// Step: Check for running processes before creating the window
if (!CheckForRunningProcesses(processNames)) {
MessageBox(NULL, "No emulator found.", "Error", MB_ICONERROR);
return 0; // Exit the application if no emulators are found
}
const char CLASS_NAME[] = "EmulatorOptimizer";
// Register the window class
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"Emulator Optimizer", // Changed title to "Emulator Optimizer"
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL,
NULL,
hInstance,
NULL);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Create the Apply Emulator Optimization button
CreateWindow(
"BUTTON", // Predefined class; Unicode assumed
"Apply Emulator Optimization", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
50, // x position
50, // y position
200, // Button width
30, // Button height
hwnd, // Parent window
(HMENU)ID_START_BUTTON, // Control identifier
(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
NULL); // Pointer not needed
// Create static text for status messages
HWND statusTextHandle = CreateWindow(
"STATIC", // Predefined class; Unicode assumed
"", // Initial text
WS_VISIBLE | WS_CHILD | SS_CENTER, // Styles
50, // x position
100, // y position
200, // width
30, // height
hwnd, // Parent window
(HMENU)ID_STATUS_TEXT, // Control identifier
(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
NULL); // Pointer not needed
// Message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// Windows Procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static HWND statusTextHandle; // Add a static handle for the status text control
switch (uMsg) {
case WM_CREATE:
// Store the status text handle
statusTextHandle = GetDlgItem(hwnd, ID_STATUS_TEXT);
break;
case WM_COMMAND: {
if (LOWORD(wParam) == ID_START_BUTTON) {
OnStartButtonClick(hwnd, statusTextHandle);
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
// Function to calculate affinity mask for all cores except core 0
DWORD_PTR GetAffinityMaskForAllCoresExceptCore0() {
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
DWORD_PTR mask = 0;
// Set bits for all cores except core 0
for (DWORD i = 1; i < sysInfo.dwNumberOfProcessors; i++) {
mask |= (1ULL << i);
}
return mask;
}
// Function to check if any of the specified processes are running
bool CheckForRunningProcesses(const std::vector<std::wstring>& processNames) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return false;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
bool found = false;
if (Process32First(hSnapshot, &pe32)) {
do {
std::wstring exeFileName(pe32.szExeFile, pe32.szExeFile + strlen(pe32.szExeFile));
for (const auto& processName : processNames) {
if (exeFileName == processName) {
found = true;
break;
}
}
if (found) break;
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
return found;
}
// Function to monitor and modify specific processes
void CheckAndModifyProcesses(const std::vector<std::wstring>& processNames, HWND statusTextHandle) {
DWORD_PTR affinityMask = GetAffinityMaskForAllCoresExceptCore0();
bool optimized = false; // Flag to check if optimization was successful
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
SetWindowText(statusTextHandle, L"Failed to create process snapshot.");
return;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
std::wstring exeFileName(pe32.szExeFile, pe32.szExeFile + strlen(pe32.szExeFile));
for (const auto& processName : processNames) {
if (exeFileName == processName) {
HANDLE hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, pe32.th32ProcessID);
if (hProcess) {
if (SetProcessAffinityMask(hProcess, affinityMask)) {
optimized = true; // Set flag if at least one process optimization was successful
}
CloseHandle(hProcess);
}
}
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
// Show message if any process was optimized
if (optimized) {
SetWindowText(statusTextHandle, L"Emulator optimized successfully.");
} else {
SetWindowText(statusTextHandle, L"No processes were optimized.");
}
}
// Event handler for button click
void OnStartButtonClick(HWND hwnd, HWND statusTextHandle) {
std::vector<std::wstring> processNames = {
L"MuMuPlayer.exe",
L"MuMuVMMHeadless.exe",
L"Ld9BoxHeadless.exe",
L"Ld9BoxSVC.exe",
L"dnplayer.exe",
L"HD-Player.exe"
};
// Check and modify processes
CheckAndModifyProcesses(processNames, statusTextHandle);
}
Changes Made:
-
Added Status Text Control: A static text control has been created to display messages regarding the optimization process. This replaces the use of MessageBox.
-
Updated the CheckAndModifyProcesses Function: Instead of calling MessageBox, the code updates the text of the status control with the results of the optimization (either success or failure).
-
Maintained a handle for Status Text: An additional parameter HWND statusTextHandle is passed to update the text control when the button is clicked.
With these changes, users will see the success or error messages in the application window itself, enhancing the user experience.