Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinh Ngoc Tu committed May 14, 2019
1 parent dce28ee commit e1a2f2d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax: glob
.vs
*.aps
*.user
bin/
obj/
Win32/
x64/
Debug/
Expand Down
55 changes: 32 additions & 23 deletions VietTypeATL/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

#pragma once

#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>


/// <summary>support function, do not use directly</summary>
Expand All @@ -28,31 +29,12 @@ void _dprint(_In_ LPCWSTR func, _In_ int line, _In_ LPCWSTR fmt, Args... args) {
OutputDebugString(&buf[0]);
}

// print formatted string to debugger output
#define DPRINT(fmt, ...) _dprint(__FUNCTIONW__, __LINE__, L"%s:%d: " fmt L"\n", __VA_ARGS__)
// check if HRESULT is successful; if not, print error information to debugger output
#define HRESULT_CHECK(hr, fmt, ...) if (FAILED(hr)) { DPRINT(L"HRESULT error %lx: " fmt, hr, __VA_ARGS__); }
// check if HRESULT is successful; if not, print error information to debugger output and return from calling function
#define HRESULT_CHECK_RETURN(hr, fmt, ...) if (FAILED(hr)) { DPRINT(L"HRESULT error %lx: " fmt, hr, __VA_ARGS__); return hr; }
// check if HRESULT is successful; if not, set *ptr to nullptr, print error information to debugger output and return from calling function
#define HRESULT_CHECK_RETURN_OUTPTR(hr, ptr, fmt, ...) if (FAILED(hr)) { *ptr = nullptr; DPRINT(L"HRESULT error %lx: " fmt, hr, __VA_ARGS__); return hr; }

#ifdef _DEBUG
// if in debug builds, print formatted string to debugger output
#define DBG_DPRINT DPRINT
// if in debug builds, check if HRESULT is successful; if not, print error information to debugger output
#define DBG_HRESULT_CHECK(hr, fmt, ...) if (FAILED(hr)) { DBG_DPRINT(L"HRESULT error %lx: " fmt, hr, __VA_ARGS__); }
#else
#define DBG_DPRINT(fmt, ...)
#define DBG_HRESULT_CHECK(hr, fmt, ...)
#endif


