Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- // Define Buffer Size
- #define BUF4 4096
- #define BUF5 4096
- // Get Hyper-V Status Function
- void GetHyperVStatus() {
- STARTUPINFO si;
- SECURITY_ATTRIBUTES sa;
- PROCESS_INFORMATION pi;
- HANDLE g_hChildStd_OUT_Rd = NULL;
- HANDLE g_hChildStd_OUT_Wr = NULL;
- DWORD dwRead;
- char chBuf[BUF4];
- char outbuf[BUF5] = { 0 };
- ZeroMemory(&sa, sizeof(sa));
- sa.nLength = sizeof(sa);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = NULL;
- // Create A Pipe For The Child Process's STDOUT
- if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0))
- {
- puts("Error: CreatePipe Failed!");
- printf("Error Code: %d\n", GetLastError());
- return;
- }
- // Ensure The Read Handle To The Pipe For STDOUT Is Not Inherited
- if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
- {
- puts("Error: SetHandleInformation Failed!");
- printf("Error Code: %d\n", GetLastError());
- return;
- }
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- si.hStdError = g_hChildStd_OUT_Wr;
- si.hStdOutput = g_hChildStd_OUT_Wr;
- si.dwFlags |= STARTF_USESTDHANDLES;
- ZeroMemory(&pi, sizeof(pi));
- // Start The Child Process
- if (!CreateProcess("C:\\Windows\\System32\\bcdedit.exe", "C:\\Windows\\System32\\bcdedit.exe /enum {default}", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
- {
- if (!CreateProcess("C:\\Windows\\Sysnative\\bcdedit.exe", "C:\\Windows\\Sysnative\\bcdedit.exe /enum {default}", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
- {
- puts("Error: CreateProcess Failed!");
- printf("Error Code: %d\n", GetLastError());
- return;
- }
- }
- // Close Child Process's STDOUT Write Handle
- CloseHandle(g_hChildStd_OUT_Wr);
- // Read STDOUT From Child Process
- while(1) {
- if (!ReadFile(g_hChildStd_OUT_Rd, chBuf, sizeof(chBuf), &dwRead, NULL))
- {
- break;
- }
- if (dwRead > 0) {
- chBuf[dwRead - 1] = '\0';
- strcat_s(outbuf, sizeof(outbuf), chBuf);
- }
- }
- // Check Hyper-V Status
- if (strstr(outbuf, "hypervisorlaunchtype Auto") != NULL) {
- puts("hypervisorlaunchtype Auto");
- }
- else if (strstr(outbuf, "hypervisorlaunchtype Off") != NULL) {
- puts("hypervisorlaunchtype Off");
- }
- else {
- puts("Unknown Hyper-V Status");
- }
- // Close Child Process's STDOUT Read Handle
- CloseHandle(g_hChildStd_OUT_Rd);
- // Wait Until Child Process Exits
- WaitForSingleObject(pi.hProcess, INFINITE);
- // Close Process And Thread Handles
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement