9

What's the API call to display an Explorer window with a specified file selected? Exactly as happens when you click the "Find Target..." button in the Properties dialog of a .lnk shortcut? I know there is function (or an interface method) for that, but I forgot the name, and cannot find it again.

Note that I'm aware of explorer /select,<pathname> command line and not interested in using it instead of API call.

Premature Optimization
  • 1,917
  • 1
  • 15
  • 24
  • It's not too late, @User. Closed questions disallow new answers, but they can still be edited, and they can be reopened. Please edit the question to ask what you really want. I reiterate the questions from my previous comment. Do you want to discover the path to the shortcut's target, or do you want to display Explorer? – Rob Kennedy Jun 03 '11 at 02:10
  • 1
    @Rob - he wants to open Explorer with focus on the given file; that's all. I've tried to edit the question but someone rejected it. –  Jun 03 '11 at 02:18
  • What? Who killed **useful** @daemon_x's comment about IPersistFile? Did i offend someone THAT much? – Premature Optimization Jun 03 '11 at 02:28
  • @Rob Kennedy, Explorer part (BTW, shortcut has been resolved already, see my comment to Sertac Akyuz below) – Premature Optimization Jun 03 '11 at 02:33
  • 1
    nothing wrong with the question at all – Sam Jun 03 '11 at 07:03
  • 1
    @sam: the downvotes were targeted at the original question. If you look at the edit history, you'll notice that the original version was very different from the current version. – jpfollenius Jun 03 '11 at 08:49
  • 2
    @sam - now I removed my downvote and give the upvote because the question is quite interesting. It's a pity that it took 5 hours to find out what is asked here :) –  Jun 03 '11 at 09:03
  • @user759588 - according to your [next question](http://stackoverflow.com/questions/6226883/is-there-a-way-to-obtain-iexplorerbrowser-interface-of-running-or-newly-open-expl) you want to "bind" or better to say "hack" already opened Explorer window(s), don't you ? –  Jun 03 '11 at 12:27
  • @daemon_x, not really *hack*, but yes, my objective is still the same. Currently, i'm in dead-end with IExplorerBrowser suggested, for obvious reason. – Premature Optimization Jun 03 '11 at 12:37
  • @user759588 - you might say in your edit, that you want to control already existing Explorer windows because we thought you want to open ones by your own. –  Jun 03 '11 at 14:23
  • @daemon_x, i believe there are restrictions, which are lifted in the parent-child relations (try 'Find Target' multiple times, explorer will **reuse** already opened window instead of creating a new one) – Premature Optimization Jun 03 '11 at 14:50

4 Answers4

8

This function opens explorer, and selects the specified file:

uses ShellAPI, ...;

procedure TForm1.ShowFile(const aFileName:String);
begin
  ShellExecute(Handle, 'OPEN', PChar('explorer.exe'), PChar('/select, "' + aFileName + '"'), nil, SW_NORMAL)
end;

procedure TForm1.ShowFolder(const aPath:String);
begin
  ShellExecute(Handle, 'OPEN', PChar('explorer.exe'), PChar('/root, "' + aPath + '"'), nil, SW_NORMAL) 
end;

Or is this the "commandline" that you didn't want to use?

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • +1; That's what Andreas suggested, but OP says that `it's very similar (but actually different)`. –  Jun 03 '11 at 08:47
  • @Sertac - Me too, that's what is asked here (except it's command line way) and I would prefer this rather than implement quite complicated interface which has been furthermore introduced in Windows Vista. As I tested this works in Windows XP as well as in Windows 7. –  Jun 03 '11 at 10:38
  • This is exactly what daemon_x suggested hours ago, and the OP downvoted and made negative comments about, leading to daemon_x deleting his answer. – Ken White Jun 03 '11 at 10:59
  • 1
    @Ken - I've posted only exploring folder (ShellExecute's `explore` operation) without selecting target file, so I was wrong. It was Andreas who mentioned this first. –  Jun 03 '11 at 11:08
  • @daemon_x: But now my original comment about `/select` has been removed by a site moderator, for some unknown reason. This question has become quite confusing! – Andreas Rejbrand Jun 03 '11 at 11:30
  • @Andreas - I know, I remember it. This question was confusing from the beginning; it was enough to ask `How do I open Windows Explorer with a certain file highlighted ?` –  Jun 03 '11 at 12:01
  • @Sertac Akyuz, the difference is outlined in my comment to daemon_x above. – Premature Optimization Jun 03 '11 at 14:51
  • @daemon_x, @Andreas: My apologies for the wrong attribution above. It was indeed Andreas' suggestion in a (now-deleted) comment, and not in the answer daemon_x deleted. – Ken White Jun 03 '11 at 22:16
7

You need SHOpenFolderAndSelectItems. This question was early discussed here - Programmatically selecting file in explorer
Dont forget to call CoInitialize before first use of SHOpenFolderAndSelectItems

Community
  • 1
  • 1
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
3

You're looking for IExplorerBrowser::BrowseToObject, I think. Based on the very vague, non-specific question and comments with no additional information when asked for it, it's the best guess I can provide. IExplorerBrowser has a lot of additional functionality you may want to explore as well, in case this specific method isn't exactly what you're looking to find.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Minimum supported OS being Vista, I wonder if 9x, 2K, XP indeed resolved the link and used "explorer /select" as Andreas suggested... – Sertac Akyuz Jun 03 '11 at 02:01
  • @Sertac: I'd think so, but I'm not "familiar with Windows shell", so it's only a *guess*. :) – Ken White Jun 03 '11 at 02:06
  • @Ken - LOL! .. It's called *shell* for a reason, what's inside is anybody's guess.. :) – Sertac Akyuz Jun 03 '11 at 02:11
  • @Sertac Akyuz, there is always a possibility what MS just published interface which was undocumented earlier. Scratch resolving link tho, shell operates directly with ITEMIDLISTs. Which also mean BrowseToIDList method. – Premature Optimization Jun 03 '11 at 02:34
  • 1
    @user759588 - I would use the mentioned `IExplorerBrowser::BrowseToObject`. You've asked me what to pass as the `punk` parameter. My guess was a `IPersistFile` variable type. –  Jun 03 '11 at 02:39
  • @daemon_x, thanks, i will (currently i'm stuck with PIDLIST anyways :) – Premature Optimization Jun 03 '11 at 02:47
  • 1
    Why do you prefer BrowseToObject instead of BrowseToIDList, @Daemon_x? The IShellLink object provides either a string or an ItemIDList of the thing it points at. It seems that BrowseToIDList can use the ItemIDList directly, whereas BrowseToObject will require generating either an IShellFolder or an IShellItem — something that implements IPersistFolder2 or IPersistIDList. And the first thing BrowseToObject will do is call IPersistFolder2.GetCurFolder or IPersistIDList.GetIDList to get the ItemIDList anyway. I say skip the middleman. – Rob Kennedy Jun 03 '11 at 07:57
  • @Rob - you're right; BrowseToIDList might be easier to implement and maybe faster than BrowseToObject. I've been confused of the term `IDList` but as MSDN says it `specifies an object's location as the destination to navigate to`. And [here](http://msdn.microsoft.com/en-us/library/cc144090(v=VS.85).aspx#unknown_74413) is the explanation what ItemID means. –  Jun 03 '11 at 08:58
  • 1
    @User - (1) That's why I was *wondering*. If MS never left undocumented interfaces, I'd be sure. (2) It's the *exposed* interfaces that operates directly with id lists. If the shell never went beyond id lists, it wouldn't be able to copy a file f.i. – Sertac Akyuz Jun 03 '11 at 10:10
  • 1
    @Sertac Akyuz, well, yes and no, ITEMIDLIST is primary data type for shell and in essence is *preparsed* pathname, absolute and relative, mapped under Desktop folder root (whats why `folder` term is more broad than `directory`). Just compare number of APIs which accepts paths and PIDL's. – Premature Optimization Jun 03 '11 at 14:01
  • @Sertac Akyuz, that distorts my words, ofcourse i wasnt talking about entire shell, i was pointing out the fact what resolving shortcut is irrelevant to my question. – Premature Optimization Jun 03 '11 at 15:01
  • [This](http://stackoverflow.com/questions/6226883/is-there-a-way-to-obtain-iexplorerbrowser-interface-of-running-or-newly-open-expl/6232916#6232916) and [another](http://stackoverflow.com/questions/6220899/how-do-i-display-explorer-with-a-file-selected/6221898#6221898) answers are contradicting to each other, meaning at least one of them is misleading. I appreciate your Google search skills, but cannot welcome such approach when answering specific technical question. – Premature Optimization Jul 08 '11 at 19:36
  • @Downvoter: Isn't it amazing that you're suddenly finding old answers of mine to downvote (well after the fact) immediately after I downvoted one of your questions? This is the fourth post of mine you've downvoted today alone. Are you really so juvenile that you need revenge downvotes? You have my sympathy. – Ken White Jul 08 '11 at 19:56
  • @Ken White, second one, perhaps? Sorry for late rating assigment, but i was vacillating about which one of your mutually exclusive answers deserves. Despite of the fact i do not care about karma earning, i DO care about misinformation because it wastes my time. Look at the accepted answer, brief and precise, no guesswork, no google suggestions. Please understand what "be specific" call applies to the answers too. If it is not an option - please ignore me. – Premature Optimization Jul 08 '11 at 21:24
  • @Downvoter: It was four, not two. As far as "mutually exclusive answers", they were two **different** questions, and therefore called from two **different** answers. If you were less quick to look for payback (and less quick to downvote), you probably would have more time to read and comprehend both the questions and answers. No matter, though. You don't have enough rep to hurt mine significantly, and trying will just deplete your own to the point you won't be able to downvote (or post answers) any longer. – Ken White Jul 08 '11 at 23:56
  • In addition, I **specifically** said your question was too vague, which proved to be the case for others as well. The question was closed until you provided clarification, at which point it was reopened. If you ask a badly worded, vague, unclear question, it's difficult to provide "specific" answers when asked to do so. Ask better questions, and get better answers. – Ken White Jul 09 '11 at 00:45
  • Both answers led me nowhere. I requested consultant to be familiar with Windows shell for the reason, but you didnt listen, ridiculed and assumed "offended" stance instead. Clearly, such kind of consulting is no more useful for me than no consulting at all. – Premature Optimization Jul 09 '11 at 00:45
2

Since you wanted a name only: IShellLink?

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384