public static void LoadFunctions(this FieldInfo[] functions, IntPtr library, object target)
        {
            foreach (var field in functions)
            {
                string functionName    = field.Name;
                IntPtr functionAddress = DLLImporter.GetProcAddress(library, functionName);

                var fieldType = field.FieldType;
                field.SetValue(target, Marshal.GetDelegateForFunctionPointer(functionAddress, fieldType));
            }
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_IsLibraryCreated == true)
            {
                DestroyLibrary();
                DLLImporter.FreeLibrary(_dllLibrary);
            }

            try
            {
                foreach (Process proc in Process.GetProcessesByName("ComCommunicator"))
                {
                    proc.Kill();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (_dllLibrary != IntPtr.Zero)
            {
                if (_IsLibraryCreated == true)
                {
                    DestroyLibrary();
                }

                DLLImporter.FreeLibrary(_dllLibrary);
                textBox2.Clear();
                textBox2.ReadOnly = false;
                button1.Enabled   = true;
                button3.Enabled   = false;
                _dllLibrary       = IntPtr.Zero;
                label8.Visible    = false;

                textBox3.Clear();
            }
            else
            {
                MessageBox.Show("No library loaded!", "Warning", MessageBoxButtons.OK);
            }
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (textBox2.TextLength > 0)
            {
                DialogResult result = System.Windows.Forms.DialogResult.Retry;
                while ((_dllLibrary == IntPtr.Zero) && (result == System.Windows.Forms.DialogResult.Retry))
                {
                    _dllLibrary = DLLImporter.LoadLibrary(textBox2.Text);
                    if (_dllLibrary != IntPtr.Zero)
                    {
                        // Load function address

                        _createMVL       = DLLImporter.GetProcAddress(_dllLibrary, "MVL_Create");
                        _destroyMVL      = DLLImporter.GetProcAddress(_dllLibrary, "MVL_Destroy");
                        _setParameterMVL = DLLImporter.GetProcAddress(_dllLibrary, "MVL_SetProperty");
                        _getParameterMVL = DLLImporter.GetProcAddress(_dllLibrary, "MVL_GetProperty");
                        _analyzeMVL      = DLLImporter.GetProcAddress(_dllLibrary, "MVL_Analyze");
                        _getLastErrorMVL = DLLImporter.GetProcAddress(_dllLibrary, "MVL_GetLastError");

                        if ((_createMVL != IntPtr.Zero) && (_destroyMVL != IntPtr.Zero) &&
                            (_setParameterMVL != IntPtr.Zero) && (_getParameterMVL != IntPtr.Zero) &&
                            (_analyzeMVL != IntPtr.Zero))
                        {
                            CreateLibrary();
                        }
                        else
                        {
                            result = MessageBox.Show("Error occurred", "Retry",
                                                     MessageBoxButtons.RetryCancel);

                            if (result == System.Windows.Forms.DialogResult.Retry)
                            {
                                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                                {
                                    textBox2.Clear();
                                    textBox2.AppendText(openFileDialog1.FileName);
                                }
                            }
                            else
                            {
                                textBox2.Clear();
                            }
                        }
                    }
                    else
                    {
                        result = MessageBox.Show("Error occurred", "Retry",
                                                 MessageBoxButtons.RetryCancel);

                        if (result == System.Windows.Forms.DialogResult.Retry)
                        {
                            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                textBox2.Clear();
                                textBox2.AppendText(openFileDialog1.FileName);
                            }
                        }
                        else
                        {
                            textBox2.Clear();
                        }
                    }
                }
            }
        }