예제 #1
0
 public static void HWInit(bool setCallback)
 {
     if (!(ExtIO._dllHandle == IntPtr.Zero) && !ExtIO._isHWInit && !ExtIO._isHWStarted)
     {
         ExtIO.logInfo("HWInit()");
         StringBuilder stringBuilder  = new StringBuilder(256);
         StringBuilder stringBuilder2 = new StringBuilder(256);
         int           hwType         = 0;
         try
         {
             ExtIO._isHWInit = ExtIO._initHW(stringBuilder, stringBuilder2, ref hwType);
         }
         catch (Exception ex)
         {
             ExtIO.logInfo("InitHW: " + ex.Message);
         }
         ExtIO.logResult("InitHW: ");
         ExtIO._name   = stringBuilder.ToString();
         ExtIO._model  = stringBuilder2.ToString();
         ExtIO._hwType = (HWTypes)hwType;
         if (!ExtIO._isHWInit)
         {
             ExtIO.logInfo("InitHW() returned " + ExtIO._isHWInit + ", ");
             ExtIO._isHWInit = true;
             ExtIO.CloseHW();
             throw new ApplicationException("InitHW() returned " + ExtIO._isHWInit);
         }
         ExtIO.logInfo("InitHW: " + ExtIO._name + ", " + ExtIO._model + ", type=" + hwType.ToString());
         if (setCallback)
         {
             ExtIO.logInfo("SetCallback: ");
             try
             {
                 ExtIO._setCallback(ExtIO._callbackInst);
             }
             catch (Exception ex2)
             {
                 ExtIO.logInfo("SetCallback: " + ex2.Message);
             }
         }
     }
 }
예제 #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);
        }
예제 #3
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);
        }