0

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?

kennyzx
  • 12,845
  • 6
  • 39
  • 83

1 Answers1

2

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.

  1. In reality, it means the Shell looks up the file extension (e.g. .htm) in the registry, under HKEY_CLASSES_ROOT\.htm. Then it checks the default value which is generally htmlfile. (It also uses other tricks, but in the vast majority of cases it's the extension which determines the progid).

  2. Next it looks up HKEY_CLASSES_ROOT\htmlfile, and uses the information there (under HKEY_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.

Ben
  • 34,935
  • 6
  • 74
  • 113
  • The ProgId is not used for file association. Foo.exe is registered as a COM server with that ProgId, and the progid is used to call bar.exe as the lpClass member in SHELLEXECUTEINFO...and I have little experience with COM, Sorry I could not explain well enough how the ProgId is related to the caller **foo.exe**. – kennyzx Oct 18 '13 at 10:29
  • 1
    @kennyzx, That's not what `ShellExecuteEx` is for. You want `CLSIDFromProgID` and `CoCreateInstance`. The COM system will take care of starting "bar.exe" if it is registered correctly. – Ben Oct 18 '13 at 12:51