/// <summary>support function, do not use directly</summary>
template <typename... Args>
void _winerrorprint(_In_ LPCWSTR func, _In_ int line, _In_ DWORD err, _In_ LPCWSTR fmt, Args... args) {
void _errorprint(_In_ LPCWSTR func, _In_ int line, _In_ DWORD err, _In_ LPCWSTR fmt, Args... args) {
std::array<WCHAR, 256> errmessage;
auto chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, &errmessage[0], static_cast<DWORD>(errmessage.size()), NULL);
auto chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, &errmessage[0], static_cast<DWORD>(errmessage.size()), NULL);
assert(chars >= 0 && chars < 256);
errmessage[chars] = 0;
for (auto c = static_cast<long>(chars) - 1; c >= 0; c--) {
Expand All @@ -68,9 +50,36 @@ void _winerrorprint(_In_ LPCWSTR func, _In_ int line, _In_ DWORD err, _In_ LPCWS
OutputDebugString(&buf[0]);
}


// print formatted string to debugger output
#define DPRINT(fmt, ...) _dprint(__FUNCTIONW__, __LINE__, L"%s:%d: " fmt L"\n", __VA_ARGS__)

#define HRESULT_PRINT(hr, fmt, ...) _errorprint(__FUNCTIONW__, __LINE__, hr, L"%s:%d: HRESULT error %lx (%s): " fmt, __VA_ARGS__)
// check if HRESULT is successful; if not, print error information to debugger output
#define HRESULT_CHECK(hr, fmt, ...) if (FAILED(hr)) { HRESULT_PRINT(hr, fmt, __VA_ARGS__); }
// check if HRESULT is successful; if not, print error information to debugger output and return from calling function
#define HRESULT_CHECK_RETURN(hr, fmt, ...) if (FAILED(hr)) { HRESULT_PRINT(hr, fmt, __VA_ARGS__); return hr; }
// check if HRESULT is successful; if not, set *ptr to nullptr, print error information to debugger output and return from calling function
#define HRESULT_CHECK_RETURN_OUTPTR(hr, ptr, fmt, ...) if (FAILED(hr)) { *ptr = nullptr; HRESULT_PRINT(hr, fmt, __VA_ARGS__); return hr; }

#ifdef _DEBUG
// if in debug builds, print formatted string to debugger output
#define DBG_DPRINT DPRINT
// if in debug builds, check if HRESULT is successful; if not, print error information to debugger output
#define DBG_HRESULT_CHECK HRESULT_CHECK
#else
#define DBG_DPRINT(fmt, ...)
#define DBG_HRESULT_CHECK(hr, fmt, ...)
#endif

// format win32 error and print formatted string to debugger output
#define WINERROR_PRINT(err, fmt, ...) _winerrorprint(__FUNCTIONW__, __LINE__, err, L"%s:%d: WINERROR %lx (%s): " fmt L"\n", __VA_ARGS__)
#define WINERROR_PRINT(err, fmt, ...) _errorprint(__FUNCTIONW__, __LINE__, err, L"%s:%d: WINERROR %lx (%s): " fmt L"\n", __VA_ARGS__)
// format win32 error, print formatted string to debugger output, and return from calling function with error converted to HRESULT
#define WINERROR_CHECK_RETURN_HRESULT(err, fmt, ...) if (err != ERROR_SUCCESS) { WINERROR_PRINT(err, fmt, __VA_ARGS__); return HRESULT_FROM_WIN32(err); }
// get and format win32 error, print formatted string to debugger output, and return from calling function with error converted to HRESULT
#define WINERROR_GLE_RETURN_HRESULT(fmt, ...) WINERROR_CHECK_RETURN_HRESULT(GetLastError(), fmt, __VA_ARGS__)


// close a handle, free a memory block, or release some other resource
// auto handle = UNIQUE_HANDLE(CreateFile(...), CloseHandle);
#define UNIQUE_HANDLE(value, deleter) std::unique_ptr<std::remove_pointer<decltype(value)>::type, decltype(&(deleter))>((value), &(deleter))
13 changes: 8 additions & 5 deletions VietTypeATL/LanguageBarHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ static int PopMenu(POINT pt, const RECT* area) {
static HRESULT CopyTfMenu(_In_ ITfMenu* menu) {
HRESULT hr;

HMENU menuSource = GetMenu();
for (int i = 0; i < GetMenuItemCount(menuSource); i++) {
// HMENU menuSource = GetMenu();
std::unique_ptr<std::remove_pointer<HMENU>::type, decltype(&DestroyMenu)> menuSource(GetMenu(), &DestroyMenu);
if (!menuSource) {
return E_FAIL;
}
for (int i = 0; i < GetMenuItemCount(menuSource.get()); i++) {
MENUITEMINFO mii;
mii.cbSize = sizeof(MENUITEMINFO);
mii.dwTypeData = NULL;
mii.fMask = MIIM_STRING;
if (!GetMenuItemInfo(menuSource, i, TRUE, &mii)) {
if (!GetMenuItemInfo(menuSource.get(), i, TRUE, &mii)) {
WINERROR_GLE_RETURN_HRESULT(L"%s", L"GetMenuItemInfo failed");
}

Expand All @@ -63,7 +67,7 @@ static HRESULT CopyTfMenu(_In_ ITfMenu* menu) {
// the vector used here can't outlive the copy function, that's why the function has to do the item addition by itself
std::vector<WCHAR> buf(mii.cch);
mii.dwTypeData = &buf[0];
if (!GetMenuItemInfo(menuSource, i, TRUE, &mii)) {
if (!GetMenuItemInfo(menuSource.get(), i, TRUE, &mii)) {
WINERROR_GLE_RETURN_HRESULT(L"%s", L"GetMenuItemInfo failed");
}

Expand Down Expand Up @@ -103,7 +107,6 @@ static HRESULT CopyTfMenu(_In_ ITfMenu* menu) {

DeleteObject(tfBitmap);
}
DestroyMenu(menuSource);

return S_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions VietTypeATL/SettingsDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class SettingsDialog {

static INT_PTR CALLBACK SettingsDialogProc(_In_ HWND hwndDlg, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam);

// template Property<resource ID> for typesafe configuration storage
// define the data type of the property in propType like the examples
template <WORD resNo>
struct propType {
using type = int;
Expand Down
1 change: 0 additions & 1 deletion VietTypeATL/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

#define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW

#include "resource.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
Expand Down

0 comments on commit e1a2f2d

Please sign in to comment.