获得剪贴板来源进程名

来源:互联网 发布:大数据的4v特征是多选 编辑:程序博客网 时间:2024/06/08 18:00
using System.Runtime.InteropServices;
using System.Diagnostics;

[DllImport(
"user32.dll")]
public static extern IntPtr GetClipboardOwner();

[DllImport(
"user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr handle, 
    
out int processId);
[DllImport(
"kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);

private void button1_Click(object sender, EventArgs e)
{
    IntPtr vOwner 
= GetClipboardOwner();
    
if (vOwner == IntPtr.Zero) return;
    
int vProcessId;
    GetWindowThreadProcessId(vOwner, 
out vProcessId);
    Process vProcess 
= Process.GetProcessById(vProcessId);
    Text 
= vProcess.MainModule.FileName;
}
uses PsAPI;

procedure TForm1.Button1Click(Sender: TObject);
var
  vOwner: THandle;
  vProcessId: THandle;
  vProcess: THandle;
  vBuffer: array[0..
255] of Char;
begin
  vOwner :
= GetClipboardOwner();
  
if vOwner = 0 then Exit;
  GetWindowThreadProcessId(vOwner, vProcessId);
  vProcess :
= OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
    False, vProcessId);
  GetModuleFileNameEx(vProcess, 0, vBuffer, SizeOf(vBuffer));
  CloseHandle(vProcess);
  Caption :
= vBuffer;
end;

 
原创粉丝点击