728x90
반응형
SystemError.h
#pragma once
#include<windows.h> //OS windows, window(s)
#include<tchar.h>
#include<locale.h>
#include<stdio.h>
#ifdef UNICODE
#define SystemErrorExit SystemErrorExitW
#define SystemErrorMsg SystemErrorMsgW
#define SystemOKMsg SystemOKMsgW
#else
#define SystemErrorExit SystemErrorExitA
#define SystemErrorMsg SystemErrorMsgA
#define SystemOKMsg SystemOKMsgA
#endif
//System error
void SystemErrorExitA(const char* str); //program exit
void SystemErrorMsgA(const char* str); //monitor output
void SystemOKMsgA(const char* str);
void SystemErrorExitW(const wchar_t* str); //program exit
void SystemErrorMsgW(const wchar_t* str); //monitor output
void SystemOKMsgW(const wchar_t* str);
SystemError.cpp
#include "MySystemtError.h"
#ifdef UNICODE //유니코드일 경우의 함수
void SystemErrorExitW(const wchar_t* str)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL, GetLastError()
, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
, (wchar_t*)&lpMsgBuf, 0, NULL);
printf("[%s]%s\n", str, (wchar_t*)lpMsgBuf);
LocalFree(lpMsgBuf);
exit(0);
}
void SystemErrorMsgW(const wchar_t* str)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL, GetLastError()
, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
, (wchar_t*)&lpMsgBuf, 0, NULL);
wprintf(L"[%s]%s\n", str, (wchar_t*)lpMsgBuf);
LocalFree(lpMsgBuf);
}
void SystemOKMsgW(const wchar_t* str)
{
wprintf(L"[%s]successfully~~~\n", str);
}
#else //멀티바이트일 경우의 함수
void SystemErrorExitA(const CHAR* str)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL, GetLastError()
, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
, (CHAR*)&lpMsgBuf, 0, NULL);
printf("[%s]%s\n", str, (CHAR*)lpMsgBuf);
LocalFree(lpMsgBuf);
exit(0);
}
void SystemErrorMsgA(const CHAR* str)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL, GetLastError()
, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
, (CHAR*)&lpMsgBuf, 0, NULL);
printf("[%s]%s\n", str, (CHAR*)lpMsgBuf);
LocalFree(lpMsgBuf);
}
void SystemOKMsgA(const CHAR* str)
{
printf("[%s]successfully~~~\n", str);
}
#endif
1. tmain_수업시간에 딴짓한다.cpp
#include "MySystemtError.h"
HANDLE hEvent; //event는 내가 원할 떄 원하는 동작을 하게 만드는 것
DWORD WINAPI ThreadFunc(LPVOID);
int _tmain(void)
{
_tsetlocale(LC_ALL, _T("korean"));
BOOL result = FALSE;
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hEvent == NULL) SystemErrorExit(_T("CreateEvent"));
else SystemOKMsg(_T("CreateEvent"));
HANDLE hThread;
hThread = CreateThread(NULL, 0, ThreadFunc, NULL, CREATE_ALWAYS, NULL);
if (hThread == NULL) SystemErrorExit(_T("CreateThread"));
else SystemOKMsg(_T("CreateThread"));
DWORD dwResult = 0;
while (2)
{
_tprintf(_T("수업시간에 딴짓한다...........\n"));
dwResult = WaitForSingleObject(hEvent, 100);
if (dwResult == WAIT_OBJECT_0)
{
_tprintf(_T("드디어 한다!\n"));
SystemOKMsg(_T("WaitForSingleObject-event"));
break;
}
else if (dwResult == WAIT_FAILED)
{
SystemErrorMsg(_T("WaitForSingleObject-event"));
break;
}
}
dwResult=WaitForSingleObject(hThread, INFINITE);
if (dwResult == WAIT_OBJECT_0)
{
SystemOKMsg(_T("WaitForSingleObject-Thread"));
}
else if (dwResult == WAIT_FAILED)
{
SystemErrorMsg(_T("WaitForSingleObject-Thread"));
}
result = CloseHandle(hThread);
if (!result) SystemErrorMsg(_T("CloseHandle-Thread"));
else SystemOKMsg(_T("CloseHandel-Thread"));
result=CloseHandle(hEvent);
if (!result) SystemErrorMsg(_T("CloseHandle-Event"));
else SystemOKMsg(_T("CloseHandel-Event"));
return 0;
}
DWORD WINAPI ThreadFunc(LPVOID IpParameter)
{
char ch = 'A';
while (2)
{
ch = getchar();
if (ch == 'q')
{
printf("간만에 코딩하네\n");
if (!SetEvent(hEvent)) SystemErrorMsg(_T("SetEvent"));
else SystemOKMsg(_T("SetEvent"));
SetEvent(hEvent);
break;
}
}
return 0;
}
728x90
반응형