SUMO - Simulation of Urban MObility
SysUtils.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // A few system-specific functions
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2005-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <stdlib.h>
31 #include "SysUtils.h"
32 
33 #ifndef WIN32
34 #include <sys/time.h>
35 #else
36 #define NOMINMAX
37 #include <windows.h>
38 #undef NOMINMAX
39 #endif
40 
41 
42 // ===========================================================================
43 // member method definitions
44 // ===========================================================================
45 long
47 #ifndef WIN32
48  timeval current;
49  gettimeofday(&current, 0);
50  long nanosecs =
51  (long) current.tv_sec * 1000L + (long) current.tv_usec / 1000L;
52  return nanosecs;
53 #else
54  LARGE_INTEGER val, val2;
55  BOOL check = QueryPerformanceCounter(&val);
56  check = QueryPerformanceFrequency(&val2);
57  return (long)(val.QuadPart * 1000 / val2.QuadPart);
58 #endif
59 }
60 
61 
62 #ifdef _MSC_VER
63 long
64 SysUtils::getWindowsTicks() {
65  return (long) GetTickCount();
66 }
67 #endif
68 
69 
70 unsigned long
71 SysUtils::runHiddenCommand(const std::string& cmd) {
72 #ifdef _MSC_VER
73  // code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
74  STARTUPINFO StartupInfo;
75  PROCESS_INFORMATION ProcessInfo;
76  unsigned long rc;
77 
78  memset(&StartupInfo, 0, sizeof(StartupInfo));
79  StartupInfo.cb = sizeof(STARTUPINFO);
80  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
81  StartupInfo.wShowWindow = SW_HIDE;
82 
83  // "/c" option - Do the command then terminate the command window
84  std::string winCmd = "CMD.exe /c " + cmd;
85  char* args = new char[winCmd.size()];
86  args[0] = 0;
87  strcpy(args, winCmd.c_str());
88  if (!CreateProcess(NULL, args, NULL, NULL, FALSE,
89  CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo)) {
90  delete args;
91  return (unsigned long)GetLastError();
92  }
93 
94  WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
95  if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) {
96  rc = 0;
97  }
98 
99  CloseHandle(ProcessInfo.hThread);
100  CloseHandle(ProcessInfo.hProcess);
101 
102  delete args;
103  return rc;
104 #else
105  return (unsigned long)system(cmd.c_str());
106 #endif
107 }
108 
109 /****************************************************************************/
110 
#define INFINITE
Definition: fxexdefs.h:93
static unsigned long runHiddenCommand(const std::string &cmd)
run a shell command without popping up any windows (particuarly on win32)
Definition: SysUtils.cpp:71
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:46