获取cmd.exe的当前工作目录 [英] Get the current working directory for cmd.exe

查看:130
本文介绍了获取cmd.exe的当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索cmd.exe的当前工作目录?

How can I retrieve the current working directory of cmd.exe?

这似乎是可能的。例如使用ProcessExplorer,选择CMD.exe,右键单击,属性,图像选项卡,当前目录选择使用CD或CHDIR命令设置的目录。

This seems possible. For example using ProcessExplorer, select CMD.exe, right click, properties, Image tab, "Current Directory" relects the directory set using the CD or CHDIR commands.

看过.NET Process和ProcessStartInfo类(ProcessStartInfo.WorkingDirectory总是返回),似乎找不到一种方法来确定这一点。 PInvoke 也没有任何一个。

I've looked at the .NET Process and ProcessStartInfo classes (ProcessStartInfo.WorkingDirectory always returns "") and can't seem to find a way of determining this. Nothing at PInvoke stands out either.

作为一个例子,我希望以编程方式能够说出一些类似的内容:Process.GetCurrentWorkingDirectory(processID),​​其中processID是另一个正在运行的进程的Windows进程ID。

As an example I'm looking to programmatically be able to say something like: Process.GetCurrentWorkingDirectory(processID) where processID is a Windows process ID of another running process.

有任何解决方案,WinAPI还是.NET?

Is there any solution, WinAPI or .NET?

[更新]

提出这个问题的原因:

我使用了命令提示管理器栏一段时间,除非我 CD到新的目录,当前的资源管理器窗口也不会改变。 (即同步只是从Explorer到commmand提示的1种方式)。

I've used the "Command Prompt Explorer Bar" for a while and it's great except if I "CD" to a new directory, the current Explorer window does not also change. (ie the Sync is only 1 way from Explorer to the commmand prompt). I'm looking to make this 2 way.

推荐答案

未经验证,一种可能的方法:

Untested, a possible approach:

使用DllMain创建一个DLL,该DLL使用GetThreadStartInformation()来查找缓冲区的地址,然后使用GetCurrentDirectory进行填充。这应该是可以的,因为这两个函数都在kernel32中,它始终存在。您将需要有一些结构来返回成功/失败。

Create a DLL with a DllMain that uses GetThreadStartInformation() to find the address of the buffer, and then uses GetCurrentDirectory to populate it. This should be OK, because both of those functions are in kernel32, which is always present. You will need to have some structure there to return success/failure.


  1. 获取cmd.exe进程的句柄。

  2. 分配一些内存(VirtualAllocEx)

  3. 将DLL的路径放在内存中。 (WriteProcessMemory)

  4. 将您的DLL加载到cmd.exe地址空间中。 (CreateRemoteThread与LoadLibrary的入口点,参数是您之前分配的内存)。

  5. WaitForSingleObject后跟GetExitCodeThread(),在cmd.exe进程中提供您的DLL的HMODULE 。

  6. ReadProcessMemory获取当前目录。

  7. 从cmd.exe地址空间卸载你的DLL。 CreateRemote线程具有FreeLibrary的入口点,参数是HMODULE。

  8. WaitForSingleObject等待DLL卸载。

  1. Get a handle to the cmd.exe process.
  2. Allocate some memory there (VirtualAllocEx)
  3. Put the path to your DLL in the memory. (WriteProcessMemory)
  4. Load your dll into the cmd.exe address space. (CreateRemoteThread with an entry point of LoadLibrary, the argument is the memory you allocated earlier.)
  5. WaitForSingleObject followed by GetExitCodeThread(), gives you the HMODULE of your DLL in the cmd.exe process.
  6. ReadProcessMemory to get the current directory.
  7. Unload your dll from the cmd.exe address space. CreateRemote Thread with an entry point of FreeLibrary, the argument is the HMODULE.
  8. WaitForSingleObject to wait for the DLL to unload.

广泛草图:细节作为锻炼!风险:在cmd.exe地址空间中分配内存,更改其状态。必须注意DllMain中调用的函数。

Broad sketch: Details left as an excercise! Risks: Allocates memory in the cmd.exe address space, changes its state. Care must be taken with the functions called in DllMain.

这篇关于获取cmd.exe的当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