Reputation: 59
I need to access the active program and Debug the name of the active program but when I use get proccess Id i get that 64 bit proccess can not be accessed by a 32 bit program error
static void Main(string[] args)
{
while (true)
{
IntPtr hWnd = GetForegroundWindow();
uint procId = 0;
GetWindowThreadProcessId(hWnd, out procId);
var proc = Process.GetProcessById((int)procId);
Debug.WriteLine(proc.MainModule);
//Console.ReadKey();
}
}
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}
Upvotes: 0
Views: 1823
Reputation: 8935
On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL). Additionally, a 32-bit process cannot load a 64-bit DLL. However, 64-bit Windows supports remote procedure calls (RPC) between 64-bit and 32-bit processes (both on the same computer and across computers). On 64-bit Windows, an out-of-process 32-bit COM server can communicate with a 64-bit client, and an out-of-process 64-bit COM server can communicate with a 32-bit client. Therefore, if you have a 32-bit DLL that is not COM-aware, you can wrap it in an out-of-process COM server and use COM to marshal calls to and from a 64-bit process.
NOTE:
But actually, this is not your problem. Because of you are calling the user32.dll
, and depend on how you compile your application as x86
or x64
platform, the proper user32.dll
will be loaded when your application is launched. The system has two versions of the user32.dll
library: one for x86 applications, an one for x64 applications.
So, there is some problem in your environment or project setting, that not exposed in your question.
Upvotes: 2