Pregunta

SHELLEXECUTEINFO shExecInfo;

shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

shExecInfo.fMask = NULL;
shExecInfo.hwnd = NULL;
shExecInfo.lpVerb = _T("runas");
shExecInfo.lpFile = filePath;
shExecInfo.lpParameters = NULL;
shExecInfo.lpDirectory = NULL;
shExecInfo.nShow = SW_MAXIMIZE;
shExecInfo.hInstApp = NULL;

ShellExecuteEx(&shExecInfo);

If the filepath is the temp file downloaded from the Internet, ShellExecuteEx would fail. But if filepath is normal file name such as "notepad", it do works.

I conclude that shellexecute need the right extension name, but normal privilege app could not write an executable file to the system partion such as c:\users\xxx\local\temp\xxx.exe.

The error code is ERROR_NO_ASSOCIATION.

Please help me solve this contradiction.

¿Fue útil?

Solución

The error that the system is returning to you is ERROR_NO_ASSOCIATION. Although you have not stated what extension filePath has, it seems likely that it is not .exe.

Were you to rename the downloaded file to have extension .exe, then the call to ShellExecuteEx would succeed. That's the hacky way to do it. The cleaner way is to use the lpClass member of the SHELLEXECUTEINFO struct to specify that you want the file to be treated as an executable. You can do that by adding the following to your code:

shExecInfo.fMask = SEE_MASK_CLASSNAME;
shExecInfo.lpClass = _T("exefile");

I'd write your code like this:

SHELLEXECUTEINFO shExecInfo = {0};
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shExecInfo.fMask = SEE_MASK_CLASSNAME;
shExecInfo.lpVerb = _T("runas");
shExecInfo.lpFile = filePath;
shExecInfo.nShow = SW_MAXIMIZE;
shExecInfo.lpClass = _T("exefile");
ShellExecuteEx(&shExecInfo);

Note the zero initialization so that we can omit explicit assignment for members that should be NULL.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top