显示调用的程序的返回参数和执行时间 cb

    技术2025-04-29  22

     

    今天 学习   codeblocks/trunk/src/tools/ConsoleRunner/main.cpp

     

    cb_console_runner.exe

    显示调用的程序的返回参数和执行时间。

     

     

     

    汉化了一下,然后想用 VC 编译的小一点在 WIN 下运行环境 简单点

    结果 VC 没有 其中的 一个 查找时间函数,后来 找到了 别人编写的 替用函数加了进去 

     

     

    /* * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3 * http://www.gnu.org/licenses/gpl-3.0.html * * $Revision: 6159 $ * $Id: main.cpp 6159 2010-02-15 04:08:57Z biplab $ * $HeadURL: file:///svnroot/repos/codeblocks/trunk/src/tools/ConsoleRunner/main.cpp $ */ #define __WXMSW__ // 单独编译 WXMSW #include <stdio.h> #include <stdlib.h> #include <time.h> // VC编译修改了 #ifdef __WXMSW__ #include <windows.h> #include <conio.h> #define wait_key getch #else #define wait_key getchar #endif #if defined(__unix__) || defined(__unix) #include <sys/wait.h> #endif #include <string.h> #ifdef __MINGW32__ int _CRT_glob = 0; #endif bool hasSpaces(const char* str) { char last = 0; while (str && *str) { if ((*str == ' ' || *str == '/t') && last != '//') return true; last = *str++; } return false; } int execute_command(char *cmdline) { #ifdef __WXMSW__ //Windows's system() seems to not be able to handle parentheses in //the path, so we have to launch the program a different way. SetConsoleTitle(cmdline); STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); // Start the child process. CreateProcess( NULL, TEXT(cmdline), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ); // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Get the return value of the child process DWORD ret; GetExitCodeProcess( pi.hProcess, &ret ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); return ret; #else int ret = system(cmdline); if(WIFEXITED(ret)) { return WEXITSTATUS(ret); } else { return -1; } #endif } int gettimeofday(struct timeval *tp, void *tzp) // VC编译修改了,VC没有 gettimeofday() 添加了 { time_t clock; struct tm tm; SYSTEMTIME wtm; GetLocalTime(&wtm); tm.tm_year = wtm.wYear - 1900; tm.tm_mon = wtm.wMonth - 1; tm.tm_mday = wtm.wDay; tm.tm_hour = wtm.wHour; tm.tm_min = wtm.wMinute; tm.tm_sec = wtm.wSecond; tm. tm_isdst = -1; clock = mktime(&tm); tp->tv_sec = clock; tp->tv_usec = wtm.wMilliseconds * 1000; return (0); } int main(int argc, char** argv) { if (argc < 2) { printf("显示调用的程序的返回参数和执行时间。/n/n"); printf("Usage: cb_console_runner <filename> <args ...>/n"); printf("用 法: cb_console_runner <可执行文件> <参数 ...>/n"); return 1; } // count size of arguments // 参数的计数大小 size_t fullsize = 0; for (int i = 1; i < argc; ++i) { fullsize += strlen(argv[i]); } // add some slack for spaces between args plus quotes around executable // 添加可执行程序周围一些参数加引号之间的空间懈怠 fullsize += argc + 32; char* cmdline = new char[fullsize]; memset(cmdline, 0, fullsize); // 1st arg (executable) enclosed in quotes to support filenames with spaces // 第一个参数(可执行文件),以支持在引号用空格文件名 bool sp = hasSpaces(argv[1]); if (sp) strcat(cmdline, "/""); strcat(cmdline, argv[1]); if (sp) strcat(cmdline, "/""); strcat(cmdline, " "); for (int i = 2; i < argc; ++i) { sp = hasSpaces(argv[i]); if (sp) strcat(cmdline, "/""); strcat(cmdline, argv[i]); if (sp) strcat(cmdline, "/""); strcat(cmdline, " "); } timeval tv; gettimeofday(&tv, NULL); double cl = tv.tv_sec + (double)tv.tv_usec / 1000000; int ret = execute_command(cmdline); gettimeofday(&tv, NULL); cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl; printf("/nProcess returned %d (0x%X) execution time : %0.3f s", ret, ret, cl); printf("/n进程返回 %d (0x%X) 执行时间 : %0.3f 秒", ret, ret, cl); printf ( "/n请按" #ifdef __WXMSW__ "任意键" #else "ENTER" #endif "继续.../n" ); wait_key(); // 前面宏定义 #define wait_key getch delete[] cmdline; return ret; }  

     

    /*- * Copyright (C) 2008 mymtom (mymtom@hotmail.com) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: src/lib/libtrace/gettimeofday.c,v 1.1 2008/03/28 12:08:44 mymtom Exp $ */ #include <time.h> #ifdef WIN32 # include <windows.h> #else # include <sys/time.h> #endif #ifdef WIN32 int gettimeofday(struct timeval *tp, void *tzp) { time_t clock; struct tm tm; SYSTEMTIME wtm; GetLocalTime(&wtm); tm.tm_year = wtm.wYear - 1900; tm.tm_mon = wtm.wMonth - 1; tm.tm_mday = wtm.wDay; tm.tm_hour = wtm.wHour; tm.tm_min = wtm.wMinute; tm.tm_sec = wtm.wSecond; tm. tm_isdst = -1; clock = mktime(&tm); tp->tv_sec = clock; tp->tv_usec = wtm.wMilliseconds * 1000; return (0); } #endif /* WIN32 */ 

     

     

    最新回复(0)