Example #1
0
        static QuickLZ()
        {
            //This is a check to see if we're running in linux
            if (System.Environment.OSVersion.Platform == PlatformID.MacOSX ||
                System.Environment.OSVersion.Platform == PlatformID.Unix ||
                (int)System.Environment.OSVersion.Platform == 128)
            {
                Available = false;
                return;
            }

            try
            {

                if (!Directory.Exists(DllDir) || !File.Exists(dllFullName))
                {
                    if (!Directory.Exists(DllDir))
                        Directory.CreateDirectory(DllDir);

                    if (!File.Exists(dllFullName))
                    {
                        using (Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(dllName))
                        {
                            using (FileStream fs = new FileStream(dllFullName, FileMode.CreateNew))
                            {
                                byte[] bytes = new byte[s.Length];
                                s.Read(bytes, 0, bytes.Length);
                                fs.Write(bytes, 0, bytes.Length);
                            }
                        }
                    }
                }

                IntPtr dllPtr = LoadLibrary(dllFullName);

                IntPtr procAddress = GetProcAddress(dllPtr, "qlz_compress");
                qlz_compress = (qlz_compress_del)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(qlz_compress_del));
                procAddress = GetProcAddress(dllPtr, "qlz_decompress");
                qlz_decompress = (qlz_decompress_del)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(qlz_decompress_del));
                procAddress = GetProcAddress(dllPtr, "qlz_size_decompressed");
                qlz_size_decompressed = (qlz_size_compressed_del)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(qlz_size_compressed_del));
                procAddress = GetProcAddress(dllPtr, "qlz_get_setting");
                qlz_get_setting = (qlz_get_settings_del)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(qlz_get_settings_del));

                AppDomain.CurrentDomain.ProcessExit += new EventHandler((o, e) =>
                {
                    try
                    {
                        if (dllPtr != IntPtr.Zero)
                            FreeLibrary(dllPtr);
                    }
                    catch (Exception) { }
                });

                Available = true;
            }
            catch (EntryPointNotFoundException)
            {
                Available = false;
            }                
            catch (Exception ex)
            {
                Available = false;
                using (StreamWriter sw = new StreamWriter("quickLZLoadError.txt", false))
                    sw.WriteLine(ex.ToString());
            }
        }