Recent

Author Topic: control of a system if and a virtual machine or not  (Read 1364 times)

codezero

  • Newbie
  • Posts: 2
control of a system if and a virtual machine or not
« on: January 19, 2019, 02:33:33 pm »
I should port this application in lazarus to see if the system is under a virtual machine:
Code: [Select]
#include <stdio.h>
#ifdef _WIN32
#include <intrin.h>
#else
#include <cpuid.h>
#endif

int isHypervisor(void)
{
#ifdef _WIN32
    int cpuinfo[4];
    __cpuid(cpuinfo, 1);
    if (cpuinfo[2] >> 31 & 1)
        return 1;
#else
    unsigned int eax, ebx, ecx, edx;
    __get_cpuid (1, &eax, &ebx, &ecx, &edx);
    if (ecx >> 31 & 1)
        return 1;
#endif
    return 0;
}

int main(int argc, char **argv)
{
    if (isHypervisor())
        printf("Virtual machine: yes\n");
    else
        printf("Virtual machine: no\n"); /* actually "maybe */

    return 0;
}
 


could someone explain to me how to do this?

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: control of a system if and a virtual machine or not
« Reply #1 on: January 19, 2019, 08:27:42 pm »
could someone explain to me how to do this?
The code above says that for Win32 you need to examine intrin.h deeper, and for other OSes you need to examine cpuid.h. What you showed is not enough.

Maybe easier way would be to try to adapt this Delphi code:
http://www.delphigeist.com/2011/01/is-your-app-running-in-virtual-machine.html
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018