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