Skip to content

Commit

Permalink
Bug, 解决Vista模式可能找不到advapi32.lib里的一些符号问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed May 28, 2024
1 parent d60bbd8 commit aa93fe1
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Thunks/api-ms-win-core-timezone.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

#include <timezoneapi.h>

#if (YY_Thunks_Support_Version < NTDDI_WIN8)
#pragma comment(lib, "Advapi32.lib")
#endif

namespace YY
{
namespace Thunks
Expand Down
6 changes: 6 additions & 0 deletions src/Thunks/api-ms-win-eventing-provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <evntrace.h>
#endif


#if (YY_Thunks_Support_Version < NTDDI_WIN7)
#pragma comment(lib, "Advapi32.lib")
#endif


namespace YY
{
namespace Thunks
Expand Down
97 changes: 97 additions & 0 deletions src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Static|Win32">
<Configuration>Static</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Static|x64">
<Configuration>Static</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{d9038cfe-f467-4db0-8609-e9f0b77e2fce}</ProjectGuid>
<RootNamespace>SymbolBuildTest</RootNamespace>
<WindowsTargetPlatformVersion>$([Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion('Windows', '10.0'))</WindowsTargetPlatformVersion>
<SymbolsTestCppRootPathTmp>$(SymbolsTestCppRootPath)</SymbolsTestCppRootPathTmp>
<SymbolsTestCppRootPathTmp Condition="'$(SymbolsTestCppRootPathTmp)' == ''">$(Platform)\$(Configuration)\</SymbolsTestCppRootPathTmp>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Static|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Static|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Static|Win32'" Label="PropertySheets">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Static|x64'" Label="PropertySheets">
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Static|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SymbolsTestCppRootPathTmp)</OutDir>
<IntDir>$(SymbolsTestCppRootPathTmp)</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Static|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SymbolsTestCppRootPathTmp)</OutDir>
<IntDir>$(SymbolsTestCppRootPathTmp)</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Static|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(YY_Thunks_File_Path)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Static|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>$(YY_Thunks_File_Path)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="$(SymbolsTestCppPath)" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions src/YY-Thunks.UnitTest/TestProject/Example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SymbolBuildTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>

#include <Windows.h>

// DllMainCRTStartupForYY_Thunks是YY-Thunks中的一个内置符号,强制引用它如果链接成功则说明YY-Thunks肯定被依赖上了。
extern "C" BOOL WINAPI DllMainCRTStartupForYY_Thunks(
HINSTANCE const _hInstance,
DWORD const _uReason,
LPVOID const _pReserved
);

int main()
{
printf("%p\n", &DllMainCRTStartupForYY_Thunks);
return 0;
}
176 changes: 175 additions & 1 deletion src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,170 @@
#pragma push_macro("InterlockedCompareExchange64")
#undef InterlockedCompareExchange64

LSTATUS RunCmd(LPCWSTR FilePath, CString CmdString, CString* pOutString, CString _szCurrentDirectory)
{
SECURITY_ATTRIBUTES sa;
HANDLE hRead, hWrite;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead, &hWrite, &sa, 0))
{
return GetLastError();
}

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;

GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
//关键步骤,CreateProcess函数参数意义请查阅MSDN
//auto TT= EXECDOSCMD.GetBuffer();

if (_szCurrentDirectory.IsEmpty())
{
_szCurrentDirectory.ReleaseBufferSetLength(GetSystemDirectoryW(_szCurrentDirectory.GetBuffer(MAX_PATH), MAX_PATH));
}

if (!CreateProcessW(FilePath, CmdString.GetBuffer(), NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT, NULL, _szCurrentDirectory.GetString(), &si, &pi))
{
return GetLastError();
}

CloseHandle(hWrite);

DWORD bytesRead;

CStringA OutString;
//OutString.reserve(1024);

//OutString.GetBuffer(1024);
while (ReadFile(hRead, OutString.GetBuffer(OutString.GetLength() + 1024) + OutString.GetLength(), 1024, &bytesRead, NULL) && bytesRead)
{
OutString.ReleaseBufferSetLength(OutString.GetLength() + bytesRead);

//OutString._Mylast() += bytesRead;
//OutString.reserve(OutString.size() + 1024);


//buffer中就是执行的结果,可以保存到文本,也可以直接输出
//TRACE(buffer);
//等待10毫秒

Sleep(5);

}

CloseHandle(hRead);

WaitForSingleObject(pi.hProcess, INFINITE);

LSTATUS lStatus = ERROR_INVALID_FUNCTION;

GetExitCodeProcess(pi.hProcess, (LPDWORD)&lStatus);


CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
if (pOutString)
{
const auto _iUtf8Length = OutString.GetLength();
auto _iUtf16Length = MultiByteToWideChar(CP_UTF8, 0, OutString.GetString(), _iUtf8Length, pOutString->GetBuffer(_iUtf8Length), _iUtf8Length);
pOutString->ReleaseBufferSetLength(_iUtf16Length);
}
return lStatus;
}

LSTATUS CreateFileByData(LPCWSTR FilePath, const void* Data, DWORD ccbData)
{
if (Data == NULL)
return ERROR_DATABASE_FULL;

DWORD FileAttr = GetFileAttributes(FilePath);

if (FileAttr != INVALID_FILE_ATTRIBUTES && (FileAttr & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(FilePath, FileAttr & (-1 ^ FILE_ATTRIBUTE_READONLY));
}

LSTATUS lStatus = ERROR_SUCCESS;

auto thFile = CreateFile(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0);

if (thFile != INVALID_HANDLE_VALUE)
{
if (!WriteFile(thFile, Data, ccbData, &ccbData, NULL))
{
lStatus = GetLastError();
}

CloseHandle(thFile);
}
else
{
lStatus = GetLastError();
}

if (FileAttr != INVALID_FILE_ATTRIBUTES)
{
SetFileAttributes(FilePath, FileAttr);
}


return lStatus;
}


CString RunMSBuildTest(
LPCWSTR szPlatform,
LPCWSTR szConfiguration,
CStringW szYY_ThunksFilePath,
LPCWSTR BuildProperty = nullptr
)
{
CStringW szSymbolsTestCppRootPath;
szSymbolsTestCppRootPath.Format(SymbolBuildTestPath L"Example\\%s\\%s\\%s", szPlatform, szConfiguration, PathFindFileNameW(szYY_ThunksFilePath));

CString Cmd;

Cmd.Format(L"\"%s\" \"%s\" -t:Rebuild \"-p:Configuration=%s;Platform=%s;YY_Thunks_File_Path=%s;SymbolsTestCppRootPath=%s\\\"",
MSBuildBinPath LR"(MSBuild.exe)",
SymbolBuildTestPath LR"(Example\SymbolBuildTest.vcxproj)",
szConfiguration,
szPlatform,
szYY_ThunksFilePath.GetString(),
szSymbolsTestCppRootPath.GetString());

if (BuildProperty)
{
Cmd.ReleaseBufferSetLength(Cmd.GetLength() - 1);
Cmd += L';';
Cmd += BuildProperty;
}

CString OutString;

auto lStatus = RunCmd(nullptr, Cmd, &OutString, CStringW());



OutString.Insert(0, (wchar_t)0xfeff);


auto BuildLog = szSymbolsTestCppRootPath + L"Build.log";

CreateFileByData(BuildLog, OutString.GetString(), OutString.GetLength() * sizeof(OutString[0]));

Assert::AreEqual(lStatus, ERROR_SUCCESS, BuildLog);

return OutString;

}

namespace YY
{
namespace Thunks
Expand Down Expand Up @@ -383,5 +547,15 @@ namespace Others

} while (false);
}

TEST_METHOD(可链接性检测)
{
RunMSBuildTest(L"Win32", L"Static", YY_ThunksRootPath L"objs\\x86\\YY_Thunks_for_Win2K.obj");
RunMSBuildTest(L"Win32", L"Static", YY_ThunksRootPath L"objs\\x86\\YY_Thunks_for_WinXP.obj");
RunMSBuildTest(L"Win32", L"Static", YY_ThunksRootPath L"objs\\x86\\YY_Thunks_for_Vista.obj");

RunMSBuildTest(L"x64", L"Static", YY_ThunksRootPath L"objs\\x64\\YY_Thunks_for_WinXP.obj");
RunMSBuildTest(L"x64", L"Static", YY_ThunksRootPath L"objs\\x64\\YY_Thunks_for_Vista.obj");
}
};
}
}
8 changes: 4 additions & 4 deletions src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>__YY_Thunks_Unit_Test;WIN32;NDEBUG;YY_Thunks_Support_Version=NTDDI_WIN2K;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SymbolBuildTestPath=LR"($(ProjectDir)TestProject\)";YY_ThunksRootPath=LR"($(ProjectDir)..\..\)";MSBuildBinPath=LR"($(MSBuildBinPath)\)";__YY_Thunks_Unit_Test;WIN32;NDEBUG;YY_Thunks_Support_Version=NTDDI_WIN2K;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand All @@ -117,7 +117,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>__YY_Thunks_Unit_Test;WIN32;_DEBUG;YY_Thunks_Support_Version=NTDDI_WIN2K;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SymbolBuildTestPath=LR"($(ProjectDir)TestProject\)";YY_ThunksRootPath=LR"($(ProjectDir)..\..\)";MSBuildBinPath=LR"($(MSBuildBinPath)\)";__YY_Thunks_Unit_Test;WIN32;_DEBUG;YY_Thunks_Support_Version=NTDDI_WIN2K;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand All @@ -136,7 +136,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>__YY_Thunks_Unit_Test;_DEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SymbolBuildTestPath=LR"($(ProjectDir)TestProject\)";YY_ThunksRootPath=LR"($(ProjectDir)..\..\)";MSBuildBinPath=LR"($(MSBuildBinPath)\)";__YY_Thunks_Unit_Test;_DEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand All @@ -157,7 +157,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>__YY_Thunks_Unit_Test;NDEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SymbolBuildTestPath=LR"($(ProjectDir)TestProject\)";YY_ThunksRootPath=LR"($(ProjectDir)..\..\)";MSBuildBinPath=LR"($(MSBuildBinPath)\)";__YY_Thunks_Unit_Test;NDEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand Down

0 comments on commit aa93fe1

Please sign in to comment.