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 |