Advertisement
Combreal

warningCheck.cpp

May 6th, 2020
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. //How to use :
  2. //warningCheck.exe arg1 arg2 arg3
  3. //arg1 window's title
  4. //arg2 message
  5. //arg3 marker's path
  6. //Exemple :
  7. //warningCheck.exe "WSL feature was installed and needs reboot to complete" "Restart now?" "C:\Windows\Temp\warningCheckMarker"
  8.  
  9. #include <windows.h>
  10. #include <string>
  11. #include "Shlwapi.h"
  12. #include "resource.h"
  13.  
  14. #pragma comment(lib, "Shlwapi.lib")
  15.  
  16. #define ID_CAPTIONSTATICBLANK 1
  17. #define ID_CAPTIONSTATIC 2
  18. #define ID_YESBUTTON 11
  19. #define ID_NOBUTTON 12
  20.  
  21. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  22. HANDLE hwndYesButton;
  23. HANDLE hwndNoButton;
  24. HANDLE hwndCaptionStaticBlank;
  25. HANDLE hwndCaptionStatic;
  26. HWND mainWindow;
  27. std::wstring windowsTitle;
  28. std::wstring captionText;
  29. std::wstring markerPath;
  30. int xSize, ySize;
  31. std::wstring yesText;
  32. std::wstring noText;
  33.  
  34. LPWSTR stringToLPWSTR(const std::string& instr);
  35.  
  36. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  37. {
  38.     LANGID language = GetUserDefaultUILanguage();
  39.     if (language == 1036)
  40.     {
  41.         yesText = L"Oui";
  42.         noText = L"Non";
  43.     }
  44.     else
  45.     {
  46.         yesText = L"Yes";
  47.         noText = L"No";
  48.     }
  49.     RECT desktop;
  50.     const HWND hDesktop = GetDesktopWindow();
  51.     GetWindowRect(hDesktop, &desktop);
  52.     xSize = desktop.right;
  53.     ySize = desktop.bottom;
  54.     LPWSTR *szArgList;
  55.     int argCount;
  56.     szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
  57.     if (szArgList == NULL)
  58.     {
  59.         MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK | MB_ICONEXCLAMATION);
  60.         return 10;
  61.     }
  62.     if (szArgList[1] != NULL)
  63.     {
  64.         windowsTitle = szArgList[1];
  65.     }
  66.     else
  67.     {
  68.         windowsTitle = L"windowsTitle";
  69.     }
  70.     if (szArgList[2] != NULL && argCount >= 3)
  71.     {
  72.         captionText = szArgList[2];
  73.     }
  74.     else
  75.     {
  76.         captionText = L"captionText";
  77.     }
  78.     if (szArgList[3] != NULL && argCount >= 4)
  79.     {
  80.         markerPath = szArgList[3];
  81.     }
  82.     else
  83.     {
  84.         markerPath = stringToLPWSTR(getenv("TEMP"));
  85.         markerPath.append(L"\\warningCheckMarker");
  86.     }
  87.     LocalFree(szArgList);
  88.     MSG msg;
  89.     WNDCLASSW wc = { 0 };
  90.     wc.lpszClassName = L"cdsMsg";
  91.     wc.hInstance = hInstance;
  92.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  93.     wc.lpfnWndProc = WndProc;
  94.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  95.     wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  96.     RegisterClassW(&wc);
  97.     mainWindow = CreateWindowW(wc.lpszClassName, windowsTitle.c_str(), WS_CAPTION | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPED | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, xSize - 400, ySize - 200, 400, 126, 0, 0, hInstance, 0);
  98.     while (GetMessage(&msg, NULL, 0, 0))
  99.     {
  100.         TranslateMessage(&msg);
  101.         DispatchMessage(&msg);
  102.     }
  103.     return (int)msg.wParam;
  104. }
  105.  
  106. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  107. {
  108.  
  109.  
  110.     switch (msg)
  111.     {
  112.     case WM_CREATE:
  113.         hwndCaptionStatic = CreateWindowEx(0, L"STATIC", captionText.c_str(), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 8, 12, 400, 32, hwnd, (HMENU)ID_CAPTIONSTATIC, GetModuleHandle(NULL), 0);
  114.         hwndCaptionStaticBlank = CreateWindowEx(0, L"STATIC", L"",  WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,0, 0, 400, 42, hwnd, (HMENU)ID_CAPTIONSTATICBLANK, GetModuleHandle(NULL), 0);
  115.         hwndYesButton = CreateWindowW(L"Button", yesText.c_str(), WS_VISIBLE | WS_CHILD, 205, 55, 61, 21, hwnd, (HMENU)ID_YESBUTTON, NULL, NULL);
  116.         hwndNoButton = CreateWindowW(L"Button", noText.c_str(), WS_VISIBLE | WS_CHILD, 300, 55, 61, 21, hwnd, (HMENU)ID_NOBUTTON, NULL, NULL);
  117.         break;
  118.     case WM_COMMAND:
  119.         switch(LOWORD(wParam))
  120.         {
  121.         case ID_YESBUTTON:
  122.         {
  123.             DWORD fileAttr = GetFileAttributes(markerPath.c_str());
  124.             if (0xFFFFFFFF == fileAttr)
  125.             {
  126.                 HANDLE h = CreateFile(markerPath.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  127.                 if (!h)
  128.                 {
  129.                     MessageBox(NULL, L"Couldn't create marker", L"Error", MB_OK | MB_ICONEXCLAMATION);
  130.                 }
  131.             }
  132.             else if (PathFileExistsW(markerPath.c_str()))
  133.             {
  134.                 MessageBox(NULL, L"Marker already exists", L"", MB_OK | MB_ICONEXCLAMATION);
  135.             }
  136.             else
  137.             {
  138.                 MessageBox(NULL, L"Couldn't create marker", L"Error", MB_OK | MB_ICONEXCLAMATION);
  139.             }
  140.             SendMessage(hwnd, WM_CLOSE, 0, 0);
  141.             break;
  142.         }
  143.         case ID_NOBUTTON:
  144.             SendMessage(hwnd, WM_CLOSE, 0, 0);
  145.             break;
  146.         default:
  147.             break;
  148.         }
  149.         break;
  150.     case WM_CTLCOLORSTATIC:
  151.         SetBkColor((HDC)wParam, RGB(255, 255, 255));
  152.         return (INT_PTR)CreateSolidBrush(RGB(255, 255, 255));
  153.         break;
  154.     case WM_SYSCOMMAND:
  155.     {
  156.         int command = wParam & 0xfff0;
  157.         if (command == SC_MOVE)
  158.             return NULL;
  159.         break;
  160.     }
  161.     case WM_DESTROY:
  162.         PostQuitMessage(0);
  163.         break;
  164.     }
  165.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  166. }
  167.  
  168. LPWSTR stringToLPWSTR(const std::string& instr)
  169. {
  170.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  171.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  172.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  173.     widestr[bufferlen] = 0;
  174.     return widestr;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement