How to determine if a DLL is installed / available

What's the most efficient way to determine if a DLL is installed in C #? I got caught up in this: msvcr80.dll. I tried to call the LoadLibrary interop API from a C # program, but it didn't work. I just need to detect it and not use it.

+3


source to share


2 answers


The msvcr80.dll call is difficult. This is a special DLL, it is stored in the Windows cache nearby (c: \ windows \ winsxs) and there are usually multiple versions installed on the machine. I have 16 of them on this machine. Side-by-side cache is the GAC equivalent for unmanaged DLLs. Using such a DLL in your code requires a manifest that specifies which specific version of msvcr80.dll you want to use.

So the first thing you need to do is add the entry to your own program manifest. Project + Add New Item, Select Application Manifest Item Template. You will need to edit it to include the msvcr80.dll dependency. Your manifest should look like this:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="asInvoker" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity type="win32" name="Microsoft.VC80.CRT" version="8.0.50727.6195" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
        </dependentAssembly>
    </dependency>
</asmv1:assembly>

      

Note that the version attribute 8.0.50727.6195

is the last one. Other common versions in the wild are 42 (original RTM version) and 762 (SP1 version) and many security updates. Typically you can use .42 in your manifest, publisher policy files deployed by updates are redirected to the latest installed version on the computer.

Also note the attribute processorArchitecture

, you will need "amd64" if you want to use the 64-bit version of the DLL. Your best bet is to stick to x86 and ensure your program runs in 32-bit mode. Right click the EXE project, Properties tab, New, Platform target = x86.



Now you can use [DllImport] to call a function from a DLL. How:

    [DllImport("msvcr80.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern IntPtr _errno();

      

I have deliberately chosen an innocent function that does nothing useful. So that you can output it to check if the DLL is installed with the required version number. Catch the exception so you know it's not there. If you want to do this without exception then pinvoke LoadLibrary ().

I'm going to assume that you didn't quite count on these complications. A completely different approach is to use the equivalent DLL used by the Windows code. This is fine if you are not trying to pump out "fancy" functions. Every Windows installation has msvcrt.dll, there is no need to check it for the presence and absence of a manifest. Just change the DLL name in the [DllImport] attribute. But beware that you run the risk that your program might break someday when Microsoft changes this personal copy of the CRT significantly.

+4


source


You need to check both the directories under the environment variable PATH

and the global assembly cache, which is best done with this API .



+1


source







All Articles