0

Trying to find a win32 process location and not just the name.

Can't seem to see any literature online about this?

Current Script is:

                Process[] localByName = Process.GetProcessesByName("lmgrd_x64_n6");
        foreach (Process proc in localByName)
        {
            try
            {
                try
                { //64bit
                    lb_instances.Items.Add(proc.MainModule.FileName + " " + proc.Id);
                }
                catch (Exception ex)
                { //32bit
                    lb_instances.Items.Add(proc.ProcessName + " " + proc.Id);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), ex.InnerException.ToString());
            }

I get exception error:

A 32 bit processes cannot access modules of a 64 bit process.

SCramphorn
  • 447
  • 4
  • 23
  • Perhaps you should explain what you mean by `Win32`, `process location` and `Process.MainModule.FileName`? What script are you referring to? Win32 is the API, not a different kind of process. All executables behave the same, no matter the language they were written on – Panagiotis Kanavos Mar 15 '17 at 15:30
  • 1
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683217(v=vs.85).aspx – Hans Passant Mar 15 '17 at 15:32
  • Are you referring to [Process.MainModule](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule(v=vs.110).aspx)? That should return the executable for 32 and 64 bit applications – Panagiotis Kanavos Mar 15 '17 at 15:34
  • @PanagiotisKanavos I get the error ' A 32 bit processes cannot access modules of a 64 bit process.' – SCramphorn Mar 15 '17 at 15:40
  • 1
    The following will return the paths of the first 100 processes, no matter how they were created. It returns 32 and 64 bit processes, system services like `svchost.exe`, user applications like Chrome etc. Just make sure you run it with *elevated* priviledges: `System.Diagnostics.Process.GetProcesses().Take(100).Select(p=>{try{return p.MainModule.FileName;}catch{return "";}})`. I used LinqPad to run this application – Panagiotis Kanavos Mar 15 '17 at 15:40
  • @SCramphorn how did you get hold of the process object in the first place? – Panagiotis Kanavos Mar 15 '17 at 15:41
  • @PanagiotisKanavos I've added my code just before then if that helps! – SCramphorn Mar 15 '17 at 15:41
  • 1
    That's a *different* issue. It doesn't mean that `MainModule` doesn't work for 32-bit applications. It means that you can't get that info for *64-bit* applications if you are running in 32-bit. For example, LinqPad is a 64-bit application, which is why it was able to retrieve info for all kinds of processes. You can simply change your target to x64 or clear the `Prefer 32-bit` checkbox in your build properties. – Panagiotis Kanavos Mar 15 '17 at 15:46
  • Anoter option is to use WMI as shown in the duplicate question – Panagiotis Kanavos Mar 15 '17 at 15:46
  • @PanagiotisKanavos thanks, that fixed my issue - please post as an answer and I will accept it! Regards, SAm – SCramphorn Mar 15 '17 at 15:50

1 Answers1

1

You can call Module32First and grab the path from the structure. That's how .NET does it, anyway.

Joey
  • 344,408
  • 85
  • 689
  • 683