C# 中的 SymLoadModule64 和 GetCurrentProcess
我想使用 dbghelp 库从 pdb 文件中获取一些类型和程序信息。现在,因为我比 C++ 更喜欢 C#,所以我目前正在尝试让它在 C# 中运行。我目前陷入对 SymLoadModule64 的调用。我在 C++ 中有一个工作调用,如下所示:
const TCHAR* pModName = argv[1];
HANDLE currentProcHandle = GetCurrentProcess();
DWORD64 ModBase = ::SymLoadModule64 (
currentProcHandle,
NULL,
pModName,
NULL,
0,
0);
但是,当尝试以某种方式从 C# 调用该调用时,我不断收到错误:
[DllImport("dbghelp.dll", SetLastError = true)]
public static extern ulong SymLoadModule64(IntPtr hProcess, IntPtr hFile,
string ImageName, string ModuleName,
ulong BaseOfDll, uint SizeOfDll);
[...]
var loadedModule = SymLoadModule64(
currentProcHandle,
System.IntPtr.Zero,
"C:\\Path\\To\\Executable.exe",
string.Empty,
0,
0);
导致 loadModule 设置为 0 且 Marshal.GetLastWin32Error() 返回 6 (ERROR_INVALID_HANDLE)。现在,我认为这似乎是句柄的问题,我可以使用本机函数来检索它(以避免由于 c# 句柄检索内容与 c++ 期望的内容不兼容而导致的任何陷阱)。然而,虽然托管
Process.GetCurrentProcess().Handle;
总是返回一些或多或少有意义的东西(1008、1036,...),但调用
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();
GetCurrentProcess();
总是返回-1。
所以:由于我很欣赏关于“主要”问题的任何想法(如何让 SymLoadModule64() 从 C# 工作),我当然也很想知道为什么调用 GetCurrentProcess() 失败。提前致谢。
I want to use the dbghelp library to get some type and program information from a pdb file. Now, because I like C# much more than C++, I'm currently trying to get it working from within C#. I'm currently stuck at a call to SymLoadModule64. I've got a working call in C++, looking like this:
const TCHAR* pModName = argv[1];
HANDLE currentProcHandle = GetCurrentProcess();
DWORD64 ModBase = ::SymLoadModule64 (
currentProcHandle,
NULL,
pModName,
NULL,
0,
0);
However, when trying to somehow call that from C#, I keep getting errors:
[DllImport("dbghelp.dll", SetLastError = true)]
public static extern ulong SymLoadModule64(IntPtr hProcess, IntPtr hFile,
string ImageName, string ModuleName,
ulong BaseOfDll, uint SizeOfDll);
[...]
var loadedModule = SymLoadModule64(
currentProcHandle,
System.IntPtr.Zero,
"C:\\Path\\To\\Executable.exe",
string.Empty,
0,
0);
results in loadedModule being set to 0 and Marshal.GetLastWin32Error() returning 6 (ERROR_INVALID_HANDLE). Now, I thought that as it seems to be a problem with the handle, I could just use the native function for retrieving it (to avoid any pitfalls due to incompatibilities from c# handle retrieving stuff to what c++ expects etc.). However, while the managed
Process.GetCurrentProcess().Handle;
always returns something more or less meaningful (1008, 1036, ...), a call to
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();
GetCurrentProcess();
always returns -1.
So: As I'd appreciate any ideas on the "major" question (how can I get the SymLoadModule64() working from C#), I'd of course really like to know as well why the call to the GetCurrentProcess() fails. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为第一个参数传递给
SymLoadModule64
的进程句柄可以是任意值,它实际上不必是进程的有效句柄。但是,对于要使用的每个进程句柄,必须首先通过调用SymInitialize
来初始化该值的 dbghelp。至于
GetCurrentProcess< /code>
,值-1是引用当前进程的伪句柄。它可以与大多数需要句柄的 Windows 函数一起使用。请注意,该函数返回伪句柄的事实已有详细记录。
The process handle that you pass as the first parameter to
SymLoadModule64
can be an arbitrary value, it does not actually have to be a valid handle to a process. However, for every process handle you want to use, you must first initialize dbghelp for that value by callingSymInitialize
.As for
GetCurrentProcess
, the value -1 is a pseudo-handle referring to the current process. It can be used with most Windows functions that expect a handle. Note that the fact that the function returns the pseudo-handle is very well documented.有关如何在 C# 中实现
SymLoadModule64
的使用的“主要问题”的工作代码(回复显示在最后),请参阅 http://social.msdn.microsoft.com/Forums/en/netfxtoolsdev/thread/d79f7876-6d37-429f-937c-57797462473a另请参阅此问题和答案DbgHelp.dll:从 C# 调用 SymGetModuleInfo64 - 它有一些不错的代码,应该帮助您开始...
For working code for your "major question" on how to implement usage of
SymLoadModule64
in C# (reply displayed at the end) see http://social.msdn.microsoft.com/Forums/en/netfxtoolsdev/thread/d79f7876-6d37-429f-937c-57797462473aAlso see this SO question and answer DbgHelp.dll : calling SymGetModuleInfo64 from C# - it has some nice code which should help you get started...