Skip to content

Commit

Permalink
Cleanup LoadLibrary() and GetModuleHandle() for Windows 2000.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Sep 10, 2017
1 parent f712959 commit 69fe443
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 66 deletions.
3 changes: 2 additions & 1 deletion metapath/src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ INT_PTR CALLBACK GotoDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
}
}

if ((fp = GetProcAddress(GetModuleHandle(L"User32"), "GetComboBoxInfo")) != NULL) {
// since Windows Vista
if ((fp = GetProcAddress(GetModuleHandle(L"user32.dll"), "GetComboBoxInfo")) != NULL) {
COMBOBOXINFO cbi;
cbi.cbSize = sizeof(COMBOBOXINFO);
if (fp(GetDlgItem(hwnd, IDC_GOTO), &cbi)) {
Expand Down
51 changes: 3 additions & 48 deletions metapath/src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,60 +161,17 @@ DWORD WINAPI GetModuleFileNameExW(HANDLE, HMODULE, LPTSTR, DWORD);
BOOL WINAPI EnumProcessModules(HANDLE, HMODULE *, DWORD, LPDWORD);

BOOL ExeNameFromWnd(HWND hwnd, LPWSTR szExeName, int cchExeName) {
//HMODULE hPSAPI;
DWORD dwProcessId;
HANDLE hProcess;
HMODULE hModule;
DWORD cbNeeded = 0;
//FARPROC fpCreateToolhelp32Snapshot;
//FARPROC fpProcess32First;
//FARPROC fpProcess32Next;
//HANDLE hProcessList;
//PROCESSENTRY32 pe;
//BOOL bMoreEntries;
//BOOL bFoundMatching = FALSE;

/*if (IsWindowsNT()) {
if (hPSAPI = LoadLibrary(L"PSAPI")) {*/

GetWindowThreadProcessId(hwnd, &dwProcessId);
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
/*GetProcAddress(hPSAPI, "EnumProcessModules")*/
EnumProcessModules(hProcess, &hModule, sizeof(HMODULE), &cbNeeded);
/*GetProcAddress(hPSAPI, "GetModuleFileNameExW")*/
GetModuleFileNameExW(hProcess, hModule, szExeName, cchExeName);
CloseHandle(hProcess);
//FreeLibrary(hPSAPI);
return TRUE;
/*} else {
return FALSE;
}
} else {
fpCreateToolhelp32Snapshot = GetProcAddress(GetModuleHandle(L"Kernel32"), "CreateToolhelp32Snapshot");
fpProcess32First = GetProcAddress(GetModuleHandle(L"Kernel32"), "Process32First");
fpProcess32Next = GetProcAddress(GetModuleHandle(L"Kernel32"), "Process32Next");
if (fpCreateToolhelp32Snapshot) {
GetWindowThreadProcessId(hwnd, &dwProcessId);
hProcessList = (HANDLE)fpCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe.dwSize = sizeof(PROCESSENTRY32);
bMoreEntries = fpProcess32First(hProcessList, &pe);
while (bMoreEntries) {
if (pe.th32ProcessID == dwProcessId) {
bMoreEntries = FALSE;
bFoundMatching = TRUE;
lstrcpyn(szExeName, pe.szExeFile, cchExeName);
} else {
bMoreEntries = fpProcess32Next(hProcessList, &pe);
}
}
CloseHandle(hProcessList);
return (bFoundMatching);
} else {
return FALSE;
}
}
*/
}

////=============================================================================
Expand Down Expand Up @@ -494,10 +451,8 @@ void DeleteBitmapButton(HWND hwnd, int nCtlId) {
// SetWindowTransparentMode()
//
void SetWindowTransparentMode(HWND hwnd, BOOL bTransparentMode) {
FARPROC fp;

if (bTransparentMode) {
if ((fp = GetProcAddress(GetModuleHandle(L"User32"), "SetLayeredWindowAttributes")) != NULL) {
if (IsWin2KAndAbove()) {
int iAlphaPercent;
BYTE bAlpha;
SetWindowLongPtr(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
Expand All @@ -509,7 +464,7 @@ void SetWindowTransparentMode(HWND hwnd, BOOL bTransparentMode) {
}
bAlpha = (BYTE)(iAlphaPercent * 255 / 100);

fp(hwnd, 0, bAlpha, LWA_ALPHA);
SetLayeredWindowAttributes(hwnd, 0, bAlpha, LWA_ALPHA);
}
} else {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
Expand Down
16 changes: 7 additions & 9 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3871,7 +3871,6 @@ typedef struct _SORTLINE {
WCHAR *pwszSortEntry;
} SORTLINE;

static FARPROC pfnStrCmpLogicalW;
typedef int (__stdcall *FNSTRCMP)(LPCWSTR, LPCWSTR);

int CmpStd(const void *s1, const void *s2) {
Expand All @@ -3885,9 +3884,9 @@ int CmpStdRev(const void *s1, const void *s2) {
}

int CmpLogical(const void *s1, const void *s2) {
int cmp = (int)pfnStrCmpLogicalW(((SORTLINE *)s1)->pwszSortEntry, ((SORTLINE *)s2)->pwszSortEntry);
int cmp = (int)StrCmpLogicalW(((SORTLINE *)s1)->pwszSortEntry, ((SORTLINE *)s2)->pwszSortEntry);
if (cmp == 0) {
cmp = (int)pfnStrCmpLogicalW(((SORTLINE *)s1)->pwszLine, ((SORTLINE *)s2)->pwszLine);
cmp = (int)StrCmpLogicalW(((SORTLINE *)s1)->pwszLine, ((SORTLINE *)s2)->pwszLine);
}
if (cmp) {
return cmp;
Expand All @@ -3898,9 +3897,9 @@ int CmpLogical(const void *s1, const void *s2) {
}

int CmpLogicalRev(const void *s1, const void *s2) {
int cmp = -1 * (int)pfnStrCmpLogicalW(((SORTLINE *)s1)->pwszSortEntry, ((SORTLINE *)s2)->pwszSortEntry);
int cmp = -1 * (int)StrCmpLogicalW(((SORTLINE *)s1)->pwszSortEntry, ((SORTLINE *)s2)->pwszSortEntry);
if (cmp == 0) {
cmp = -1 * (int)pfnStrCmpLogicalW(((SORTLINE *)s1)->pwszLine, ((SORTLINE *)s2)->pwszLine);
cmp = -1 * (int)StrCmpLogicalW(((SORTLINE *)s1)->pwszLine, ((SORTLINE *)s2)->pwszLine);
}
if (cmp) {
return cmp;
Expand Down Expand Up @@ -3941,7 +3940,6 @@ void EditSortLines(HWND hwnd, int iSortFlags) {
BOOL bLastDup = FALSE;
FNSTRCMP pfnStrCmp;

pfnStrCmpLogicalW = GetProcAddress(GetModuleHandle(L"shlwapi"), "StrCmpLogicalW");
pfnStrCmp = (iSortFlags & SORT_NOCASE) ? StrCmpIW : StrCmpW;

iCurPos = (int)SendMessage(hwnd, SCI_GETCURRENTPOS, 0, 0);
Expand Down Expand Up @@ -4054,7 +4052,7 @@ void EditSortLines(HWND hwnd, int iSortFlags) {
}

if (iSortFlags & SORT_DESCENDING) {
if ((iSortFlags & SORT_LOGICAL) && pfnStrCmpLogicalW) {
if ((iSortFlags & SORT_LOGICAL) && IsWinXPAndAbove()) {
qsort(pLines, iLineCount, sizeof(SORTLINE), CmpLogicalRev);
} else {
qsort(pLines, iLineCount, sizeof(SORTLINE), CmpStdRev);
Expand All @@ -4071,7 +4069,7 @@ void EditSortLines(HWND hwnd, int iSortFlags) {
pLines[j].pwszSortEntry = sLine.pwszSortEntry;
}
} else {
if ((iSortFlags & SORT_LOGICAL) && pfnStrCmpLogicalW) {
if ((iSortFlags & SORT_LOGICAL) && IsWinXPAndAbove()) {
qsort(pLines, iLineCount, sizeof(SORTLINE), CmpLogical);
} else {
qsort(pLines, iLineCount, sizeof(SORTLINE), CmpStd);
Expand Down Expand Up @@ -6135,7 +6133,7 @@ INT_PTR CALLBACK EditSortDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lPa
CheckDlgButton(hwnd, 106, BST_CHECKED);
}

if (GetProcAddress(GetModuleHandle(L"shlwapi"), "StrCmpLogicalW")) {
if (IsWinXPAndAbove()) {
if (*piSortFlags & SORT_LOGICAL) {
CheckDlgButton(hwnd, 107, BST_CHECKED);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ HRESULT PrivateSetCurrentProcessExplicitAppUserModelID(PCWSTR AppID) {
return S_OK;
}

// since Windows 7
pfnSetCurrentProcessExplicitAppUserModelID =
GetProcAddress(GetModuleHandleA("shell32.dll"), "SetCurrentProcessExplicitAppUserModelID");
GetProcAddress(GetModuleHandle(L"shell32.dll"), "SetCurrentProcessExplicitAppUserModelID");

if (pfnSetCurrentProcessExplicitAppUserModelID) {
return (HRESULT)pfnSetCurrentProcessExplicitAppUserModelID(AppID);
Expand Down Expand Up @@ -455,8 +456,7 @@ BOOL SetWindowTitle(HWND hwnd, UINT uIDAppName, BOOL bIsElevated, UINT uIDUntitl
//
void SetWindowTransparentMode(HWND hwnd, BOOL bTransparentMode) {
if (bTransparentMode) {
FARPROC fp;
if ((fp = GetProcAddress(GetModuleHandle(L"User32"), "SetLayeredWindowAttributes")) != NULL) {
if (IsWin2KAndAbove()) {
int iAlphaPercent;
BYTE bAlpha;
SetWindowLongPtr(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
Expand All @@ -468,7 +468,7 @@ void SetWindowTransparentMode(HWND hwnd, BOOL bTransparentMode) {
}
bAlpha = (BYTE)(iAlphaPercent * 255 / 100);

fp(hwnd, 0, bAlpha, LWA_ALPHA);
SetLayeredWindowAttributes(hwnd, 0, bAlpha, LWA_ALPHA);
}
} else {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, GetWindowLongPtr(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
Expand Down
5 changes: 1 addition & 4 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5245,10 +5245,7 @@ void LoadSettings(void) {
bAlwaysOnTop = IniSectionGetBool(pIniSection, L"AlwaysOnTop", 0);
bMinimizeToTray = IniSectionGetBool(pIniSection, L"MinimizeToTray", 0);
bTransparentMode = IniSectionGetBool(pIniSection, L"TransparentMode", 0);

// Check if SetLayeredWindowAttributes() is available
bTransparentModeAvailable =
(GetProcAddress(GetModuleHandle(L"User32"), "SetLayeredWindowAttributes") != NULL);
bTransparentModeAvailable = IsWin2KAndAbove();

IniSectionGetString(pIniSection, L"ToolbarButtons", L"", tchToolbarButtons, COUNTOF(tchToolbarButtons));

Expand Down

0 comments on commit 69fe443

Please sign in to comment.