IT/System & Network
GetLastError() 함수 : 시스템 에러 코드 확인
Hotman
2011. 8. 8. 01:27
GetLastError.cpp
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
int _tmain(void)
{
HANDLE hFile =
CreateFile( // Windows System 함수
_T("ABC.DAT"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE) // 함수 호출 실패할 경우 INVALID_HANDLE_VALUE 반환
{
_tprintf(_T("error code : %d \n"), GetLastError() );
return 0;
}
return 0;
}
|
ErrorStateChange.cpp
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
int _tmain(void)
{
HANDLE hFile =
CreateFile(
_T("ABC.DAT"), // 파일명
GENERIC_READ, // 파일 읽기 모드
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
_tprintf(_T("error code : %d \n"), GetLastError() ); // 오류 확인
hFile =
CreateFile(
_T("ABC2.DAT"),
GENERIC_WRITE, // 파일 쓰기 보드 개방
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
_tprintf(_T("error code : %d \n"), GetLastError() ); // 오류 확인
return 0;
}
|
error code : 2 ("The system cannot find the file specified.")
error code : 0 ("The operation completely successfully.")
오류 확인은 오류 발생 직후에 한다.
error code : 2
error code : 80 ("The file exists.")
ABC2.DAT 파일이 생성되어 있는 상태이기 때문에 80을 출력한다.
오류가 발생하면 GetLastError() 함수를 이용하여 오류 코드를 얻는다.
** CreateFile() 함수
HANDLE CreateFile(
LPCTSTR lpFileName, // address of name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // address of security descriptor
DWORD dwCreationDistribution, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle of file with attributes to copy
);
[출처] CreateFile|작성자 미락꿀