123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // StartAPPbyCOM.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <tchar.h>
- #include "CmdHelper.h"
- #include "SerialPortManager.h"
- #include "Assist.h"
- #include <thread>
- #ifdef _UNICODE
- #pragma comment(linker, "/subsystem:\"windows\" /entry:\"wmainCRTStartup\"")
- #else
- #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
- #endif
- #define TAG_CMD_START_APP _T("-startApp")
- #define TAG_ARG_START_APP _T("-startAppArg")
- int _tmain(int argc, TCHAR* argv[])
- {
- int ret = 0;
- CmdHelper<TCHAR> cmdHelper;
- if ((ret = cmdHelper.initialize(argc, argv)) != 0)
- {
- printf("Initialize command failed! ret=%d\n", ret);
- return ret;
- }
- std::basic_string<TCHAR> tcsAppPath = _T("");
- std::basic_string<TCHAR> tcsAppArgs = _T("");
- if ((ret = cmdHelper.getCmdValue(TAG_CMD_START_APP, tcsAppPath)) != 0 || tcsAppPath.empty())
- {
- printf("Please specific your app path!\n");
- return ret;
- }
- cmdHelper.getCmdValue(TAG_ARG_START_APP, tcsAppArgs);
- SerialPortManager spManager;
- if ((ret = spManager.initialize()) != 0)
- {
- printf("Initialize SerialPortManager failed! ret=%d\n", ret);
- return ret;
- }
- #if 1
- Assist::Init();
- Assist::WriteTextLog(_T("进程启动"));
- // 判断是否有飞书进程;
- HWND hWnd = Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms"));
- if (hWnd == NULL)
- {
- Assist::WriteTextLog(_T("飞书未启动"));
- // 结束所有飞书进程;
- Assist::CloseProcess(_T("FeishuRooms.exe"));
- #pragma warning(disable:28159)
- // 判断是否有任务栏;
- DWORD dwTickCount = GetTickCount64();
- while (FindWindow(_T("Shell_TrayWnd"), NULL) == NULL)
- {
- Sleep(500);
- Assist::WriteTextLog(_T("未找到系统任务栏"));
- if (GetTickCount64() - dwTickCount > 20000)
- {
- Assist::WriteTextLog(_T("未找到系统任务栏超过20秒"));
- break;
- }
- }
- Assist::WriteTextLog(_T("找到系统任务栏,开始启动飞书进程"));
- // 启动飞书;
- Assist::StartApp(tcsAppPath.c_str(), tcsAppArgs.c_str());
- dwTickCount = GetTickCount64();
- while (Assist::GetWindowNameWnd(_T("FeishuRooms.exe"), _T("Feishu Rooms")) == NULL)
- {
- Sleep(500);
- Assist::WriteTextLog(_T("飞书启动中……"));
- if (GetTickCount64() - dwTickCount > 15000)
- {
- Assist::WriteTextLog(_T("飞书启动超过10秒,默认启动失败"));
- break;
- }
- }
- // 发送串口消息;
- spManager.sendStartedApp();
- }
- std::thread t([&]() {
- bool bStart = true;
- while ( true )
- {
- if (Assist::GetProcessId(_T("FeishuRooms.exe")) == 0)
- {
- if (bStart)
- {
- Assist::WriteTextLog(_T("飞书已关闭, 发送关闭命令给Android"));
- spManager.sendCloseApp();
- bStart = false;
- }
- }
- else
- {
- bStart = true;
- }
- Sleep(50);
- }
- });
- t.detach();
- #endif
- if ((ret = spManager.listenCOMandStartApp(tcsAppPath.c_str(), tcsAppArgs.c_str())) != 0)
- {
- printf("listenCOMandStartApp failed ret=%d\n", ret);
- return ret;
- }
- return ret;
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 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
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
|