CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    Join Date
    Sep 2004
    Posts
    1,361

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    No, i didn't try that in an application by itself. It is final step in my other app. I could paste that into a new app. If it crashes or does not, what does that tell me? Note this is crashing the development environment. My application, if you run it outside of the development environment runs just fine and launches the URL correctly. I want to know why this call causes my development environment to crash.

    Ok, I just created a new MFC Dialog based project. On Init dialog I put that code (in my last post). It ran fine and did not crash Dev Studio 2008.

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    Ok, this code block also crashes Dev Studio 2008 and has nothing to do with CStrings:
    Code:
    #define URL "https://www.google.com"
    
    		int Len = strlen(URL);
    		char Buff[1024] = {0};
    		CopyMemory( &Buff[0], URL, Len );
    		ShellExecute(NULL,_T("open"),Buff, NULL,NULL,SW_SHOW);
    This code is wrong!
    Please, try what Paul suggested:
    Quote Originally Posted by Paul McKenzie View Post
    Then create a brand new program, and just these two lines:
    Code:
    CString URL = _T("https://www.google.com");
    ShellExecute(NULL,NULL, (LPCTSTR)URL, NULL,NULL,SW_SHOW);
    Victor Nijegorodov

  3. #18
    Join Date
    Sep 2004
    Posts
    1,361

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Ok, VictornN, I tried that exact code you pasted and it crashes Dev Studio 2008 in my project.

    This code:
    Code:
    CString URL = _T("https://www.google.com");
    ShellExecute(NULL,NULL, (LPCTSTR)URL, NULL,NULL,SW_SHOW);

  4. #19
    Join Date
    Apr 1999
    Posts
    27,449

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    Ok, this code block also crashes Dev Studio 2008 and has nothing to do with CStrings:
    That code is wrong if it's a Unicode app.

    Not only should you get out of the habit of casting, get into the habit of using _T(), TEXT(), TCHAR, etc. instead of char, char*, etc. Also, your "CopyMemory" would be wrong, since CopyMemory takes the number of bytes, not the number of characters. A Unicode app has 2-byte characters, not 1 byte.

    I know you say you're using ANSI, but mistakes like this gives the impression you may be making mistakes in other places in your app, causing memory corruption in some way.,
    Code:
    TCHAR Buff[1024] = _T("https://www.google.com");
    ShellExecute(NULL,_T("open"),Buff, NULL,NULL,SW_SHOW);
    This code should work, regardless of the build type.

    Regards,

    Paul McKenzie

  5. #20
    Join Date
    Sep 2004
    Posts
    1,361

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    It is not unicode. Anyway Ill just put the debug macros around it and not execute it in my development environment. My app runs just fine when run stand-alone.

  6. #21
    Join Date
    Apr 1999
    Posts
    27,449

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    Anyway Ill just put the debug macros around it and not execute it in my development environment. My app runs just fine when run stand-alone.
    That is not a fix. If your app ran great on one computer and flunked out on another, do you throw the computer away where your application fails, so as to "fix the bug" and say "it works on my main machine, so it's OK"? Honestly, that "fix" you came up with would be totally unacceptable in a professional environment.

    As I stated previously, if a C++ application has a bug, there is no guarantee how it will run. You need to figure out exactly why mysteriously your CStrings are corrupted. If a simple ShellExecute of a web address were so buggy, then it would have been reported by hundreds if not thousands of programmers.

    Regards,

    Paul McKenzie

  7. #22
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Remember it's not his app that's crashing, it's Visual Studio. Nothing in his code should be able to cause that. It is possible that there's nothing wrong with his code at all.

  8. #23
    Join Date
    Dec 2012
    Posts
    1

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    I have the same thing happening. Nothing to do with the code, it's Visual Studio bug.

    Nikita Leontiev
    Just Manager Developer

  9. #24
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    strlen does not include the the null-termination so your copy wont be null-terminated. See http://msdn.microsoft.com/en-us/library/78zh94ax.aspx
    Edit: Sorry, I didnt account for your init
    Last edited by S_M_A; December 10th, 2012 at 04:32 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  10. #25
    Join Date
    Nov 2005
    Location
    NC, USA
    Posts
    99

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    (Two years later...)

    I was having this problem, too, but I solved it by making the following change.

    From this:
    Code:
    CFile htmFile;
    CString strFileName = _T("C:\\Temp\\TempHTML.htm");
    
    if ( htmFile.Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite )
    {
        // Write stuff to the file
        ...
    
        htmFile.Flush();   // I put the Flush in as a test to see if it would help, but it didn't. I left it in anyway.
        htmFile.Close();
    
        ::ShellExecute(NULL, _T("open"), strFileName, NULL, NULL, SW_SHOW);   // VS crashes here in Debug only
    }
    To this:
    Code:
    CFile htmFile;
    CString strFileName = _T("C:\\Temp\\TempHTML.htm");
    
    BOOL rc = htmFile.Open(strFileName.GetBuffer(), CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
    strFileName.ReleaseBuffer();
    
    if ( rc )
    {
        // Write stuff to the file
        ...
    
        htmFile.Flush();
        htmFile.Close();
    
        ::ShellExecute(NULL, _T("open"), strFileName, NULL, NULL, SW_SHOW);   // This works in Debug & Release
    }
    Apparently, CFile::Open must be holding onto something vital in the CString.
    Last edited by yooper; December 10th, 2014 at 12:12 PM. Reason: Highlighting

Page 2 of 2 FirstFirst 12

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