gpt4 book ai didi

c# - 从 .NET 动态解析 C stdlib 函数

转载 作者:行者123 更新时间:2023-11-30 14:00:45 24 4
gpt4 key购买 nike

我想在运行时从 .NET 代码解析函数的地址,例如来自 C stdlib 的函数地址,例如 malloc(这样我就可以为我的 VM 调用这些地址的 JIT 机器代码).我相信我应该使用 LoadLibraryGetProcAddress 来提供 kernel32.dll,但这不起作用。使用 F# interactive 我得到:

> [<DllImport("kernel32.dll", CharSet=CharSet.Ansi, SetLastError=true)>]
extern IntPtr LoadLibrary(string fileName);;
val LoadLibrary : string -> IntPtr

> [<DllImport("kernel32.dll", CharSet=CharSet.Ansi, SetLastError=true)>]
extern uint32 GetProcAddress(IntPtr hModule, string fn);;
val GetProcAddress : IntPtr * string -> uint32

> let kernel32 = LoadLibrary @"kernel32.dll";;
val kernel32 : IntPtr = 1993146368n

> let malloc = GetProcAddress(kernel32, "malloc");;
val malloc : uint32 = 0u

所以这似乎已经获得了 DLL 的句柄,但试图解析 malloc 却返回了 NULL 指针。

我应该怎么做?

最佳答案

我不相信 mallockernel32.dll 的一部分。但是,如果我没记错的话,所有的 malloc 实现最终都会深入到 HeapAlloc。 ,在 kernel32.dll 中可用。

关于c# - 从 .NET 动态解析 C stdlib 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com