C# check if a 32bit process is running by name on a 64bit os

Joined
Feb 9, 2023
Messages
16
Programming Experience
10+
Good Morning,

so i have a little issue that i have run into well least i think this is the problem,

so have tryied the basic,
check if process exists:
            Process[] processes = Process.GetProcessesByName("HydraMobile.exe");
            if (processes.Length > 0)
                processes[0].CloseMainWindow();

very basic one however i had issue it did not return anything so thought i would check names
process.png


so tryied the following,
attempt 2:
        public bool checkIfProcessIsRunning()
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (process.ProcessName.ToLower().Contains("HydraMobile v3.11"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

witch also returned false,
so thought i would check details and see what application is called there,

and found in details its called,
HydraMobile.exe
so i created another methord,

Attempt 3:
        public string GetProcessPath(string name)
        {
            Process[] processes = Process.GetProcessesByName(name);

            if (processes.Length > 0)
            {
                return processes[0].MainModule.FileName;
            }
            else
            {
                return string.Empty;
            }
        }

and then passed in HydraMobile.exe
however still would not find it,

so did some more searching and noticed it could be because its in 32 bit app,
now my program is a anycpu and that has to be like that because i need it to run on both 32 and 64 bit os ,

my question is is it possible to access a 32bit process and stop and start it in a 64 bit os ?

kind regards,
elfenliedtopfan5
 
Why would you expect this to return true?
C#:
process.ProcessName.ToLower().Contains("HydraMobile v3.11")
You are trying to check if a string that is all lower case characters contains a string that has a mix of upper and lower case characters.
 
so did some more searching and noticed it could be because its in 32 bit app,
now my program is a anycpu and that has to be like that because i need it to run on both 32 and 64 bit os ,

If your app is AnyCPU, if your are running on a 64-bit machine, it will run as 64-bit, and if it running on a 32-bit machine, it will run as 32-bit.

The only time it would run as 32-bit on 64-bit machine is if you also checked the checkbox that says "Prefer 32-bit"; OR
you are running your code in the debugger in Visual Studio and you have an older version of Visual Studio which prefers to debug apps in 32-bit rather than 64-bit.
 
Why would you expect this to return true?
C#:
process.ProcessName.ToLower().Contains("HydraMobile v3.11")
You are trying to check if a string that is all lower case characters contains a string that has a mix of upper and lower case characters.

ok well yes did not notice that darn messed up on that one however the other ones i wrote still did not find the the active process and the others did not have the tolower
 
This seems to work for me wiithout issues:
C#:
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var thisProcess = Process.GetCurrentProcess();
        var processName = thisProcess.ProcessName;
        Console.WriteLine($"My process name is {processName}.");
        Console.WriteLine($"My main module file name is {thisProcess.MainModule.FileName}.");

        var myClones = Process.GetProcessesByName(processName);
        foreach (var clone in myClones)
            Console.WriteLine($"Process ID {clone.Id}");

        Console.WriteLine("Done.");
    }
}

I suspect that your use of GetProcessesByName() is mistakenly adding in the ".exe" as part of the process name.
 
This seems to work for me wiithout issues:
C#:
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var thisProcess = Process.GetCurrentProcess();
        var processName = thisProcess.ProcessName;
        Console.WriteLine($"My process name is {processName}.");
        Console.WriteLine($"My main module file name is {thisProcess.MainModule.FileName}.");

        var myClones = Process.GetProcessesByName(processName);
        foreach (var clone in myClones)
            Console.WriteLine($"Process ID {clone.Id}");

        Console.WriteLine("Done.");
    }
}

I suspect that your use of GetProcessesByName() is mistakenly adding in the ".exe" as part of the process name.

Thank you really thank you,

however its not the process of my application i need its a external one
the hydramobile.exe

this code works amazing for my own apps process but how would one go about passing in a name of external application process the program i am making checks to see if a program is running if it is it will close this works fine just need to be able to pass in a name and for the app to be able to locate it
you are right though the get processname does seem to be doing some strange things like adding the .exe to the name but i tryied just HydraMobile without the .exe on the end and wont find it still
 
Your test should be to get all process names and see which one corresponds to what you want. If there is no such process then obviously you can't get it by name. If there is such a process then you'll be able to see how the name is different to what you're looking for.
 
Back
Top Bottom