Skip to content

Commit

Permalink
Fea #66, 添加FindNLSStringEx(让.NET 8/9 支持 Windows XP)
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed Sep 3, 2024
1 parent 0dcf233 commit 69b3baa
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 241 deletions.
1 change: 1 addition & 0 deletions ThunksList.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@
| GetCalendarInfoEx | 不存在时,调用GetCalendarInfoW。
| GetNLSVersionEx | 不存在时,返回一个假版本。
| IsNLSDefinedString | 不存在时,调用GetStringTypeW。
| FindNLSStringEx | 调用 CompareStringW。
| SetProcessWorkingSetSizeEx | 不存在时,调用SetProcessWorkingSetSize。
| GetProcessWorkingSetSizeEx | 不存在时,调用GetProcessWorkingSetSize。
| GetTimeZoneInformationForYear | 不存在时,直接读取`Time Zones`注册表。
Expand Down
134 changes: 134 additions & 0 deletions src/Thunks/api-ms-win-core-localization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2537,4 +2537,138 @@

}
#endif


#if (YY_Thunks_Target < __WindowsNT6)

// 最低受支持的客户端 Windows Vista [桌面应用 | UWP 应用]
// 最低受支持的服务器 Windows Server 2008[桌面应用 | UWP 应用]
__DEFINE_THUNK(
kernel32,
40,
int,
WINAPI,
FindNLSStringEx,
_In_opt_ LPCWSTR _szLocaleName,
_In_ DWORD _fFindNLSStringFlags,
_In_reads_(_cchSource) LPCWSTR _szStringSource,
_In_ int _cchSource,
_In_reads_(_cchValue) LPCWSTR _szStringValue,
_In_ int _cchValue,
_Out_opt_ LPINT _pcchFound,
_In_opt_ LPNLSVERSIONINFO _pVersionInformation,
_In_opt_ LPVOID _pReserved,
_In_opt_ LPARAM _hSortHandle
)
{
if (auto const _pfnFindNLSStringEx = try_get_FindNLSStringEx())
{
return _pfnFindNLSStringEx(_szLocaleName, _fFindNLSStringFlags, _szStringSource, _cchSource, _szStringValue, _cchValue, _pcchFound, _pVersionInformation, _pReserved, _hSortHandle);
}

__WarningMessage__("FindNLSStringEx 暂时只支持搜索 _cchValue 的子字符串。");

if (_pVersionInformation || _pReserved|| _hSortHandle
|| _szStringSource == nullptr || _cchSource == 0 || _cchSource < -1
|| _szStringValue == nullptr || _cchValue == 0 || _cchValue < -1)
{
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}

const auto _Locale = LocaleNameToLCID(_szLocaleName, 0);
if (_Locale == 0)
{
SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}

if (_cchSource == -1)
{
_cchSource = wcslen(_szStringSource);
}

if (_cchValue == -1)
{
_cchValue = wcslen(_szStringValue);
}

if (_cchSource < _cchValue)
return -1;

const DWORD _fCmpFlags = _fFindNLSStringFlags & ~(FIND_STARTSWITH | FIND_ENDSWITH | FIND_FROMSTART | FIND_FROMEND);
if (_fFindNLSStringFlags & (FIND_FROMSTART | FIND_STARTSWITH))
{
// 从头开始搜索
if (_fFindNLSStringFlags & FIND_STARTSWITH)
{
const auto _nResult = CompareStringW(_Locale, _fCmpFlags, _szStringSource, _cchValue, _szStringValue, _cchValue);
if (_nResult == CSTR_EQUAL)
{
if (_pcchFound)
*_pcchFound = _cchValue;

return 0;
}
}
else
{
auto _szStr = _szStringSource;
auto _szStrEnd = _szStringSource + _cchSource - _cchValue + 1;
for (; _szStr != _szStrEnd;++_szStr)
{
const auto _nResult = CompareStringW(_Locale, _fCmpFlags, _szStr, _cchValue, _szStringValue, _cchValue);
if (_nResult == 0)
return -1;

if (_nResult == CSTR_EQUAL)
{
if (_pcchFound)
*_pcchFound = _cchValue;

return _szStr - _szStringSource;
}
}
}
}
else
{
// 反向搜索
auto _szStr = _szStringSource + _cchSource - _cchValue;
if (_fFindNLSStringFlags & FIND_ENDSWITH)
{
const auto _nResult = CompareStringW(_Locale, _fCmpFlags, _szStr, _cchValue, _szStringValue, _cchValue);
if (_nResult == CSTR_EQUAL)
{
if (_pcchFound)
*_pcchFound = _cchValue;

return _szStr - _szStringSource;
}
}
else
{
for(;; --_szStr)
{
const auto _nResult = CompareStringW(_Locale, _fCmpFlags, _szStr, _cchValue, _szStringValue, _cchValue);
if (_nResult == 0)
return -1;

if (_nResult == CSTR_EQUAL)
{
if (_pcchFound)
*_pcchFound = _cchValue;

return _szStr - _szStringSource;
}

if (_szStr == _szStringSource)
break;
}
}
}

return -1;
}
#endif
} //namespace YY
Loading

0 comments on commit 69b3baa

Please sign in to comment.