none
MessageBox.Show in .NET 4 versus .NET 2 RRS feed

  • Question

  • I have an odd problem with .NET COM interop where MessageBox.Show doesn't display the message box if I am using .NET 4. It works fine with .NET 2 (I am changing the setting in the VS 2012 project I have).

    Background:

    I have a .NET class object that I expose to COM. I have a COM MFC windows application that calls CoCreateInstance and then calls a method on the .NET object as the app is starting up (splash screen has displayed but frame has not yet displayed). I can step thru the code of my COM app and see that CoCreateInstance succeeds and so does the call to the method. I cannot debug the .NET code from the .NET project directly as my breakpoints never trip (they are enabled) when I am using .NET 4. So I put a message box call into the method I call when the COM app is starting up and one in the method I call when the COM app is shutting down. The latter method shows the box.

    I have a UI that lets me release and restart the .NET object. Once I am fully up and running if I create the .NET object, the box shows. So this seems to be related to when I call MessageBox.Show. It appears that .NET 4 simply cannot display a window when I make the call? Why? Is there a windows subsystem that is not yet ready?

    All I have to do to fix the issue of MessageBox.Show not doing anything is go to the project/application setting and choose .NET 2. rebuild and run and the box shows. But chaning to .NET 2 negates the reason I have the box. With the .NET 2 setting, all my breakpoints work fine. That is, I can debug the code by launching the COM app as the debug targeta and debug as normal.

    I know one cannot debug two .NET frameworks at once. I can run my COM app in debug and before I call CoCreateInstance on the .NET component I can see that there is no .NET runtime loaded. It loads when I call CoCreateInstance. Hence I am also confused as to why the .NET 2 shows the box AND lets me debug fine while the .NET 4 setting allows neither.

    A standard trick to debug .NET components is to add a message box and when it shows up simply debug/attachtoprocess and pick the .NET runtime I want to debug. But this inexplicable non-appearance of the message box has killed that ability when it comes to .NET 4.

    What is the difference between .NET 2 and 4 when it comes to displaying a window? And what about the difference when it comes to debugging in this scenario?


    R.D. Holland

    Thursday, January 30, 2014 4:40 PM

Answers

  • If debugging is your aim, does replacing MessageBox with DebugBreak() help?

    If no debugger is attached, users are asked if they want to attach a debugger. If yes, the debugger is started. If a debugger is attached, the debugger is signaled with a user break point event, and the debugger suspends execution of the process just as if a debugger break point had been hit.

    Ideally yes the MessageBox should work. Is the main application an desktop application or a service?


    Blog: http://ntcoder.com/bab


    Posts are provided as is without warranties or guaranties.

    • Marked as answer by RD Holland Tuesday, February 11, 2014 3:05 PM
    Monday, February 10, 2014 3:25 PM
    Moderator

