1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "VPCDetect.h"
20 #include "DebugServices.h"
24 # pragma comment(lib, "wbemuuid.lib")
26 static BOOL g_doneCheck
= FALSE
;
27 static BOOL g_isVPC
= FALSE
;
31 IsVPCRunning( BOOL
* inVirtualPC
)
33 IWbemLocator
* pLoc
= 0;
34 IWbemServices
* pSvc
= 0;
35 IEnumWbemClassObject
* pEnumerator
= NULL
;
39 SC_HANDLE service
= NULL
;
40 SERVICE_STATUS status
;
48 // Find out if WMI is running
50 scm
= OpenSCManager( 0, 0, SC_MANAGER_CONNECT
);
51 err
= translate_errno( scm
, (OSStatus
) GetLastError(), kOpenErr
);
52 require_noerr( err
, exit
);
54 service
= OpenService( scm
, TEXT( "winmgmt" ), SERVICE_QUERY_STATUS
);
55 err
= translate_errno( service
, (OSStatus
) GetLastError(), kNotFoundErr
);
56 require_noerr( err
, exit
);
58 ok
= QueryServiceStatus( service
, &status
);
59 err
= translate_errno( ok
, (OSStatus
) GetLastError(), kAuthenticationErr
);
60 require_noerr( err
, exit
);
61 require_action( status
.dwCurrentState
== SERVICE_RUNNING
, exit
, err
= kUnknownErr
);
65 hres
= CoInitializeEx(0, COINIT_MULTITHREADED
);
66 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
69 // Initialize Security
71 hres
= CoInitializeSecurity(NULL
, -1, NULL
, NULL
, RPC_C_AUTHN_LEVEL_DEFAULT
, RPC_C_IMP_LEVEL_IMPERSONATE
, NULL
, EOAC_NONE
, NULL
);
72 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
75 // Obtain the initial locator to Windows Management on a particular host computer.
77 hres
= CoCreateInstance( CLSID_WbemLocator
, 0, CLSCTX_INPROC_SERVER
, IID_IWbemLocator
, (LPVOID
*) &pLoc
);
78 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
80 // Connect to the root\cimv2 namespace with the
81 // current user and obtain pointer pSvc
82 // to make IWbemServices calls.
84 hres
= pLoc
->ConnectServer( _bstr_t(L
"ROOT\\CIMV2"), NULL
, NULL
, 0, WBEM_FLAG_CONNECT_USE_MAX_WAIT
, 0, 0, &pSvc
);
85 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
87 // Set the IWbemServices proxy so that impersonation
88 // of the user (client) occurs.
90 hres
= CoSetProxyBlanket( pSvc
, RPC_C_AUTHN_WINNT
, RPC_C_AUTHZ_NONE
, NULL
, RPC_C_AUTHN_LEVEL_CALL
, RPC_C_IMP_LEVEL_IMPERSONATE
, NULL
, EOAC_NONE
);
91 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
93 // Use the IWbemServices pointer to make requests of WMI.
94 // Make requests here:
96 hres
= pSvc
->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * from Win32_BaseBoard"), WBEM_FLAG_FORWARD_ONLY
| WBEM_FLAG_RETURN_IMMEDIATELY
, NULL
, &pEnumerator
);
98 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
102 IWbemClassObject
* pInstance
= NULL
;
103 ULONG dwCount
= NULL
;
105 hres
= pEnumerator
->Next( WBEM_INFINITE
, 1, &pInstance
, &dwCount
);
110 BSTR strClassProp
= SysAllocString(L
"Manufacturer");
113 hr
= pInstance
->Get(strClassProp
, 0, &v
, 0, 0);
114 SysFreeString(strClassProp
);
116 // check the HRESULT to see if the action succeeded.
118 if (SUCCEEDED(hr
) && (V_VT(&v
) == VT_BSTR
))
120 wchar_t * wstring
= wcslwr( V_BSTR( &v
) );
122 if (wcscmp( wstring
, L
"microsoft corporation" ) == 0 )
130 } while (hres
== WBEM_S_NO_ERROR
);
151 CloseServiceHandle( service
);
156 CloseServiceHandle( scm
);
161 dlog( kDebugLevelTrace
, "Virtual PC detected" );