RunPython.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // RunPython.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "RunPython.h"
  5. #include "ScriptExecutor.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // 唯一的应用程序对象
  10. CWinApp theApp;
  11. using namespace std;
  12. BOOL Python27Dir(LPTSTR lpPython27Dir, int nBufferLen);
  13. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  14. {
  15. int nRetCode = 0;
  16. // 初始化 MFC 并在失败时显示错误
  17. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  18. {
  19. // TODO: 更改错误代码以符合您的需要
  20. _tprintf(_T("错误: MFC 初始化失败\n"));
  21. nRetCode = 1;
  22. }
  23. else
  24. {
  25. while(true)
  26. {
  27. // TODO: 在此处为应用程序的行为编写代码。
  28. CScriptExecutor excutor;
  29. excutor.InitScript(
  30. //"E:\\bin\\ScbcCopyKey\\ScbcTest.py",
  31. "D:\\SAT\\runner\\btc_runner_se\\runner\\output\\ODF_NPI_RT2841\\20191119172310094\\192.168.1.119_5555\\cases\\RT_2841\\ODF_NPI_RT2841\\picture\\22.py",
  32. "D:\\SAT\\log.txt",
  33. "",
  34. SUBPROCESS);
  35. excutor.StartScript();
  36. Sleep(500);
  37. while( !excutor.IsScriptOver() )
  38. Sleep(100);
  39. Sleep(2000);
  40. OutputDebugString("--------------------------------------\n");
  41. }
  42. }
  43. system("pause");
  44. return nRetCode;
  45. }
  46. // 全局变量;
  47. std::vector<CScriptExecutor*> g_vtScriptExcutor;
  48. RUNPYTHON_API DWORD RunPyScript(LPCTSTR lpScriptFile, LPCTSTR lpExtraSentence, LPCTSTR lpScriptLog, BOOL bSubProcess)
  49. {
  50. CScriptExecutor *pExcutor = new CScriptExecutor();
  51. if ( pExcutor )
  52. {
  53. // 初始化脚本;
  54. pExcutor->InitScript(lpScriptFile, lpScriptLog, lpExtraSentence, bSubProcess ? SUBPROCESS : EMBEDDED);
  55. // 执行脚本;
  56. if ( !pExcutor->StartScript() )
  57. {
  58. delete pExcutor;
  59. pExcutor = NULL;
  60. return -1;
  61. }
  62. // 压入全局变量;
  63. g_vtScriptExcutor.push_back(pExcutor);
  64. // 返回脚本Id;
  65. return pExcutor->GetScriptId();
  66. }
  67. return -1;
  68. }
  69. RUNPYTHON_API bool StopPyScript(DWORD dwScriptId, BOOL bSubProcess)
  70. {
  71. bool ret = false;
  72. CScriptExecutor *pExcutor = NULL;
  73. std::vector<CScriptExecutor*>::iterator it = g_vtScriptExcutor.begin();
  74. for ( ; it != g_vtScriptExcutor.end(); it++ )
  75. {
  76. pExcutor = *it;
  77. if ( pExcutor->GetScriptId() == dwScriptId )
  78. {
  79. ret = true;
  80. pExcutor->StopScript();
  81. break;
  82. }
  83. }
  84. return ret;
  85. }
  86. RUNPYTHON_API bool IsPyScriptOver(DWORD dwScriptId, BOOL bSubProcess)
  87. {
  88. bool ret = false;
  89. CScriptExecutor *pExcutor = NULL;
  90. std::vector<CScriptExecutor*>::iterator it = g_vtScriptExcutor.begin();
  91. for ( ; it != g_vtScriptExcutor.end(); it++ )
  92. {
  93. pExcutor = *it;
  94. if ( pExcutor->GetScriptId() == dwScriptId )
  95. {
  96. if ( ret = pExcutor->IsScriptOver() )
  97. {
  98. break;
  99. }
  100. }
  101. }
  102. return ret;
  103. }
  104. RUNPYTHON_API void CleanPyScript(DWORD dwScriptId)
  105. {
  106. CScriptExecutor *pExcutor = NULL;
  107. std::vector<CScriptExecutor*>::iterator it = g_vtScriptExcutor.begin();
  108. for ( ; it != g_vtScriptExcutor.end(); it++ )
  109. {
  110. pExcutor = *it;
  111. if ( pExcutor->GetScriptId() == dwScriptId )
  112. {
  113. // 先停止脚本;
  114. pExcutor->StopScript();
  115. // 再清除;
  116. delete pExcutor;
  117. pExcutor = NULL;
  118. g_vtScriptExcutor.erase(it);
  119. break;
  120. }
  121. }
  122. }