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.
17 Change History (most recent first):
19 $Log: VPCDetect.cpp,v $
20 Revision 1.3 2006/08/14 23:25:20 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.2 2006/02/26 19:31:05 herscher
24 <rdar://problem/4455038> Bonjour For Windows takes 90 seconds to start. This was caused by a bad interaction between the VirtualPC check, and the removal of the WMI dependency. The problem was fixed by: 1) checking to see if WMI is running before trying to talk to it. 2) Retrying the VirtualPC check every 10 seconds upon failure, stopping after 10 unsuccessful tries.
26 Revision 1.1 2005/11/27 20:21:16 herscher
27 <rdar://problem/4210580> Workaround Virtual PC bug that incorrectly modifies incoming mDNS packets
32 #include "VPCDetect.h"
33 #include "DebugServices.h"
37 # pragma comment(lib, "wbemuuid.lib")
39 static BOOL g_doneCheck
= FALSE
;
40 static BOOL g_isVPC
= FALSE
;
44 IsVPCRunning( BOOL
* inVirtualPC
)
46 IWbemLocator
* pLoc
= 0;
47 IWbemServices
* pSvc
= 0;
48 IEnumWbemClassObject
* pEnumerator
= NULL
;
52 SC_HANDLE service
= NULL
;
53 SERVICE_STATUS status
;
61 // Find out if WMI is running
63 scm
= OpenSCManager( 0, 0, SC_MANAGER_CONNECT
);
64 err
= translate_errno( scm
, (OSStatus
) GetLastError(), kOpenErr
);
65 require_noerr( err
, exit
);
67 service
= OpenService( scm
, TEXT( "winmgmt" ), SERVICE_QUERY_STATUS
);
68 err
= translate_errno( service
, (OSStatus
) GetLastError(), kNotFoundErr
);
69 require_noerr( err
, exit
);
71 ok
= QueryServiceStatus( service
, &status
);
72 err
= translate_errno( ok
, (OSStatus
) GetLastError(), kAuthenticationErr
);
73 require_noerr( err
, exit
);
74 require_action( status
.dwCurrentState
== SERVICE_RUNNING
, exit
, err
= kUnknownErr
);
78 hres
= CoInitializeEx(0, COINIT_MULTITHREADED
);
79 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
82 // Initialize Security
84 hres
= CoInitializeSecurity(NULL
, -1, NULL
, NULL
, RPC_C_AUTHN_LEVEL_DEFAULT
, RPC_C_IMP_LEVEL_IMPERSONATE
, NULL
, EOAC_NONE
, NULL
);
85 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
88 // Obtain the initial locator to Windows Management on a particular host computer.
90 hres
= CoCreateInstance( CLSID_WbemLocator
, 0, CLSCTX_INPROC_SERVER
, IID_IWbemLocator
, (LPVOID
*) &pLoc
);
91 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
93 // Connect to the root\cimv2 namespace with the
94 // current user and obtain pointer pSvc
95 // to make IWbemServices calls.
97 hres
= pLoc
->ConnectServer( _bstr_t(L
"ROOT\\CIMV2"), NULL
, NULL
, 0, WBEM_FLAG_CONNECT_USE_MAX_WAIT
, 0, 0, &pSvc
);
98 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
100 // Set the IWbemServices proxy so that impersonation
101 // of the user (client) occurs.
103 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
);
104 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
106 // Use the IWbemServices pointer to make requests of WMI.
107 // Make requests here:
109 hres
= pSvc
->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * from Win32_BaseBoard"), WBEM_FLAG_FORWARD_ONLY
| WBEM_FLAG_RETURN_IMMEDIATELY
, NULL
, &pEnumerator
);
111 require_action( SUCCEEDED( hres
), exit
, err
= kUnknownErr
);
115 IWbemClassObject
* pInstance
= NULL
;
116 ULONG dwCount
= NULL
;
118 hres
= pEnumerator
->Next( WBEM_INFINITE
, 1, &pInstance
, &dwCount
);
123 BSTR strClassProp
= SysAllocString(L
"Manufacturer");
126 hr
= pInstance
->Get(strClassProp
, 0, &v
, 0, 0);
127 SysFreeString(strClassProp
);
129 // check the HRESULT to see if the action succeeded.
131 if (SUCCEEDED(hr
) && (V_VT(&v
) == VT_BSTR
))
133 wchar_t * wstring
= wcslwr( V_BSTR( &v
) );
135 if (wcscmp( wstring
, L
"microsoft corporation" ) == 0 )
143 } while (hres
== WBEM_S_NO_ERROR
);
164 CloseServiceHandle( service
);
169 CloseServiceHandle( scm
);
174 dlog( kDebugLevelTrace
, "Virtual PC detected" );