Skip to content

Get reg.exe query output by c language #2

@BoQsc

Description

@BoQsc
/*
Merged from https://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output/646254#646254
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 


int main( int argc, char *argv[] )
{

  FILE *pipeStream;

  /* Open the command for reading. */
  pipeStream = popen("reg query \"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v Path", "r");
  if (pipeStream == NULL) {
    printf("Failed to run command\n" );
    exit(1);
  }

  /* Read the output a line at a time - output it. */
  int i = 0;
  int i2 = 0;
  char line[1035];
  char * delimiterCharacter = "%SystemRoot%";
  while (fgets(line, sizeof(line), pipeStream) != NULL) {
	if (i == 2){
		printf("%i. ", i);
		
		char * token = strstr (line, delimiterCharacter);
		while( token != NULL ) {
			
			printf( "\n %i %s", i2, token );
			token = strstr (NULL, delimiterCharacter);
			i2++;
		}
		
		
	}
	i++;
  }

  /* close */
  pclose(pipeStream);

  return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions