简体   繁体   English

相对路径.EXE名称上的:: ShellExecute和静态CFile :: GetStatus()的路径搜索顺序应该相同吗?

[英]Should path search order be identical for ::ShellExecute and static CFile::GetStatus() on relative path .EXE name?

From within my MFC App, I am doing something like 从我的MFC应用程序中,我正在做类似的事情

CFileStatus fs;
if (CFile::GetStatus("MyOtherProg.exe", fs))
  {
  // found the file
  ::ShellExecute(NULL, NULL, "MyOtherProg.exe", NULL, NULL, SW_SHOW);
  }

but the full path to the file found in the static CFile::GetStatus is NOT the one being executed by ::ShellExecute (it has the same name, but is running a different version of "MyOtherProg.exe", in a different folder). 但是在静态CFile :: GetStatus中找到的文件的完整路径不是:: ShellExecute正在执行的路径(它具有相同的名称,但是在不同的文件夹中运行“ MyOtherProg.exe”的不同版本) 。

I have tried this on different PCs with the same O/S (Win7 64 bit), with different sets of "MyOtherProg.exe" in various folders. 我已经在具有相同O / S(Win7 64位)的不同PC上尝试了此操作,并且在各个文件夹中使用了不同的“ MyOtherProg.exe”集。 Neither PC's ShellExecute runs the same one found in the CFile::GetStatus. 两台PC的ShellExecute都不会运行在CFile :: GetStatus中找到的相同程序。 One PC always ends up running version 3, another PC always ends up running version 2 (why isn't THAT consistent?). 一台PC始终运行版本3,另一台PC始终运行版本2(为什么不一致?)。

Note 1: Across the 2 PCs, at least 3 versions are "installed", but NONE of them are installed in the PATH. 注1:在2台PC上,至少“安装”了3个版本,但PATH中没有安装任何版本。 Some DO have SHORTCUTS on the Desktop, if that is some undocumented feature. 如果某些未记录的功能,某些确实在桌面上具有快捷方式。

Note 2: ALL of them are different versions of "MyOtherProg.exe", but installed in different directories. 注意2:它们都是“ MyOtherProg.exe”的不同版本,但安装在不同的目录中。

Note 3: The full path of the one that ends up being run via ::ShellExecute is the same one found when I just type "MyOtherProg.exe" in the Search edit field below the Task Bar's START menu. 注意3:最终通过:: ShellExecute运行的完整路径与我在任务栏的“开始”菜单下的“搜索”编辑字段中键入“ MyOtherProg.exe”时发现的路径相同。 But WHY is the one found by CFile::GetStatus NOT the SAME one? 但是,为什么CFile :: GetStatus发现的不是同一一个? And why on one PC it's version 2, but on another PC its version 3? 以及为什么在一台PC上是版本2,而在另一台PC上是版本3?

Note 4: On both PCs, the resulting search list of the RUN command "MyOtherProg.exe" only shows 1 version (although on one PC, 3 versions are installed, and on the other PC 4 different versions are installed with a fifth one being a "debug" build). 注意4:在两台PC上,运行命令“ MyOtherProg.exe”的结果搜索列表仅显示1个版本(尽管在一台PC上安装了3个版本,而在另一台PC上安装了4个不同的版本,其中第5个是一个“调试”版本)。 The PC with 3 versions always ONLY lists version 2, the PC with 5 versions always ONLY lists version 3. 具有3个版本的PC始终仅列出版本2,具有5个版本的PC始终仅列出版本3。

  • CFile::GetStatus expands the relative path with the working directory. CFile::GetStatus扩展工作目录的相对路径。
  • ShellExecute looks for the file using the process creation search order. ShellExecute使用流程创建搜索顺序查找文件。 This is described in the CreateProcess documentation. CreateProcess文档中对此进行了描述。 As well as the paths listed there, the per-application path is searched. 除了此处列出的路径外,还将搜索每个应用程序的路径。

In short this means that CFile::GetStatus alone is simply not suitable for the task you have in mind. 简而言之,这意味着仅CFile::GetStatus根本不适合您要执行的任务。 You'd have to re-create the shell search. 您必须重新创建shell搜索。

But why would you do that? 但是,为什么要这么做呢? The logical thing to is to call ShellExecute directly and check for errors. 逻辑上的事情是直接调用ShellExecute并检查错误。 Let it perform the search, because it knows the rules. 让它执行搜索,因为它知道规则。 Since ShellExecute has deficient error reporting you should rather call ShellExecuteEx . 由于ShellExecute错误报告不足,因此您应该调用ShellExecuteEx

On the other hand, if you only want to search in the working directory as you do with your current call to CFile::GetStatus then use an absolute rather than relative path. 另一方面,如果您只想像当前对CFile::GetStatus调用那样在工作目录中进行搜索,请使用绝对路径而不是相对路径。 I doubt that this is what you want to do but mention it for completeness. 我怀疑这是您要执行的操作,但为了完整起见请提及它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM