Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string>
- #include "Shlwapi.h"
- #include "resource.h"
- #include <fstream>
- #pragma comment(lib, "Shlwapi.lib")
- /*#ifdef _MSC_VER
- #define _CRT_SECURE_NO_WARNINGS
- #endif*/
- #define ID_CAPTIONSTATICBLANK 1
- #define ID_CAPTIONSTATIC 2
- #define ID_YESBUTTON 11
- #define ID_NOBUTTON 12
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- HANDLE hwndYesButton;
- HANDLE hwndNoButton;
- HANDLE hwndCaptionStaticBlank;
- HANDLE hwndCaptionStatic;
- HWND mainWindow;
- std::wstring windowsTitle;
- std::wstring captionText;
- std::wstring markerPath;
- std::wstring yesText;
- std::wstring noText;
- LANGID language;
- std::wstring bashPath;
- LPWSTR stringToLPWSTR(const std::string& instr);
- BOOL IsElevated();
- void reboot();
- bool isWSLEnabled();
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- bool noReboot = false;
- bool forceReboot = false;
- LPWSTR* szArgList;
- int argCount;
- szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
- if (szArgList == NULL)
- {
- MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK | MB_ICONEXCLAMATION);
- return 10;
- }
- if (szArgList[1] != NULL && (wcscmp(szArgList[1], L"/NOREBOOT") == 0 || wcscmp(szArgList[1], L"/noreboot") == 0))
- {
- noReboot = true;
- }
- else if (szArgList[1] != NULL && (wcscmp(szArgList[1], L"/REBOOT") == 0 || wcscmp(szArgList[1], L"/reboot") == 0))
- {
- forceReboot = true;
- }
- LocalFree(szArgList);
- bashPath = L"C:\\Windows\\System32\\bash.exe";//bash //telnet
- if (!PathFileExistsW(bashPath.c_str()) && IsElevated())
- {
- 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
- while(!isWSLEnabled())
- {
- Sleep(820);
- }
- if (noReboot)
- {
- return 0;
- }
- else if (forceReboot)
- {
- reboot();
- return 0;
- }
- }
- else
- {
- return 0;
- }
- LANGID language = GetUserDefaultUILanguage();
- if (language == 1036)
- {
- windowsTitle = L"La fonctionnalité WSL a été installé et demande un redémarrage";
- captionText = L"Redémarrer maintenant?";
- yesText = L"Oui";
- noText = L"Non";
- }
- else
- {
- windowsTitle = L"WSL feature was installed and needs reboot to complete";
- captionText = L"Restart now?";
- yesText = L"Yes";
- noText = L"No";
- }
- int xSize, ySize;
- RECT desktop;
- const HWND hDesktop = GetDesktopWindow();
- GetWindowRect(hDesktop, &desktop);
- xSize = desktop.right;
- ySize = desktop.bottom;
- MSG msg;
- WNDCLASSW wc = { 0 };
- wc.lpszClassName = L"InstallWSL";
- wc.hInstance = hInstance;
- wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wc.lpfnWndProc = WndProc;
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
- RegisterClassW(&wc);
- 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);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg)
- {
- case WM_CREATE:
- 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);
- hwndCaptionStaticBlank = CreateWindowEx(0, L"STATIC", L"", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 0, 0, 400, 42, hwnd, (HMENU)ID_CAPTIONSTATICBLANK, GetModuleHandle(NULL), 0);
- hwndYesButton = CreateWindowW(L"Button", yesText.c_str(), WS_VISIBLE | WS_CHILD, 205, 55, 61, 21, hwnd, (HMENU)ID_YESBUTTON, NULL, NULL);
- hwndNoButton = CreateWindowW(L"Button", noText.c_str(), WS_VISIBLE | WS_CHILD, 300, 55, 61, 21, hwnd, (HMENU)ID_NOBUTTON, NULL, NULL);
- break;
- case WM_COMMAND:
- switch (LOWORD(wParam))
- {
- case ID_YESBUTTON:
- {
- if (isWSLEnabled())
- {
- reboot();
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- }
- break;
- }
- case ID_NOBUTTON:
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- break;
- default:
- break;
- }
- break;
- case WM_CTLCOLORSTATIC:
- SetBkColor((HDC)wParam, RGB(255, 255, 255));
- return (INT_PTR)CreateSolidBrush(RGB(255, 255, 255));
- break;
- case WM_SYSCOMMAND:
- {
- int command = wParam & 0xfff0;
- if (command == SC_MOVE)
- return NULL;
- break;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- LPWSTR stringToLPWSTR(const std::string& instr)
- {
- int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
- LPWSTR widestr = new WCHAR[bufferlen + 1];
- ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
- widestr[bufferlen] = 0;
- return widestr;
- }
- BOOL IsElevated()
- {
- BOOL fRet = FALSE;
- HANDLE hToken = NULL;
- if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
- {
- TOKEN_ELEVATION Elevation;
- DWORD cbSize = sizeof(TOKEN_ELEVATION);
- if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
- {
- fRet = Elevation.TokenIsElevated;
- }
- }
- if (hToken)
- {
- CloseHandle(hToken);
- }
- return fRet;
- }
- void reboot()
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES tkp;
- OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
- LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
- tkp.PrivilegeCount = 1;
- tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
- ExitWindowsEx(EWX_REBOOT | EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_FLAG_PLANNED);
- }
- bool isWSLEnabled()
- {
- std::string status;
- 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
- //ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C dism /online /Enable-Feature /featurename:Microsoft-Windows-Subsystem-Linux", NULL, SW_HIDE);
- std::ifstream file("featureStatus.txt");
- getline(file, status, '\n');
- if ((status.find("Enabled") != std::string::npos) || (status.find("Activ") != std::string::npos))
- {
- file.close();
- remove("featureStatus.txt");
- return true;
- }
- file.close();
- remove("featureStatus.txt");
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement