StartAPPbyCOM.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // StartAPPbyCOM.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. #include <iostream>
  4. #include <tchar.h>
  5. #include "CmdHelper.h"
  6. #include "SerialPortManager.h"
  7. #include "Assist.h"
  8. #include <thread>
  9. #ifdef _UNICODE
  10. #pragma comment(linker, "/subsystem:\"windows\" /entry:\"wmainCRTStartup\"")
  11. #else
  12. #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
  13. #endif
  14. #define TAG_CMD_START_APP _T("-startApp")
  15. #define TAG_ARG_START_APP _T("-startAppArg")
  16. int _tmain(int argc, TCHAR* argv[])
  17. {
  18. int ret = 0;
  19. CmdHelper<TCHAR> cmdHelper;
  20. if ((ret = cmdHelper.initialize(argc, argv)) != 0)
  21. {
  22. printf("Initialize command failed! ret=%d\n", ret);
  23. return ret;
  24. }
  25. std::basic_string<TCHAR> tcsAppPath = _T("");
  26. std::basic_string<TCHAR> tcsAppArgs = _T("");
  27. if ((ret = cmdHelper.getCmdValue(TAG_CMD_START_APP, tcsAppPath)) != 0 || tcsAppPath.empty())
  28. {
  29. printf("Please specific your app path!\n");
  30. return ret;
  31. }
  32. cmdHelper.getCmdValue(TAG_ARG_START_APP, tcsAppArgs);
  33. SerialPortManager spManager;
  34. if ((ret = spManager.initialize()) != 0)
  35. {
  36. printf("Initialize SerialPortManager failed! ret=%d\n", ret);
  37. return ret;
  38. }
  39. #if 1
  40. Assist::Init();
  41. Assist::WriteTextLog(_T("进程启动"));
  42. // 判断是否有飞书进程;
  43. HWND hWnd = Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms"));
  44. if (hWnd == NULL)
  45. {
  46. Assist::WriteTextLog(_T("飞书未启动"));
  47. // 结束所有飞书进程;
  48. Assist::CloseProcess(_T("FeishuRooms.exe"));
  49. #pragma warning(disable:28159)
  50. // 判断是否有任务栏;
  51. DWORD dwTickCount = GetTickCount64();
  52. while (FindWindow(_T("Shell_TrayWnd"), NULL) == NULL)
  53. {
  54. Sleep(500);
  55. Assist::WriteTextLog(_T("未找到系统任务栏"));
  56. if (GetTickCount64() - dwTickCount > 20000)
  57. {
  58. Assist::WriteTextLog(_T("未找到系统任务栏超过20秒"));
  59. break;
  60. }
  61. }
  62. Assist::WriteTextLog(_T("找到系统任务栏,开始启动飞书进程"));
  63. // 启动飞书;
  64. Assist::StartApp(tcsAppPath.c_str(), tcsAppArgs.c_str());
  65. dwTickCount = GetTickCount64();
  66. while (Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms")) == NULL)
  67. {
  68. Sleep(500);
  69. Assist::WriteTextLog(_T("飞书启动中……"));
  70. if (GetTickCount64() - dwTickCount > 15000)
  71. {
  72. Assist::WriteTextLog(_T("飞书启动超过10秒,默认启动失败"));
  73. break;
  74. }
  75. }
  76. // 发送串口消息;
  77. spManager.sendStartedApp();
  78. }
  79. std::thread t([&]() {
  80. bool bStart = true;
  81. while ( true )
  82. {
  83. if (Assist::GetProcessId(_T("FeishuRooms.exe")) == 0)
  84. {
  85. if (bStart)
  86. {
  87. Assist::WriteTextLog(_T("飞书已关闭, 发送关闭命令给Android"));
  88. spManager.sendCloseApp();
  89. bStart = false;
  90. }
  91. }
  92. else
  93. {
  94. bStart = true;
  95. }
  96. Sleep(50);
  97. }
  98. });
  99. t.detach();
  100. #endif
  101. if ((ret = spManager.listenCOMandStartApp(tcsAppPath.c_str(), tcsAppArgs.c_str())) != 0)
  102. {
  103. printf("listenCOMandStartApp failed ret=%d\n", ret);
  104. return ret;
  105. }
  106. return ret;
  107. }
  108. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  109. // Debug program: F5 or Debug > Start Debugging menu
  110. // Tips for Getting Started:
  111. // 1. Use the Solution Explorer window to add/manage files
  112. // 2. Use the Team Explorer window to connect to source control
  113. // 3. Use the Output window to see build output and other messages
  114. // 4. Use the Error List window to view errors
  115. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  116. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file