C#将获得窗口句柄转化成路径的问题
最近在做一个项目,通过鼠标钩子鼠标所在窗口的句柄,然后转化成相应路径,现在的问题是:虽然已经获得了句柄,但
是不知道该如何转化成路径。看了一下C++貌似很容易。C#的就不知道该如何去做了。请各位帮下忙~谢啦
代码:
[DllImport("user32.dll")]//
public static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll")]//指定坐标处窗体句柄
public static extern int WindowFromPoint( int xPoint, int yPoint);
Point p;
int hwnd;
//获取鼠标处的window的handle
hwnd = GlobalHook.WindowFromPoint(p.X, p.Y);//获取指定坐标处窗口的句柄
--------------------编程问答-------------------- 什么叫转换成路径?
你是说获得这个句柄对应的应用程序所在的路径吗?
--------------------编程问答-------------------- 应该说是坐标处窗口的句柄转化成这个窗口的路径~~不太会转化啊,我看C++很轻松就转化了。。C#就无存下手了 --------------------编程问答-------------------- C++怎么转换的 都知道了 C#也不难吧? --------------------编程问答--------------------
private const int MAX_PATH = 260;
public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
[DllImport("coredll.dll")]
public extern static int GetWindowThreadProcessId(IntPtr hWnd, ref int lpdwProcessId);
[DllImport("coredll.dll")]
public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess);
[DllImport("coredll.dll")]
public extern static bool TerminateProcess(IntPtr hProcess, int uExitCode);
[DllImport("coredll.dll")]
public extern static bool CloseHandle(IntPtr hObject);
[DllImport("Coredll.dll", EntryPoint = "GetModuleFileName")]
private static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize);
//通过句柄获取运行程序路径
public static String GetAppRunPathFromHandle(IntPtr hwnd)
{
int pId = 0;
IntPtr pHandle = IntPtr.Zero;
GetWindowThreadProcessId(hwnd, ref pId);
pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pId);
StringBuilder sb = new StringBuilder(MAX_PATH);
GetModuleFileName(pHandle, sb, sb.Capacity);
CloseHandle(pHandle);
return sb.ToString();
}
看看可以解决你的问题不 --------------------编程问答-------------------- C++ 那个看的不是很懂...愁死了 --------------------编程问答-------------------- 我获取的句柄是int 的 怎么转化成 IntPtr?
[DllImport("user32.dll")]//指定坐标处窗体句柄
public static extern int WindowFromPoint( int xPoint, int yPoint);
Point p;
int hwnd;
if (GlobalHook.GetCursorPos(out p))
{
//获取鼠标处的window的handle
hwnd = GlobalHook.WindowFromPoint(p.X, p.Y);//获取指定坐标处窗口的句柄
this.textBox1.Text = hwnd.ToString();
}
--------------------编程问答-------------------- 其实int类型的定义不完全对
其实应该是定义为IntPtr类型的 在C#里被称为句柄
[DllImport("user32.dll")]//指定坐标处窗体句柄
public static extern IntPtr WindowFromPoint( int xPoint, int yPoint); --------------------编程问答--------------------
我调试了一下~~就一直报这个错误~~都是找不到模块..
无法加载 DLL“coredll.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E) --------------------编程问答--------------------
不好意思 [DllImport("coredll.dll")] 这个coredll.dll 是wince下的
你是windows的话 得改成user32.dll
补充:.NET技术 , C#