Click here to Skip to main content
15,885,949 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Get a process's executable path from a 64-bit application. Using the following code only works for 32-bit applications but throws an error on 64-bit apps.
C#
Process proc = Process.GetProcessById((int)pid);
return proc.MainModule.FileName;
Is there a way to get the full path to the executable file on 64-bit applications?
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jun-12 21:42pm    
You have four possible combinations as your calling code can be 32- or 64-bits, and your process indicated by pid could be 32- or 64-bit. Which case causes this problem? Besides, are you sure that both of the processes, if build as 64-bit applications, run on the machine with the same instruction-set architecture as the target (or the target architecture is "any CPU")?
--SA
androgenic_human 26-Jun-12 13:08pm    
This is a 32-bit application trying to get the path of a 64-bit application on the same machine. It is difficult to know what the process is that the calling code is getting as it is the user's choice and could be any running application. I'm getting the PID from the user32.dll via GetWindowThreadProcessID(IntPtr handle, out uint processId) then trying to find my way to the full path from the PID. I definitely need a better way to do this.
Greg
Sergey Alexandrovich Kryukov 29-Jun-12 19:41pm    
Thank you for answering. Finally, I got time to test it and draw the conclusion. First of all, I voted 5 for this really interesting and important question.
Please see my answer -- I hope it closes the issue.
--SA

Thank you for answering my question and clarification. I confirm the problem. Well, it appears to be something that really works this way. To me, it looks like some peculiarity in WoW64 (something a 32-bit application works with on Windows for 64-bit instruction-set architectures, http://en.wikipedia.org/wiki/WOW64[^]).

Why do I think so? Because I also tested the opposite case: when a 64-bit .NET application fetch process information on a 32-bit process, not a .NET one. In both cases, running as administrator did not make any difference, this is really an instruction-set architecture problem.

Here is one practical conclusion: in such application, always use the platform target "Any CPU", it will make you code working correctly in all 9 cases (three different instruction-set architectures: x86, x86-64, IA-64 (Itanium), in all combinations).

—SA
 
Share this answer
 
try this

C#
public String GetExeFFN()
{
    Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    String mExeFFN = a.CodeBase.Substring(8);
    mExeFFN = mExeFFN.Replace('/', '\\');
    return mExeFFN;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900