0

I'm trying to get the logged in sessions on a remote PC using the below command:

.\psexec.exe -s -i -h -n 10 \\<remote machine> -u <username> -p <password> -accepteula -nobanner query session

psexec.exe is the SysInternals tool and is located in the current working directory.

It works fine via the command prompt, and shows a list of 4 active sessions on the remote machine (censored with #):

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
>########                                    #  Disc
 #######              #####                  #  Active
 ################                        #####  Listen
 #######                                 #####  Listen

However, if I try to call it via _popen in C++, it runs but doesn't find any sessions:

SESSIONNAME USERNAME ID STATE TYPE DEVICE

I'm calling it via this C++ function (the codebase uses char* extensively hence the type):

char* concat(const char *s1, const char *s2)
{
    char* result = (char*)malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

char* ExecCmd(
    char* cmd
) {
    FILE* command_result_file = _popen(cmd, "rt"); //send the command to the cmd and return a pointer to the command result
    char line[256]; //a buffer to read from the file
    char* result = "";
    while(fgets(line, 256, command_result_file)) {
        result = concat(result, line); //read and print all lines one by one
    }
    _pclose(command_result_file);
    return result;
}

The above function works fine with commands like dir and ping 8.8.8.8, and if I run one of those after psexec in the same ExecCmd call then I get its output after the psexec output, so it's not just missing the end of the message. I've also tried redirecting stderr to stdout, which works (I see errors with other commands) but in this case no error messages show up. I've tried running the C++ application as an admin too, it makes no difference.

Does anyone know how to fix this, or another way to call the command above and get stdout?

e: Should've specified, I use a raw string to call the command in C++:

R"(.\psexec.exe -s -i -h -n 10 \\<remote machine> -u <username> -p <password> -accepteula -nobanner query session)"

I've also tried escaped backslashes:

".\\psexec.exe -s -i -h -n 10 \\\\<remote machine> -u <username> -p <password> -accepteula -nobanner query session"

I've also added concat() to the snippet.

3
  • Probably this is because \ is a special character in string literals used for escape sequences: so \\ expands to single \ and you need \\\\ in a string if you want \\. Commented Nov 6, 2023 at 12:27
  • And what is the concat() function in concat(result, line)? Please provide a complete example. Commented Nov 6, 2023 at 12:30
  • I accounted for the backslashes, I've tried both raw strings and escaping them. I added concat() to the snippet. Commented Nov 6, 2023 at 13:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.