VC++でもVBScriptの様に、Wscript.Shellを使ってイベントログを出力出来ると思い、調べてみた所、LogEvent関数というのが用意されていたので、それを使ってイベントログを出力しようとしましたが、うまく行きません。
ソース: http://d.hatena.ne.jp/neuromancer_sho/20110806/1312599469
wshom.tliのIWshShell2::LogEvent内で実行される、raw_LogEvent関数で エラー「0x800706ba RPC サーバーを利用できません。」が発生してしまいます。
その他のWscript.Shellの機能(get_CurrentDirectory, SpecialFoldersなど)を書いてみると、ちゃんと動作します。
何がダメなんでしょうか?
LogEvent メソッドの使い方が誤っているような気がします。
第3引数を "Application" → "." に変更したところ、イベントログ出力することを確認しました。
#include <stdio.h> #include <tchar.h> #include <windows.h> #undef GetFreeSpace #define GetFreeSpace GetFreeSpaceEx #import "wshom.ocx" #include <locale> #include "atlsafe.h" #include "comutil.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { WCHAR buf[1024]; size_t num; HRESULT hr; CoInitialize(NULL); setlocale(LC_ALL, "Japanese"); IWshRuntimeLibrary::IWshShell3Ptr wshShell = NULL; hr = wshShell.CreateInstance("Wscript.Shell"); CComVariant v = EVENTLOG_INFORMATION_TYPE; // objShell.LogEvent (種類), "(書き込む文字列)" , "(コンピュータ名)" hr = wshShell->LogEvent(&v, L"イベントです", L"."); return 0; }
<参考情報>
■ @IT:運用 Windows管理者のためのWindows Script Host入門 第7回 WshShellオブジェクトの詳細(3) 1.アプリケーション・イベント・ログの作成 ― LogEventメソッド ―
http://www.atmarkit.co.jp/fwin2k/operation/wsh07/wsh07_01.html
以下のサイトは参考になりますでしょうか。
http://www.lansweeper.com/kb/The-RPC-server-is-unavailable.aspx
0x80070005 のエラーが起きるかもしれません。その場合には以下の情報が役に立つと思います。
http://www.lansweeper.com/kb/WMI-Access-is-denied.aspx
LogEvent メソッドの使い方が誤っているような気がします。
第3引数を "Application" → "." に変更したところ、イベントログ出力することを確認しました。
#include <stdio.h> #include <tchar.h> #include <windows.h> #undef GetFreeSpace #define GetFreeSpace GetFreeSpaceEx #import "wshom.ocx" #include <locale> #include "atlsafe.h" #include "comutil.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { WCHAR buf[1024]; size_t num; HRESULT hr; CoInitialize(NULL); setlocale(LC_ALL, "Japanese"); IWshRuntimeLibrary::IWshShell3Ptr wshShell = NULL; hr = wshShell.CreateInstance("Wscript.Shell"); CComVariant v = EVENTLOG_INFORMATION_TYPE; // objShell.LogEvent (種類), "(書き込む文字列)" , "(コンピュータ名)" hr = wshShell->LogEvent(&v, L"イベントです", L"."); return 0; }
<参考情報>
■ @IT:運用 Windows管理者のためのWindows Script Host入門 第7回 WshShellオブジェクトの詳細(3) 1.アプリケーション・イベント・ログの作成 ― LogEventメソッド ―
http://www.atmarkit.co.jp/fwin2k/operation/wsh07/wsh07_01.html
ありがとうございます。解決しました。
_bstr_t Targetというのがイベントの種類だと思い込んでいました。コンピュータ名だったとは・・・
ありがとうございます。解決しました。
2012/03/12 01:39:14_bstr_t Targetというのがイベントの種類だと思い込んでいました。コンピュータ名だったとは・・・