目标:编写一个在后台运行的C#应用程序,侦听Win-V组合键,当发生这种情况时,将剪贴板内容粘贴到当前活动窗口(某个任意应用程序).基本上我正在尝试模仿PureText,但我并不打算先将文本转换为纯文本.
问题:粘贴到当前活动的窗口不起作用.
详细信息:要在后台监听按键,我正在使用A Simple C#Global Low Level Keyboard Hook中的globalKeyboardHook类.我能够捕获Win-V事件,但我无法正确发送粘贴命令.我可以使用SendKeys.Send或keybd_event函数发送粘贴.然而,他们发送另一个"V"按下管道,该管道被gkh_KeyDown事件捕获并导致多个粘贴事件被触发.
我期待我需要使用SendMessage或PostMessage,但到目前为止我所做的所有尝试都失败了.下面是最后一个函数SendCtrlV的完整代码.这些评论解释了我迄今为止所尝试的一切.你能看到我错过的东西吗?
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Utilities;
namespace KeyHookTest
{
public partial class Form1 : Form
{
private bool LWin_down;
private bool V_down;
globalKeyboardHook gkh = new globalKeyboardHook();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint …Run Code Online (Sandbox Code Playgroud) 我成功冒充用户帐户,但我无法使用模拟帐户绑定到AD并下拉DirectoryEntry.
以下代码输出:
我的问题似乎与:
如何在ASP.NET中使用System.DirectoryServices命名空间
我正在获得一个主令牌.我知道我需要使用委托在远程计算机上使用模拟令牌.我确认该帐户没有选中"帐户敏感且无法委派"的标志.我还确认本地组策略和域组策略不会阻止委派:
计算机配置\ Windows设置\安全设置\本地策略\用户权限分配\
我错过了什么?
谢谢!
using System;
using System.DirectoryServices;
using System.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Runtime.ConstrainedExecution;
namespace ADSI_Impersonation
{
class Program
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
static void Main(string[] args)
{
const int LOGON32_PROVIDER_DEFAULT = 0; …Run Code Online (Sandbox Code Playgroud) 问题:我希望以编程方式(使用 PowerShell)获取我完全没有权限的文件的所有权。
更新:我已经彻底重写了问题,以给出重现问题的步骤。这就是我正在做的:
##########################################################
# Logon as UserA
##########################################################
$file = "C:\temp\file.txt"
new-item C:\temp -type dir
new-item $file -type file
# Remove inheritence
$isProtected = $true
$preserveInheritance = $true
$FileSecurity = Get-ACL $file
$FileSecurity.SetAccessRuleProtection($isProtected, $preserveInheritance)
Set-ACL $file -AclObject $FileSecurity
# Remove authenticated users
$user = "Authenticated Users"
$permission = "Modify"
$Account = New-Object System.Security.Principal.NTAccount($user)
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]$permission
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]"None"
$AccessControlType =[System.Security.AccessControl.AccessControlType]::Allow
$FileSystemAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Account, $FileSystemRights, $InheritanceFlag, $PropagationFlag, $AccessControlType)
$FileSecurity = Get-ACL $file …Run Code Online (Sandbox Code Playgroud) PowerShell代码:
$string = @'
Line 1
Line 3
'@
$string
Run Code Online (Sandbox Code Playgroud)
输出:
Line 1
Line 3
Run Code Online (Sandbox Code Playgroud)
但我希望它输出:
Line 1
Line 3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我想通过SSH连接到服务器,使用PowerShell,然后更改为其他用户.
为了通过SSH实现与PowerShell的服务器连接,我使用SSH.NET库和如何通过SSH连接PowerShell来使用PowerShell中的SSH.这很棒,并给了我简单的命令行,如new-sshsession和invoke- sshcommand.
接下来,我想su给不同的用户.当我尝试通过命令行开关执行此操作时,我收到错误:标准输入必须是tty.
据我所知,我可以编辑服务器上的安全设置,让我在没有处于交互模式的情况下使用su.但是,我无法在我想要连接的所有服务器上更改该设置.
使用我在https://sshnet.codeplex.com/discussions/285853上找到的C#示例,我把它放在一起:
$server = "server1"
$port = 22
$username = "user1"
$password = "password1"
$ssh = new-object Renci.SshNet.SshClient($server, $port, $username, $password)
$ssh.Connect()
$inputstream = new-object System.IO.MemoryStream
$streamwriter = new-object System.IO.StreamWriter($inputstream)
$outputstream = new-object System.IO.MemoryStream
$streamreader = new-object System.IO.StreamReader($outputstream)
$shell = $ssh.CreateShell($inputstream, $outputstream, $outputstream)
$shell.Start()
$streamwriter.WriteLine("echo hello > tempfile.txt")
$streamwriter.Flush()
$streamreader.ReadToEnd()
$shell.Stop()
Run Code Online (Sandbox Code Playgroud)
运行,我得到输出,如:
Last login: Fri Nov …Run Code Online (Sandbox Code Playgroud) 如何在一个线程中工作并更新主线程上的进度条?
以下是我尝试解决问题,但它无法正常工作.我相信原因是更新在Windows消息泵中排队,但是所有这些都立即执行,所以它们发生得太快,用户看不到它们.
我正在尝试创建一个GUI应用程序,它将在后台运行线程并更新进度条.我意识到使用backgroundworkerprocess会有更简单的时间,但这会带来我需要的一些灵活性.
我正在使用InvokeRequired和回调函数来处理从工作线程到主线程的调用,因此线程安全不应成为问题.
在我的电脑上,当我点击button1时,工作线程需要大约5秒才能完成其工作,并将结果输出到textbox1.在此期间,工作线程对updateProgressBar函数进行大约100次调用.此功能正在执行:
progressBar1.Value = percent;
progressBar1.Update();
progressBar1.Refresh();
progressBar1.Invalidate();
Run Code Online (Sandbox Code Playgroud)
但是,用户从未看到这些命令的影响.我相信他们都被放入消息泵来更新GUI,但他们只是排队等候并立即执行.
我已经尝试插入睡眠并玩弄正在进行的工作流程,但这没有帮助.如何刷新进度条更新?
需要注意的是,这是一个用来说明问题的人为例子.
// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
namespace ProgressBarExample
{
public partial class Form1 : Form
{
Thread processThread;
delegate void updateGUICallback(int read, int unread);
delegate void updateProgressBarCallback(int percent);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string input = "abc1234 123456 domain\abc1234 …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
using System;
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
namespace TestAsync
{
class Program
{
private const string conn = "Data Source=UNREACHABLESERVER;Initial Catalog=master;Integrated Security=True";
static void Main(string[] args)
{
try
{
TestConnection();
}
catch
{
Console.WriteLine("Caught in main");
}
}
private static async void TestConnection()
{
bool connected = false;
using (var tokenSource = new CancellationTokenSource())
using (var connection = new SqlConnection(conn))
{
tokenSource.CancelAfter(2000);
try
{
await connection.OpenAsync(tokenSource.Token);
connected = true;
}
catch(TaskCanceledException)
{
Console.WriteLine("Caught timeout");
}
catch
{
Console.Write("Caught …Run Code Online (Sandbox Code Playgroud) c# ×5
powershell ×3
.net ×2
async-await ×1
asynchronous ×1
forms ×1
permissions ×1
sql ×1
ssh ×1
winapi ×1
winforms ×1