title: C++\CLI applicatin crash on load
body: <p>I have a C++ app that loads lot of C++ DLL and few selected C++\CLI ones. On one of the machines (Windows Server 2003 SP2) on start-up I was getting error message </p>
  
  <blockquote>
    <p>The application failed to initialize properly (0xC0000005). Click on OK to terminate the application.</p>
  </blockquote>
  
  <p>When app was opened with WinDbg instead of getting proper DbgBbreak breakpoint I was getting this:</p>
  
  <pre><code>Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
  Copyright (c) Microsoft Corporation. All rights reserved.
  
  CommandLine: d:\appdir\MyTrueApp.exe
  Symbol search path is: .sympath SRV*c:\localsymbols*http://msdl.microsoft.com/download/symbols
  Executable search path is: 
  ModLoad: 00400000 0044b000   MyTrueApp.exe
  ModLoad: 7c800000 7c8c3000   ntdll.dll
  ModLoad: 77e40000 77f42000   C:\WINDOWS\system32\kernel32.dll
  ModLoad: 60200000 60264000   d:\AppDir\CustomCppDLL_ONE.dll
  ModLoad: 76aa0000 76acd000   C:\WINDOWS\system32\WINMM.dll
  ModLoad: 77380000 77411000   C:\WINDOWS\system32\USER32.dll
  ModLoad: 77c00000 77c49000   C:\WINDOWS\system32\GDI32.dll
  ModLoad: 7d1e0000 7d27c000   C:\WINDOWS\system32\ADVAPI32.dll
  ModLoad: 77c50000 77cf0000   C:\WINDOWS\system32\RPCRT4.dll
  ModLoad: 76f50000 76f63000   C:\WINDOWS\system32\Secur32.dll
  ModLoad: 10000000 10023000   d:\AppDir\CustomCppDLL_TWO.dll
  ModLoad: 7c420000 7c4a7000   C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.5592_x-ww_179798C8\MSVCP80.dll
  ModLoad: 78130000 781cb000   C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.5592_x-ww_179798C8\MSVCR80.dll
  ModLoad: 77ba0000 77bfa000   C:\WINDOWS\system32\msvcrt.dll
  ModLoad: 60300000 60332000   d:\AppDir\CustomCppDLL_3RD.DLL
  ModLoad: 781d0000 782df000   C:\WINDOWS\WinSxS\x86_Microsoft.VC80.MFC_1fc8b3b9a1e18e3b_8.0.50727.5592_x-ww_E87E0BCD\MFC80.DLL
  ModLoad: 7d180000 7d1d2000   C:\WINDOWS\system32\SHLWAPI.dll
  ModLoad: 77670000 777a9000   C:\WINDOWS\system32\ole32.dll
  ModLoad: 77d00000 77d8b000   C:\WINDOWS\system32\OLEAUT32.dll
  ModLoad: 00360000 00375000   d:\AppDir\CustomCppDLL_4th.DLL
  ModLoad: 60800000 6081a000   d:\AppDir\CustomCppDLL_5th.DLL
  ModLoad: 60600000 6074b000   d:\AppDir\CustomCppDLL_6th.DLL
  ModLoad: 003b0000 003c5000   d:\AppDir\CustomCppDLL_7th.DLL
  ModLoad: 77b90000 77b98000   C:\WINDOWS\system32\VERSION.dll
  ModLoad: 00450000 004d6000   d:\AppDir\CustomCppDLL_7th.DLL
  ModLoad: 7c8d0000 7d0cf000   C:\WINDOWS\system32\SHELL32.dll
  ModLoad: 61880000 618bb000   C:\WINDOWS\system32\OLEACC.dll
  ModLoad: 73070000 73097000   C:\WINDOWS\system32\WINSPOOL.DRV
  ModLoad: 762b0000 762f9000   C:\WINDOWS\system32\comdlg32.dll
  ModLoad: 77530000 775c7000   C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_5.82.3790.4770_x-ww_A689AB02\COMCTL32.dll
  ModLoad: 004e0000 00506000   d:\AppDir\CustomCppDLL_9th.DLL
  ModLoad: 00510000 00547000   d:\AppDir\CustomCppDLL_10th.DLL
  ModLoad: 71c00000 71c17000   C:\WINDOWS\system32\WS2_32.dll
  ModLoad: 71bf0000 71bf8000   C:\WINDOWS\system32\WS2HELP.dll
  ModLoad: 76cf0000 76d0a000   C:\WINDOWS\system32\iphlpapi.dll
  ModLoad: 76b70000 76b7b000   C:\WINDOWS\system32\PSAPI.DLL
  ModLoad: 6d580000 6d628000   C:\WINDOWS\system32\dbghelp.dll
  ModLoad: 00560000 0056a000   d:\AppDir\CustomCpp_CLI.DLL
  ModLoad: 79000000 7904a000   C:\WINDOWS\system32\mscoree.dll
  ModLoad: 71bc0000 71bc8000   C:\WINDOWS\system32\rdpsnd.dll
  ModLoad: 771f0000 77201000   C:\WINDOWS\system32\WINSTA.dll
  ModLoad: 71c40000 71c97000   C:\WINDOWS\system32\NETAPI32.dll
  *** ERROR: Symbol file could not be found.  Defaulted to export symbols for ntdll.dll - 
  (1510.1304): Access violation - code c0000005 (first chance)
  First chance exceptions are reported before any exception handling.
  This exception may be expected and handled.
  </code></pre>
  
  <p>Result of <a href="http://www.google.com/search?q=windbg%20kc&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla%3apl%3aofficial&amp;client=firefox-a" rel="nofollow"><code>kc</code></a> command was</p>
  
  <pre><code>ntdll!LdrOpenImageFileOptionsKey
  ntdll!LdrQueryImageFileExecutionOptionsEx
  ntdll!LdrQueryImageFileExecutionOptions
  ntdll!RtlIpv4StringToAddressExW
  ntdll!RtlLogStackBackTrace
  ntdll!LdrGetProcedureAddress
  ntdll!EtwTraceMessage
  ntdll!RtlIsThreadWithinLoaderCallout
  ntdll!RtlGetActiveActivationContext
  ntdll!RtlGetActiveActivationContext
  ntdll!RtlGetActiveActivationContext
  ntdll!RtlGetActiveActivationContext
  ntdll!RtlGetActiveActivationContext
  ntdll!RtlGetActiveActivationContext
  ntdll!CsrClientConnectToServer
  ntdll!KiUserApcDispatcher
  </code></pre>
  
  <p><a href="http://www.google.com/search?q=LdrOpenImageFileOptionsKey&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla%3apl%3aofficial&amp;client=firefox-a" rel="nofollow">Googling</a> for <code>ntdll!LdrOpenImageFileOptionsKey</code> reveals that it is a part of a <a href="http://www.geoffchappell.com/viewer.htm?doc=studies/windows/win32/ntdll/api/rtl/rtlexec/openimagefileoptionskey.htm" rel="nofollow">system loader</a>. No more help from here except that it seems <a href="http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/885fbafc-1df4-4b00-8815-12a579ed91f5/" rel="nofollow">one poor guy</a> had a similar issue but without solution. There was <a href="http://msdn.microsoft.com/en-us/library/aa290048%28v=vs.71%29.aspx" rel="nofollow">once</a> a bug in loading of mixed assemblies but it was for Visual Studio 2003 and I was on 2005 and .NET Framework 2.0.</p>
  
  <p>In sheer despair I tried <a href="http://blogs.msdn.com/b/astebner/archive/2009/02/18/9432938.aspx" rel="nofollow">.Net Framework repair</a> but of course with no luck. I went back to <a href="http://www.google.com/search?q=0xc0000005%20cli&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla%3apl%3aofficial&amp;client=firefox-a" rel="nofollow">Google</a> and got this <a href="http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/4582da99-8d75-4451-bf50-f2cec54f5917" rel="nofollow">post</a> that had a small clue. It states </p>
  
  <blockquote>
    <p>Since you received the error message
    "An unhandled non-continuable
    exception was thrown during process
    load", it seems the problem is caused
    by a difficult issue related to DLL
    files. There are several discussions
    on this topic, maybe some of them can
    help:</p>
  
  <pre><code>  http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=325627&amp;SiteID=1
    http://groups.google.com/group/microsoft.public.vc.mfc/browse_thread/thread/560a31bb72b2bca8/44fb800374c2f3b0%2344fb800374c2f3b0
    http://groups.google.com/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/1243abff27e677ae/901c0a23deec7e47%23901c0a23deec7e47
  </code></pre>
  </blockquote>
  
  <p>First two link are no use but the <a href="http://groups.google.com/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/1243abff27e677ae/901c0a23deec7e47#901c0a23deec7e47" rel="nofollow">last</a> one mentions that with similar problem <code>/DELAYLOAD</code> linker option helped. By setting <code>CustomCpp_CLI.DLL</code> as delay loadable I got my solution. My application <strong>rock'n'rolls</strong> now!</p>
  
  <p>I still don't know exactly a reason why this helps. On the other side my C++ DLL do some strange stuff in DllMain and as <a href="http://blogs.msdn.com/b/larryosterman/archive/2004/04/23/118979.aspx" rel="nofollow">Larry Osterman</a> and <a href="http://blogs.msdn.com/b/oldnewthing/archive/2004/01/28/63880.aspx" rel="nofollow">Raymond Chen</a> write this is <a href="http://www.google.com/search?num=100&amp;hl=en&amp;safe=off&amp;sa=X&amp;ei=a9XBTY6-CpDHtAbnwY3DBQ&amp;ved=0CBUQvwUoAQ&amp;q=raymond%20chen%20dllmain&amp;spell=1" rel="nofollow">scary</a>. Maybe my DLLs mess somehow with loader? I don't know.</p>
  
  <p>I wrote this as a <a href="http://www.google.com/search?q=aar%20universalis&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla%3apl%3aofficial&amp;client=firefox-a" rel="nofollow">AAR (After Action Report)</a> so it pops out in Google results and will poor soul one day in the future so Internet <a href="http://www.google.com/search?num=100&amp;hl=en&amp;safe=off&amp;client=firefox-a&amp;hs=mil&amp;rls=org.mozilla%3apl%3aofficial&amp;sa=X&amp;ei=NNfBTaLUJs_Lsgap-_nCBQ&amp;ved=0CBUQvwUoAQ&amp;q=joel%20internet%20is%20a%20better%20place&amp;spell=1" rel="nofollow">is a better place.</a></p>
  
  <p>BTW If someone knows why <code>/DELAYLOAD</code> helped please enlighten me.</p>