GetLastErrorで得られるシステムエラーの多くはシステムから対応するエラー文字列を取り出すことができます。これを使うと他の標準のWindowsアプリケーションと同じ文字列を使えることと、言語にあわせた文字列を得ることもできるというメリットがあります。
以下は、エラー文字列を取り出すサンプルです。
----
#include <string>
using namespace std;
#include <windows.h>
/*
* #GetSystemErrorString---GetLastErrorで選られたシステムエラーに対応する
* エラー文字列を取得する。
*/
string GetSystemErrorString(DWORD dwError)
{
string str;
LPVOID lpMessageBuffer = NULL ;
DWORD dwResult = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMessageBuffer,
0,
NULL);
if ( dwResult != 0 ) {
str = (LPCTSTR)lpMessageBuffer;
LocalFree(lpMessageBuffer);
}
return str;
}
------
更新履歴
・2000/8/21 作成