So far I know Windows display context menu based on the file extension rather than the file type. I just tested with empty text file with mp3 extension with winamp and the result is below:
The same way I test empty file with extension exe and Windows gives me context menu like "Run as Administrator" and so.
But if I create a sfx archive which has the same exe extension, how Windows gives me different icon and WinRAR gives me special context menu like "Extract here" as shown in the below picture?
My question is either Windows readers file header and represents relevant data or WinRAR has any special file detection scheme? I doubt that second one, since during the context menu creation WinRAR didn't invoked.
Another question is if it's because of Windows's behavior to read headers (like it creating thumbnail for pictures or videos), now extension has less importance? Don't they have any influence in context menu?
Best Answer
Windows context menu handlers can be both static as well as dynamic. If you're interested in delving into it further, I advise you to read the Shortcut (Context) Menus and Shortcut Menu Handlers article, especially Choosing a Static or Dynamic Shortcut Menu Method and Customizing a Shortcut Menu Using Dynamic Verbs.
Quoting from this Visual Basic Shell Programming book excerpt:
Finally, if you want proof that the file is actually being read by WinRAR's shell extension DLL (since you seem to doubt it), here are the various ReadFile calls registered by Process Monitor on simply right-clicking a WinRAR SFX:
(The process name is displayed as explorer.exe and not rarext.dll, because the context menu handler is an "in-process" COM object that the shell loads directly into explorer.exe's memory space for execution.)
As you can see, it reads the first 7 bytes to confirm that it's an EXE:
After reading more data (no doubt to obtain and parse the header), it then reads 7 bytes from offset 101,376 to confirm that it's a WinRAR SFX and not just any old EXE:
This prompts it to add various context menu entries such as Open with WinRAR, Extract with WinRAR and so on, which don't get added for "normal" EXEs.
Furthermore, in WinRAR's Settings dialog there's an option titled Where to check for SFX archives:
Here's what the help file has to say about it:
Hope that puts your doubts to rest. :) As for your second question about whether extensions have "less importance" now and lack "any influence in context menu[s]", I don't understand what you mean. Even image/video thumbnails are generated by shell extension handlers (DLLs). A list of different handlers can be found here. As you can see, it's possible to have custom shell extension handlers for everything ranging from shortcut menus, drag & drop operations, icons, icon overlays, property sheets, thumbnails, infotips, metadata, Explorer columns, copy/move/delete/rename dialogs, search...
Edit: Coincidentally, Raymond Chen happened to post an article about shell extensions today as well (don't forget to read the previous one too).