我如何GetModuleFileName()如果我只有一个窗口句柄(HWND)? [英] How do I GetModuleFileName() if I only have a window handle (hWnd)?

查看:381
本文介绍了我如何GetModuleFileName()如果我只有一个窗口句柄(HWND)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个窗口的可执行文件是我的C#2.0应用程序之外的名称。我的应用目前得到使用GetForegroundWindow从user32.dll中()调用一个窗口句柄(HWND)。



这是我能够做到的挖掘,我想,我想用GetModuleFileNameEx()函数(从PSAPI)来获取这个名字,但GetModuleFileNameEx()需要一个处理一个过程,而不是一个窗口。



是否有可能从一个窗口句柄进程句柄? (我需要首先获取窗口的线程处理?)



编辑的第一句话说得清楚我想要做的。



更新!这里,我发现我工作的C#代码。唯一需要注意的是偶尔的它返回一个文件/路径所在的盘符是一个?而不是实际的盘符(如C)。 - 有没有想通了,为什么还没有

 函数[DllImport(user32.dll中)] 
静态外部UINT GetWindowThreadProcessId(IntPtr的的HWND,UINT出lpdwProcessId);

函数[DllImport(KERNEL32.DLL)]
静态外部的IntPtr调用OpenProcess(UInt32的dwDesiredAccess,的Int32 bInheritHandle,UInt32的dwProcessId);

函数[DllImport(PSAPI.DLL)]
静态外部UINT GetModuleFileNameEx(IntPtr的hProcess,IntPtr的HMODULE,[出]的StringBuilder lpBaseName,[中] [的MarshalAs(UnmanagedType.U4) INT n大小);

函数[DllImport(KERNEL32.DLL)]
[返回:的MarshalAs(UnmanagedType.Bool)
静态的extern BOOL CloseHandle的(IntPtr的hObject);

私人字符串GetWindowModuleFileName(IntPtr的的hWnd)
{
UINT进程ID = 0;
const int的nChars = 1024;
StringBuilder的文件名=新的StringBuilder(nChars);
GetWindowThreadProcessId(HWND,出来的ProcessID);
IntPtr的hProcess =调用OpenProcess(1040,0,进程ID);
GetModuleFileNameEx(hProcess,IntPtr.Zero,文件名,nChars);
CloseHandle的(hProcess);
回报(filename.ToString());
}


解决方案

您可以调用的 GetWindowThreadProcessId 并且将返回与窗口关联的进程。



从这一点,你可以调用调用OpenProcess 打开过程并获得句柄过程


I'm trying to get the name of the executable of a window that is outside my C# 2.0 application. My app currently gets a window handle (hWnd) using the GetForegroundWindow() call from "user32.dll".

From the digging that I've been able to do, I think I want to use the GetModuleFileNameEx() function (from PSAPI) to obtain the name, but GetModuleFileNameEx() requires a handle to a Process, not a Window.

Is it possible to get a process handle from a window handle? (Do I need to get the thread handle of the window first?)

EDITED the first sentence to make it clearer what I'm trying to do.

UPDATE! Here's the C# code that I found worked for me. The only caveat is occasionally it returns a file/path where the drive letter is a "?" instead of the actual drive letter (like "C"). -- Haven't figured out why yet.

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("kernel32.dll")]
static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);

[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize);

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);

private string GetWindowModuleFileName(IntPtr hWnd)
{
    uint processId = 0;
    const int nChars = 1024;
    StringBuilder filename = new StringBuilder(nChars);
    GetWindowThreadProcessId(hWnd, out processId);
    IntPtr hProcess = OpenProcess(1040, 0, processId);
    GetModuleFileNameEx(hProcess,IntPtr.Zero,filename,nChars);
    CloseHandle(hProcess);
    return (filename.ToString());
}

解决方案

You can call GetWindowThreadProcessId and that will return you the process associated with the window.

From that, you can call OpenProcess to open the process and get the handle to the process.

这篇关于我如何GetModuleFileName()如果我只有一个窗口句柄(HWND)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