The call to sprintf in the posted code is printing 8 characters to a 1 character buffer. The buffer overrun has corrupted the stack. The destination buffer should be at least 9 characters, 8 for the text and 1 for the null terminator.
I get the error (stack around the variable buf was corrupted) after the message box, how do I fix it?

How do I fix the error (stack around the variable "buf" was corrupted)
The code:
if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) != IDM_ABOUT && LOWORD(wParam) != IDM_EXIT) {
GetWindowTextA(hWndEdit, &fin[0], 64300);
GetWindowTextA(hWndEdit2, &scin[0], 64300);
if (scin == (LPSTR)"") {
ShellExecuteA(NULL, "runas", &fin[0], NULL, NULL, SW_SHOWNORMAL);
}
else {
ShellExecuteA(NULL, "runas", &fin[0], &scin[0], NULL, SW_SHOWNORMAL);
}
if (GetLastError() != 0) {
char buf;
sprintf(&buf, "%08x", (int)GetLastError());
std::string res(&buf);
DWORD le = GetLastError();
std::string message = (std::string)"APC Administrative Application Execution Utility encountured an error of code 0x" + res + ": " + "\"" + std::system_category().message(GetLastError()); +"\"";
int lastError = GetLastError();
std::wstring hexerror = L"0x" + WCHAR(DecToHex(GetLastError()));
const char* HEs = message.c_str();
MessageBoxA(hWnd, LPCSTR(message.c_str()), "APC Administrative Application Execution Utility", MB_ICONERROR);
}
It says:
Run-Time Check Failure #2 - Stack around the variable 'buf' was corrupted.
Image:
How to fix this problem and without causing errors?
1 additional answer
Sort by: Most helpful
-
Ahmed Osama 120 Reputation points
2023-03-01T15:26:45.4033333+00:00 Here's the final executable:
https://1drv.ms/u/s!AmKkgU04nxaruFZRxwdLcKkmHsPR?e=Gatu9S
WARNING: ALL THE FILES CONTAINED ARE THE INTELLECTUAL PROPERTY RIGHTS AND COPYRIGHTS OF ME, ANY REVERSE ENGINNIRING, COPYING, MODIFIYING, OR OTHERWISE TRANSFERRING THE EXECUTABLE OR MODIFIYING IT OR MOVING THE EXECUTABLE IN ANY WAY, OR REVERSE ENGINNIRING OR TRANSFERRING THE SOURCE FORM OF THE EXECUTABLE, NON-COMPILED WITHOUT THE EXPRESS WRITTEN PERMISSION OF ME, ANY THING OF THOSE OBLIGATIONS IF MADE WITHOUT EXPRESS WRITTEN PERMISSION, IT WOULD BE CONSIDERED COPYRIGHT INFIRGMENT, VIOLATORS WILL BE PROSECUTED TO THE MAXIUMUM EXTENT POSSIBLE ALLOWED BY LAW.