共用方式為


IInitiateWinSATAssessment::InitiateFormalAssessment 方法 (winsatcominterfacei.h)

[IInitiateWinSATAssessment::InitiateFormalAssessment 可能會在Windows 8.1之後變更或無法使用版本。]

起始正式評定。

語法

HRESULT InitiateFormalAssessment(
  [in, optional] IWinSATInitiateEvents *pCallbacks,
  [in, optional] HWND                  callerHwnd
);

參數

[in, optional] pCallbacks

IWinSATInitiateEvents介面,您可以在評估完成或進行時實作以接收通知。 如果您不想收到通知,可以是 Null

[in, optional] callerHwnd

用戶端的視窗控制碼。 控制碼用於置中 WinSAT 對話方塊。 如果 為 Null,對話方塊會置中于桌面上。

傳回值

這個方法可以傳回下列其中一個值。

下表列出這個方法傳回的一些 HRESULT 值。

傳回碼/值 描述
S_OK
已成功啟動 WinSAT。 若要判斷評估是否成功執行,請實作 IWinSATInitiateEvents::WinSATComplete 方法,並檢查 hresult 參數的值。
WINSAT_ERROR_WINSAT_DOES_NOT_EXIST
0x80040011
找不到預期的 WinSAT 程式。

備註

您通常會執行正式評定來評估電腦的所有子元件,而臨機操作評定會評估電腦的一個子元件。 若要執行臨機操作評定,請呼叫 IInitiateWinSATAssessment::InitiateAssessment 方法。

若要取得正式評定的結果,請使用 IQueryRecentWinSATAssessment 介面。

如果您從 Windows 應用程式呼叫此函式,請實作 IWinSATInitiateEvents 介面,以便在評估完成時顯示進度資訊和接收通知。 若為 Windows 主控台應用程式,則不需要顯示進度,因為 WinSAT 會將進度資訊寫入主控台視窗。

請注意,WinSAT 需要系統管理員許可權才能執行。 如果使用者沒有系統管理員許可權,WinSAT 會顯示一個對話方塊來要求認證。

範例

下列範例示範如何執行正式評定,並接收進度通知。

#include <windows.h>
#include <stdio.h>
#include <conio.h>  // For kbhit()
#include <winsatcominterfacei.h>

#pragma comment(lib, "ole32.lib")

// Class that implements IWinSATInitiateEvents. Implement this class to
// get progress information and completion notification.
class CWinSATCallbacks : public IWinSATInitiateEvents
{
    LONG m_lRefCount;

public:

    // Constructor, Destructor
    CWinSATCallbacks() {m_lRefCount = 1;};
    ~CWinSATCallbacks() {};

    // IUnknown methods
    HRESULT __stdcall QueryInterface(REFIID riid, LPVOID *ppvObj);
    ULONG __stdcall AddRef();
    ULONG __stdcall Release();

    // IWinSATInitiateEvents methods
    HRESULT __stdcall WinSATComplete(HRESULT hr, LPCWSTR description);
    HRESULT __stdcall WinSATUpdate(UINT currentTick, UINT tickTotal, LPCWSTR currentState);
};


HRESULT CWinSATCallbacks::QueryInterface(REFIID riid, LPVOID* ppvObj) 
{
    if (riid == __uuidof(IUnknown) || riid == __uuidof(IWinSATInitiateEvents)) 
    {
        *ppvObj = this;
    }
    else
    {
        *ppvObj = NULL;
        return E_NOINTERFACE;
    }

    AddRef();
    return NOERROR;
}

ULONG CWinSATCallbacks::AddRef() 
{
    return InterlockedIncrement(&m_lRefCount);
}

ULONG CWinSATCallbacks::Release() 
{
    ULONG  ulCount = InterlockedDecrement(&m_lRefCount);

    if(0 == ulCount) 
    {
        delete this;
    }

    return ulCount;
}

// Is called when WinSAT completes the assessment or an error occurs.
HRESULT CWinSATCallbacks::WinSATComplete(HRESULT hr, LPCWSTR description)
{
    if (SUCCEEDED(hr))
    {
        wprintf(L"\n*** %s", description);
    }
    else
    {
        wprintf(L"\n*** The assessment failed with 0x%x (%s)\n", hr, description);
    }

    return S_OK;
}

// Is called when the assessment makes progress. Indicates the percentage of the assessment
// that is complete and the current component being assessed.
HRESULT CWinSATCallbacks::WinSATUpdate(UINT currentTick, UINT tickTotal, LPCWSTR currentState)
{
    // Typically, you would provide the tick values to a ProgressBar control.

    if (tickTotal > 0)
    {
        wprintf(L"\n*** Percent complete: %u%%\n", 100*currentTick/tickTotal);
        wprintf(L"*** Currently assessing: %s\n\n", currentState);
    }

    return S_OK;
}

void main(void)
{
    HRESULT hr = S_OK;
    IInitiateWinSATAssessment* pAssessment = NULL;
    CWinSATCallbacks* pCallbacks = NULL;  // Class that implements IWinSATInitiateEvents

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    // Get an instance of the assessment interface.
    hr = CoCreateInstance(__uuidof(CInitiateWinSAT),
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          __uuidof(IInitiateWinSATAssessment),
                          (void**)&pAssessment);

    if (FAILED(hr))
    {
        wprintf(L"Failed to create an instance of IInitiateWinSATAssessment. Failed with 0x%x.\n", hr);
        goto cleanup;
    }

    wprintf(L"Running formal assessment... hit any key when complete.\n");

    pCallbacks = new CWinSATCallbacks();
    if (NULL == pCallbacks)
    {
        wprintf(L"Failed to create an instance of the CWinSATCallbacks class.\n");
        goto cleanup;
    }

    // Run the formal assessment.
    hr = pAssessment->InitiateFormalAssessment(pCallbacks, NULL);
    if (FAILED(hr))
    {
        // This is a failure to start WinSAT. If WinSAT fails while running, 
        // your implementation of the IWinSATInitiateEvents::WinSATComplete 
        // method will receive the failure code.
        wprintf(L"InitiateFormalAssessment failed with 0x%x.\n", hr);
        goto cleanup;
    }

    while (!_kbhit())
        Sleep(10);

cleanup:

    if (pAssessment)
        pAssessment->Release();

    if (pCallbacks)
        pCallbacks->Release();

    CoUninitialize();
}

規格需求

   
最低支援的用戶端 Windows Vista [僅限傳統型應用程式]
最低支援的伺服器 都不支援
目標平台 Windows
標頭 winsatcominterfacei.h
Dll Winsatapi.dll

另請參閱

IInitiateWinSATAssessment

IInitiateWinSATAssessment::InitiateAssessment

IQueryRecentWinSATAssessment