Option Explicit

'---------------------------------------------------------------------------------------
' Module    : mAntiVMWare
' Author    : Karcrack
' Now$      : 020810
' Used for? : Known if being Virtualized inside VMWARE
' Original C source:
'    bool IsVMWare()
'    {
'      unsigned long _EBX;
'      __try
'      {
'        __asm
'        {
'          // Run the magic code sequence
'          push ebx
'          mov eax, 0x564D5868
'          mov ebx, 0x8685D465 // Ensure EBX doesn't contain 0x564D5868 
'          mov ecx, 10 // The command for obtaining VMWare version information
'          mov dx, 0x5658
'          in eax, dx
'          mov _EBX, ebx
'          pop ebx
'        };
'      }
'      __except(1)
'      {
'        // An exception occured, we ain't in VMWare
'        return false;
'      }
'      // The code was executed successfuly, check for the magic value
'      return _EBX == 0x564D5868;
'    }
'---------------------------------------------------------------------------------------

'KERNEL32
Private Declare Function SetUnhandledExceptionFilter Lib "KERNEL32" (ByVal lpTopLevelExceptionFilter As Long) As Long
'USER32
Private Declare Function CallWindowProcW Lib "USER32" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private m_bFlag                 As Boolean

Public Function IsVMWare() As Boolean
    On Error Resume Next
    Dim cCode(2)                As Currency
    Dim lOldSEH                 As Long
    Dim lRet                    As Long

    If App.LogMode = 0 Then MsgBox "Test only compiled": Exit Function

    m_bFlag = True
    lOldSEH = SetUnhandledExceptionFilter(AddressOf ExceptionHandler)

    cCode(0) = 733054770867134.2675@
    cCode(1) = 4606227.4004@
    cCode(2) = 661819130486985.3798@

    lRet = CallWindowProcW(VarPtr(cCode(0)), 0&, 0&, 0&, 0&)

    Call SetUnhandledExceptionFilter(lOldSEH)

    If m_bFlag = True Then IsVMWare = (lRet = &H564D5868)
End Function

Public Function ExceptionHandler(ByRef uException As Long) As Long
    m_bFlag = False: ExceptionHandler = -1
    ' VB Will process our error 
    Call Mid$(vbNullString, 0)
End Function