當在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