2025-11-28 00:35:46 +09:00

180 lines
3.6 KiB
C++

//***************************************************************************
//
// UTILS.CPP
//
// Module: WMI Instance provider sample code
//
// Purpose: General purpose utilities.
//
// Copyright (c) Microsoft Corporation, All Rights Reserved
//
//***************************************************************************
#include <objbase.h>
#include "sample.h"
//***************************************************************************
//
// CreateInst
//
// Purpose: Creates a new instance and sets
// the inital values of the properties.
//
// Return: S_OK if all is well, otherwise an error code is returned
//
//***************************************************************************
SCODE CreateInst(IWbemServices * pNamespace, LPWSTR pKey, long lVal,
IWbemClassObject ** pNewInst,
WCHAR * pwcClassName,
IWbemContext *pCtx)
{
SCODE sc;
IWbemClassObject * pClass = NULL;
sc = pNamespace->GetObject(pwcClassName, 0, pCtx, &pClass, NULL);
if(sc != S_OK)
return WBEM_E_FAILED;
sc = pClass->SpawnInstance(0, pNewInst);
pClass->Release();
if(FAILED(sc))
return sc;
VARIANT v;
// Set the key property value.
v.vt = VT_BSTR;
v.bstrVal = SysAllocString(pKey);
if (!v.bstrVal)
return WBEM_E_OUT_OF_MEMORY;
sc = (*pNewInst)->Put(L"MyKey", 0, &v, 0);
VariantClear(&v);
if(FAILED(sc))
return sc;
// Set the number property value.
v.vt = VT_I4;
v.lVal = lVal;
sc = (*pNewInst)->Put(L"MyValue", 0, &v, 0);
return sc;
}
/******************************************************************************
*
* Name: GetCurrentImpersonationLevel
*
*
* Description:
*
* Get COM impersonation level of caller.
*
*****************************************************************************/
DWORD GetCurrentImpersonationLevel ()
{
DWORD t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
HANDLE t_ThreadToken = NULL ;
BOOL t_Status = OpenThreadToken (
GetCurrentThread() ,
TOKEN_QUERY,
TRUE,
&t_ThreadToken
) ;
if ( t_Status )
{
SECURITY_IMPERSONATION_LEVEL t_Level = SecurityAnonymous ;
DWORD t_Returned = 0 ;
t_Status = GetTokenInformation (
t_ThreadToken ,
TokenImpersonationLevel ,
& t_Level ,
sizeof ( SECURITY_IMPERSONATION_LEVEL ) ,
& t_Returned
) ;
CloseHandle ( t_ThreadToken ) ;
if ( t_Status == FALSE )
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
}
else
{
switch ( t_Level )
{
case SecurityAnonymous:
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
}
break ;
case SecurityIdentification:
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_IDENTIFY ;
}
break ;
case SecurityImpersonation:
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE ;
}
break ;
case SecurityDelegation:
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_DELEGATE ;
}
break ;
default:
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
}
break ;
}
}
}
else
{
ULONG t_LastError = GetLastError () ;
if ( t_LastError == ERROR_NO_IMPERSONATION_TOKEN || t_LastError == ERROR_NO_TOKEN )
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_DELEGATE ;
}
else
{
if ( t_LastError == ERROR_CANT_OPEN_ANONYMOUS )
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
}
else
{
t_ImpersonationLevel = RPC_C_IMP_LEVEL_ANONYMOUS ;
}
}
}
return t_ImpersonationLevel ;
}