Skip to content

Commit

Permalink
Fea, YY-Thunks等级添加Win7、Win8以及Win10
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed May 30, 2024
1 parent 6cc251b commit 33dc0f7
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 262 deletions.
10 changes: 10 additions & 0 deletions NuGet/build/native/YY-Thunks.targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="UserMacros" Condition="('$(PlatformShortName)'=='x86') Or ('$(PlatformShortName)'=='x64')">
<!--[MSBuild]::VersionGreaterThanOrEquals 需要16.5才支持,暂时凑合这样凑合用吧-->
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('5.0.')) == 0) And ('$(PlatformShortName)'=='x86')">YY_Thunks_for_Win2K.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And (('$(SupportWinXP)'=='true') Or ($(PlatformToolset.IndexOf('_xp')) != -1))">YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('5.1.')) == 0)">YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('5.2.')) == 0)">YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('6.0.')) == 0)">YY_Thunks_for_Vista.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('6.1.')) == 0)">YY_Thunks_for_Win7.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('6.2.')) == 0)">YY_Thunks_for_Win8.obj</YY_Thunks_File>
<YY_Thunks_File Condition="('$(YY_Thunks_File)'=='') And ($(WindowsTargetPlatformMinVersion.IndexOf('10.')) == 0)">YY_Thunks_for_Win10.obj</YY_Thunks_File>

<!--默认值-->
<YY_Thunks_File Condition="'$(YY_Thunks_File)'==''">YY_Thunks_for_Vista.obj</YY_Thunks_File>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@

