Skip to content

Commit

Permalink
[win32] Always call SetLastError(ERROR_SUCCESS) before GetModuleFileN…
Browse files Browse the repository at this point in the history
…ame().

Windows XP doesn't call SetLastError() in GetModuleFileName() at all,
so it's possible that a previous error could show up here. In this case,
RegKey::RegisterComObject() was getting error 127 (ERROR_PROC_NOT_FOUND),
which prevented registering rom-properties.dll on Windows XP.

I'm not sure when exactly this showed up, since it could be completely
random depending on GetProcAddress() usage.

Interestingly, a few files already called SetLastError(ERROR_SUCCESS):
- i18n.c
- DelayLoadHelper.c
- DllMain.cpp
  • Loading branch information
GerbilSoft committed Jul 24, 2022
1 parent 5d67072 commit e845f51
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/libi18n/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void rp_i18n_init_int(void)
// Get the current module filename.
// NOTE: Delay-load only supports ANSI module names.
// We'll assume it's ASCII and do a simple conversion to Unicode.
SetLastError(ERROR_SUCCESS);
SetLastError(ERROR_SUCCESS); // required for XP
dwResult = GetModuleFileName(HINST_THISCOMPONENT,
tpathname, ARRAY_SIZE(tpathname));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
Expand Down
1 change: 1 addition & 0 deletions src/libromdata/data/AmiiboData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ string AmiiboDataPrivate::getAmiiboBinFilename(AmiiboBinFileType amiiboBinFileTy
filename = DIR_INSTALL_SHARE DIR_SEP_STR AMIIBO_BIN_FILENAME;
#elif defined(_WIN32)
TCHAR dll_filename[MAX_PATH];
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(HINST_THISCOMPONENT,
dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
Expand Down
3 changes: 2 additions & 1 deletion src/libromdata/img/ExecRpDownload_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ROM Properties Page shell extension. (libromdata) *
* ExecRpDownload_win32.cpp: Execute rp-download.exe. (Win32) *
* *
* Copyright (c) 2016-2020 by David Korth. *
* Copyright (c) 2016-2022 by David Korth. *
* SPDX-License-Identifier: GPL-2.0-or-later *
***************************************************************************/

Expand Down Expand Up @@ -32,6 +32,7 @@ int CacheManager::execRpDownload(const string &filteredCacheKey)
{
// The executable should be located in the DLL directory.
TCHAR dll_filename[MAX_PATH];
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(HINST_THISCOMPONENT,
dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
Expand Down
2 changes: 1 addition & 1 deletion src/libwin32common/DelayLoadHelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static HMODULE WINAPI rp_loadLibrary(LPCSTR pszModuleName)

// NOTE: Delay-load only supports ANSI module names.
// We'll assume it's ASCII and do a simple conversion to Unicode.
SetLastError(ERROR_SUCCESS);
SetLastError(ERROR_SUCCESS); // required for XP
dwResult = GetModuleFileName(HINST_THISCOMPONENT,
dll_fullpath, _countof(dll_fullpath));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
Expand Down
1 change: 1 addition & 0 deletions src/libwin32common/RegKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ LONG RegKey::RegisterComObject(HINSTANCE hInstance, REFCLSID rclsid,
// Set the default value to the filename of the specified DLL.
// TODO: Duplicated from win32/. Consolidate the two?
TCHAR dll_filename[MAX_PATH];
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(hInstance, dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
// Cannot get the DLL filename.
Expand Down
1 change: 1 addition & 0 deletions src/svrplus/svrplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ static InstallServerResult InstallServer(bool isUninstall, bool is64, DWORD *pEr

// Construct arguments
// Construct path to rom-properties.dll inside the arguments
SetLastError(ERROR_SUCCESS); // required for XP
szModuleFn = GetModuleFileName(HINST_THISCOMPONENT, &args[14], MAX_PATH);
assert(szModuleFn != 0);
assert(szModuleFn < MAX_PATH);
Expand Down
2 changes: 1 addition & 1 deletion src/win32/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
switch (dwReason) {
case DLL_PROCESS_ATTACH: {
// Get the DLL filename.
SetLastError(ERROR_SUCCESS);
SetLastError(ERROR_SUCCESS); // required for XP
DWORD dwResult = GetModuleFileName(hInstance,
dll_filename, _countof(dll_filename));
if (dwResult == 0 || GetLastError() != ERROR_SUCCESS) {
Expand Down
1 change: 1 addition & 0 deletions src/win32/config/rp-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
exe_path = malloc(MAX_PATH*sizeof(TCHAR));
if (!exe_path)
FAIL_MESSAGE(_T("Failed to allocate memory for the EXE path."));
SetLastError(ERROR_SUCCESS); // required for XP
exe_path_len = GetModuleFileName(hInstance, exe_path, EXE_PATH_LEN);
if (exe_path_len == 0 || exe_path_len >= EXE_PATH_LEN)
FAIL_MESSAGE(_T("Failed to get the EXE path."));
Expand Down

0 comments on commit e845f51

Please sign in to comment.