Github https://github.com/gongluck/Windows .git ...
Github
https://github.com/gongluck/Windows-.git
//第1章 錯誤處理.cpp: 定義應用程式的入口點。
//
#include "stdafx.h"
#include "第1章 錯誤處理.h"
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
HANDLE hFile = CreateFile(TEXT("C:\\gongluck"), 0, 0, NULL, OPEN_EXISTING, 0, NULL);
DWORD err = GetLastError();;
HLOCAL hlocal = NULL;
if (err != ERROR_SUCCESS)
{
//將錯誤碼(可指定自然語言)格式化輸出
DWORD res = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
(LPTSTR)&hlocal,
0,
NULL);
MessageBox(NULL, (PCTSTR)hlocal, TEXT("err"), MB_OK);
}
//FormatMessage的格式化輸出功能
int nYear = 2018, nMonth = 5, nDay = 22;
TCHAR szYear[5], szMonth[3], szDay[3];
wsprintf(szYear, TEXT("%d"), nYear);
wsprintf(szMonth, TEXT("%d"), nMonth);
wsprintf(szDay, TEXT("%d"), nDay);
LPWSTR lpSource = (LPWSTR)TEXT("今天是:%1年%2月%3日");
DWORD_PTR pArgs[] = { (DWORD_PTR)szYear, (DWORD_PTR)szMonth, (DWORD_PTR)szDay };
const DWORD size = 100 + 1;
WCHAR buffer[size];
if (FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
lpSource,
NULL,
NULL,
buffer,
size,
(va_list*)pArgs))
{
MessageBox(NULL, buffer, TEXT("Date"), MB_ICONINFORMATION);
}
//
system("pause");
return 0;
}