[英]How do I run command line “explorer /n, /select,(filename)” in c++ builder?
I've tried shellexecute, I've tried createprocess, I can't seem to get this to do anything. 我试过了shellexecute,我试过了createprocess,我似乎什么也做不了。
Running the command line manually (at the actual command prompt in a console window) works, but nothing I've tried so far will run it from within a c++ builder app. 手动运行命令行(在控制台窗口中的实际命令提示符下)是可行的,但是到目前为止,我尝试过的任何操作都无法从c ++ builder应用程序中运行它。
Obviously (filename) is just a place holder. 显然,(文件名)只是一个占位符。 It would be given a valid file name, such as 它会被赋予一个有效的文件名,例如
explorer /n, /select,c:\\123.doc 资源管理器/ n,/select,c:\\123.doc
Are you using escaped backslashes in your filename? 您是否在文件名中使用转义的反斜杠? For example:; 例如:;
"c:\123.doc"
should be: 应该:
"c:\\123.doc"
Edit: 编辑:
execlp("explorer", "/n, /select,c:\\foo.txt", 0)
works for me. 为我工作。
To avoid replacing the the current process, use spawnlp instead 为避免替换当前进程,请使用spawnlp代替
I'm sure there's someway to make CreateProcess
and ShellExecute
work with this, but I suspect the simplest way to do this will be good old system
, eg system("explorer /n, /select,c:\\\\123.doc")
. 我确定有某种方法可以使CreateProcess
和ShellExecute
与此一起工作,但是我怀疑最简单的方法是使用良好的旧system
,例如system("explorer /n, /select,c:\\\\123.doc")
。
Just because it was bugging me, I went ahead and wrote a simple program that does this with CreateProcess: 仅仅因为它困扰着我,所以我继续写了一个简单的程序,用CreateProcess做到了这一点:
#define UNICODE
#include <windows.h>
#include <string>
void SimpleWriteConsole(std::wstring msg) {
DWORD written = 0;
WriteConsole( GetStdHandle(STD_OUTPUT_HANDLE),
msg.c_str(), msg.length(), &written, NULL);
}
int wmain(int argc, wchar_t **argv, wchar_t **envp) {
SimpleWriteConsole(L"Opening explorer...\n");
std::wstring commandLine = L"explorer /n, /select,";
if( argc < 2 ) {
SimpleWriteConsole(L"Please include a file to select.\n");
return EXIT_FAILURE;
}
commandLine += argv[1];
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
PROCESS_INFORMATION procInfo;
if( ! CreateProcess(NULL, const_cast<wchar_t*>(commandLine.c_str()),
NULL, NULL, 0, 0, NULL, NULL, &startupInfo, &procInfo) ) {
SimpleWriteConsole(L"Couldn't create process :(\n");
return EXIT_FAILURE;
}
CloseHandle( procInfo.hThread );
CloseHandle( procInfo.hProcess );
SimpleWriteConsole(L"Hooray launched explorer.\n");
return EXIT_SUCCESS;
}
It takes the C:\\abc.txt part as a parameter on the command line. 它以C:\\ abc.txt部分作为命令行上的参数。 There's no extra dos box, and doesn't eat your existing process (exec is supposed to do that, btw) and it doesn't use a deprecated API. 没有多余的dos框,也不会吃掉您现有的进程(exec应该这样做,顺便说一句),并且它不使用已弃用的API。
OK, these replies have been useful but unfortunately they both have drawbacks which I would rather not have in my app... 好的,这些答复很有用,但不幸的是,它们都有缺点,我不希望在我的应用程序中遇到……
execlp()
causes the calling process to close. execlp()
导致调用进程关闭。
system()
shows a dos box which stays until I close the explorer window. system()
显示一个dos框,直到我关闭资源管理器窗口为止。
I am greatful for your your answers though. 不过,我非常感谢您的回答。
I've got it to work with winexec
. 我已经将它与winexec
一起winexec
。 Even though winexec's help file says this - 即使winexec的帮助文件说明了这一点-
Note This function is provided only for compatibility with 16-bit Windows. 注意仅提供此功能是为了与16位Windows兼容。 Applications should use the CreateProcess function. 应用程序应使用CreateProcess函数。
I can't get createprocess
to work, so I'm going to stick with winexec
for now... 我无法使createprocess
正常工作,所以我现在将坚持使用winexec
...
WinExec(String("explorer /n, /select,"+ FileName).c_str(),SW_SHOWDEFAULT);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.