GetLastError 함수를 통해 오류를 확인해보자
많은 Windows 시스템 함수들은 오류 발생 시 NULL 을 반환하지만 원인 확인은 불가능하다
DWORD GetLastError(void);
Windows 시스템 함수 호출 과정에서 오류가 발생한 경우,
이어서 GetLastError 함수 호출을 통해 오류의 원인에 해당하는 에러코드를 얻을 수 있다
오류 확인
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int _tmain(void)
{
SetCurrentDirectory(NULL);
_tprintf(_T("error code %d \n"), GetLastError());
return 0;
}
// 실행 결과
// error code 87
SetCurrentDirectory 함수는 현재 디렉토리를 변경하는 함수로, NULL 을 매개변수로 호출하여 오류가 발생했다
에러코드 87 은 올바르지 않은 매개변수인 경우 발생한다
주의사항
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int _tmain(void)
{
TCHAR CurrentDirectory[MAX_PATH];
GetCurrentDirectory(MAX_PATH, CurrentDirectory);
_tprintf(_T("error code %d \n"), GetLastError());
SetCurrentDirectory(NULL);
_tprintf(_T("error code %d \n"), GetLastError());
return 0;
}
// 실행 결과
// error code 0
// error code 87
GetCurrentDirectory 힘수는 현재 디렉토리를 확인하는 함수이다
정상적으로 실행된 경우에는 에러코드 0 을 반환한다
오류 확인은 오류가 발생한 직후 바로 수행해야 한다
다른 시스템 함수를 호출하는 경우 이전 오류 코드를 확인할 수 없다
참고 자료 링크를 통해 각 에러코드를 확인할 수 있다
참고 자료
https://pharan.tistory.com/entry/%ED%91%9C%EC%A4%80-%EA%B2%80%EC%83%89%EA%B2%BD%EB%A1%9C
표준 검색경로
표준 검색경로 검색 순서 1. 실행 중인 프로세스의 실행파일이 존재하는 디렉토리 2. 실행 중인 프로세스의 현재 디렉토리 (Current Directory) 3. Windows 시스템 디렉토리 (System Directory) 4. Windows 디렉토
pharan.tistory.com
https://learn.microsoft.com/ko-kr/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
GetLastError 함수(errhandlingapi.h) - Win32 apps
호출 스레드의 마지막 오류 코드 값을 검색합니다.
learn.microsoft.com
https://learn.microsoft.com/ko-kr/windows/win32/debug/system-error-codes
자습서 - 시스템 오류 코드 디버그 - Win32 apps
WinError.h 헤더 파일에 정의된 시스템 오류 코드 및 링크 디버깅에 대한 지침을 제공합니다.
learn.microsoft.com
https://learn.microsoft.com/ko-kr/cpp/c-runtime-library/errno-constants?view=msvc-170
errno 상수
자세한 정보: errno 상수
learn.microsoft.com
https://www.youtube.com/playlist?list=PLVsNizTWUw7E2KrfnsyEjTqo-6uKiQoxc
뇌를 자극하는 윈도우즈 시스템 프로그래밍
www.youtube.com
'Windows' 카테고리의 다른 글
커널 오브젝트와 핸들 (0) | 2024.04.26 |
---|---|
표준 검색경로 (0) | 2024.04.11 |
프로세스 생성 (0) | 2024.04.10 |
64비트 기반 프로그래밍 (0) | 2024.04.03 |
MBCS / WBCS 동시 지원 (0) | 2024.04.02 |