Advertisement
Combreal

InstallWSL01.cpp

May 7th, 2020
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3. #include "Shlwapi.h"
  4. #include "resource.h"
  5. #include <fstream>
  6.  
  7. #pragma comment(lib, "Shlwapi.lib")
  8. /*#ifdef _MSC_VER
  9. #define _CRT_SECURE_NO_WARNINGS
  10. #endif*/
  11.  
  12. #define ID_CAPTIONSTATICBLANK 1
  13. #define ID_CAPTIONSTATIC 2
  14. #define ID_YESBUTTON 11
  15. #define ID_NOBUTTON 12
  16.  
  17. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  18. HANDLE hwndYesButton;
  19. HANDLE hwndNoButton;
  20. HANDLE hwndCaptionStaticBlank;
  21. HANDLE hwndCaptionStatic;
  22. HWND mainWindow;
  23. std::wstring windowsTitle;
  24. std::wstring captionText;
  25. std::wstring markerPath;
  26. std::wstring yesText;
  27. std::wstring noText;
  28.  
  29. LANGID language;
  30. std::wstring bashPath;
  31.  
  32. LPWSTR stringToLPWSTR(const std::string& instr);
  33. BOOL IsElevated();
  34. void reboot();
  35. bool isWSLEnabled();
  36.  
  37. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  38. {
  39.     bool noReboot = false;
  40.     bool forceReboot = false;
  41.     LPWSTR* szArgList;
  42.     int argCount;
  43.     szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
  44.     if (szArgList == NULL)
  45.     {
  46.         MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK | MB_ICONEXCLAMATION);
  47.         return 10;
  48.     }
  49.     if (szArgList[1] != NULL && (wcscmp(szArgList[1], L"/NOREBOOT") == 0 || wcscmp(szArgList[1], L"/noreboot") == 0))
  50.     {
  51.         noReboot = true;
  52.     }
  53.     else if (szArgList[1] != NULL && (wcscmp(szArgList[1], L"/REBOOT") == 0 || wcscmp(szArgList[1], L"/reboot") == 0))
  54.     {
  55.         forceReboot = true;
  56.     }
  57.     LocalFree(szArgList);
  58.     bashPath = L"C:\\Windows\\System32\\bash.exe";//bash //telnet
  59.     if (!PathFileExistsW(bashPath.c_str()) && IsElevated())
  60.     {
  61.         ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C dism /online /Enable-Feature /featurename:Microsoft-Windows-Subsystem-Linux", NULL, SW_HIDE);//Microsoft-Windows-Subsystem-Linux /All //TelnetClient
  62.         while(!isWSLEnabled())
  63.         {
  64.             Sleep(820);
  65.         }
  66.         if (noReboot)
  67.         {
  68.             return 0;
  69.         }
  70.         else if (forceReboot)
  71.         {
  72.             reboot();
  73.             return 0;
  74.         }
  75.     }
  76.     else
  77.     {
  78.         return 0;
  79.     }
  80.     LANGID language = GetUserDefaultUILanguage();
  81.     if (language == 1036)
  82.     {
  83.         windowsTitle = L"La fonctionnalité WSL a été installé et demande un redémarrage";
  84.         captionText = L"Redémarrer maintenant?";
  85.         yesText = L"Oui";
  86.         noText = L"Non";
  87.     }
  88.     else
  89.     {
  90.         windowsTitle = L"WSL feature was installed and needs reboot to complete";
  91.         captionText = L"Restart now?";
  92.         yesText = L"Yes";
  93.         noText = L"No";
  94.     }
  95.     int xSize, ySize;
  96.     RECT desktop;
  97.     const HWND hDesktop = GetDesktopWindow();
  98.     GetWindowRect(hDesktop, &desktop);
  99.     xSize = desktop.right;
  100.     ySize = desktop.bottom;
  101.     MSG msg;
  102.     WNDCLASSW wc = { 0 };
  103.     wc.lpszClassName = L"InstallWSL";
  104.     wc.hInstance = hInstance;
  105.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  106.     wc.lpfnWndProc = WndProc;
  107.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  108.     wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  109.     RegisterClassW(&wc);
  110.     mainWindow = CreateWindowW(wc.lpszClassName, windowsTitle.c_str(), WS_CAPTION | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPED | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, xSize - 394, ySize - 160, 400, 126, 0, 0, hInstance, 0);
  111.     while (GetMessage(&msg, NULL, 0, 0))
  112.     {
  113.         TranslateMessage(&msg);
  114.         DispatchMessage(&msg);
  115.     }
  116.     return (int)msg.wParam;
  117. }
  118.  
  119. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  120. {
  121.  
  122.  
  123.     switch (msg)
  124.     {
  125.     case WM_CREATE:
  126.         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);
  127.         hwndCaptionStaticBlank = CreateWindowEx(0, L"STATIC", L"", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 0, 0, 400, 42, hwnd, (HMENU)ID_CAPTIONSTATICBLANK, GetModuleHandle(NULL), 0);
  128.         hwndYesButton = CreateWindowW(L"Button", yesText.c_str(), WS_VISIBLE | WS_CHILD, 205, 55, 61, 21, hwnd, (HMENU)ID_YESBUTTON, NULL, NULL);
  129.         hwndNoButton = CreateWindowW(L"Button", noText.c_str(), WS_VISIBLE | WS_CHILD, 300, 55, 61, 21, hwnd, (HMENU)ID_NOBUTTON, NULL, NULL);
  130.         break;
  131.     case WM_COMMAND:
  132.         switch (LOWORD(wParam))
  133.         {
  134.         case ID_YESBUTTON:
  135.         {
  136.             if (isWSLEnabled())
  137.             {
  138.                 reboot();
  139.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  140.             }
  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. }
  176.  
  177. BOOL IsElevated()
  178. {
  179.     BOOL fRet = FALSE;
  180.     HANDLE hToken = NULL;
  181.     if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
  182.     {
  183.         TOKEN_ELEVATION Elevation;
  184.         DWORD cbSize = sizeof(TOKEN_ELEVATION);
  185.         if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
  186.         {
  187.             fRet = Elevation.TokenIsElevated;
  188.         }
  189.     }
  190.     if (hToken)
  191.     {
  192.         CloseHandle(hToken);
  193.     }
  194.     return fRet;
  195. }
  196.  
  197. void reboot()
  198. {
  199.     HANDLE hToken;
  200.     TOKEN_PRIVILEGES tkp;
  201.     OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
  202.     LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  203.     tkp.PrivilegeCount = 1;
  204.     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  205.     AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  206.     ExitWindowsEx(EWX_REBOOT | EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_FLAG_PLANNED);
  207. }
  208.  
  209. bool isWSLEnabled()
  210. {
  211.     std::string status;
  212.     ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C dism /online /get-featureinfo /featurename:Microsoft-Windows-Subsystem-Linux | findstr /R /C:tat > featureStatus.txt", NULL, SW_HIDE);//| findstr /R /C:tat
  213.     //ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C dism /online /Enable-Feature /featurename:Microsoft-Windows-Subsystem-Linux", NULL, SW_HIDE);
  214.     std::ifstream file("featureStatus.txt");
  215.     getline(file, status, '\n');
  216.     if ((status.find("Enabled") != std::string::npos) || (status.find("Activ") != std::string::npos))
  217.     {
  218.         file.close();
  219.         remove("featureStatus.txt");
  220.         return true;
  221.     }
  222.     file.close();
  223.     remove("featureStatus.txt");
  224.     return false;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement