IInitiateWinSATAssessment::InitiateAssessment 方法 (winsatcominterfacei.h)
[IInitiateWinSATAssessment::InitiateAssessment 可能會在Windows 8.1之後變更或無法使用。
起始臨機操作評定。
語法
HRESULT InitiateAssessment(
[in] LPCWSTR cmdLine,
[in, optional] IWinSATInitiateEvents *pCallbacks,
[in, optional] HWND callerHwnd
);
參數
[in] cmdLine
要傳遞至 WinSAT 的命令列引數。 命令列不可以是空的。 如需命令列使用方式,請參閱 Microsoft TechNet 上的 WinSAT 命令參考 。
[in, optional] pCallbacks
IWinSATInitiateEvents介面,您可以在評估完成或進行時實作以接收通知。 如果您不想收到通知,可以是 Null 。
[in, optional] callerHwnd
用戶端的視窗控制碼。 控制碼用於置中 WinSAT 對話方塊。 如果 為 Null,對話方塊會置中于桌面上。
傳回值
這個方法可以傳回下列其中一個值。
下表列出這個方法傳回的一些 HRESULT 值。
傳回碼/值 | 描述 |
---|---|
|
已成功啟動 WinSAT。 若要判斷評估是否成功執行,請實作 IWinSATInitiateEvents::WinSATComplete 方法,並檢查 hresult 參數的值。 |
|
命令列不可以是空的;您必須提供命令列引數。 |
|
命令列太長。 最大長度為 30,720 個位元組。 |
|
找不到預期的 WinSAT 程式。 |
備註
您通常會執行臨機操作評定來評估電腦的一個子元件,而正式評估會評估電腦的所有子元件。 若要執行正式評定,請呼叫 IInitiateWinSATAssessment::InitiateFormalAssessment 方法。
臨機操作評定不會儲存在 WinSAT 資料存放區中;只有正式評定會儲存在資料存放區 (您無法使用 IQueryRecentWinSATAssessment 介面來查詢結果) 。 若要取得臨機操作評定的結果,請包含–xml FileName引數,這會將結果儲存到您稍後可以剖析的 XML 檔案。
WinSAT 需要系統管理員許可權才能執行。 如果使用者沒有系統管理員許可權,WinSAT 會顯示一個對話方塊來要求認證。
範例
下列範例示範如何執行臨機操作評定,並接收其進度的通知。
#include <windows.h>
#include <stdio.h>
#include <conio.h> // For kbhit()
#include <winsatcominterfacei.h>
#pragma comment(lib, "ole32.lib")
BOOL IsKeyEvent(HANDLE hStdIn);
// 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;
}
// There is no progress information for ad hoc assessment. The method provides the
// name of the component being assessed.
HRESULT CWinSATCallbacks::WinSATUpdate(UINT currentTick, UINT tickTotal, LPCWSTR currentState)
{
return S_OK;
}
void main(void)
{
HRESULT hr = S_OK;
IInitiateWinSATAssessment* pAssessment = NULL;
CWinSATCallbacks* pCallbacks = NULL; // Class that implements IWinSATInitiateEvents
LPWSTR pCommand = L"mem -buffersize 32MB -xml .\\MemoryAssessment.xml";
HANDLE hConsole = INVALID_HANDLE_VALUE;
DWORD dwWait = 0;
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");
// Get a handle for console input, so you can break out of the loop.
hConsole = GetStdHandle(STD_INPUT_HANDLE);
if (INVALID_HANDLE_VALUE == hConsole)
{
wprintf(L"GetStdHandle failed with %lu.\n", GetLastError());
goto cleanup;
}
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->InitiateAssessment(pCommand, 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;
}
// Loop until the user presses a key or there is an error.
while (true)
{
dwWait = WaitForSingleObject(hConsole, INFINITE);
if (WAIT_OBJECT_0 == dwWait) // Console input
{
if (IsKeyEvent(hConsole))
break;
}
else if (WAIT_FAILED == dwWait)
{
wprintf(L"WaitForSingleObject failed with %lu\n", GetLastError());
break;
}
}
cleanup:
if (pAssessment)
pAssessment->Release();
if (pCallbacks)
pCallbacks->Release();
if (hConsole)
CloseHandle(hConsole);
CoUninitialize();
}
// Determines whether the console input was a key event.
BOOL IsKeyEvent(HANDLE hStdIn)
{
INPUT_RECORD Record[128];
DWORD dwRecordsRead = 0;
BOOL fKeyPress = FALSE;
if (ReadConsoleInput(hStdIn, Record, 128, &dwRecordsRead))
{
for (DWORD i = 0; i < dwRecordsRead; i++)
{
if (KEY_EVENT == Record[i].EventType)
{
fKeyPress = TRUE;
break;
}
}
}
return fKeyPress;
}
規格需求
最低支援的用戶端 | Windows Vista [僅限傳統型應用程式] |
最低支援的伺服器 | 都不支援 |
目標平台 | Windows |
標頭 | winsatcominterfacei.h |
Dll | Winsatapi.dll |