public async Task <int> SendOutputReportAsync(Win32HidOutputReport report, CancellationToken cancellationToken = default(CancellationToken)) { if (report == null) { throw new ArgumentNullException(nameof(report)); } var outputBuffer = report.GetOutputBuffer(); log.Trace("Sending output report:\r\n\r\n " + outputBuffer.ToHexString()); log.Trace(outputBuffer.ToLoggableAsHex("Sending output report:")); try { return(await Kernel32Dll.WriteFileAsync(Handle, outputBuffer, cancellationToken)); } catch (Win32Exception exception) { log.Error("Sending output report failed", exception); throw ExceptionConversion.ConvertException(exception); } catch (Exception exception) { log.Error("Sending output report failed", exception); throw; } }
/// <summary> /// Write Data to the INI File /// </summary> /// <param name="Section"></param> /// Section name /// <param name="Key"></param> /// Key Name /// <param name="Value"></param> /// Value Name public void WriteString(string Section, string Key, string Value) { Value = Value.Replace("\r", "\\0x0d"); Value = Value.Replace("\n", "\\0x0a"); Value = Value.Replace("\"", "\\\""); Kernel32Dll.WritePrivateProfileStringW(Section, Key, Value, path); }
/// <summary> /// Finds the Trillian installation path and initializes the list of available profiles. /// </summary> public TrillianProfileManager() { string trillianDir = GetTrillianDirectory(); if (trillianDir == null) { return; } string globalDir = GetGlobalDirectory(trillianDir); if (globalDir != null) { string iniPath = Path.Combine(globalDir, "profiles.ini"); var profileCount = (int)Kernel32Dll.GetPrivateProfileIntW("Profiles", "num", 0, iniPath); for (int i = 0; i < profileCount; i++) { string section = "Profile" + i.ToString("D3"); string sName = Kernel32Dll.Helpers.GetProfileString(iniPath, section, "Name", ""); if (sName.Length > 0) { var prefType = (int)Kernel32Dll.GetPrivateProfileIntW(section, "Preferences Type", 0, iniPath); var profile = new TrillianProfile(trillianDir, sName, prefType); if (profile.IsValid()) { _profiles.Add(profile); } } } } }
public NativeDll(string path) { _dllHandle = Kernel32Dll.LoadLibrary(path); if (_dllHandle == IntPtr.Zero) { throw new InteropException("LoadLibrary failed to load native.dll"); } IntPtr procAddress = IntPtr.Zero; if ((procAddress = Kernel32Dll.GetProcAddress(_dllHandle, "SetupSubclass")) == IntPtr.Zero) { throw new InteropException("SetupSubclass function is not present in native.dll"); } _setupSubclassFunc = (BoolActionOnIntPtr)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(BoolActionOnIntPtr)); if ((procAddress = Kernel32Dll.GetProcAddress(_dllHandle, "TearDownSubclass")) == IntPtr.Zero) { throw new InteropException("TearDownSubclass function is not present in native.dll"); } _tearDownFunc = (BoolAction)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(BoolAction)); if ((procAddress = Kernel32Dll.GetProcAddress(_dllHandle, "IsSubclassed")) == IntPtr.Zero) { throw new InteropException("TearDownSubclass function is not present in native.dll"); } _isSubclassedFunc = (BoolAction)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(BoolAction)); }
private int GetIdleTicks() { WindowsAPI.GetLastInputInfo(ref _LASTINPUTINFO); int idleTicks = (int)(Kernel32Dll.GetTickCount() - _LASTINPUTINFO.dwTime); return(idleTicks); }
protected virtual void Dispose(bool isDiposing) { if (_dllHandle != IntPtr.Zero) { Kernel32Dll.FreeLibrary(_dllHandle); _dllHandle = IntPtr.Zero; } }
/// <summary> /// A kick for the MILCORE to query for the display settings again, this time with our improved data. /// </summary> private static void Execute_InvalidateMil() { // Send an update to all the top-level windows on our thread Win32Declarations.EnumWindows(delegate(IntPtr hWnd, IntPtr lParam) { int dwDummy; if (Win32Declarations.GetWindowThreadProcessId(hWnd, out dwDummy) == Kernel32Dll.GetCurrentThreadId()) { User32Dll.Helpers.SendMessageW(hWnd, WindowsMessages.WM_SETTINGCHANGE, (IntPtr)Win32Declarations.SPI_SETFONTSMOOTHING, IntPtr.Zero); User32Dll.Helpers.SendMessageW(hWnd, WindowsMessages.WM_SETTINGCHANGE, (IntPtr)Win32Declarations.SPI_SETFONTSMOOTHINGTYPE, IntPtr.Zero); } return(true); // Go on }, 0); }
static unsafe void Test(Win32HidDevice device) { var init = new InitializationPacket(); init.CommandIdentifier = (byte)U2FHidCommand.Init; init.ChannelIdentifier = U2FHID_BROADCAST_CID; init.PayloadLength = 8; var caps = device.Information.Capabilities; var buffer = new byte[caps.InputReportByteLength]; fixed(byte *pBuffer = buffer) { Marshal.StructureToPtr(init, new IntPtr(pBuffer + 1), false); buffer[0] = 0x00; buffer[8] = 0xCA; buffer[9] = 0xFE; buffer[10] = 0xBA; buffer[11] = 0xBE; buffer[12] = 0xDE; buffer[13] = 0xAD; buffer[14] = 0xBA; buffer[15] = 0xBE; WriteBuffer(buffer); var task = Kernel32Dll.WriteFileAsync(device.Handle, new IntPtr(pBuffer), buffer.Length); var writen = task.Result; Console.WriteLine("Writen {0} bytes", writen); } var bufferOut = new byte[caps.OutputReportByteLength]; fixed(byte *pBuffer = bufferOut) { var intPtr = new IntPtr(pBuffer); var task = Kernel32Dll.ReadFileAsync(device.Handle, intPtr, bufferOut.Length); var read = task.Result; Console.WriteLine("Read {0} bytes", read); } WriteBuffer(bufferOut); Wink(device, bufferOut[16], bufferOut[17], bufferOut[18], bufferOut[19]); }
public async Task <HidInputReport> GetInputReportAsync( CancellationToken cancellationToken = default(CancellationToken)) { try { var bytes = await Kernel32Dll.ReadFileAsync <byte>(Handle, Information.Capabilities.InputReportByteLength, cancellationToken); log.Trace(bytes.ToLoggableAsHex("Received input report:")); return(new HidInputReport(bytes)); } catch (Win32Exception exception) { log.Error("Receiving input report failed", exception); throw ExceptionConversion.ConvertException(exception); } catch (Exception exception) { log.Error("Receiving input report failed", exception); throw; } }
static unsafe void Wink(Win32HidDevice device, byte b1, byte b2, byte b3, byte b4) { var msg = new FidoU2FHidMessage( (uint)(unchecked (b1 << 24 | b2 << 16 | b3 << 8 | b4)), U2FHidCommand.Wink); device.WriteFidoU2FHidMessageAsync(msg, CancellationToken.None).Wait(); var caps = device.Information.Capabilities; var bufferOut = new byte[caps.OutputReportByteLength]; fixed(byte *pBuffer = bufferOut) { var intPtr = new IntPtr(pBuffer); var task = Kernel32Dll.ReadFileAsync(device.Handle, intPtr, bufferOut.Length); var read = task.Result; Console.WriteLine("Read {0} bytes", read); } WriteBuffer(bufferOut); }
public int CloseHandle(IntPtr handle) { return(Kernel32Dll.CloseHandle(handle)); }
public bool FreeMemory(int address, int memorySize) { return(Kernel32Dll.VirtualFreeEx(_processHandle, (IntPtr)address, memorySize, FreeType.Release)); }
public void WaitForSingleObject(IntPtr threadHandle) { Kernel32Dll.WaitForSingleObject(threadHandle, Infinite); }
public IntPtr CreateRemoteThread(int address) { return(Kernel32Dll.CreateRemoteThread(_processHandle, (IntPtr)0, 0, (IntPtr)address, (IntPtr)0, 0, (IntPtr)0)); }
public int AllocateMemory(int memorySize) { return((int)Kernel32Dll.VirtualAllocEx(_processHandle, (IntPtr)0, (uint)memorySize, 0x1000, 0x40)); }
private bool ReadProcessMemory(IntPtr lpBaseAddress, [In, Out] byte[] lpBuffer, UInt32 nSize) { uint lpNumberOfBytesRead = 0; return(Kernel32Dll.ReadProcessMemory(_processHandle, lpBaseAddress, lpBuffer, nSize, ref lpNumberOfBytesRead)); }
private bool WriteProcessMemory(IntPtr lpBaseAddress, byte[] lpBuffer, UInt32 nSize) { uint lpNumberOfBytesWritten = 0; return(Kernel32Dll.WriteProcessMemory(_processHandle, lpBaseAddress, lpBuffer, nSize, ref lpNumberOfBytesWritten)); }
public static bool IsCoreApiDllAlreadyLoaded() { return(Kernel32Dll.GetModuleHandleW(CoreApiDll.LibraryName) != IntPtr.Zero); }