Advertisement
teknoraver

wingetty.c

Nov 18th, 2018
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     char devname[MAX_PATH] = "\\\\.\\";
  8.     HANDLE serial;
  9.     BOOL ret;
  10.     PROCESS_INFORMATION pi;
  11.     DCB dcb = {
  12.         .DCBlength = sizeof(DCB),
  13.         .BaudRate = CBR_115200,
  14.         .ByteSize = 8,
  15.         .Parity = NOPARITY,
  16.         .StopBits = ONESTOPBIT,
  17.     };
  18.  
  19.     if (argc != 2) {
  20.         puts("no device supplied");
  21.         return 1;
  22.     }
  23.  
  24.     if (strlen(devname) + strlen(argv[1]) > MAX_PATH) {
  25.         puts("invalid device");
  26.         return 1;
  27.     }
  28.  
  29.     strcat(devname, argv[1]);
  30.  
  31.     serial = CreateFile(devname, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  32.                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  33.  
  34.     if (serial == INVALID_HANDLE_VALUE) {
  35.         puts("CreateFile() failed");
  36.         return 1;
  37.     }
  38.     SetCommState(serial, &dcb);
  39.  
  40.     /* Method 1: via STARTUPINFO.hStd{Input,Output,Error} */
  41.     STARTUPINFO si = {
  42.         .cb = sizeof(si),
  43.         .dwFlags = STARTF_USESTDHANDLES,
  44.         .hStdInput = serial,
  45.         .hStdOutput = serial,
  46.         .hStdError = serial,
  47.     }
  48.  
  49.     ret = CreateProcess(NULL, "cmd.exe", NULL, NULL, FALSE,
  50.                 HIGH_PRIORITY_CLASS, NULL, "C:\\", &si, &pi);
  51.  
  52.     /* Method 2: via handle inheritance */
  53.     if (!SetStdHandle(STD_INPUT_HANDLE, serial)) {
  54.         puts("SetStdHandle(STD_INPUT_HANDLE) failed");
  55.         return 1;
  56.     }
  57.     if (!SetStdHandle(STD_OUTPUT_HANDLE, serial)) {
  58.         puts("SetStdHandle(STD_OUTPUT_HANDLE) failed");
  59.         return 1;
  60.     }
  61.     if (!SetStdHandle(STD_ERROR_HANDLE, serial)) {
  62.         puts("SetStdHandle(STD_ERROR_HANDLE) failed");
  63.         return 1;
  64.     }
  65.  
  66.     ret = CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE,
  67.                 HIGH_PRIORITY_CLASS, NULL, "C:\\", &si, &pi);
  68.  
  69.     if (!ret) {
  70.         puts("CreateProcess() failed");
  71.         return 1;
  72.     }
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement