diff --git a/src/Thunks/api-ms-win-core-timezone.hpp b/src/Thunks/api-ms-win-core-timezone.hpp index a2c693b..ea23b5f 100644 --- a/src/Thunks/api-ms-win-core-timezone.hpp +++ b/src/Thunks/api-ms-win-core-timezone.hpp @@ -1,6 +1,10 @@  #include +#if (YY_Thunks_Support_Version < NTDDI_WIN8) +#pragma comment(lib, "Advapi32.lib") +#endif + namespace YY { namespace Thunks diff --git a/src/Thunks/api-ms-win-eventing-provider.hpp b/src/Thunks/api-ms-win-eventing-provider.hpp index 7171c79..f2619fb 100644 --- a/src/Thunks/api-ms-win-eventing-provider.hpp +++ b/src/Thunks/api-ms-win-eventing-provider.hpp @@ -6,6 +6,12 @@ #include #endif + +#if (YY_Thunks_Support_Version < NTDDI_WIN7) +#pragma comment(lib, "Advapi32.lib") +#endif + + namespace YY { namespace Thunks diff --git a/src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj b/src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj new file mode 100644 index 0000000..486b35a --- /dev/null +++ b/src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj @@ -0,0 +1,97 @@ + + + + + Static + Win32 + + + Static + x64 + + + + 16.0 + Win32Proj + {d9038cfe-f467-4db0-8609-e9f0b77e2fce} + SymbolBuildTest + $([Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion('Windows', '10.0')) + $(SymbolsTestCppRootPath) + $(Platform)\$(Configuration)\ + + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + + + + + + + + + + + false + $(SymbolsTestCppRootPathTmp) + $(SymbolsTestCppRootPathTmp) + + + false + $(SymbolsTestCppRootPathTmp) + $(SymbolsTestCppRootPathTmp) + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + MultiThreaded + + + Console + true + true + $(YY_Thunks_File_Path) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + MultiThreaded + + + Console + true + true + $(YY_Thunks_File_Path) + + + + + + + + + \ No newline at end of file diff --git a/src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj.filters b/src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj.filters new file mode 100644 index 0000000..d776b23 --- /dev/null +++ b/src/YY-Thunks.UnitTest/TestProject/Example/SymbolBuildTest.vcxproj.filters @@ -0,0 +1,23 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + \ No newline at end of file diff --git a/src/YY-Thunks.UnitTest/TestProject/Example/main.cpp b/src/YY-Thunks.UnitTest/TestProject/Example/main.cpp new file mode 100644 index 0000000..2c1df59 --- /dev/null +++ b/src/YY-Thunks.UnitTest/TestProject/Example/main.cpp @@ -0,0 +1,19 @@ +// SymbolBuildTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include + +#include + +// 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; +} diff --git a/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.cpp b/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.cpp index 3df3437..b1a5c92 100644 --- a/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.cpp +++ b/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.cpp @@ -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 @@ -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"); + } }; -} \ No newline at end of file +} diff --git a/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.vcxproj b/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.vcxproj index 0234357..20e2e59 100644 --- a/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.vcxproj +++ b/src/YY-Thunks.UnitTest/YY-Thunks.UnitTest.vcxproj @@ -96,7 +96,7 @@ true true $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - __YY_Thunks_Unit_Test;WIN32;NDEBUG;YY_Thunks_Support_Version=NTDDI_WIN2K;%(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) true pch.h Use @@ -117,7 +117,7 @@ Level3 $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - __YY_Thunks_Unit_Test;WIN32;_DEBUG;YY_Thunks_Support_Version=NTDDI_WIN2K;%(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) true pch.h Use @@ -136,7 +136,7 @@ Level3 $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - __YY_Thunks_Unit_Test;_DEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions) + SymbolBuildTestPath=LR"($(ProjectDir)TestProject\)";YY_ThunksRootPath=LR"($(ProjectDir)..\..\)";MSBuildBinPath=LR"($(MSBuildBinPath)\)";__YY_Thunks_Unit_Test;_DEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions) true pch.h Use @@ -157,7 +157,7 @@ true true $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - __YY_Thunks_Unit_Test;NDEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions) + SymbolBuildTestPath=LR"($(ProjectDir)TestProject\)";YY_ThunksRootPath=LR"($(ProjectDir)..\..\)";MSBuildBinPath=LR"($(MSBuildBinPath)\)";__YY_Thunks_Unit_Test;NDEBUG;YY_Thunks_Support_Version=NTDDI_WS03;%(PreprocessorDefinitions) true pch.h Use