-2

I want to call tshark.exe from a c++ script via ShellExecute. Is there any way to parse cmd arguments to the application? e.g. specify output file like this

tshark -w output.pcap

Here is the code

#include <Windows.h>
#include <shellapi.h>

int main()
{
    ShellExecute(NULL, "open", "tshark.exe", NULL, "C:\Program Files\Wireshark", SW_SHOWDEFAULT);
    return 0;
}
3
  • 1
    learn.microsoft.com/en-us/windows/win32/api/shellapi/… fourth argument is parameters Commented Dec 12, 2019 at 17:21
  • I've read it, but I don't quite understand the syntax. How an I supposed to add -w output.pcap to this? Commented Dec 12, 2019 at 19:54
  • Code doesn't compile. Go here before moving forward. Commented Dec 12, 2019 at 19:59

1 Answer 1

0

The 4th parameter to ShellExecute() passes command-line arguments to the new process, eg:

ShellExecute(NULL, "open", "tshark.exe", "-w output.pcap", "C:\\Program Files\\Wireshark", SW_SHOWDEFAULT);

Though, you really should be using CreatProcess() instead (ShellExecute() is just going to call it anyway):

STARTUPINFO si = {};
PROCESS_INFORMATION pi = {};

si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWDEFAULT;

char cmd[] = "tshark.exe -w output.pcap";
if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, "C:\\Program Files\\Wireshark", &si, &pi))
{
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks. I tried it before but for some reason it didn't work. Don't know why. Now everything is OK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.