C# 获取窗口文件名不需要';行不通
我在谷歌上搜索了几个小时,尝试了我找到的所有东西,但似乎都不起作用 我的代码是:C# 获取窗口文件名不需要';行不通,c#,C#,我在谷歌上搜索了几个小时,尝试了我找到的所有东西,但似乎都不起作用 我的代码是: private static long _i; private static readonly System.Timers.Timer Timer = new System.Timers.Timer(1000); [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll
private static long _i;
private static readonly System.Timers.Timer Timer = new System.Timers.Timer(1000);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count);
static void Main()
{
Timer.Elapsed += Timer_Elapsed;
Timer.AutoReset = true;
Timer.Start();
while (Timer.Enabled)
Console.ReadLine();
}
static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("-|- - " + _i + " - -|-");
Console.WriteLine("WindowHandle: " + GetForegroundWindow());
var buff = new StringBuilder(2048);
GetWindowModuleFileName(GetForegroundWindow(), buff, 1024);
Console.WriteLine("ModuleName: " + Path.GetFileName(buff.ToString()));
Console.WriteLine("-|- - | - -|-");
_i++;
}
此代码的输出始终如下所示:
-|- - 1 - -|-
WindowHandle: 8128962
ModuleName: ConsoleApplication.vshost.exe
-|- - | - -|-
但是,即使我将目标对准另一个应用程序,如Chrome或Fiddler,它也总是打印出我的可执行文件名,或者根本不打印文件名。
是否存在我可能缺少的问题?您必须添加对System.Management的引用,此引用使用WMI
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static long _i;
private static readonly System.Timers.Timer Timer = new System.Timers.Timer( 1000 );
[DllImport( "user32.dll" )]
private static extern IntPtr GetForegroundWindow();
[DllImport( "user32.dll" )]
private static extern int GetWindowText( IntPtr hWnd, StringBuilder text, int count );
[DllImport( "user32.dll" )]
private static extern int GetWindowModuleFileName( IntPtr hWnd, StringBuilder text, int count );
[DllImport( "user32.dll", SetLastError = true )]
public static extern int GetWindowThreadProcessId( IntPtr hwnd, ref int lpdwProcessId );
static void Main()
{
Timer.Elapsed += Timer_Elapsed;
Timer.AutoReset = true;
Timer.Start();
while ( Timer.Enabled )
Console.ReadLine();
}
static void Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e )
{
Console.WriteLine( "-|- - " + _i + " - -|-" );
Console.WriteLine( "WindowHandle: " + GetForegroundWindow() );
string name = GetProcessEXENameUsingWMI( GetProcess( GetForegroundWindow() ) );
Console.WriteLine( "ModuleName: " + Path.GetFileName( name ) );
Console.WriteLine( "-|- - | - -|-" );
_i++;
}
private static string GetProcessEXENameUsingWMI( Process p )
{
ManagementObjectSearcher mos = new ManagementObjectSearcher( "SELECT ExecutablePath FROM Win32_Process WHERE ProcessId=" + p.Id.ToString() );
ManagementObjectCollection moc = mos.Get();
foreach ( ManagementObject mo in moc )
{
string executablePath = mo[ "ExecutablePath" ].ToString();
return executablePath;
}
return "";
}
public static Process GetProcess( IntPtr hwnd )
{
int intID = 0;
GetWindowThreadProcessId( hwnd, ref intID );
return Process.GetProcessById( intID );
}
}
}
你必须多走一步。您需要获取属于foregoundwindow的进程。这可能会奏效:
[Flags]
enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VMOperation = 0x00000008,
VMRead = 0x00000010,
VMWrite = 0x00000020,
DupHandle = 0x00000040,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
Synchronize = 0x00100000
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
private static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);
[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);
[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize);
[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);
static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("-|- - " + _i + " - -|-");
Console.WriteLine("WindowHandle: " + GetForegroundWindow());
uint pid;
GetWindowThreadProcessId(GetForegroundWindow(), out pid);
IntPtr proc = OpenProcess(ProcessAccessFlags.VMRead | ProcessAccessFlags.QueryInformation, true, pid);
var buff = new StringBuilder(2048);
GetModuleFileNameEx(proc, (IntPtr) 0, buff, 1024);
Console.WriteLine("ModuleName: " + Path.GetFileName(buff.ToString()));
Console.WriteLine("-|- - | - -|-");
_i++;
CloseHandle(proc);
}
这基本上是一个c#实现的你能给出更多的代码吗。。。比如路径是什么等等?@Alker你什么意思路径是什么?这应该是所有相关的代码。哦,我的VisualStudio被窃听了…我的意思是“\I”变量。这不是“它似乎不起作用”:什么是“它”?您希望您的代码具体做什么?在不知道问题是什么的情况下回答你的问题有点困难。请不要用另一条评论回复此评论;相反,编辑你的问题,使其更清楚地说明问题的内容。