std::wstring TestUtils::exec(LPCTSTR szCmd) { FILE* pipe = _tpopen(szCmd, _T("rt")); if (!pipe) return L"ERROR"; wchar_t buffer[4096]; std::wstring result; while(!feof(pipe)) { if(fgetws(buffer, 4096, pipe) != NULL) result += buffer; } wtrim(result); _pclose(pipe); return result; }
int _tmain(int argc, TCHAR ** argv) { TCHAR * buf; int bufsize; int i; int offset; bufsize = 0; for(i = 1; i < argc; i++) { bufsize += _tcslen(argv[i]) + 1; } if (!bufsize) { return -1; } if (_tcsstr(argv[1], "--winetest") && (argc == 3)) { char psBuffer[128]; char psBuffer2[128]; char *nlptr2; char cmd[255]; char test[300]; FILE *pPipe; FILE *pPipe2; /* get available tests */ strcpy(cmd, argv[2]); strcat(cmd, " --list"); pPipe = _tpopen(cmd, "r"); if (pPipe != NULL) { while(fgets(psBuffer, 128, pPipe)) { if (psBuffer[0] == ' ') { strcpy(cmd, argv[2]); strcat(cmd, " "); strcat(cmd, psBuffer+4); /* run the current test */ strcpy(test, "\n\nRunning "); strcat(test, cmd); OutputDebugStringA(test); pPipe2 = _popen(cmd, "r"); if (pPipe2 != NULL) { while(fgets(psBuffer2, 128, pPipe2)) { nlptr2 = strchr(psBuffer2, '\n'); if (nlptr2) *nlptr2 = '\0'; puts(psBuffer2); OutputDebugStringA(psBuffer2); } _pclose(pPipe2); } } } _pclose(pPipe); } } else if (_tcsstr(argv[1], "--process") && (argc == 3)) { char psBuffer[128]; FILE *pPipe; pPipe = _tpopen(argv[2], "r"); if (pPipe != NULL) { while(fgets(psBuffer, 128, pPipe)) { puts(psBuffer); OutputDebugStringA(psBuffer); } _pclose(pPipe); } } else { buf = HeapAlloc(GetProcessHeap(), 0, (bufsize+1) * sizeof(TCHAR)); if (!buf) { return -1; } offset = 0; for(i = 1; i < argc; i++) { int length = _tcslen(argv[i]); _tcsncpy(&buf[offset], argv[i], length); offset += length; if (i + 1 < argc) { buf[offset] = _T(' '); } else { buf[offset] = _T('\n'); buf[offset+1] = _T('\0'); } offset++; } _putts(buf); OutputDebugString(buf); HeapFree(GetProcessHeap(), 0, buf); } return 0; }
/** Prints packet timestaps regardless of format*/ int _tmain(int argc, _TCHAR* argv[]) { char errbuf[PCAP_ERRBUF_SIZE]; _TCHAR cmd[1024]; _TCHAR tshark_path[MAX_PATH]; _TCHAR file_path[MAX_PATH]; /* Load Npcap and its functions. */ if (!LoadNpcapDlls()) { fprintf(stderr, "Couldn't load Npcap\n"); exit(1); } if ( argc != 3 ) { _tprintf(_T("Prints packet timestaps regardless of format.\n")); _tprintf(_T("Usage:\n\t%s <tshark path> <trace file>\n"), argv[0]); return 1; } // conversion to short path name in case there are spaces if ( ! GetShortPathName(argv[1], tshark_path, MAX_PATH) || ! GetShortPathName(argv[2], file_path, MAX_PATH) ) { _tprintf(_T("Failed to convert paths to short form.")); return 1; } // create tshark command, which will make the trace conversion and print in libpcap format to stdout if ( _stprintf_s(cmd, 1024, _T("%s -r %s -w - -F libpcap"), tshark_path, file_path) < 0 ) { _tprintf(_T("Failed to create command\n")); return 1; } // start tshark FILE *tshark_out = _tpopen(cmd, _T("rb")); if ( tshark_out == NULL ) { strerror_s(errbuf, PCAP_ERRBUF_SIZE, errno); printf("Failed run tshark: %s\n", errbuf); _tprintf(_T("Command: %s"), cmd); return 1; } // open stdout from tshark pcap_t *pcap = pcap_fopen_offline(tshark_out, errbuf); if ( pcap == NULL ) { printf("Error opening stream from tshark: %s\n", errbuf); return 1; } // print information about every packet int trace struct pcap_pkthdr hdr; while ( pcap_next(pcap, &hdr) ) { printf("packet: ts: %u.%06u, len: %4u, caplen: %4u\n", hdr.ts.tv_sec, hdr.ts.tv_usec, hdr.len, hdr.caplen); } // clean up pcap_close(pcap); _pclose(tshark_out); return 0; }