Eclipse SUMO - Simulation of Urban MObility
SysUtils.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // A few system-specific functions
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <stdlib.h>
24 #include "SysUtils.h"
25 
26 #ifndef WIN32
27 #include <sys/time.h>
28 #else
29 #define NOMINMAX
30 #include <windows.h>
31 #undef NOMINMAX
32 #endif
33 
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
38 long
40 #ifndef WIN32
41  timeval current;
42  gettimeofday(&current, 0);
43  long nanosecs =
44  (long) current.tv_sec * 1000L + (long) current.tv_usec / 1000L;
45  return nanosecs;
46 #else
47  LARGE_INTEGER val, val2;
48  BOOL check = QueryPerformanceCounter(&val);
49  check = QueryPerformanceFrequency(&val2);
50  return (long)(val.QuadPart * 1000 / val2.QuadPart);
51 #endif
52 }
53 
54 
55 #ifdef WIN32
56 long
57 SysUtils::getWindowsTicks() {
58  return (long) GetTickCount();
59 }
60 #endif
61 
62 
63 unsigned long
64 SysUtils::runHiddenCommand(const std::string& cmd) {
65 #ifdef WIN32
66  // code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
67  STARTUPINFO StartupInfo;
68  PROCESS_INFORMATION ProcessInfo;
69  unsigned long rc;
70 
71  memset(&StartupInfo, 0, sizeof(StartupInfo));
72  StartupInfo.cb = sizeof(STARTUPINFO);
73  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
74  StartupInfo.wShowWindow = SW_HIDE;
75 
76  // "/c" option - Do the command then terminate the command window
77  std::string winCmd = "CMD.exe /c " + cmd;
78  char* args = new char[winCmd.size() + 1];
79  args[0] = 0;
80  strcpy(args, winCmd.c_str());
81  if (!CreateProcess(nullptr, args, nullptr, nullptr, FALSE,
82  CREATE_NEW_CONSOLE, nullptr, nullptr, &StartupInfo, &ProcessInfo)) {
83  delete[] args;
84  return (unsigned long)GetLastError();
85  }
86 
87  WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
88  if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) {
89  rc = 0;
90  }
91 
92  CloseHandle(ProcessInfo.hThread);
93  CloseHandle(ProcessInfo.hProcess);
94 
95  delete[] args;
96  return rc;
97 #else
98  return (unsigned long)system(cmd.c_str());
99 #endif
100 }
101 
102 
103 /****************************************************************************/
static unsigned long runHiddenCommand(const std::string &cmd)
run a shell command without popping up any windows (particuarly on win32)
Definition: SysUtils.cpp:64
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:39
#define INFINITE
Definition: fxexdefs.h:87