Пример #1
0
        private CSerialPortLibraryLoader(DirectoryInfo dynamicLinkLibrariesPath)
        {
            this.dynamicLinkLibrariesPath = dynamicLinkLibrariesPath;
            refCount = 1;

            var libCSerialPortDllPath = Path.Combine(dynamicLinkLibrariesPath.FullName, "libcserialport.dll");

            if (!File.Exists(libCSerialPortDllPath))
            {
                throw new FileNotFoundException();
            }
            libCSerialPortDllHandle = Win32Interops.LoadLibrary(libCSerialPortDllPath);
            if (libCSerialPortDllHandle == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            var libCSerialPortExportDllPath = Path.Combine(dynamicLinkLibrariesPath.FullName, "libcserialportexport.dll");

            if (!File.Exists(libCSerialPortExportDllPath))
            {
                throw new FileNotFoundException();
            }
            libCSerialPortExportDllHandle = Win32Interops.LoadLibrary(libCSerialPortExportDllPath);
            if (libCSerialPortExportDllHandle == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Пример #2
0
 void IDisposable.Dispose()
 {
     if (libCSerialPortDllHandle != IntPtr.Zero)
     {
         Win32Interops.FreeLibrary(libCSerialPortDllHandle);
         libCSerialPortDllHandle = IntPtr.Zero;
     }
     if (libCSerialPortExportDllHandle != IntPtr.Zero)
     {
         Win32Interops.FreeLibrary(libCSerialPortExportDllHandle);
         libCSerialPortExportDllHandle = IntPtr.Zero;
     }
 }
Пример #3
0
        internal T GetInteropDelegate <T>()
        {
            string functionName = null;

            try
            {
                var attrs = typeof(T).GetCustomAttributes(typeof(LibCSerialPortFunctionAttribute), false);
                if (attrs.Length == 0)
                {
                    throw new Exception("Could not find the LibCSerialPortFunctionAttribute.");
                }
                var attr = (LibCSerialPortFunctionAttribute)attrs[0];
                functionName = attr.FunctionName;

                lock (this.interopDelegatesLockObject)
                {
                    if (interopDelegates.ContainsKey(functionName))
                    {
                        return((T)interopDelegates[attr.FunctionName]);
                    }

                    var procAddress = Win32Interops.GetProcAddress(libCSerialPortExportDllHandle, attr.FunctionName);
                    if (procAddress == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }

                    var delegateForFunctionPointer = MarshalHelper.GetDelegateForFunctionPointer <T>(procAddress);

                    interopDelegates[attr.FunctionName] = delegateForFunctionPointer;

                    return(delegateForFunctionPointer);
                }
            }
            catch (Win32Exception e)
            {
                throw new MissingMethodException(string.Format("The address of the function '{0}' does not exist in library.", functionName), e);
            }
        }