public static void UseLibrary(string fileName) { ExtIO._dllName = fileName; if (ExtIO._handles.ContainsKey(ExtIO._dllName)) { ExtIO._dllHandle = ExtIO._handles[ExtIO._dllName]; } else { ExtIO._dllHandle = ExtIO.LoadLibrary(ExtIO._dllName); ExtIO._handles[ExtIO._dllName] = ExtIO._dllHandle; } if (ExtIO._dllHandle == IntPtr.Zero) throw new Exception("Unable to load ExtIO library"); ExtIO._initHW = (ExtIO.InitHWDelegate) null; ExtIO._openHW = (ExtIO.OpenHWDelegate) null; ExtIO._startHW = (ExtIO.StartHWDelegate) null; ExtIO._stopHW = (ExtIO.StopHWDelegate) null; ExtIO._closeHW = (ExtIO.CloseHWDelegate) null; ExtIO._setHWLO = (ExtIO.SetHWLODelegate) null; ExtIO._getHWLO = (ExtIO.GetHWLODelegate) null; ExtIO._getHWSR = (ExtIO.GetHWSRDelegate) null; ExtIO._getStatus = (ExtIO.GetStatusDelegate) null; ExtIO._showGUI = (ExtIO.ShowGUIDelegate) null; ExtIO._hideGUI = (ExtIO.HideGUIDelegate) null; ExtIO._setCallback = (ExtIO.SetCallbackDelegate) null; IntPtr procAddress1 = ExtIO.GetProcAddress(ExtIO._dllHandle, "InitHW"); if (procAddress1 != IntPtr.Zero) ExtIO._initHW = (ExtIO.InitHWDelegate) Marshal.GetDelegateForFunctionPointer(procAddress1, typeof (ExtIO.InitHWDelegate)); IntPtr procAddress2 = ExtIO.GetProcAddress(ExtIO._dllHandle, "OpenHW"); if (procAddress2 != IntPtr.Zero) ExtIO._openHW = (ExtIO.OpenHWDelegate) Marshal.GetDelegateForFunctionPointer(procAddress2, typeof (ExtIO.OpenHWDelegate)); IntPtr procAddress3 = ExtIO.GetProcAddress(ExtIO._dllHandle, "StartHW"); if (procAddress3 != IntPtr.Zero) ExtIO._startHW = (ExtIO.StartHWDelegate) Marshal.GetDelegateForFunctionPointer(procAddress3, typeof (ExtIO.StartHWDelegate)); IntPtr procAddress4 = ExtIO.GetProcAddress(ExtIO._dllHandle, "StopHW"); if (procAddress4 != IntPtr.Zero) ExtIO._stopHW = (ExtIO.StopHWDelegate) Marshal.GetDelegateForFunctionPointer(procAddress4, typeof (ExtIO.StopHWDelegate)); IntPtr procAddress5 = ExtIO.GetProcAddress(ExtIO._dllHandle, "CloseHW"); if (procAddress5 != IntPtr.Zero) ExtIO._closeHW = (ExtIO.CloseHWDelegate) Marshal.GetDelegateForFunctionPointer(procAddress5, typeof (ExtIO.CloseHWDelegate)); IntPtr procAddress6 = ExtIO.GetProcAddress(ExtIO._dllHandle, "SetCallback"); if (procAddress6 != IntPtr.Zero) ExtIO._setCallback = (ExtIO.SetCallbackDelegate) Marshal.GetDelegateForFunctionPointer(procAddress6, typeof (ExtIO.SetCallbackDelegate)); IntPtr procAddress7 = ExtIO.GetProcAddress(ExtIO._dllHandle, "SetHWLO"); if (procAddress7 != IntPtr.Zero) ExtIO._setHWLO = (ExtIO.SetHWLODelegate) Marshal.GetDelegateForFunctionPointer(procAddress7, typeof (ExtIO.SetHWLODelegate)); IntPtr procAddress8 = ExtIO.GetProcAddress(ExtIO._dllHandle, "GetHWLO"); if (procAddress8 != IntPtr.Zero) ExtIO._getHWLO = (ExtIO.GetHWLODelegate) Marshal.GetDelegateForFunctionPointer(procAddress8, typeof (ExtIO.GetHWLODelegate)); IntPtr procAddress9 = ExtIO.GetProcAddress(ExtIO._dllHandle, "GetHWSR"); if (procAddress9 != IntPtr.Zero) ExtIO._getHWSR = (ExtIO.GetHWSRDelegate) Marshal.GetDelegateForFunctionPointer(procAddress9, typeof (ExtIO.GetHWSRDelegate)); IntPtr procAddress10 = ExtIO.GetProcAddress(ExtIO._dllHandle, "GetStatus"); if (procAddress10 != IntPtr.Zero) ExtIO._getStatus = (ExtIO.GetStatusDelegate) Marshal.GetDelegateForFunctionPointer(procAddress10, typeof (ExtIO.GetStatusDelegate)); IntPtr procAddress11 = ExtIO.GetProcAddress(ExtIO._dllHandle, "ShowGUI"); if (procAddress11 != IntPtr.Zero) ExtIO._showGUI = (ExtIO.ShowGUIDelegate) Marshal.GetDelegateForFunctionPointer(procAddress11, typeof (ExtIO.ShowGUIDelegate)); IntPtr procAddress12 = ExtIO.GetProcAddress(ExtIO._dllHandle, "HideGUI"); if (procAddress12 != IntPtr.Zero) ExtIO._hideGUI = (ExtIO.HideGUIDelegate) Marshal.GetDelegateForFunctionPointer(procAddress12, typeof (ExtIO.HideGUIDelegate)); if (ExtIO._initHW == null || ExtIO._openHW == null || (ExtIO._startHW == null || ExtIO._setHWLO == null) || (ExtIO._getStatus == null || ExtIO._setCallback == null || (ExtIO._stopHW == null || ExtIO._closeHW == null))) { ExtIO.FreeLibrary(ExtIO._dllHandle); ExtIO._handles[ExtIO._dllName] = IntPtr.Zero; ExtIO._dllHandle = IntPtr.Zero; throw new ApplicationException("ExtIO DLL is not valid"); } if (!ExtIO._names.ContainsKey(ExtIO._dllName) || !ExtIO._models.ContainsKey(ExtIO._dllName) || !ExtIO._hwTypes.ContainsKey(ExtIO._dllName)) return; ExtIO._name = ExtIO._names[ExtIO._dllName]; ExtIO._model = ExtIO._models[ExtIO._dllName]; ExtIO._hwType = ExtIO._hwTypes[ExtIO._dllName]; }
public static void PerformExtIOInitHW() { if (ExtIO._dllHandle == IntPtr.Zero) throw new ApplicationException("ExtIO not loaded"); StringBuilder name = new StringBuilder(256); StringBuilder model = new StringBuilder(256); int type; int num = ExtIO._initHW(name, model, out type); ExtIO._name = name.ToString(); ExtIO._model = model.ToString(); if (num < 1) { ExtIO._stopHW(); ExtIO._closeHW(); ExtIO.FreeLibrary(ExtIO._dllHandle); ExtIO._dllHandle = IntPtr.Zero; ExtIO._handles[ExtIO._dllName] = IntPtr.Zero; throw new ApplicationException("InitHW() returned " + (object) num); } ExtIO._hwType = (ExtIO.HWTypes) type; ExtIO._names[ExtIO._dllName] = ExtIO._name; ExtIO._models[ExtIO._dllName] = ExtIO._model; ExtIO._hwTypes[ExtIO._dllName] = ExtIO._hwType; ExtIO._setCallback(ExtIO._callbackInst); }