main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <flutter/dart_project.h>
  2. #include <flutter/flutter_view_controller.h>
  3. #include <windows.h>
  4. #include "flutter_window.h"
  5. #include "utils.h"
  6. #include <protocol_handler/protocol_handler_plugin.h>
  7. BOOL isRunAsAdmin() {
  8. BOOL isRunAsAdmin = FALSE;
  9. HANDLE hToken = NULL;
  10. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
  11. {
  12. return FALSE;
  13. }
  14. TOKEN_ELEVATION tokenEle;
  15. DWORD dwRetLen = 0;
  16. if (GetTokenInformation(hToken, TokenElevation, &tokenEle, sizeof(tokenEle), &dwRetLen))
  17. {
  18. if (dwRetLen == sizeof(tokenEle))
  19. {
  20. isRunAsAdmin = tokenEle.TokenIsElevated;
  21. }
  22. }
  23. CloseHandle(hToken);
  24. return isRunAsAdmin;
  25. }
  26. void runAsAdmin() {
  27. WCHAR czFileName[1024] = { 0 };
  28. GetModuleFileName(NULL, czFileName, _countof(czFileName) - 1);
  29. SHELLEXECUTEINFO EI;
  30. memset(&EI, 0, sizeof(EI));
  31. EI.cbSize = sizeof(SHELLEXECUTEINFO);
  32. EI.lpVerb = TEXT("runas");
  33. EI.fMask = 0x00000040;
  34. EI.lpFile = czFileName;
  35. EI.nShow = SW_SHOW;
  36. ShellExecuteEx(&EI);
  37. }
  38. int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
  39. _In_ wchar_t *command_line, _In_ int show_command) {
  40. if (!isRunAsAdmin()) {
  41. runAsAdmin();
  42. exit(0);
  43. }
  44. if (wcsstr(command_line, L"--runas-admin") != NULL) {
  45. }
  46. else {
  47. HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"naiyouwl");
  48. if (hwnd != NULL) {
  49. DispatchToProtocolHandler(hwnd);
  50. ::ShowWindow(hwnd, SW_NORMAL);
  51. ::SetForegroundWindow(hwnd);
  52. return EXIT_FAILURE;
  53. }
  54. }
  55. /*
  56. *
  57. else {
  58. AllocConsole();
  59. ShowWindow(GetConsoleWindow(), SW_HIDE);
  60. }
  61. * */
  62. // https://github.com/flutter/flutter/issues/47891#issuecomment-708850435
  63. // https://github.com/flutter/flutter/issues/47891#issuecomment-869729956
  64. // https://github.com/dart-lang/sdk/issues/39945#issuecomment-870428151
  65. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  66. CreateAndAttachConsole();
  67. }
  68. else {
  69. AllocConsole();
  70. ShowWindow(GetConsoleWindow(), SW_HIDE);
  71. }
  72. // Initialize COM, so that it is available for use in the library and/or
  73. // plugins.
  74. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  75. flutter::DartProject project(L"data");
  76. std::vector<std::string> command_line_arguments =
  77. GetCommandLineArguments();
  78. project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
  79. FlutterWindow window(project);
  80. Win32Window::Point origin(10, 10);
  81. Win32Window::Size size(1280, 720);
  82. if (!window.Create(L"naiyouwl", origin, size)) {
  83. return EXIT_FAILURE;
  84. }
  85. window.SetQuitOnClose(true);
  86. ::MSG msg;
  87. while (::GetMessage(&msg, nullptr, 0, 0)) {
  88. ::TranslateMessage(&msg);
  89. ::DispatchMessage(&msg);
  90. }
  91. ::CoUninitialize();
  92. return EXIT_SUCCESS;
  93. }