ShellExecute返回成功,但不执行文件
问题描述:
我最近关于Win32 API ShellExecute()
函数的项目中有一个奇怪的问题。ShellExecute返回成功,但不执行文件
该程序使用ANSI C模式下的Visual C++ 2015进行编译。
int ret = (int)ShellExecuteA(0, "open", "C:\\Users\\Maverick\\stratum.jpg", NULL, NULL, SW_SHOWNORMAL);
printf("ShellExecute return value: %i\n", ret);
在上面的代码中,ShellExecute()
返回42,所以它应该是成功的。但是,它并没有真正打开文件。
我没有特权问题,即使以管理员身份运行程序时也会出现同样的问题。
其实,我可以成功运行该文件是这样的:
system("C:\\Users\\Maverick\\stratum.jpg");
我不想*使用system()
,虽然。
此外,在将项目迁移到较新的Visual Studio之前,我使用的是Visual C++ 6.0,并且代码工作正常。
任何线索可能是什么问题?
编辑:ShellExecuteEx()
也返回成功(1),但不打开文件。
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "C:\\Users\\Maverick\\stratum.jpg";
ShExecInfo.lpParameters = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOWNORMAL;
ShExecInfo.hInstApp = NULL;
int ret = (int)ShellExecuteExA(&ShExecInfo);
答
好吧,看起来像我发现问题,确实很奇怪。也许是内存泄漏。 在ShellExecute/ShellExecuteEx调用之前,在循环(如成千上万)中多次调用函数CreatePopupMenu()会导致此问题发生。 除非我们在ShellExecute调用之前使用DestroyMenu释放HMENUs。 看起来像是某种内存泄漏也许,非常感谢你的答案。
+0
不是内存泄漏,_resource_泄漏。 – 1201ProgramAlarm
IDE /编译器版本对ShellExecute()的行为没有影响,因为它是在OS层实现的Win32 API函数。这两个版本都会调用相同的函数。在任何情况下,尝试使用'ShellExecuteEx()'来代替它,它比'ShellExecute()'提供更准确的错误报告。还可以考虑用'NULL'替换''open''来调用默认操作,该操作可能不是“打开”,具体取决于您安装的映像软件。 –
感谢您的评论。 试图用NULL替换“open”,但结果相同。 将与ShellExecuteEx – Flavio
尝试与ShellExecuteEx,同样的问题,返回成功,但它不会执行文件:( – Flavio