main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(!::IsDebuggerPresent()){
  41. if (!isRunAsAdmin()) {
  42. runAsAdmin();
  43. exit(0);
  44. }
  45. if (wcsstr(command_line, L"--runas-admin") != NULL) {
  46. }
  47. else {
  48. HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"naiyouwl");
  49. if (hwnd != NULL) {
  50. DispatchToProtocolHandler(hwnd);
  51. ::ShowWindow(hwnd, SW_NORMAL);
  52. ::SetForegroundWindow(hwnd);
  53. return EXIT_FAILURE;
  54. }
  55. }
  56. }
  57. /*
  58. *
  59. else {
  60. AllocConsole();
  61. ShowWindow(GetConsoleWindow(), SW_HIDE);
  62. }
  63. * */
  64. // https://github.com/flutter/flutter/issues/47891#issuecomment-708850435
  65. // https://github.com/flutter/flutter/issues/47891#issuecomment-869729956
  66. // https://github.com/dart-lang/sdk/issues/39945#issuecomment-870428151
  67. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
  68. CreateAndAttachConsole();
  69. }
  70. else {
  71. AllocConsole();
  72. ShowWindow(GetConsoleWindow(), SW_HIDE);
  73. }
  74. // Initialize COM, so that it is available for use in the library and/or
  75. // plugins.
  76. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  77. flutter::DartProject project(L"data");
  78. std::vector<std::string> command_line_arguments =
  79. GetCommandLineArguments();
  80. project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
  81. FlutterWindow window(project);
  82. Win32Window::Point origin(10, 10);
  83. Win32Window::Size size(1280, 720);
  84. if (!window.Create(L"naiyouwl", origin, size)) {
  85. return EXIT_FAILURE;
  86. }
  87. window.SetQuitOnClose(true);
  88. ::MSG msg;
  89. while (::GetMessage(&msg, nullptr, 0, 0)) {
  90. ::TranslateMessage(&msg);
  91. ::DispatchMessage(&msg);
  92. }
  93. ::CoUninitialize();
  94. return EXIT_SUCCESS;
  95. }