当在C#.net的currentProcess.MainModule的C++等价物是什么?
// get the current process
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
这样做我可以做
currentProcess.MainModule
有没有在C任何类似的功能++?
当在C#.net的currentProcess.MainModule的C++等价物是什么?
// get the current process
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
这样做我可以做
currentProcess.MainModule
有没有在C任何类似的功能++?
与ILSpy综观GetProcess()
反编译源,它说:
public static Process GetCurrentProcess()
{
return new Process(".", false, NativeMethods.GetCurrentProcessId(), null);
}
随着NativeMethods.GetCurrentProcessId()
被宣布为
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetCurrentProcessId();
其中referes到GetCurrentProcessId
function。
的MainModule
被定义为
public ProcessModule MainModule
{
get
{
if (this.OperatingSystem.Platform == PlatformID.Win32NT)
{
this.EnsureState((Process.State)3);
ModuleInfo firstModuleInfo =
NtProcessManager.GetFirstModuleInfo(this.processId);
return new ProcessModule(firstModuleInfo);
}
ProcessModuleCollection processModuleCollection = this.Modules;
this.EnsureState(Process.State.HaveProcessInfo);
foreach (ProcessModule processModule in processModuleCollection)
{
if (processModule.moduleInfo.Id == this.processInfo.mainModuleId)
{
return processModule;
}
}
return null;
}
}
这又似乎踏踏实实地EnumProcessModules
native function。
所以双方使用GetCurrentProcessId
和EnumProcessModules
功能,你应该能够得到类似的结果
currentProcess.MainModule
我不明白这一点。您将调用EnumProcessModules来枚举模块句柄,然后等到您看到等于“GetModuleHandle(NULL)'的模块句柄,然后返回该句柄?!真的? – 2012-03-21 22:30:01
@DavidHeffernan对不起,我的答案太复杂了。回到MFC的方式,我做的和你描述的一样(带有一些'Afx'前缀,IIRC)。我刚刚描述了一个(不完整的,如你所说)通过.NET Framework的路径,因为它似乎正在做它。 – 2012-03-22 05:43:52
.net库正在维护模块句柄周围的包装类。不是你的错,即使单行版本存在,你的答案也被接受!我只是希望提问者明白存在微不足道的解决方案。 – 2012-03-22 07:20:17
我假设你指的是Windows。如果是这样,那么你需要这个:
GetModuleHandle(NULL);
这将返回用于创建过程的模块的模块句柄。在GetModuleHandle
的文档中查找完整的详细信息。
如果你想要模块的文件名,而不是模块句柄,那么你需要改为GetModuleFileName
。
你能提供你所需要的更多信息?你想获取正在运行的代码的位置,或者是启动该进程的main .exe的位置吗? – 2012-03-21 16:48:20