CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    15

    ShellExecute problem

    In the following code the first ShellExecute statement does not work even though even though strAppDirectory contains the same path as the next ShellExecute statement that does work.

    Do I have a dumb mistake that I am missing?


    void CDBcalcDlg::OnHelp()
    {
    char szAppPath[MAX_PATH] = "";
    CString strAppDirectory;

    ::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);

    // Extract directory
    strAppDirectory = szAppPath;
    strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));

    ShellExecute(hWnd, _T("open"), _T("C:\\strAppDirectory\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);

    ShellExecute(hWnd, _T("open"), _T("C:\\Users\\Clay\\Documents\\Visual Studio 2005\\Projects\\Chm_dBcalc\\dBcalc\\Debug\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);


    }
    Last edited by Clay L; April 13th, 2010 at 02:56 PM.

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

    Re: ShellExecute problem

    Is there supposed to be a space in the first path?

  3. #3
    Join Date
    Jun 2008
    Posts
    15

    Re: ShellExecute problem

    No there isn't, but I removed it and rebuilt the program but it didn't help.

    Good catch though.

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

    Re: ShellExecute problem

    Quote Originally Posted by Clay L View Post
    In the following code the first ShellExecute statement does not work even though even though strAppDirectory contains the same path as the next ShellExecute statement that does work.

    Do I have a dumb mistake that I am missing?

    Code:
     
    void CDBcalcDlg::OnHelp() 
    {
        char szAppPath[MAX_PATH] = "";
        CString strAppDirectory;
     
        ::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
     
        // Extract directory
        strAppDirectory = szAppPath;
        strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));    
     
        ShellExecute(hWnd, _T("open"), _T("C:\\strAppDirectory\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);
     
        ShellExecute(hWnd, _T("open"), _T("C:\\Users\\Clay\\Documents\\Visual Studio 2005\\Projects\\Chm_dBcalc\\dBcalc\\Debug\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);
     
     
    }
    What strAppDirectory contains is irrelevant because you aren't using it in your ShellExecute calls.

    Btw, you probably want to get out of the practice of using 'char' like in the following code:
    Code:
     
    char szAppPath[MAX_PATH] = "";
    Instead use TCHAR
    Code:
     
    TCHAR szAppPath[MAX_PATH] = _T("");
    Actually, in your case you don't need to create a temp buffer to store string data because the CString class allows you to write to it directly.

    Code:
     
    ::GetModuleFileName(0, strAppDirectory.GetBuffer( MAX_PATH ), MAX_PATH - 1);
    strAppDirectory.ReleaseBuffer( );

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: ShellExecute problem

    Quote Originally Posted by MSDN
    If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure.
    So....the million-dollar-question is....?

  6. #6
    Join Date
    Jun 2008
    Posts
    15

    Re: ShellExecute problem

    The problem was that I had strAppDirectory inside quote marks.

    Either of the two statements below work properly to get the path into strToOpen.

    strToOpen.Format(_T("%s\\dBcalc.chm"), strAppDirectory); //This works
    strToOpen = strAppDirectory + _T("\\dBcalc.chm"); //So does this

    ShellExecute(hWnd, _T("open"), _T(strToOpen),NULL, NULL, SW_SHOWNORMAL); // executes normally now

    The answers came from two different guys on a MSD forum.

    Arjay also pointed out my error but I didn't understand it at the time.
    Last edited by Clay L; April 14th, 2010 at 02:05 PM.

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: ShellExecute problem

    Code:
    _T(strToOpen),
    You don't need the _T here because it is a CString, not a char*.

  8. #8
    Join Date
    Jun 2008
    Posts
    15

    Re: ShellExecute problem

    Quote Originally Posted by Skizmo View Post
    Code:
    _T(strToOpen),
    You don't need the _T here because it is a CString, not a char*.
    You are right of course. I just copied it from the answer and didn't notice that.

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