C# 32位进程无法访问64位进程的模块
我试图在鼠标下获取窗口,但是,当我到达代码:Process.GetProcessById时,出现以下错误:C# 32位进程无法访问64位进程的模块,c#,.net,wpf,windows,winapi,C#,.net,Wpf,Windows,Winapi,我试图在鼠标下获取窗口,但是,当我到达代码:Process.GetProcessById时,出现以下错误: A 32 bit processes cannot access modules of a 64 bit process. 这是我的密码: NativeMethods.POINT p; if (NativeMethods.GetCursorPos(out p)) { IntPtr hWnd = NativeMethods.Win
A 32 bit processes cannot access modules of a 64 bit process.
这是我的密码:
NativeMethods.POINT p;
if (NativeMethods.GetCursorPos(out p))
{
IntPtr hWnd = NativeMethods.WindowFromPoint(p);
NativeMethods.GetWindowModuleFileName(hWnd, fileName, 2000);
string WindowTitle= fileName.ToString().Split('\\')[fileName.ToString().Split('\\').Length - 1];
// WindowTitle will never change, it will get my window only!
}
////////////////////////////////////////////////////////////////////////////////////////
static class NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(POINT Point);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint GetWindowModuleFileName(IntPtr hwnd,
StringBuilder lpszFileName, uint cchFileNameMax);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out NativeMethods.POINT lpPoint);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
public static implicit operator System.Drawing.Point(POINT p)
{
return new System.Drawing.Point(p.X, p.Y);
}
public static implicit operator POINT(System.Drawing.Point p)
{
return new POINT(p.X, p.Y);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
}
以下是我在WPF中尝试使用的调用:
void dispatcherOp_Completed(object sender, EventArgs e)
{
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
System.Windows.Threading.DispatcherOperation
dispatcherOp = this.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
NativeMethods.POINT p;
if (NativeMethods.GetCursorPos(out p))
{
IntPtr hWnd = NativeMethods.WindowFromPoint(p);
NativeMethods.GetWindowModuleFileName(hWnd, fileName, 2000);
uint processID = 0;
uint threadID = GetWindowThreadProcessId(hWnd, out processID);
string filename= Process.GetProcessById((int)processID).MainModule.FileName;
}
}
));
dispatcherOp.Completed -= new EventHandler(dispatcherOp_Completed);
dispatcherOp.Completed += new EventHandler(dispatcherOp_Completed);
}
));
thread.Start();
}
您看到这个答案了吗:您应该尝试使用任意CPU配置编译应用程序。正如错误所说,您无法通过Process.GetProcessById获取信息。因此,p/调用本机方法以获取所需内容。@David,正如您所看到的,我已经在调用它了above@spender,我会看到答案的,谢谢