Windbg: The call to LoadLibrary SOS failed

When debugging managed code application in Windbg, we have to load sos, correct version of SOS. One of occasional error during SOS loading is LoadLibrary failure as shown below.

0:000> .loadby sos clr
The call to LoadLibrary(C:\Windows\Microsoft.NET\Framework\v4.0.30319\sos) failed, HRESULT 0x80004005
    "Unspecified error"
Please check your debugger configuration and/or network access.
A simlar error (The call to  LoadLibrary(C:\Windows\Microsoft.NET\Framework\v2.0.50727\sos) failed, Win32  error 0n193) can occur for .loadby sos mscorwks command for .NET 2.0 application (or prior to .NET 4.0 application).

To investigate the issue, we can check CLR.dll information since .loadby command loads sos.dll based on the clr.dll location information (or based on mscorwks.dll for prior to .NET 4.0).

0:000> lmvm clr
start             end                 module name

00000000`69180000 00000000`697ee000   clr        (deferred)             

    Image path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll    Image name: clr.dll

    Timestamp:        Sat Jul 09 02:07:57 2011 (4E181A6D)

    CheckSum:         0067171F

    ImageSize:        0066E000

    File version:     4.0.30319.239

    Product version:  4.0.30319.239

    File flags:       8 (Mask 3F) Private

    File OS:          4 Unknown Win32

    File type:        2.0 Dll

    File date:        00000000.00000000

    Translations:     0409.04b0

    CompanyName:      Microsoft Corporation

    ProductName:      Microsoft® .NET Framework

    InternalName:     clr.dll

    OriginalFilename: clr.dll

    ProductVersion:   4.0.30319.239

    FileVersion:      4.0.30319.239 (RTMGDR.030319-2300)

    PrivateBuild:     DDBLD234

    FileDescription:  Microsoft .NET Runtime Common Language Runtime - WorkStation

    LegalCopyright:   © Microsoft Corporation.  All rights reserved.

    Comments:         Flavor=Retail

The lmvm clr command shows that clr.dll is located in 32bit Framework directory. If the clr.dll is located in Framework64 directory (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll), it means clr.dll is loaded from 64bit .NET  Framework.
Since the CLR is loaded from 32bit .NET Framework, SOS.dll will be loaded from 32bit Framework which is why the error message said LoadLibrary(..\Framework\sos).

Now why is LoadLibrary() for 32bit SOS.dll failing, even if the sos.dll is there in the directory? The thing is SOS does not support cross-platform debugging. That is, 64bit debugger can only use 64bit SOS, while 32bit debugger only uses 32bit SOS. So for 32bit .NET application debugging, one has to use 32bit debugger and same goes for 64bit.

So original error occurred when using 64bit Windbg against 32bit .NET application dump. When 32 bit Windbg was used, the problem was gone.