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