<EnumValue
Name="YY_Thunks_for_WinXP.obj"
DisplayName="为我的代码自动修正到Windows XP兼容,谢谢。"/>
DisplayName="为我的代码自动修正到Windows XP RTM兼容,谢谢。"/>
<EnumValue
Name="YY_Thunks_for_Vista.obj"
DisplayName="为我的代码自动修正到Windows Vista兼容,谢谢。"/>
DisplayName="为我的代码自动修正到Windows Vista RTM兼容,谢谢。"/>
<EnumValue
Name="YY_Thunks_for_Win7.obj"
DisplayName="为我的代码自动修正到Windows 7 RTM兼容,谢谢。"/>
<EnumValue
Name="YY_Thunks_for_Win8.obj"
DisplayName="为我的代码自动修正到Windows 8 RTM兼容,谢谢。"/>
<EnumValue
Name="YY_Thunks_for_Win10.obj"
DisplayName="为我的代码自动修正到Windows 10 10240兼容,谢谢。"/>
</EnumProperty>
</Rule>
</ProjectSchemaDefinitions>
33 changes: 17 additions & 16 deletions NuGet/build/net8.0-windows7.0/YY-Thunks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="DotNET_Import_YY_Thunks_907ECCAB-58CA-4C11-B809-B53585C6BFE1" BeforeTargets="SetupOSSpecificProps">
<PropertyGroup>
<YY_Thunks_File></YY_Thunks_File>
<YY_Thunks_PlatformShortName></YY_Thunks_PlatformShortName>
<YY_Thunks_PlatformShortName Condition="'$(RuntimeIdentifier)'=='win-x64'">x64</YY_Thunks_PlatformShortName>
<YY_Thunks_PlatformShortName Condition="'$(RuntimeIdentifier)'=='win-x86'">x86</YY_Thunks_PlatformShortName>
</PropertyGroup>
<PropertyGroup Condition="'$(RuntimeIdentifier)'=='win-x64'">
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 6.0))">x64\YY_Thunks_for_Vista.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == ''">x64\YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<PropertyGroup Condition="'$(RuntimeIdentifier)'=='win-x64' or '$(RuntimeIdentifier)'=='win-x86'">
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 10.0))">YY_Thunks_for_Win10.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 6.2))">YY_Thunks_for_Win8.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 6.1))">YY_Thunks_for_Win7.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 6.0))">YY_Thunks_for_Vista.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 5.1))">YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and '$(RuntimeIdentifier)'=='win-x64'">YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == ''">YY_Thunks_for_Win2K.obj</YY_Thunks_File>
</PropertyGroup>
<PropertyGroup Condition="'$(RuntimeIdentifier)'=='win-x86'">
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 6.0))">x86\YY_Thunks_for_Vista.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == '' and $([MSBuild]::VersionGreaterThanOrEquals($(TargetPlatformMinVersion), 5.1))">x86\YY_Thunks_for_WinXP.obj</YY_Thunks_File>
<YY_Thunks_File Condition="'$(YY_Thunks_File)' == ''">x86\YY_Thunks_for_Win2K.obj</YY_Thunks_File>
</PropertyGroup>
<PropertyGroup Condition="'$(LinkerSubsystem)'=='WINDOWS' or '$(LinkerSubsystem)'=='CONSOLE'">
<LinkerSubsystem Condition="'$(YY_Thunks_File)'=='x64\YY_Thunks_for_WinXP.obj'">$(LinkerSubsystem),5.02</LinkerSubsystem>
<PropertyGroup Condition="('$(LinkerSubsystem)'=='WINDOWS' or '$(LinkerSubsystem)'=='CONSOLE') and ('$(YY_Thunks_File)'=='YY_Thunks_for_WinXP.obj' or '$(YY_Thunks_File)'=='YY_Thunks_for_Win2K.obj')">
<LinkerSubsystem Condition="'$(YY_Thunks_PlatformShortName)'=='x64'">$(LinkerSubsystem),5.02</LinkerSubsystem>
<!--微软的链接器无法设置 5.0,一般人应该也不需要兼容Windows 2K,先统一设置为Windows XP吧。-->
<LinkerSubsystem Condition="'$(YY_Thunks_File)'=='x86\YY_Thunks_for_WinXP.obj' or '$(YY_Thunks_File)'=='x86\YY_Thunks_for_Win2K.obj'">$(LinkerSubsystem),5.01</LinkerSubsystem>
<LinkerSubsystem Condition="'$(YY_Thunks_PlatformShortName)'=='x86'">$(LinkerSubsystem),5.01</LinkerSubsystem>
</PropertyGroup>
<ItemGroup Condition="'$(YY_Thunks_File)' != ''">
<LinkerArg Include="$(MSBuildThisFileDirectory)..\native\objs\$(YY_Thunks_File)" />
<LinkerArg Include="$(MSBuildThisFileDirectory)..\native\objs\$(YY_Thunks_PlatformShortName)\$(YY_Thunks_File)" />
<!--为早期版本的系统增强Tls支持,如果不使用DllMainCRTStartupForYY_Thunks,使用thread_local时可能发生崩溃。-->
<LinkerArg Condition="'$(NativeLib)' == 'Shared' and '$(OutputType)' == 'Library' and '$(YY_Thunks_File)' == 'x64\YY_Thunks_for_WinXP.obj'" Include="/ENTRY:DllMainCRTStartupForYY_Thunks" />
<LinkerArg Condition="'$(NativeLib)' == 'Shared' and '$(OutputType)' == 'Library' and '$(YY_Thunks_File)' == 'x86\YY_Thunks_for_WinXP.obj'" Include="/ENTRY:DllMainCRTStartupForYY_Thunks" />
<LinkerArg Condition="'$(NativeLib)' == 'Shared' and '$(OutputType)' == 'Library' and '$(YY_Thunks_File)' == 'x86\YY_Thunks_for_Win2K.obj'" Include="/ENTRY:DllMainCRTStartupForYY_Thunks" />
<LinkerArg Condition="'$(NativeLib)' == 'Shared' and '$(OutputType)' == 'Library' and '$(YY_Thunks_File)' == 'YY_Thunks_for_WinXP.obj'" Include="/ENTRY:DllMainCRTStartupForYY_Thunks" />
<LinkerArg Condition="'$(NativeLib)' == 'Shared' and '$(OutputType)' == 'Library' and '$(YY_Thunks_File)' == 'YY_Thunks_for_Win2K.obj'" Include="/ENTRY:DllMainCRTStartupForYY_Thunks" />
</ItemGroup>
</Target>
</Project>
7 changes: 7 additions & 0 deletions src/Build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,25 @@ set PointType=4
call:BuildObj YY_Thunks_for_Win2K.obj NTDDI_WIN2K PSAPI2Kernel32.def+esent.def
call:BuildObj YY_Thunks_for_WinXP.obj NTDDI_WINXP PSAPI2Kernel32.def+esent.def
call:BuildObj YY_Thunks_for_Vista.obj NTDDI_WIN6 PSAPI2Kernel32.def
call:BuildObj YY_Thunks_for_Win7.obj NTDDI_WIN7
call:BuildObj YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildObj YY_Thunks_for_Win10.obj NTDDI_WIN10
goto:eof


:Buildx64
set PointType=8
call:BuildObj YY_Thunks_for_WinXP.obj NTDDI_WS03SP1 PSAPI2Kernel32.def+esent.def
call:BuildObj YY_Thunks_for_Vista.obj NTDDI_WIN6 PSAPI2Kernel32.def
call:BuildObj YY_Thunks_for_Win7.obj NTDDI_WIN7
call:BuildObj YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildObj YY_Thunks_for_Win10.obj NTDDI_WIN10
goto:eof

