Skip to main content
added 746 characters in body
Source Link
user3629249
  • 2.8k
  • 11
  • 10

a typical problem in the OPs codeRegarding:

size_t totalProcessesMatched = 0;
size_t totalProcessesTerminated = 0;
...
printf("Info: total matching processes: %d, total terminated: %d.\n", 
       totalProcessesMatched, 
       totalProcessesTerminated);

the printf() is trying to output a int, but the variables to output are size_t which should be output using a %zu format specifier.

If you compiler is not telling you about these kinds of problems, then enable the warnings until it does.

regarding;

fprintf(stderr, "processkiller.exe PROCESS_NAME");  

This would be 1) left in the stderr output stream because the format string does not have a \n on the end of it. 2) any program can be executed via any name so hard coding the name is a bad idea. Suggest:

fprintf(stderr, "%s PROCESS_NAME\n", argv[0] );

regarding:

fputs("Error: could not get the process snapshot.", stderr);

strongly suggest making use of the get_last_error() facility and printing the actual error text to stderr rather than some 'random' error message

regarding;

if (strcmp(entry.szExeFile, argv[1]) == 0) {

this does not compile because the statement:

#include <string.h>

is missing.

a typical problem in the OPs code:

size_t totalProcessesMatched = 0;
size_t totalProcessesTerminated = 0;
...
printf("Info: total matching processes: %d, total terminated: %d.\n", 
       totalProcessesMatched, 
       totalProcessesTerminated);

the printf() is trying to output a int, but the variables to output are size_t which should be output using a %zu format specifier.

If you compiler is not telling you about these kinds of problems, then enable the warnings until it does.

Regarding:

size_t totalProcessesMatched = 0;
size_t totalProcessesTerminated = 0;
...
printf("Info: total matching processes: %d, total terminated: %d.\n", 
       totalProcessesMatched, 
       totalProcessesTerminated);

the printf() is trying to output a int, but the variables to output are size_t which should be output using a %zu format specifier.

If you compiler is not telling you about these kinds of problems, then enable the warnings until it does.

regarding;

fprintf(stderr, "processkiller.exe PROCESS_NAME");  

This would be 1) left in the stderr output stream because the format string does not have a \n on the end of it. 2) any program can be executed via any name so hard coding the name is a bad idea. Suggest:

fprintf(stderr, "%s PROCESS_NAME\n", argv[0] );

regarding:

fputs("Error: could not get the process snapshot.", stderr);

strongly suggest making use of the get_last_error() facility and printing the actual error text to stderr rather than some 'random' error message

regarding;

if (strcmp(entry.szExeFile, argv[1]) == 0) {

this does not compile because the statement:

#include <string.h>

is missing.

Source Link
user3629249
  • 2.8k
  • 11
  • 10

a typical problem in the OPs code:

size_t totalProcessesMatched = 0;
size_t totalProcessesTerminated = 0;
...
printf("Info: total matching processes: %d, total terminated: %d.\n", 
       totalProcessesMatched, 
       totalProcessesTerminated);

the printf() is trying to output a int, but the variables to output are size_t which should be output using a %zu format specifier.

If you compiler is not telling you about these kinds of problems, then enable the warnings until it does.