CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    ShellExecute returns error on a link with parameters

    I have a link with parameters that may be passed to the application, pretty much a line like this:
    "C:\Documents and Settings\All Users\Start Menu\Program.lnk" "C:\Documents and Settings\PC12\Desktop\Sample.txt"
    If I copy and paste the link above into Start -> Run, it works just fine and launches the Program with the Sample.txt attached to it, but when I try to open it with ShellExecute like this:
    Code:
    ShellExecute(NULL, _T("Open"), _T("\"C:\\Documents and Settings\\All Users\\Start Menu\\Program.lnk\" \"C:\\Documents and Settings\\PC12\\Desktop\\Sample.txt\""), NULL, SW_SHOW);
    it doesn't work. Why?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ShellExecute returns error on a link with parameters

    Quote Originally Posted by ahmd View Post
    ... when I try to open it with ShellExecute like this:
    Code:
    ShellExecute(NULL, _T("Open"), _T("\"C:\\Documents and Settings\\All Users\\Start Menu\\Program.lnk\" \"C:\\Documents and Settings\\PC12\\Desktop\\Sample.txt\""), NULL, SW_SHOW);
    it doesn't work. Why?
    Doesn't it work?
    Or does it NOT compile?
    FYI: ShellExecute has six parameters, not five!
    Try:
    Code:
    ShellExecute(
                  NULL, 
                  _T("Open"), 
                  _T("\"C:\\Documents and Settings\\All Users\\Start Menu\\Program.lnk\""),
                  _T("\"C:\\Documents and Settings\\PC12\\Desktop\\Sample.txt\""),
                  NULL, 
                  SW_SHOW
                  );
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    Yes, I apologize, I missed one NULL, then it would compile. As for separating path from parameters, I agree that it may work that way but my question then becomes, how do you separate path from parameters? You see, in real situation I don't have it hardcoded like that, it is provided by a user.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: ShellExecute returns error on a link with parameters

    I don't know if there's a better way, but I just parse it with some reasonable heuristics. Here's a clip from a hotlink control I did a while back. Should give you an idea of how I parse the command string as supplied by the user:

    Code:
    void CXHotlink::OnLButtonDown(CView* /*pView*/, UINT /*nFlags*/, const CPoint& /*point*/)
    {
      CString cs;
      wchar_t *p,*q,buf1[MAX_PATH],buf2[MAX_PATH];
      long i;
      HINSTANCE rc;  //shellexecute() returns an HINSTANCE.
    
      if(m_PropStruct.nAction>0){
        p=wcschr(m_PropStruct.LinkText,L'\"');
        if(p){
          q=wcschr(p+1,L'\"');
          if(q){
            //somethying is enclosed in quotes
            //copy everything between the quotes to the first buffer
            wcsncpy_s(buf1,MAX_PATH,p+1,q-(p+1));
            //copy the rest to the second buffer
            wcsncpy_s(buf2,MAX_PATH,q+1,MAX_PATH-1);
          }else{
            //malformed
            AfxMessageBox(L"Unable to parse command string.",MB_ICONERROR);
            return;
          }
        }else{
          //no quotes      
          //find space separator
          p=wcschr(m_PropStruct.LinkText,L' ');
          if(p){
            wcsncpy_s(buf1,MAX_PATH,m_PropStruct.LinkText,p-m_PropStruct.LinkText);
            //copy the rest to the second buffer
            wcsncpy_s(buf2,MAX_PATH,p+1,MAX_PATH-1);        
          }else{
            wcsncpy_s(buf1,MAX_PATH,m_PropStruct.LinkText,MAX_PATH-1);
            buf2[0]=0;
          } 
        }
        rc=ShellExecute(AfxGetMainWnd()->m_hWnd,L"open",buf1,buf2,NULL,1);
        if((int)rc<33){
          AfxMessageBox(L"Unable to launch program",MB_ICONERROR);
        }
      }else{
        i=wcstol(m_PropStruct.LinkText,NULL,10);
        
        AfxGetMainWnd()->PostMessage(WM_GOTOVIEW,i,0);
      }
    
    
    }

  5. #5
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    Thanks. That's not that easy after all.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    hoxsiew, I'm trying your technique, but I've come up with a situation when it doesn't work:

    c:\c++\test.exe "C:\Documents and Settings\PC12\Desktop\Sample.txt"

    There're really almost endless possibilities how those quotes can be placed. Still, there's no shell API to do that for us, is there?

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: ShellExecute returns error on a link with parameters

    In my project I allowed the user to browse for the program to execute, then specify the command line. I originally just looked for the first space to delimit command from arguments, but, if the program path had any spaces in it, I was screwed. I noticed that if the user browsed for the path, and the path had spaces in it, it came back from the CFileDIalog with quotes around it. That's why I added the check for quotes first.

    Like I said, there is probably a better way, but this worked in every case we know of with our implementation.

  8. #8
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    I guess I should go with File Open dialog instead of letting a user type in the path in an edit box. I'm learning now that some shortcuts don't like the verb "Open", so that has to be NULL as well.

    I'm not sure if Microsoft itself knows their own creation, i.e. ShellExecute?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ShellExecute returns error on a link with parameters

    Have you tried CreateProcess( NULL, _T("full path w parameters"), ... );

  10. #10
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    Quote Originally Posted by Arjay View Post
    Have you tried CreateProcess( NULL, _T("full path w parameters"), ... );
    Arjay, CreateProcess unfortunately doesn't work for a link, and here I'm coming to a catch-22. Some of those links don't get resolved with a simple IShellLink call. I started a separate thread on this subject. For some reason Microsoft introduced a new type of links that return gibberish in IShellLink::GetPath, and since I have no clue what's going on there I thought that maybe ShellExecute can help.... but, as always with MS it's not that easy.
    Last edited by ahmd; September 28th, 2009 at 04:07 PM.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ShellExecute returns error on a link with parameters

    Ahmd, post a valid link of something like Word and I'll try it using the Process class in C#. If that works, we can drill down and see how it's implemented under the covers.

  12. #12
    Join Date
    Sep 2009
    Posts
    19

    Re: ShellExecute returns error on a link with parameters

    Hello, can you tell me how you detect if something is a link ?

  13. #13
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    Quote Originally Posted by Arjay View Post
    Ahmd, post a valid link of something like Word and I'll try it using the Process class in C#. If that works, we can drill down and see how it's implemented under the covers.
    Arjay, I'm attaching a link to MS Word 2007 that exhibits this behavior.

    Hello, can you tell me how you detect if something is a link ?
    Please start a new thread for a new topic. To answer your question, I believe that all you need to do is to check the file extension and see that it's .lnk for the file system link.
    Attached Files Attached Files

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ShellExecute returns error on a link with parameters

    I was looking for the actual lnk file.

    Thanks, I got one from my machine.

    From

    Code:
     
    C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office
    I grabbed
    "Microsoft Office Word 2007" (lnk)

    and opened it in Visual Studio. It opens as what appears to be a resource file.

    Given that, I believe you can extract the exe name out of it which is what I believe you are looking for.
    Attached Images Attached Images

  15. #15
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: ShellExecute returns error on a link with parameters

    Arjay, isn't it what you showed a Winword itself? People tell me that it's an MSI (I believe) link, whatever that beast is. That's why I was ragging on Microsoft for reinventing the wheel.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured