Advertisement
rc-chuah

GetHyperVStatus2

May 3rd, 2022
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. // Define Buffer Size
  5. #define BUF4 4096
  6. #define BUF5 4096
  7.  
  8. // Get Hyper-V Status Function
  9. void GetHyperVStatus() {
  10.     STARTUPINFOA si;
  11.     SECURITY_ATTRIBUTES sa;
  12.     PROCESS_INFORMATION pi;
  13.     HANDLE g_hChildStd_OUT_Rd = NULL;
  14.     HANDLE g_hChildStd_OUT_Wr = NULL;
  15.     DWORD dwRead;
  16.     char chBuf[BUF4];
  17.     char outbuf[BUF5] = { 0 };
  18.     ZeroMemory(&sa, sizeof(sa));
  19.     sa.nLength = sizeof(sa);
  20.     sa.bInheritHandle = TRUE;
  21.     sa.lpSecurityDescriptor = NULL;
  22.     // Create A Pipe For The Child Process's STDOUT
  23.     if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0))
  24.     {
  25.         puts("Error: CreatePipe Failed!");
  26.         printf("Error Code: %d\n", GetLastError());
  27.         return;
  28.     }
  29.     // Ensure The Read Handle To The Pipe For STDOUT Is Not Inherited
  30.     if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
  31.     {
  32.         puts("Error: SetHandleInformation Failed!");
  33.         printf("Error Code: %d\n", GetLastError());
  34.         return;
  35.     }
  36.     ZeroMemory(&si, sizeof(si));
  37.     si.cb = sizeof(si);
  38.     si.hStdError = g_hChildStd_OUT_Wr;
  39.     si.hStdOutput = g_hChildStd_OUT_Wr;
  40.     si.dwFlags |= STARTF_USESTDHANDLES;
  41.     ZeroMemory(&pi, sizeof(pi));
  42.     // Start The Child Process
  43.     if (!CreateProcessA("C:\\Windows\\System32\\bcdedit.exe", "C:\\Windows\\System32\\bcdedit.exe /enum {default}", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
  44.     {
  45.         if (!CreateProcessA("C:\\Windows\\Sysnative\\bcdedit.exe", "C:\\Windows\\Sysnative\\bcdedit.exe /enum {default}", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
  46.         {
  47.             puts("Error: CreateProcess Failed!");
  48.             printf("Error Code: %d\n", GetLastError());
  49.             return;
  50.         }
  51.     }
  52.     // Close Child Process's STDOUT Write Handle
  53.     CloseHandle(g_hChildStd_OUT_Wr);
  54.     // Read STDOUT From Child Proces
  55.     while(1) {
  56.        if (!ReadFile(g_hChildStd_OUT_Rd, chBuf, sizeof(chBuf), &dwRead, NULL))
  57.        {
  58.            break;
  59.        }
  60.        if (dwRead > 0) {
  61.            chBuf[dwRead - 1] = '\0';
  62.            strcat_s(outbuf, sizeof(outbuf), chBuf);
  63.        }
  64.     }
  65.     // Check Hyper-V Status
  66.     if (strstr(outbuf, "hypervisorlaunchtype    Auto") != NULL) {
  67.       puts("hypervisorlaunchtype     Auto");
  68.     }
  69.     else if (strstr(outbuf, "hypervisorlaunchtype    Off") != NULL) {
  70.       puts("hypervisorlaunchtype    Off");
  71.     }
  72.     else {
  73.       puts("Unknown Hyper-V Status");
  74.     }
  75.     // Close Child Process's STDOUT Read Handle
  76.     CloseHandle(g_hChildStd_OUT_Rd);
  77.     // Wait Until Child Process Exits
  78.     WaitForSingleObject(pi.hProcess, INFINITE);
  79.     // Close Process And Thread Handles
  80.     CloseHandle(pi.hProcess);
  81.     CloseHandle(pi.hThread);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement