Log Parser C Example
Log Parser is a free command-line tool from Microsoft that lets you run SQL queries against a variety of log files and other system data sources, and get the results out to an array of destinations, from SQL tables to CSV files
#include <atlbase.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "LogParser.tlh" //由下一行產生
//#import "LogParser.dll" raw_native_types //由type library產生.tlh .tli
int main()
{
::CoInitialize( NULL ); // COM 初始化
MSUtil::ILogQuery *plog;
MSUtil::ICOMCSVInputContext *pin;
MSUtil::ILogRecordset *pset;
//CLSID clsid1;
//HRESULT hr1 = ::CLSIDFromProgID( L"MSUtil.LogQuery", &clsid1 );
//assert( SUCCEEDED( hr1 ) ); // 如果失敗,說明沒有註冊組件
//__uuidof()可取代CLSIDFromProgID
HRESULT hr = ::CoCreateInstance( __uuidof(MSUtil::LogQueryClass), NULL, CLSCTX_INPROC_SERVER, __uuidof(MSUtil::ILogQuery), (LPVOID *)&plog );
assert( SUCCEEDED( hr ) );
hr = ::CoCreateInstance( __uuidof(MSUtil::COMCSVInputContextClass), NULL, CLSCTX_INPROC_SERVER, __uuidof(MSUtil::ICOMCSVInputContext), (LPVOID *)&pin );
pset = plog->Execute(L"select * from DailyLog.csv where Score>50",pin);
int ncount = 0;
while(!pset->atEnd())
{
/*VARIANT idx;
VariantInit(&idx);
idx.bstrVal = L"Score";
idx.vt = VT_BSTR;
*/
for(int i=2;i<=11;i++)
{
CComVariant idx(i); //若不用CComVariant則須用上面Variant 被mark的部分
//用debug檢查傳回Variant裡面是什麼型態
CComVariant val = pset->getRecord()->getValue(idx);
if(val.vt == VT_BSTR)
wprintf(L"%st",val.bstrVal);
else if(val.vt == VT_I4)
printf("%dt",val.lVal);
}
printf("n");
ncount++;
pset->moveNext();
}
printf("Total:%dn",ncount);
return 0;
}