Example #1
0
        public static void UseLibrary(string fileName)
        {
            _dllName = fileName;

            if (_handles.ContainsKey(_dllName))
            {
                _dllHandle = _handles[_dllName];
            }
            else
            {
                _dllHandle = LoadLibrary(_dllName);
            }

            if (_dllHandle == IntPtr.Zero)
            {
                throw new Exception("Unable to load ExtIO library");
            }

            _initHW      = null;
            _openHW      = null;
            _startHW     = null;
            _stopHW      = null;
            _closeHW     = null;
            _setHWLO     = null;
            _getHWLO     = null;
            _getHWSR     = null;
            _getStatus   = null;
            _showGUI     = null;
            _hideGUI     = null;
            _setCallback = null;

            IntPtr pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "InitHW");

            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _initHW = (InitHWDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(InitHWDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "OpenHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _openHW = (OpenHWDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(OpenHWDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "StartHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _startHW = (StartHWDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(StartHWDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "StopHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _stopHW = (StopHWDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(StopHWDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "CloseHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _closeHW = (CloseHWDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CloseHWDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "SetCallback");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _setCallback = (SetCallbackDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(SetCallbackDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "SetHWLO");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _setHWLO = (SetHWLODelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(SetHWLODelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "GetHWLO");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _getHWLO = (GetHWLODelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetHWLODelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "GetHWSR");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _getHWSR = (GetHWSRDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetHWSRDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "GetStatus");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _getStatus = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetStatusDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "ShowGUI");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _showGUI = (ShowGUIDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(ShowGUIDelegate));
            }

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "HideGUI");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
            {
                _hideGUI = (HideGUIDelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(HideGUIDelegate));
            }

            if (_initHW == null || _openHW == null || _startHW == null || _setHWLO == null ||
                _getStatus == null || _setCallback == null || _stopHW == null || _closeHW == null)
            {
                //FreeLibrary(_dllHandle);
                _dllHandle = IntPtr.Zero;
                throw new ApplicationException("ExtIO DLL is not valid");
            }

            var name  = new StringBuilder(256);
            var model = new StringBuilder(256);
            int type;

            var result = _initHW(name, model, out type);

            _name  = name.ToString();
            _model = model.ToString();

            if (result < 1)
            {
                //FreeLibrary(_dllHandle);
                _dllHandle = IntPtr.Zero;
                throw new ApplicationException("InitHW() returned " + result);
            }

            _hwType = (HWTypes)type;

            /* Give the library the managed callback address */
            _setCallback(_callbackInst);
        }
Example #2
0
        public static void UseLibrary(string fileName)
        {
            _dllName = fileName;

            if (_handles.ContainsKey(_dllName))
            {
                _dllHandle = _handles[_dllName];
            }
            else
            {
                _dllHandle = LoadLibrary(_dllName);
            }

            if (_dllHandle == IntPtr.Zero)
                throw new Exception("Unable to load ExtIO library");

            _initHW = null;
            _openHW = null;
            _startHW = null;
            _stopHW = null;
            _closeHW = null;
            _setHWLO = null;
            _getHWLO = null;
            _getHWSR = null;
            _getStatus = null;
            _showGUI = null;
            _hideGUI = null;
            _setCallback = null;

            IntPtr pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "InitHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _initHW = (InitHWDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(InitHWDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "OpenHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _openHW = (OpenHWDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(OpenHWDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "StartHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _startHW = (StartHWDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(StartHWDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "StopHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _stopHW = (StopHWDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(StopHWDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "CloseHW");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _closeHW = (CloseHWDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CloseHWDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "SetCallback");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _setCallback = (SetCallbackDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(SetCallbackDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "SetHWLO");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _setHWLO = (SetHWLODelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(SetHWLODelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "GetHWLO");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _getHWLO = (GetHWLODelegate)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetHWLODelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "GetHWSR");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _getHWSR = (GetHWSRDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetHWSRDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "GetStatus");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _getStatus = (GetStatusDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetStatusDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "ShowGUI");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _showGUI = (ShowGUIDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(ShowGUIDelegate));

            pAddressOfFunctionToCall = GetProcAddress(_dllHandle, "HideGUI");
            if (pAddressOfFunctionToCall != IntPtr.Zero)
                _hideGUI = (HideGUIDelegate) Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(HideGUIDelegate));
            
            if (_initHW == null || _openHW == null || _startHW == null || _setHWLO == null ||
               _getStatus == null || _setCallback == null || _stopHW == null || _closeHW == null)
            {
                //FreeLibrary(_dllHandle);
                _dllHandle = IntPtr.Zero;
                throw new ApplicationException("ExtIO DLL is not valid");
            }
            
            var name = new StringBuilder(256);
            var model = new StringBuilder(256);
            int type;

            var result = _initHW(name, model, out type);

            _name = name.ToString();
            _model = model.ToString();

            if (result < 1)
            {
                //FreeLibrary(_dllHandle);
                _dllHandle = IntPtr.Zero;
                throw new ApplicationException("InitHW() returned " + result);
            }
            
            _hwType = (HWTypes) type;

            /* Give the library the managed callback address */
            _setCallback(_callbackInst);
        }