// GetLastErrorTest.cpp : コンソール アプリケーションの // エントリ ポイントを定義します。 // #include "stdafx.h" #include "windows.h" void dspErrorInfo ( void ); int _tmain( int argc, _TCHAR* argv[] ) { FILE* fp; fp = fopen( "\\hoge\\hoge.txt", "a" ); dspErrorInfo(); fp = fopen( "hoge.txt", "a" ); fp = fopen( "hoge.txt", "a" ); dspErrorInfo(); getchar(); return 0; } void dspErrorInfo() { DWORD err = GetLastError(); printf( "err=%d\n", err ); LPTSTR msg = NULL; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM , NULL , err , MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US) , ( LPTSTR )&msg , 0 , NULL ); printf( "msg=%S\n", msg ); // FormatMessage内部で確保した領域を解放 LocalFree( msg ); }