// Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (_disposed) { return; } // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. _dllhandle = IntPtr.Zero; _open = null; _close = null; _start = null; _reset = null; _config = null; _read = null; _write = null; _checkTx = null; _getDeviceList = null; } // Dispose native ( unmanaged ) resources, if exits // Note disposing has been done. _disposed = true; }
/// <summary> /// Подпрограмма открытия платы /// </summary> /// <param name="serialNumber"></param> public int Open(ushort serialNumber) { // Выделение памяти в неуправляемой памяти процесса // var pnt = Marshal.AllocHGlobal( Marshal.SizeOf( zzz ) ); // Копирование структуры zzz в неуправляемую память // Marshal.StructureToPtr( zzz, pnt, false ); // Возврат структуры из неупраяляемой памяти // var zzz1 = ( pci429_4_tag) Marshal.PtrToStructure( pnt, typeof( pci429_4_tag ) ); // Освобождение неуправляемой памяти // Marshal.FreeHGlobal( pnt ); var error = 0; // Synchronously wait to enter the Semaphore. SemaphoreSlim.Wait(); // Get handle to .dll file _dllhandle = NativeMethods.LoadLibrary(NameDll); if (_dllhandle == IntPtr.Zero) { // Handle error loading error = 1; } else { // Get handle to Open method in .dll file var open_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_open"); if (open_handle == IntPtr.Zero) { error = 2; } else { _open = ( OpenDelegate )Marshal.GetDelegateForFunctionPointer( open_handle, typeof(OpenDelegate)); if (_open == null) { error = 3; } } // Get handle to Close method in .dll file var close_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_close"); if (close_handle == IntPtr.Zero) { error = 4; } else { _close = ( CloseDelegate )Marshal.GetDelegateForFunctionPointer(close_handle, typeof(CloseDelegate)); if (_close == null) { error = 5; } } // Get handle to Start method in .dll file var start_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_start"); if (start_handle == IntPtr.Zero) { error = 6; } else { _start = ( StartDelegate )Marshal.GetDelegateForFunctionPointer(start_handle, typeof(StartDelegate)); if (_start == null) { error = 7; } } // Get handle to Reset method in .dll file var reset_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_reset"); if (reset_handle == IntPtr.Zero) { error = 8; } else { _reset = ( ResetDelegate )Marshal.GetDelegateForFunctionPointer(reset_handle, typeof(ResetDelegate)); if (_reset == null) { error = 9; } } // Get handle to Config method in .dll file var config_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_set_freq"); if (config_handle == IntPtr.Zero) { error = 10; } else { _config = ( ConfigDelegate )Marshal.GetDelegateForFunctionPointer(config_handle, typeof(ConfigDelegate)); if (_config == null) { error = 11; } } // Get handle to Read method in .dll file var read_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_read_rx"); if (read_handle == IntPtr.Zero) { error = 12; } else { _read = ( ReadDelegate )Marshal.GetDelegateForFunctionPointer(read_handle, typeof(ReadDelegate)); if (_read == null) { error = 13; } } // Get handle to Write method in .dll file var write_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_write_tx"); if (write_handle == IntPtr.Zero) { error = 14; } else { _write = ( WriteDelegate )Marshal.GetDelegateForFunctionPointer(write_handle, typeof(WriteDelegate)); if (_write == null) { error = 15; } } // Get handle to CheckTx method in .dll file var check_tx_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_check_tx"); if (check_tx_handle == IntPtr.Zero) { error = 16; } else { _checkTx = ( CheckTxDelegate )Marshal.GetDelegateForFunctionPointer(check_tx_handle, typeof(CheckTxDelegate)); if (_checkTx == null) { error = 17; } } // Get handle to GetDevice method in .dll file var get_device_handle = NativeMethods.GetProcAddress(_dllhandle, "pci429_4_get_dev_list"); if (get_device_handle == IntPtr.Zero) { error = 18; } else { _getDeviceList = ( GetDeviceDelegate )Marshal.GetDelegateForFunctionPointer(get_device_handle, typeof(GetDeviceDelegate)); if (_getDeviceList == null) { error = 19; } } // Открытие устройства error += _open?.Invoke(serialNumber, out _device) ?? int.MaxValue; if (_device.handle == ( IntPtr )0) { error = 18; } } // Release the Semaphore. SemaphoreSlim.Release(); return(error); }