:Buildarm
set PointType=4
call:BuildObj YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildObj YY_Thunks_for_Win10.obj NTDDI_WIN10
goto:eof

:Buildarm64
Expand Down
53 changes: 25 additions & 28 deletions src/Thunks/BCryptPrimitives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,34 @@ extern "C" BOOL WINAPI ProcessPrng(
_In_ ULONG _cbBuffer
);

namespace YY
namespace YY::Thunks
{
namespace Thunks
{
#if (YY_Thunks_Support_Version < NTDDI_WIN7)
#if (YY_Thunks_Support_Version < NTDDI_WIN8)

// 用户反馈Windows Vista会报告此API不存在。
__DEFINE_THUNK(
bcryptprimitives,
8,
BOOL,
WINAPI,
ProcessPrng,
_Out_writes_bytes_(_cbBuffer) PUCHAR _pbBuffer,
_In_ ULONG _cbBuffer
)
// 用户反馈Windows 7也会报告此API不存在。
__DEFINE_THUNK(
bcryptprimitives,
8,
BOOL,
WINAPI,
ProcessPrng,
_Out_writes_bytes_(_cbBuffer) PUCHAR _pbBuffer,
_In_ ULONG _cbBuffer
)
{
if (auto _pfnProcessPrng = try_get_ProcessPrng())
{
if (auto _pfnProcessPrng = try_get_ProcessPrng())
{
return _pfnProcessPrng(_pbBuffer, _cbBuffer);
}
return _pfnProcessPrng(_pbBuffer, _cbBuffer);
}

const auto _pfnRtlGenRandom = try_get_SystemFunction036();
if (!_pfnRtlGenRandom)
{
SetLastError(ERROR_FUNCTION_FAILED);
return FALSE;
}
const auto _pfnRtlGenRandom = try_get_SystemFunction036();
if (!_pfnRtlGenRandom)
{
SetLastError(ERROR_FUNCTION_FAILED);
return FALSE;
}

return _pfnRtlGenRandom(_pbBuffer, _cbBuffer);
}
return _pfnRtlGenRandom(_pbBuffer, _cbBuffer);
}
#endif
} // namespace Thunks
} // namespace YY
} // namespace YY::Thunks
9 changes: 4 additions & 5 deletions src/Thunks/YY_Thunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ static HMODULE __fastcall try_load_library_from_system_directory(wchar_t const*
#define USING_UNSAFE_LOAD 0x00000001
#define LOAD_AS_DATA_FILE 0x00000002

template<int Flags>
static HMODULE __fastcall try_get_module(volatile HMODULE* pModule, const wchar_t* module_name) noexcept
static HMODULE __fastcall try_get_module(volatile HMODULE* pModule, const wchar_t* module_name, int Flags) noexcept
{
// First check to see if we've cached the module handle:
if (HMODULE const cached_handle = __crt_interlocked_read_pointer(pModule))
Expand All @@ -272,11 +271,11 @@ static HMODULE __fastcall try_get_module(volatile HMODULE* pModule, const wchar_
// this fails, cache the sentinel handle value INVALID_HANDLE_VALUE so that
// we don't attempt to load the module again:
HMODULE new_handle = NULL;
if constexpr (Flags & LOAD_AS_DATA_FILE)
if (Flags & LOAD_AS_DATA_FILE)
{
new_handle = LoadLibraryExW(module_name, NULL, LOAD_LIBRARY_AS_DATAFILE);
}
else if constexpr (Flags & USING_UNSAFE_LOAD)
else if (Flags & USING_UNSAFE_LOAD)
{
new_handle = LoadLibraryW(module_name);
}
Expand Down Expand Up @@ -312,7 +311,7 @@ static HMODULE __fastcall try_get_module(volatile HMODULE* pModule, const wchar_
static HMODULE __fastcall _CRT_CONCATENATE(try_get_module_, _MODULE)() noexcept \
{ \
__declspec(allocate(".YYThu$AAA")) static volatile HMODULE hModule; \
return try_get_module<_FLAGS>(&hModule, _CRT_CONCATENATE(module_name_, _MODULE)); \
return try_get_module(&hModule, _CRT_CONCATENATE(module_name_, _MODULE), _FLAGS); \
}
_YY_APPLY_TO_LATE_BOUND_MODULES(_APPLY)
#undef _APPLY
Expand Down
Loading

0 comments on commit 33dc0f7

Please sign in to comment.