invoke libMain by CRT

Co-authored-by: HolographicHat <holographichat@outlook.com>
Co-authored-by: 34736384 <34736384@users.noreply.github.com>
This commit is contained in:
HolographicHat
2025-08-02 07:48:44 +08:00
parent 0522078582
commit c4fb5a0366
3 changed files with 17 additions and 6 deletions

View File

@@ -24,3 +24,5 @@ GetCurrentConsoleFontEx
OpenProcess
GetModuleFileNameEx
LoadLibraryEx

View File

@@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.System.LibraryLoader;
using Windows.Win32.System.Threading;
using Spectre.Console;
using static Windows.Win32.System.Memory.VIRTUAL_ALLOCATION_TYPE;
@@ -72,6 +73,20 @@ public sealed unsafe class GameProcess {
if (Native.WaitForSingleObject(hThread, 2000) == 0) {
Native.VirtualFreeEx(Handle, lpLibPath, 0, MEM_RELEASE);
}
var libHandle = Native.LoadLibraryEx(libPath, LOAD_LIBRARY_FLAGS.DONT_RESOLVE_DLL_REFERENCES);
if (libHandle.IsInvalid) {
throw new Win32Exception { Data = { { "api", "LoadLibraryEx" } } };
}
var libMainProc = Native.GetProcAddress(libHandle, "YaeMain");
if (libMainProc.IsNull) {
throw new Win32Exception { Data = { { "api", "GetProcAddress" } } };
}
var lpStartAddress2 = (delegate*unmanaged[Stdcall]<void*, uint>) libMainProc.Value; // THREAD_START_ROUTINE
var hThread2 = Native.CreateRemoteThread(Handle, null, 0, lpStartAddress2, null, 0);
if (hThread2.IsNull) {
throw new Win32Exception { Data = { { "api", "CreateRemoteThread2" } } };
}
Native.CloseHandle(hThread2);
Native.CloseHandle(hThread);
} catch (Win32Exception e) {
_ = Terminate(0);

View File

@@ -19,7 +19,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Elysia.Bootstrap" Version="1.0.14"/>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.183">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
@@ -34,7 +33,6 @@
</ItemGroup>
<ItemGroup>
<LibraryEntrypoint Include="YaeMain"/>
<AssemblyAttribute Include="System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute"/>
</ItemGroup>
@@ -43,8 +41,4 @@
<Using Include="System.Diagnostics.CodeAnalysis"/>
</ItemGroup>
<ItemGroup>
<Folder Include="obj\" />
</ItemGroup>
</Project>