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

    ShellExecute crashes Dev Studio 2008 SP 1

    I have a MFC Dialog based application, and when I get a certain user-defined windows message I do the following:
    ShellExecute(NULL,NULL,(const char*)WLC.Settings.FinishURL, NULL,NULL,SW_SHOW);

    That FinishURL is just a basic CString. Anyway, this causes Dev Studio 2008 to crash. I can rum my app stand alone by going into the build folder and running the debug or release version and it runs just fine.

    I know this is a long shot, but does anyone know why this is crashing Dev Studio 2008 and how I can fix it? I have since added some #ifndef DEBUG around it, but I would rather it actually execute while I am testing.

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    I have no idea, but FWIW, you don't need to cast a CString to a const char*. It has an overloaded operator for that.

    Are you saying VIsual Studio itself is crashing, or your app is crashing inside the debugger?

  3. #3
    Join Date
    Oct 2011
    Posts
    97

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Could you give us some more information? Like what kind of 'crash' are you getting and what the call stack looks like? Have you tried debugging to see if any of the variables contain garbage data when making the call?
    Last edited by Access_Denied; June 21st, 2012 at 03:46 PM.

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    1. Is it so hard to use Code tags and white spaces between the argument list? Doesn't it look better:
      Code:
      ShellExecute(NULL, NULL, (const char*)WLC.Settings.FinishURL, NULL, NULL, SW_SHOW);
    2. What is the purpose to cast "CString" to const char* type?
    3. Is your build causing the crash an ANSI or UNICODE?
    4. How does the message handler look like?
    5. What exactly does this CString contain?
    6. Do you by chance make some GetBuffer without the following ReleaseBuffer?
    Victor Nijegorodov

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Dev Studio is crashing. Not my application. I can't look at the call stack because the development environment crashes.

  6. #6
    Join Date
    Sep 2004
    Posts
    1,361

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    2. To make sure I get the char * type of pointer. CString does this with a cast. I just want to make sure I end up with a pointer to a buffer of *char and not a reference to an object.
    3. I am using ANSI, the build works fine, the app runs until it hits that statement then Dev Studio crashes. Not my App, but the development environment.
    4. Message Handler hook?
    5. It constains, "https://www.google.com". You can launch URLs this way and it will use the default system web browser.
    6. No.

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    2. To make sure I get the char * type of pointer. CString does this with a cast. I just want to make sure I end up with a pointer to a buffer of *char and not a reference to an object.
    Then, please, read the post from GCDEF.

    Did you try to rebuild your project, ... to delete .ncb, .opt, .aps and so on files?
    To restart Windows?
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    5. It contains, "https://www.google.com". You can launch URLs this way and it will use the default system web browser.
    We can't see what's behind those variables, so do this -- hardcode into the call of ShellExecute that string. Does it crash then?

    Or even do this -- declare a CString right before the call and set it to the string:
    Code:
    CString whatever = "https://www.google.com";
    ShellExecute(... whatever, ...);
    If these do not crash, then the suspicion is that you've corrupted the CString object in some way, or the CString doesn't contain the string you say it contains. If the "whatever" version crashes, then there still is a suspicion of corruption (since CString uses reference counting).
    I can rum my app stand alone by going into the build folder and running the debug or release version and it runs just fine
    Means very little, seriously. Many buggy app can "run fine" when run under one circumstances, but when run under the debugger, or another computer, then all heck breaks loose. C++ isn't Java or C#, where if you make a mistake, you're guaranteed some sort of diagnostic to be produced.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 21st, 2012 at 07:51 PM.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    Dev Studio is crashing. Not my application. I can't look at the call stack because the development environment crashes.
    Do you mind to explain what is Dev Studio? Is it Visual Studio? Or something else?
    Best regards,
    Igor

  10. #10
    Join Date
    Sep 2004
    Posts
    1,361

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    I am Visual Studio 2008.
    I tried this:
    Code:
    CString URL = "https://www.google.com";
    		ShellExecute(NULL,NULL,(const char*)URL, NULL,NULL,SW_SHOW);
    and this also crashes Visual Studio 2008. Putting in the string directly in place of the CString works without a crash.

    However, even this:
    Code:
    CString URL = "https://www.google.com";
    		char *str = (char *)(const char *)URL;
    		ShellExecute(NULL,NULL,str, NULL,NULL,SW_SHOW);
    Crashes Visual Studio 2008. I put a break point into the call and str does point at the correct string.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Quote Originally Posted by DeepT View Post
    I am Visual Studio 2008.
    I tried this:
    Code:
    CString URL = "https://www.google.com";
    		ShellExecute(NULL,NULL,(const char*)URL, NULL,NULL,SW_SHOW);
    and this also crashes Visual Studio 2008. Putting in the string directly in place of the CString works without a crash.
    If the build is Unicode, those casts are incorrect. As a matter of fact, why are you doing all of those C-style casts to begin with?

    Any time I see casting all over the place like that, it gives the indication that the code would never have compiled unless you forced the compiler to keep quiet by casting. So to be honest with you, I am not surprised the call crashes or the string being pointed to is not correct.

    The ShellExecute function takes LPCTSTR as the string parameters, not const char*. This very simple example should have worked:
    Code:
    LPCTSTR URL = _T("https://www.google.com");
    ShellExecute(NULL,NULL, URL, NULL,NULL,SW_SHOW);
    Notice the _T() macro?

    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);
    The reason why you should try this with an untouched, new program is because CString can get corrupted due to it being a reference counted type. If you've messed up the reference counting in some way, then all bets are off.

    Regards,

    Paul McKenzie

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Seriously, stop casting CStrings to const char*. Here's why you don't need to.

    http://msdn.microsoft.com/en-us/libr...=vs.60%29.aspx

    I know that's not the problem here, but it's an unnecessary habit you should get out of.

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    I'd also try to replace the second NULL with the _T("open") verb.
    Victor Nijegorodov

  14. #14
    Join Date
    Sep 2004
    Posts
    1,361

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    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);

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

    Re: ShellExecute crashes Dev Studio 2008 SP 1

    Just that in an application by itself?

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