开发者

Check for native library in .NET managed code

开发者 https://www.devze.com 2023-01-04 14:37 出处:网络
I have a .NET application written in C#. My application uses 3rd party libraries, which use 3rd party libraries, which in turn rely on the existence of sqlceme35.dll (Microsoft SSCE). Somewhere along

I have a .NET application written in C#. My application uses 3rd party libraries, which use 3rd party libraries, which in turn rely on the existence of sqlceme35.dll (Microsoft SSCE). Somewhere along the line the dependency on sqlceme35.dll is not being accounted for, and we've had a number of situations where my software has been installed on a computer without this library, the application appears to run fine for most functions, but crashes in a spectacular way with cryptic error messages when we get to the point when we try to call into sqlceme35.dll.

Even though we now know what the effects l开发者_StackOverflow社区ook like when the library isn't present, I would still like to be more proactive about detecting when the library is not available and giving the user a friendly error message "here is the problem, here is the solution".

The immediate question is: How do I detect, at runtime, the presence of the sqlceme35.dll library?

The larger question is: How do I detect, at runtime, the presence of any arbitrary .dll file, whether it be a native code or a managed code library?


You could using PInvoke to use the LoadLibrary function, which should search the same locations that Windows would.


We embed the unmanaged dlls into an assembly and then copy them out to the executing dll location (for web apps the shadow copy bin folder). This guarantees that the correct version is being run for a particular version of our app. Unfortunately this may not be doable (legal) depending on licensing terms for your various applications. In these cases, your best option is likely to use LoadLibrary to verify that the library is found, but beware of loading the wrong version (see also: DLL hell); this may or may not be a problem you can even solve (for us, the only solution was embedding the dlls and extracting them from the assemblies where needed).

Here is our code in question for the Sybase ASE ADO drivers:

public static class SybaseResourceExtractor {
    public static void ExtractSybaseDependencies() {
        ExtractSybaseDependency("QueryLibrary.Unmanaged.sybdrvado20.dll", "sybdrvado20.dll");
        ExtractSybaseDependency("QueryLibrary.Unmanaged.msvcr80.dll", "msvcr80.dll");
        ExtractSybaseDependency("QueryLibrary.Unmanaged.sybcsi_certicom_fips26.dll", "sybcsi_certicom_fips26.dll");
        ExtractSybaseDependency("QueryLibrary.Unmanaged.sybcsi_core26.dll", "sybcsi_core26.dll");
        ExtractSybaseDependency("QueryLibrary.Unmanaged.sbgse2.dll", "sbgse2.dll");
    }

    /// <summary>
    /// Extracts a resource to a file.
    /// </summary>
    /// <param name="resourceName">Name of the resource.</param>
    /// <param name="filename">The filename including absolute path.</param>
    static void ExtractSybaseDependency(string resourceName, string filename) {
        try {
            var assembly = Assembly.GetAssembly(typeof(AseConnection));
            var executingAssembly = Assembly.GetExecutingAssembly();
            filename = Path.GetDirectoryName(assembly.Location) + "\\" + filename;

            if (File.Exists(filename)) {
                File.Delete(filename);    
            }

            if (!File.Exists(filename)) {
                using (Stream s = executingAssembly.GetManifestResourceStream(resourceName)) {
                    using (var fs = new FileStream(filename, FileMode.Create)) {
                        if (s == null) {
                            throw new Exception("Failed to get resource stream for " + resourceName);
                        }

                        var b = new byte[s.Length];
                        s.Read(b, 0, b.Length);
                        fs.Write(b, 0, b.Length);
                    }
                }
            }
        } catch {
            //Doing nothing
        }
    }


You can check all the locations that the dll may be present.

http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消