Google
 
Web itpro-blogger.blogspot.com
金曜日, 6月 06, 2008

[C++]エラーメッセージを取得する

▼質問
予期しないエラーでアプリケーションが終了したんだ。

そのときのエラー内容を取得したいんだ。どうしたらいいの?

▼回答
GetLastError関数とFormatMessage関数で次の様に取得できるよ。

#include
#include
#include
using namespace std;

try {
throw "Exception";
bool b;
if (b) throw 10.1;
else throw 100;
}
catch ( ... ) {
LPTSTR lpBuffer = NULL;
BOOL result = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER FORMAT_MESSAGE_FROM_SYSTEM FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
(LPTSTR)&lpBuffer, 0, NULL );
CString str = (LPCTSTR)lpBuffer;
str.Replace( "\r\n", "" );
logprintf( "エラー番号:%d,エラー内容:%s", GetLastError(), str );
// MessageBox( NULL, lpBuffer, "Last Error Message", MB_ICONHANDMB_OK );
LocalFree( lpBuffer );
// cout << "Exception : " << e << '\n'; exit( EXIT_FAILURE );
}


■参考サイト
peslab C,C++
http://peslab.blog70.fc2.com/category5-1.html
FormatMessage 関数
http://msdn.microsoft.com/ja-jp/library/cc428939.aspx
[C/C++]なんとエラーメッセージが取得出来た

http://blogs.wankuma.com/ch3cooh/archive/2008/03/22/129144.aspx








Comments: コメントを投稿

<< Home

This page is powered by Blogger. Isn't yours?