Skip to content

Instantly share code, notes, and snippets.

@Justasic
Created July 26, 2014 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justasic/a176586c337b3a901c74 to your computer and use it in GitHub Desktop.
Save Justasic/a176586c337b3a901c74 to your computer and use it in GitHub Desktop.
Who needs C++ standards! Undefined behavior is way more fun!
void HandleEvents(HWND hwnd, UINT message, WPARAM wParam, LPARAM lparam)
{
switch (LOWORD(wParam))
{
case 1: // "Scan" button
{
//MessageBox(NULL, L"Scan button", L"You clicked scan", MB_OK);
// Disable the window, get the text from it, then start the scan threads.
HWND txtbox = GetDlgItem(hwnd, 8);
Edit_Enable(txtbox, FALSE);
// Plenty of space. Get the damn string.
#if 1
std::wstring str;
str.reserve(1 << 16);
// This is HORRIBLY undefined behavior to copy directly into the buffer
// but whatever. I want to be lazy. If it breaks then I'll fix it correctly.
// Technically, this should be valid but the C++ standards commitee decided
// that .data() shouldn't do the below but rather should return the exact
// same thing as .c_str(). Fuck that shit.
if (GetDlgItemText(hwnd, 8, (wchar_t*)str.data(), str.capacity()) == -1)
{
MessageBox(NULL, L"Failed to get text!", L"Error", MB_OK | MB_ICONERROR);
Edit_Enable(txtbox, TRUE);
UpdateActivityLabel(L"Ready.");
break;
}
// Nasty hack for the windows libc++
#ifdef _MSC_VER
str._Mysize = wcslen((wchar_t*)str.data());
#endif
#else // This is the stupid and not-cool but correct version of the above.
wchar_t buf[(1 << 16)];
if (GetDlgItemText(hwnd, 8, buf, (1 << 16)) == -1)
{
MessageBox(NULL, L"Failed to get text!", L"Error", MB_OK | MB_ICONERROR);
Edit_Enable(txtbox, TRUE);
UpdateActivityLabel(L"Ready.");
break;
}
std::wstring str = buf;
#endif
// Deallocate any unneeded space
//str.shrink_to_fit();
// Add our first function call to the thread pool and submit.
UpdateActivityLabel(L"Queueing function...");
th->AddQueue(CheckDirectory, _wcsdup( str.c_str()));
th->Submit();
UpdateActivityLabel(L"Waiting on threads...");
break;
}
case 2: // "Cancel" button
MessageBox(NULL, L"Cancel Button", L"You clicked cancel!", MB_OK);
break;
case 3: // Listbox root window
break;
case 4: // File menu item "Show message"
break;
case 5: // File menu item "Exit"
// Destroy the thread pool
if (th)
delete th;
// Tell windows "Bye!"
PostQuitMessage(0);
break;
default:
// We don't have a handler for this item.
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment