C#GetWindowModuleFileName和GetWindowText给出了不同句柄的结果
我对两者使用相同的句柄,但收到的结果不同 我的代码:C#GetWindowModuleFileName和GetWindowText给出了不同句柄的结果,c#,.net,dll,handle,user32,C#,.net,Dll,Handle,User32,我对两者使用相同的句柄,但收到的结果不同 我的代码: [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); private string GetActiveWi
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
[DllImport("user32.dll")]
static extern IntPtr GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, uint count);
private string GetActiveWindowName()
{
const uint nChars = 256;
StringBuilder Buff = new StringBuilder((int)nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowModuleFileName(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return Buff.ToString();
}
我的输出是:
对于GetWindowText:邮递员
对于GetWindowModuleFileName:
C:\Users\Suleman\Desktop\SocketService\DataReport\DataReport\bin\Debug\DataReport.vshost.exe
但如果句柄相同,这又有什么不同呢?我的目标框架是.NET 3.5“vshost.exe”是关键。VisualStudio做了一些恶作剧,这样你就可以调试你的程序了。这不会发生在真实的live exe文件上。@Davesoft我检查了在没有Visual studio的情况下运行,但它给出了应用程序本身的.exe位置。C:\Users\Suleman\Desktop\SocketService\DataReport\DataReport\bin\Debug\DataReport.exe,而我的活动窗口仍然是Postman