Visual C++ からIWshRuntimeLibraryを使用したイベントログ出力
VC++でもVBScriptの様に、Wscript.Shellを使ってイベントログを出力出来ると思い、調べてみた所、LogEvent関数というのが用意されていたので、以下の様にしてイベントログを出力してみた。
#include <windows.h> #undef GetFreeSpace #define GetFreeSpace GetFreeSpaceEx #import "wshom.ocx" #include <locale> #include "atlsafe.h" #include "comutil.h" int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr; CoInitialize(NULL); setlocale(LC_ALL, "Japanese"); IWshRuntimeLibrary::IWshShell3Ptr wshShell = NULL; hr = wshShell.CreateInstance("Wscript.Shell"); CComVariant v = EVENTLOG_INFORMATION_TYPE; hr = wshShell->LogEvent(&v, L"イベントです", L"."); return 0; }
これで、 RegisterEventSource 〜 ReportEvent なんていう面倒くさい関数を使わなくて済む。
環境:
WindowsXP
Visual C++ 2008 Express Edition + WTL + Windows Driver Kit