public static void Initialize() { string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG MessageBox.Show("The injection.dll is only designed for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) throw new FileNotFoundException( string.Format( "Failed to load [{0}]", path) ); IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) throw new MethodAccessException("Failed to locate UpdateILCodes function!"); _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetStatus"); if (ptr == IntPtr.Zero) throw new MethodAccessException("Failed to locate GetStatus function!"); _getStatusDelegate = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetStatusDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); }
public static void Initialize(bool showLoggingUI = false) { //if (showLoggingUI) //{ // _loggingForm = new LoggingForm(); // _loggingForm.Show(); //} string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG Log("The injection.dll is only designed for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { path = path.Replace("\\bin", "\\bin\\x86"); // try x86 _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { throw new FileNotFoundException(string.Format("Failed to load [{0}]", path)); } } IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate UpdateILCodes function!"); } _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetStatus"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate GetStatus function!"); } _getStatusDelegate = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetStatusDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); } _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); }
private static void InternalInitialize() { string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); currentDir = Regex.Replace(currentDir, @"^(file\:\\)", string.Empty); // Environment.Is64BitProcess string path = Path.Combine(currentDir, (IntPtr.Size == 8) ? "Injection64.dll" : "Injection32.dll"); #if DEBUG Debug.WriteLine("The injection.dll is only designed for .Net release mode process. it is not supposed to be used for debug mode."); #endif _moduleHandle = LoadLibraryW(path); if (_moduleHandle == IntPtr.Zero) { var err = GetLastError(); Console.WriteLine(err); throw new FileNotFoundException(string.Format("Failed to load [{0}]", path)); } IntPtr ptr = GetProcAddress(_moduleHandle, "UpdateILCodes"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate UpdateILCodes function!"); } _updateILCodesMethod = (UpdateILCodesDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(UpdateILCodesDelegate)); ptr = GetProcAddress(_moduleHandle, "GetStatus"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate GetStatus function!"); } _getStatusDelegate = (GetStatusDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(GetStatusDelegate)); ptr = GetProcAddress(_moduleHandle, "WaitForIntializationCompletion"); if (ptr == IntPtr.Zero) { throw new MethodAccessException("Failed to locate WaitForIntializationCompletion function!"); } _waitForIntializationCompletionDelegate = (WaitForIntializationCompletionDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(WaitForIntializationCompletionDelegate)); }
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); }
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); }