You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovProcessUtilities.hpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <string>
  3. #if defined TARGET_OS_Windows
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <Windows.h>
  6. #include <tlhelp32.h>
  7. #include <tchar.h>
  8. #elif defined TARGET_OS_Linux || defined TARGET_OS_MacOS
  9. #include <cstdlib>
  10. #include <unistd.h>
  11. #include <vector>
  12. #include <fs/IEntryEnumerator.h>
  13. #include <fs/Files.h>
  14. #include <iostream>
  15. #include <fstream>
  16. #endif
  17. #if defined TARGET_OS_Linux || defined TARGET_OS_MacOS
  18. namespace
  19. {
  20. class CProcessEnumeratorCB : public FS::IEntryEnumeratorCallBack
  21. {
  22. public:
  23. CProcessEnumeratorCB(std::string processName, bool& rbFoundProcess) : m_name(processName), m_found(rbFoundProcess) { }
  24. virtual FS::boolean callback(FS::IEntryEnumerator::IEntry& entry, FS::IEntryEnumerator::IAttributes& /*attributes*/)
  25. {
  26. std::ifstream fsInputStream;
  27. FS::Files::openIFStream(fsInputStream, entry.getName());
  28. std::string fileContents;
  29. if (fsInputStream.is_open())
  30. {
  31. while(!fsInputStream.eof()) { fsInputStream >> fileContents; }
  32. size_t pos = fileContents.rfind(m_name);
  33. if (pos != std::string::npos && pos == fileContents.length() - m_name.length() - 1 ) { m_found = true; }
  34. }
  35. fsInputStream.close();
  36. return true;
  37. }
  38. std::string m_name;
  39. bool& m_found;
  40. };
  41. }
  42. #endif
  43. namespace OpenViBE {
  44. namespace ProcessUtilities {
  45. bool doesProcessExist(const std::string& name)
  46. {
  47. #if defined TARGET_OS_Windows
  48. PROCESSENTRY32 pe32;
  49. // Take a snapshot of all processes in the system.
  50. const HANDLE processSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  51. if (processSnap == INVALID_HANDLE_VALUE) { return false; }
  52. // Set the size of the structure before using it.
  53. pe32.dwSize = sizeof(PROCESSENTRY32);
  54. // Retrieve information about the first process,
  55. // and exit if unsuccessful
  56. if (!Process32First(processSnap, &pe32))
  57. {
  58. CloseHandle(processSnap); // clean the snapshot object
  59. return false;
  60. }
  61. bool bReturnValue = false;
  62. // Now walk the snapshot of processes
  63. do { if (name + ".exe" == std::string(pe32.szExeFile)) { bReturnValue = true; } } while (Process32Next(processSnap, &pe32));
  64. CloseHandle(processSnap);
  65. return bReturnValue;
  66. #elif defined TARGET_OS_Linux || defined TARGET_OS_MacOS
  67. bool found = false;
  68. CProcessEnumeratorCB* cb = new CProcessEnumeratorCB(name, found);
  69. FS::IEntryEnumerator* entry = FS::createEntryEnumerator(*cb);
  70. entry->enumerate("/proc/*/cmdline");
  71. entry->release();
  72. delete cb;
  73. return found;
  74. #endif
  75. }
  76. bool launchCommand(const char* sCommandLine)
  77. {
  78. #if defined TARGET_OS_Windows
  79. /*
  80. LPTSTR szCmdline = TEXT(const_cast<char*>(sCommandLine));
  81. STARTUPINFO lpStartupInfo;
  82. ZeroMemory(&lpStartupInfo,sizeof(lpStartupInfo));
  83. lpStartupInfo.cb = sizeof(lpStartupInfo);
  84. lpStartupInfo.dwFlags |= CREATE_NEW_CONSOLE;
  85. PROCESS_INFORMATION lpProcessInfo;
  86. // Create the process
  87. if (!CreateProcess(nullptr,szCmdline, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &lpStartupInfo, &lpProcessInfo)) { exit(1); }
  88. */
  89. const std::string cmd = "start \"\" \"" + std::string(sCommandLine) + "\"";
  90. system(cmd.c_str());
  91. #elif defined TARGET_OS_Linux || defined TARGET_OS_MacOS
  92. if (fork() == 0)
  93. {
  94. system(sCommandLine);
  95. exit(0);
  96. }
  97. // FIXME: temporary solution using system()
  98. #endif
  99. return true;
  100. }
  101. } // namespace ProcessUtilities
  102. } // namespace OpenViBE