Hey guys, I'm creating a program that prompts the user to inputt the name of the process that they wish to end, but the problem is, the system("TASKKILL") function won't accept variables.

for example:

string cProcess;
cout << "Enter the name of the process  that you would wish to end" << endl;
cin >> cProcess;
system("TASKKILL " cProcess "/t"); //or something similar?

Anyone ever tried to do something similar before?
Any help would be greatly appreciated. :)

Recommended Answers

All 4 Replies

The system() function does not accept but one string parameter, so you have to make the command all one big string.

string command;
command = "TASKKILL " + cProcess + "/t"; // or something like that
system(command.c_str());

You can use execv also.

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
  char *args[3];
  args[0]="sample.txt" ;
  args[1]=NULL;
  printf("This is before execv \n");
  execv("c:\\windows\\notepad.exe", args);
  return 0;
}

Read more about -
exec

You can use execv also.

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
  char *args[3];
  args[0]="sample.txt" ;
  args[1]=NULL;
  printf("This is before execv \n");
  execv("c:\\windows\\notepad.exe", args);
  return 0;
}

Read more about -
exec

One minor thing....isn't unistd.h a Unix/Linux header file??
I don't think any of the windows compilers ship with unistd.h do they?? (except perhaps Cygwin!).
If memory serves correctly, on windows, you need process.h for the execv() function.
I haven't tested it, but I think swapping unistd.h for process.h should work for anybody trying to build adataposts listing on the windows platform!

Cheers for now,
Jas.

>>I don't think any of the windows compilers ship with unistd.h do they?
cygwin has it and all the other *nix header files. That can be configured with g++ and Code::Blocks. The Microsoft compilers do not support it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.