SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
49 long
51 #ifndef WIN32
52  timeval current;
53  gettimeofday(&current, 0);
54  long nanosecs =
55  (long) current.tv_sec * 1000L + (long) current.tv_usec / 1000L;
56  return nanosecs;
57 #else
58  LARGE_INTEGER val, val2;
59  BOOL check = QueryPerformanceCounter(&val);
60  check = QueryPerformanceFrequency(&val2);
61  return (long)(val.QuadPart * 1000 / val2.QuadPart);
62 #endif
63 }
64 
65 
66 #ifdef _MSC_VER
67 long
68 SysUtils::getWindowsTicks() {
69  return (long) GetTickCount();
70 }
71 #endif
72 
73 
74 unsigned long
75 SysUtils::runHiddenCommand(const std::string& cmd) {
76 #ifdef _MSC_VER
77  // code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
78  STARTUPINFO StartupInfo;
79  PROCESS_INFORMATION ProcessInfo;
80  unsigned long rc;
81 
82  memset(&StartupInfo, 0, sizeof(StartupInfo));
83  StartupInfo.cb = sizeof(STARTUPINFO);
84  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
85  StartupInfo.wShowWindow = SW_HIDE;
86 
87  // "/c" option - Do the command then terminate the command window
88  std::string winCmd = "CMD.exe /c " + cmd;
89  char* args = new char[winCmd.size()];
90  args[0] = 0;
91  strcpy(args, winCmd.c_str());
92  if (!CreateProcess(NULL, args, NULL, NULL, FALSE,
93  CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo)) {
94  delete args;
95  return (unsigned long)GetLastError();
96  }
97 
98  WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
99  if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) {
100  rc = 0;
101  }
102 
103  CloseHandle(ProcessInfo.hThread);
104  CloseHandle(ProcessInfo.hProcess);
105 
106  delete args;
107  return rc;
108 #else
109  return (unsigned long)system(cmd.c_str());
110 #endif
111 }
112 
113 /****************************************************************************/
114 
#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:75
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:50