Use decltype instead of typedefs for function pointers

This commit is contained in:
Birunthan Mohanathas 2014-02-28 18:35:09 +02:00
parent 27af737a3a
commit 3190fc267f
9 changed files with 55 additions and 73 deletions

View File

@ -89,8 +89,7 @@ bool Is64BitWindows()
return true; return true;
#endif #endif
typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE hProcess, PBOOL Wow64Process); auto isWow64Process = (decltype(IsWow64Process)*)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process");
auto isWow64Process = (IsWow64ProcessFunc)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process");
if (isWow64Process) if (isWow64Process)
{ {
BOOL isWow64 = FALSE; BOOL isWow64 = FALSE;

View File

@ -58,14 +58,12 @@ std::wstring GetFormattedString(UINT id, ...)
HICON GetIcon(UINT id, bool large) HICON GetIcon(UINT id, bool large)
{ {
typedef HRESULT (WINAPI * FPLOADICONMETRIC)(HINSTANCE hinst, PCWSTR pszName, int lims, HICON* phico);
HINSTANCE hExe = GetModuleHandle(nullptr); HINSTANCE hExe = GetModuleHandle(nullptr);
HINSTANCE hComctl = GetModuleHandle(L"Comctl32"); HINSTANCE hComctl = GetModuleHandle(L"Comctl32");
if (hComctl) if (hComctl)
{ {
// Try LoadIconMetric for better quality with high DPI // Try LoadIconMetric for better quality with high DPI
FPLOADICONMETRIC loadIconMetric = (FPLOADICONMETRIC)GetProcAddress(hComctl, "LoadIconMetric"); auto loadIconMetric = (decltype(LoadIconMetric)*)GetProcAddress(hComctl, "LoadIconMetric");
if (loadIconMetric) if (loadIconMetric)
{ {
HICON icon; HICON icon;

View File

@ -26,8 +26,8 @@ UINT MeasureNet::c_NumOfTables = 0;
std::vector<ULONG64> MeasureNet::c_StatValues; std::vector<ULONG64> MeasureNet::c_StatValues;
std::vector<ULONG64> MeasureNet::c_OldStatValues; std::vector<ULONG64> MeasureNet::c_OldStatValues;
FPGETIFTABLE2 MeasureNet::c_GetIfTable2 = nullptr; decltype(GetIfTable2)* MeasureNet::c_GetIfTable2 = nullptr;
FPFREEMIBTABLE MeasureNet::c_FreeMibTable = nullptr; decltype(FreeMibTable)* MeasureNet::c_FreeMibTable = nullptr;
/* /*
** The constructor. This is the base class for the net-meters. ** The constructor. This is the base class for the net-meters.
@ -694,8 +694,8 @@ void MeasureNet::InitializeStatic()
HMODULE IpHlpApiLibrary = GetModuleHandle(L"IpHlpApi.dll"); HMODULE IpHlpApiLibrary = GetModuleHandle(L"IpHlpApi.dll");
if (IpHlpApiLibrary) if (IpHlpApiLibrary)
{ {
c_GetIfTable2 = (FPGETIFTABLE2)GetProcAddress(IpHlpApiLibrary, "GetIfTable2"); c_GetIfTable2 = (decltype(c_GetIfTable2))GetProcAddress(IpHlpApiLibrary, "GetIfTable2");
c_FreeMibTable = (FPFREEMIBTABLE)GetProcAddress(IpHlpApiLibrary, "FreeMibTable"); c_FreeMibTable = (decltype(c_FreeMibTable))GetProcAddress(IpHlpApiLibrary, "FreeMibTable");
} }
if (!c_GetIfTable2 || !c_FreeMibTable) if (!c_GetIfTable2 || !c_FreeMibTable)

View File

@ -24,9 +24,6 @@
#include <Iphlpapi.h> #include <Iphlpapi.h>
#include "Measure.h" #include "Measure.h"
typedef NETIO_STATUS (NETIOAPI_API_ * FPGETIFTABLE2)(PMIB_IF_TABLE2* Table);
typedef VOID (NETIOAPI_API_ * FPFREEMIBTABLE)(PVOID Memory);
class MeasureNet : public Measure class MeasureNet : public Measure
{ {
public: public:
@ -75,8 +72,8 @@ private:
static BYTE* c_Table; static BYTE* c_Table;
static UINT c_NumOfTables; static UINT c_NumOfTables;
static FPGETIFTABLE2 c_GetIfTable2; static decltype(GetIfTable2)* c_GetIfTable2;
static FPFREEMIBTABLE c_FreeMibTable; static decltype(FreeMibTable)* c_FreeMibTable;
}; };
#endif #endif

View File

@ -64,10 +64,10 @@ enum INTERVAL
int MeterWindow::c_InstanceCount = 0; int MeterWindow::c_InstanceCount = 0;
HINSTANCE MeterWindow::c_DwmInstance = nullptr; HINSTANCE MeterWindow::c_DwmInstance = nullptr;
FPDWMENABLEBLURBEHINDWINDOW MeterWindow::c_DwmEnableBlurBehindWindow = nullptr; decltype(DwmEnableBlurBehindWindow)* MeterWindow::c_DwmEnableBlurBehindWindow = nullptr;
FPDWMGETCOLORIZATIONCOLOR MeterWindow::c_DwmGetColorizationColor = nullptr; decltype(DwmGetColorizationColor)* MeterWindow::c_DwmGetColorizationColor = nullptr;
FPDWMSETWINDOWATTRIBUTE MeterWindow::c_DwmSetWindowAttribute = nullptr; decltype(DwmSetWindowAttribute)* MeterWindow::c_DwmSetWindowAttribute = nullptr;
FPDWMISCOMPOSITIONENABLED MeterWindow::c_DwmIsCompositionEnabled = nullptr; decltype(DwmIsCompositionEnabled)* MeterWindow::c_DwmIsCompositionEnabled = nullptr;
/* /*
** Constructor ** Constructor
@ -142,16 +142,17 @@ MeterWindow::MeterWindow(const std::wstring& folderPath, const std::wstring& fil
m_FontCollection(), m_FontCollection(),
m_ToolTipHidden(false) m_ToolTipHidden(false)
{ {
if (!c_DwmInstance && IsWindowsVistaOrGreater()) if (!c_DwmInstance && IsWindowsVistaOrGreater() &&
(c_DwmInstance = System::RmLoadLibrary(L"dwmapi.dll")) != nullptr)
{ {
c_DwmInstance = System::RmLoadLibrary(L"dwmapi.dll"); c_DwmEnableBlurBehindWindow =
if (c_DwmInstance) (decltype(c_DwmEnableBlurBehindWindow))GetProcAddress(c_DwmInstance, "DwmEnableBlurBehindWindow");
{ c_DwmGetColorizationColor =
c_DwmEnableBlurBehindWindow = (FPDWMENABLEBLURBEHINDWINDOW)GetProcAddress(c_DwmInstance, "DwmEnableBlurBehindWindow"); (decltype(c_DwmGetColorizationColor))GetProcAddress(c_DwmInstance, "DwmGetColorizationColor");
c_DwmGetColorizationColor = (FPDWMGETCOLORIZATIONCOLOR)GetProcAddress(c_DwmInstance, "DwmGetColorizationColor"); c_DwmSetWindowAttribute =
c_DwmSetWindowAttribute = (FPDWMSETWINDOWATTRIBUTE)GetProcAddress(c_DwmInstance, "DwmSetWindowAttribute"); (decltype(c_DwmSetWindowAttribute))GetProcAddress(c_DwmInstance, "DwmSetWindowAttribute");
c_DwmIsCompositionEnabled = (FPDWMISCOMPOSITIONENABLED)GetProcAddress(c_DwmInstance, "DwmIsCompositionEnabled"); c_DwmIsCompositionEnabled =
} (decltype(c_DwmIsCompositionEnabled))GetProcAddress(c_DwmInstance, "DwmIsCompositionEnabled");
} }
if (c_InstanceCount == 0) if (c_InstanceCount == 0)

View File

@ -40,11 +40,6 @@
#define RI_MOUSE_HORIZONTAL_WHEEL 0x0800 #define RI_MOUSE_HORIZONTAL_WHEEL 0x0800
typedef HRESULT (WINAPI * FPDWMENABLEBLURBEHINDWINDOW)(HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
typedef HRESULT (WINAPI * FPDWMGETCOLORIZATIONCOLOR)(DWORD* pcrColorization, BOOL* pfOpaqueBlend);
typedef HRESULT (WINAPI * FPDWMSETWINDOWATTRIBUTE)(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
typedef HRESULT (WINAPI * FPDWMISCOMPOSITIONENABLED)(BOOL* pfEnabled);
enum BUTTONPROC enum BUTTONPROC
{ {
BUTTONPROC_DOWN, BUTTONPROC_DOWN,
@ -433,10 +428,10 @@ private:
static HINSTANCE c_DwmInstance; static HINSTANCE c_DwmInstance;
static FPDWMENABLEBLURBEHINDWINDOW c_DwmEnableBlurBehindWindow; static decltype(DwmEnableBlurBehindWindow)* c_DwmEnableBlurBehindWindow;
static FPDWMGETCOLORIZATIONCOLOR c_DwmGetColorizationColor; static decltype(DwmGetColorizationColor)* c_DwmGetColorizationColor;
static FPDWMSETWINDOWATTRIBUTE c_DwmSetWindowAttribute; static decltype(DwmSetWindowAttribute)* c_DwmSetWindowAttribute;
static FPDWMISCOMPOSITIONENABLED c_DwmIsCompositionEnabled; static decltype(DwmIsCompositionEnabled)* c_DwmIsCompositionEnabled;
}; };
#endif #endif

View File

@ -1000,12 +1000,12 @@ LRESULT CALLBACK System::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa
*/ */
ULONGLONG System::GetTickCount64() ULONGLONG System::GetTickCount64()
{ {
typedef ULONGLONG (WINAPI * FPGETTICKCOUNT64)(); static auto s_GetTickCount64 =
static FPGETTICKCOUNT64 c_GetTickCount64 = (FPGETTICKCOUNT64)GetProcAddress(GetModuleHandle(L"kernel32"), "GetTickCount64"); (decltype(GetTickCount64)*)GetProcAddress(GetModuleHandle(L"kernel32"), "GetTickCount64");
if (c_GetTickCount64) if (s_GetTickCount64)
{ {
return c_GetTickCount64(); return s_GetTickCount64();
} }
else else
{ {
@ -1089,19 +1089,16 @@ void System::ResetWorkingDirectory()
*/ */
void System::InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection) void System::InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
{ {
typedef BOOL (WINAPI * FPINITCRITEX)(LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD Flags); static auto s_InitializeCriticalSectionEx = IsWindowsVistaOrGreater() ?
static FPINITCRITEX InitializeCriticalSectionEx = IsWindowsVistaOrGreater() ? (decltype(InitializeCriticalSectionEx)*)GetProcAddress(GetModuleHandle(L"kernel32"), "InitializeCriticalSectionEx") : nullptr;
(FPINITCRITEX)GetProcAddress(GetModuleHandle(L"Kernel32"), "InitializeCriticalSectionEx") : nullptr;
if (InitializeCriticalSectionEx) if (s_InitializeCriticalSectionEx &&
s_InitializeCriticalSectionEx(lpCriticalSection, 0, CRITICAL_SECTION_NO_DEBUG_INFO))
{ {
if (InitializeCriticalSectionEx(lpCriticalSection, 0, CRITICAL_SECTION_NO_DEBUG_INFO)) return;
{
return;
}
} }
::InitializeCriticalSectionAndSpinCount(lpCriticalSection, 0); InitializeCriticalSectionAndSpinCount(lpCriticalSection, 0);
} }
/* /*

View File

@ -91,28 +91,26 @@ void PlayerCAD::Initialize()
this); this);
// Add WM_USER/WM_COPYDATA to allowed messages from lower level processes // Add WM_USER/WM_COPYDATA to allowed messages from lower level processes
HMODULE hUser32 = LoadLibrary(L"user32.dll"); const HMODULE hUser32 = GetModuleHandle(L"user32");
if (hUser32)
{
// Try ChangeWindowMessageFilterEx first (Win7+)
FPCHANGEWINDOWMESSAGEFILTEREX ChangeWindowMessageFilterEx = (FPCHANGEWINDOWMESSAGEFILTEREX)GetProcAddress(hUser32, "ChangeWindowMessageFilterEx");
if (ChangeWindowMessageFilterEx)
{
ChangeWindowMessageFilterEx(m_Window, WM_USER, MSGFLT_ALLOW, nullptr);
ChangeWindowMessageFilterEx(m_Window, WM_COPYDATA, MSGFLT_ALLOW, nullptr);
}
else
{
// Try ChangeWindowMessageFilter (Vista)
FPCHANGEWINDOWMESSAGEFILTER ChangeWindowMessageFilter = (FPCHANGEWINDOWMESSAGEFILTER)GetProcAddress(hUser32, "ChangeWindowMessageFilter");
if (ChangeWindowMessageFilter)
{
ChangeWindowMessageFilter(WM_USER, MSGFLT_ALLOW);
ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ALLOW);
}
}
FreeLibrary(hUser32); // Try ChangeWindowMessageFilterEx first (Win7+)
auto changeWindowMessageFilterEx =
(decltype(ChangeWindowMessageFilterEx)*)GetProcAddress(hUser32, "ChangeWindowMessageFilterEx");
if (changeWindowMessageFilterEx)
{
changeWindowMessageFilterEx(m_Window, WM_USER, MSGFLT_ALLOW, nullptr);
changeWindowMessageFilterEx(m_Window, WM_COPYDATA, MSGFLT_ALLOW, nullptr);
}
else
{
// Try ChangeWindowMessageFilter (Vista)
auto changeWindowMessageFilter =
(decltype(ChangeWindowMessageFilter)*)GetProcAddress(hUser32, "ChangeWindowMessageFilter");
if (changeWindowMessageFilter)
{
changeWindowMessageFilter(WM_USER, MSGFLT_ALLOW);
changeWindowMessageFilter(WM_COPYDATA, MSGFLT_ALLOW);
}
} }
WCHAR buffer[MAX_PATH]; WCHAR buffer[MAX_PATH];

View File

@ -21,9 +21,6 @@
#include "Player.h" #include "Player.h"
typedef BOOL (WINAPI * FPCHANGEWINDOWMESSAGEFILTER)(UINT message, DWORD dwFlag);
typedef BOOL (WINAPI * FPCHANGEWINDOWMESSAGEFILTEREX)(HWND hWnd, UINT message, DWORD dwFlag, PCHANGEFILTERSTRUCT pChangeFilterStruct);
class PlayerCAD : public Player class PlayerCAD : public Player
{ {
public: public: