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 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(); } } } } }