问题
I have read this and understand lpClass can be used to fix the "wrong file extension issue". However, when I am reading the following lines of code, I can't figure out what lpClass is used for when opening an executable file.
//code excerpt from foo.exe
SHELLEXECUTEINFO info;
ZeroMemory(&info, sizeof(SHELLEXECUTEINFO));
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.nShow = SW_NORMAL;
info.lpVerb = L"open";
info.lpClass = L"ProgId Of foo.exe"; //what is this used for???
info.fMask = SEE_MASK_FLAG_LOG_USAGE | SEE_MASK_CLASSNAME;
info.lpFile = L"bar.exe";
info.lpParameters = lpszParam;
ShellExecuteEx(&info);
Without lpClass being specified, if lpVerb is "open" and lpFile is an exe, running the code simply executes the exe. But what if lpClass is specified as in this case?
回答1:
The parameter lpClass
should be the progID of the file type. What does that mean?
Well consider what happens if you don't pass the class.
In reality, it means the Shell looks up the file extension (e.g.
.htm
) in the registry, underHKEY_CLASSES_ROOT\.htm
. Then it checks the default value which is generallyhtmlfile
. (It also uses other tricks, but in the vast majority of cases it's the extension which determines the progid).Next it looks up
HKEY_CLASSES_ROOT\htmlfile
, and uses the information there (underHKEY_CLASSES_ROOT\htmlfile\shell\open
) to decide how to open the file.
So how do you use lpClass
? Well, for example, suppose you have an .TXT
file, but you know it is really html, you can pass "htmlfile"
as the lpclass
parameter. This will skip the step 1 (looking at the file extension to find the class) and go straight to step 2. This will (usually) cause the file to be opened in a browser instead of notepad.
In your example you have passed "bar.exe"
as the lpFile
parameter. If you pass "txtfile"
as lpClass
you should find that instead of running bar.exe
it opens it in notepad.
来源:https://stackoverflow.com/questions/19445960/using-shellexecuteex-to-open-an-executable-also-specifying-an-lpclass