All replies

  • Perhaps this is a timing issue. Sometimes when I start the app, the .NET 4 call to MessageBox.Show works and sometimes it does not.

    R.D. Holland

    Thursday, January 30, 2014 5:19 PM
  • Hi RD,

    I am trying to invite someone who familiar with this thread to come into.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 31, 2014 6:16 AM
    Moderator
  • Can you share a repro with us? We would like to check this out.

    Blog: http://ntcoder.com/bab


    Posts are provided as is without warranties or guaranties.

    Thursday, February 6, 2014 12:41 PM
    Moderator
  • Nibu,

    I tried duplicating using a simple MFC dialog app but so far no repro with it. I even made the CoCreateInstance call inside OnInitDialog hoping an early call makes the difference.

    The app I am actually using where this shows up is a huge application so no way I can post that. I did make some modifications so that I make sure I don't call CoCreateInstance until the application is fully shown on the desktop and so far no problem with that. Here I am only speaking about the message box, not the fact that I simply cannot debug from the csharp app itself. In any case it really looks like some sort of timing issue to me and the code change I made was in the C# code where I had the C# object register itself as requiring a document be loaded before I load the C# object. Unfortunately that is not a real fix because some objects really do need to load as our app is loading, and do so before the UI (app window/frame) displays.

    I also added try/catch around the call to show the message box but I catch no exceptions (debug.writeline if I do). And from my COM app I turned on first chance exception handling for CLR and managed debug but I get no exceptions either.

    I wonder what assemblies need to load in order for the message box to appear. I have a break point set right after I call the first method on the C# object where I have the message box display. So I can see what .NET modules are loaded.

    Ordering by path I have these as the first 12 out of 264:

     mscorlib.ni.dll C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\bf2ecabcd96ec8238dc385b0a3ffa084\mscorlib.ni.dll N/A N/A Skipped loading symbols for NGen binary.  251 4.0.30319.18052 built by: FX45RTMGDR 4/21/2013 4:36 AM 5A4B0000-5B47A000 [7948] Edged.exe  
     System.Configuration.ni.dll C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Configuration\edb27e2c25837f79902054965d6813cd\System.Configuration.ni.dll N/A N/A Skipped loading symbols for NGen binary.  262 4.0.30319.18060 built by: FX45RTMGDR 7/22/2013 10:38 PM 51F00000-51FEE000 [7948] Edged.exe  
     System.Drawing.ni.dll C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Drawing\cceaf9d7891fc325a90473aa9a661661\System.Drawing.ni.dll N/A N/A Skipped loading symbols for NGen binary.  260 4.0.30319.18021 built by: FX45RTMGDR 10/1/2012 10:50 PM 598B0000-59A41000 [7948] Edged.exe  
     System.Windows.Forms.ni.dll C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Windows.Forms\c5db04fde4893300ff28045ce4f7567d\System.Windows.Forms.ni.dll N/A N/A Skipped loading symbols for NGen binary.  261 4.0.30319.18047 built by: FX45RTMGDR 3/29/2013 12:32 AM 58C90000-598AF000 [7948] Edged.exe  
     System.Xml.ni.dll C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xml\82d58d49946f82eb56bae40f3b097784\System.Xml.ni.dll N/A N/A Skipped loading symbols for NGen binary.  263 4.0.30319.18060 built by: FX45RTMGDR 7/22/2013 10:38 PM 52EA0000-535D9000 [7948] Edged.exe  
     System.ni.dll C:\Windows\assembly\NativeImages_v4.0.30319_32\System\ac79b74f022d9a096de2b884f4249543\System.ni.dll N/A N/A Skipped loading symbols for NGen binary.  259 4.0.30319.18044 built by: FX45RTMGDR 2/21/2013 10:35 PM 59B30000-5A4AD000 [7948] Edged.exe  
     clr.dll C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll N/A N/A Loading disabled by Include/Exclude setting.  249 4.0.30319.18052 built by: FX45RTMGDR 4/21/2013 4:41 AM 5B560000-5BBF3000 [7948] Edged.exe  
     clrjit.dll C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll N/A N/A Loading disabled by Include/Exclude setting.  254 4.0.30319.18047 built by: FX45RTMGDR 3/29/2013 12:04 AM 59A50000-59ABE000 [7948] Edged.exe  
     mscoreei.dll C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll N/A N/A Loading disabled by Include/Exclude setting.  247 4.0.30319.17929 built by: FX45RTMREL 7/8/2012 9:52 PM 71F30000-71FAA000 [7948] Edged.exe  
     nlssorting.dll C:\Windows\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll N/A N/A Loading disabled by Include/Exclude setting.  264 4.0.30319.17929 built by: FX45RTMREL 7/8/2012 9:54 PM 65ED0000-65EE2000 [7948] Edged.exe  
     advapi32.dll C:\Windows\SysWOW64\advapi32.dll N/A N/A Loading disabled by Include/Exclude setting.  7 6.1.7601.18247 (win7sp1_gdr.130828-1532) 8/28/2013 7:48 PM 74F30000-74FD0000 [7948] Edged.exe  


    R.D. Holland

    Thursday, February 6, 2014 11:04 PM
  • If debugging is your aim, does replacing MessageBox with DebugBreak() help?

    If no debugger is attached, users are asked if they want to attach a debugger. If yes, the debugger is started. If a debugger is attached, the debugger is signaled with a user break point event, and the debugger suspends execution of the process just as if a debugger break point had been hit.

    Ideally yes the MessageBox should work. Is the main application an desktop application or a service?


    Blog: http://ntcoder.com/bab


    Posts are provided as is without warranties or guaranties.

    • Marked as answer by RD Holland Tuesday, February 11, 2014 3:05 PM
    Monday, February 10, 2014 3:25 PM
    Moderator
  • That's a good ideal Nibu.

    I use system.diagnostics to use Debug.WriteXXX functions. Let me try it out.

    Hm. No Debug.Break() API. But I found Debugger.Break()

    When I run my debug target from VS2012 I get the "xxx has stopped working"  dialog and I can click "Debug the program". Then a message box saying:

    "a debugger is attached to xxx.exe but not configured to debug this unhandled exception ... The current debugger is configured to debug code that uses Microsoft .NET Framework v1.0, 1.2 or 2.0. An unhandled exception is being thrown from .NET Framework v4.0 code."

    I have the project set to use .NET 4 and am running the COM app as the debug target but the debugger is being short-circuited. That means that even though I didn't see any .NET DLLs loaded when I ran from my COM MFC app, .NET 2 is being brought into the picture.

    In any case I just chose "run without debugging" and when the dialog came up I could choose "Debug" and then pick the Visual Studio 2012 instance I am using and presto, I am at the Debugger.Break() line.

    That's the ticket. I owe you big-time!

    But wait there's more. Now that I know .NET 2 is really involved, I see that I have an old .config file for the app and (this is embarrassing) it has a supportedRuntime version = "v2.0.50727" entry. I created that file years ago to keep .NET 1.0/1.1 from loading into our process because it had some bugs that prevented certain interop functionality from working (fixed in 2.0). Apparently that config file prevents the debugger from debugging .NET 4 even though the project is a .NET 4 project. All I had to do is hide that config file to allow me to debug directly from the project. I don't even deliver the config file as part of setup anymore.

    I'll keep that Break call in the code and comment it out when not debugging since the message box randomly appearing issue still exists. This is an add-in template I am working on and it is the rare add-in that needs to load as the app starts up and I have already found that any add-in that doesn't load until the app frame is fully up and displayed doesn't have the message box issue.

    If you don't mind, I'll give you credit and add your byline to the comment and include your blog link.


    R.D. Holland

    Tuesday, February 11, 2014 2:51 PM
  • Sure! Happy to hear that we could be of help to you.

    Nibu Thomas

    Microsoft Support


    Blog: http://ntcoder.com/bab


    Posts are provided as is without warranties or guaranties.

    Tuesday, February 11, 2014 3:21 PM
    Moderator