main.cpp 2.3 KB

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