Share via


Reporting Informational, Warning, and Error Status Messages

You can report user-defined informational, warning, and error status messages using the following methods defined in the SMS_StatusMessage class:

The difference between these methods is the severity level that is generated by each method and the context in which you would use it.

The following code fragment shows you how to use RaiseErrorStatusMsg to report an error message.

This example uses the constant strings defined in the SMSProv.h header. For more information, see Using String Constants in C/C++. For information on creating the context object (pSMSContext) used in this example, see SMS Context Qualifiers.[C/C++]

  [C/C++]
// This example does not handle errors and assumes that the IWbemServices
// object exists.

    IWbemClassObject  *pclsStatusMsg = NULL;    //SMS_StatusMessage class
    IWbemClassObject  *pdefInParams = NULL;     //Definition of method in parameters
    IWbemClassObject  *pinstInParams = NULL;    //Instance of method in parameters
    SAFEARRAY         *psaAttrIDs;
    SAFEARRAY         *psaAttrValues;
    long               Idx = 0;


    //Get the RaiseErrorStatusMsg method from SMS_StatusMessage.
    hr = pServices->GetObject(_bstr_t(CLASS_SMS_StatusMessage), 0, NULL,
                              &pclsStatusMsg, NULL);
    hr = pclsStatusMsg->GetMethod(SMS_StatusMessage::METHOD_RaiseErrorStatusMsg, 0,
                                  &pdefInParams, NULL);
    pclsStatusMsg->Release();
    hr = pdefInParams->SpawnInstance(0, &pinstInParams);
    pdefInParams->Release();
    pdefInParams = NULL;

    //Change the namespace in smsprov.h to reduce typing.
    using namespace SMS_StatusMessage::RaiseErrorStatusMsg;

    // Create all the variant parameters and add them to the method object.
    // Note that only the MessageText parameter is required.
    vTemp = _bstr_t(L"The appropriate message text.");
    hr = pinstInParams->Put(In::PARAM_MessageText, 0, &vTemp, 0);

    vTemp = (long)In::MessageType::ENUM_Audit;
    hr = pinstInParams->Put(In::PARAM_MessageType, 0, &vTemp, 0);

    vTemp = (long)GetLastError();
    hr = pinstInParams->Put(In::PARAM_Win32Error, 0, &vTemp, 0);

    vTemp = (long)GetCurrentProcessId();
    hr = pinstInParams->Put(In::PARAM_ProcessID, 0, &vTemp, 0);

    vTemp = (long)GetCurrentThreadId();
    hr = pinstInParams->Put(In::PARAM_ThreadID, 0, &vTemp, 0);

    //Create single dimension safe arrays of size 1.
    psaAttrIDs = SafeArrayCreateVector(VT_I4, 0, 1);
    psaAttrValues = SafeArrayCreateVector(VT_BSTR, 0, 1);

    long  id = 400;     //Specifies that the value is a PackageID.
    hr = SafeArrayPutElement(psaAttrIDs, &Idx, &id);

    _bstr_t bstr = L"<packageid>";
    hr = SafeArrayPutElement(psaAttrValues, &Idx, (void *)(BSTR)bstr);

    vTemp.vt = VT_ARRAY | VT_I4;
    vTemp.parray = psaAttrIDs;
    hr = pinstInParams->Put(In::PARAM_AttrIDs, 0, &vTemp, 0);

    vTemp.vt = VT_ARRAY | VT_BSTR;
    vTemp.parray = psaAttrValues;
    hr = pinstInParams->Put(In::PARAM_AttrValues, 0, &vTemp, 0);

    hr = pServices->ExecMethod(_bstr_t(CLASS_SMS_StatusMessage),
                               _bstr_t(SMS_StatusMessage::METHOD_RaiseErrorStatusMsg),
                               0, pSMSContext, pinstInParams, NULL, NULL);
    pinstInParams->Release();
    pSMSContext->Release();
    vTemp.vt = VT_NULL;
    vTemp.parray = NULL;

    hr = SafeArrayDestroy(psaAttrIDs);
    hr = SafeArrayDestroy(psaAttrValues);

[Visual Basic]
    Dim Services As SWbemServices
    Dim SMSContext As New SWbemNamedValueSet
    Dim InParams As SWbemObject

    'The raise status message methods require a context object with
    'the MachineName and ApplicationName.
    SMSContext.Add "LocaleID", "MS\1033"
    SMSContext.Add "MachineName", "MyMachine"
    SMSContext.Add "ApplicationName", "MyApp"

    Set Services = GetObject("winmgmts:root/sms/site_<sitecode>")

    'Because a context object is required, you must use the old format
    'of calling a method.
    Set InParams = services.Get("SMS_StatusMessage").Methods_("RaiseErrorStatusMsg").InParameters.SpawnInstance_

    InParams.MessageText = "Some message"
    InParams.MessageType = 768           'message type
    InParams.AttrIDs = Array(400)        'specifies that the value is a PackageID
    InParams.AttrValues = Array("<packageid>")

    services.ExecMethod "SMS_StatusMessage", "RaiseErrorStatusMsg", InParams, , SMSContext