This should at least point you in the right direction: http://www.codeproject.com/Articles/14423/Getting-the-active-TCP-UDP-connections-using-the-G
Code: FOR /F "tokens=5 delims= " %%P IN ('netstat -ano ^| findstr :1688') DO taskkill.exe /f /PID %%P XP should be tokens=4
Implement timesurfer's logic Code: if not exist "%Windir%\system32\schtasks.exe" ( cls echo You don't have schtasks.exe installed echo. echo IORRT is not compatable with XP Home Edition echo Install XP Professional or schtasks.exe ping -n 10 127.0.0.1 >NUL goto MAINMENU )
It should have tskill though Code: FOR /F "tokens=4 delims= " %%P IN ('netstat -ano ^| findstr :1688') DO tskill.exe %%P /a You could also return the result and use WinAPI to do the kill: Code: BOOL TerminateProcess(DWORD dwProcessId, UINT uExitCode) { DWORD dwDesiredAccess = PROCESS_TERMINATE; BOOL bInheritHandle = FALSE; HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); if (hProcess == NULL) return FALSE; BOOL result = TerminateProcess(hProcess, uExitCode); CloseHandle(hProcess); return result; }
I'm not sure i understand what you mean: you want to retrieve PID from a batch and kill process with C++ , is that right ? if it is, a solution could exists but not clean ^^: you can retrieve PID with batch and write it in txt file, open it in C++ read it in a buffer and kill process, the remove txt file... otherwise, have a look at GetProcessId function -> http://msdn.microsoft.com/fr-fr/library/windows/desktop/ms683215(v=vs.85).aspx
I believe this should work: http://msdn.microsoft.com/en-us/lib....processstartinfo.redirectstandardoutput.aspx You just redirect the output to a string. If you wanted to make it easy though then just issue the batch commands with tskill or taskkill. One for XP and one for Windows Vista and up.
So you mean execute the batch command and return the result? You could pipe it. http://witnessmind.blogspot.co.uk/2011/12/execute-shell-command-from-within-c-c.html Code: FOR /F "tokens=5 delims= " %%P IN ('netstat -ano ^| findstr :1688') DO echo %%P It's untested if it will execute the above commands.
Well, i've done exactly what you mean in a project. You can redirect I/O using pipe, but there's also the _popen() function, use it like this: FILE *pRc=NULL; pRc=_popen("cscript.exe %windir%\\system32\\slmgr.vbs -dlv | FINDSTR /i \"count\"","r");//As system but auto-redirect output in a pipe fgets(rCBuff,sizeof(rCBuff),pRc);//Output will be stored in rcBuff it's the simplest way i found to redirectI/O in C Hope it helps..
You should probably avoid using Regex, especially in C++. You can approach it using the std::string class. Code: FILE *fp; char path[MAX_PATH]; fp = _popen("netstat -anno | findstr :1688", "r"); while (fgets(path, MAX_PATH, fp) != NULL) { std::string sProcessId(path); // Trim any whitespaces at the end of the string. sProcessId.erase(sProcessId.find_last_not_of(' ') + 1); // Only take the part of the string after the last space. sProcessId = sProcessId.substr(sProcessId.find_last_of(' ') + 1); // Convert the process Id string to an integer. int iProcessId = atoi(sProcessId.c_str()); } _pclose(fp);