none
How to find installation directory from process name or process ID... RRS feed

  • Question

  • I'm looking for a Visual Studio .NET C++ solution to this problem on a Windows 7 x64 platform, but a reasonably parallel example from another language/system will probably be sufficient to guide me.

    Given a running process name (preferred) or process ID (less preferred), I want to find the installation directory of that running process.

    I'm positive this can be done, because Task Manager can do it. If one opens Task Manager, goes to the Processes tab, selects a process and right clicks, one see "Open File Location" and "Properties". Both these possibilities will yield the information I want.

    The reason I want to do this is because I have two different versions (for mutually exclusive hardware) of a software application. Only one of them runs at a time, the applications are mutually exclusive as well. But they both use the SAME process name. I want to programmatically determine which one is currently running, and the only way I can be sure is by knowing the INSTALLATION directory of the one that is currently running (the two similar applications are installed in uniquely separate directories).

    I have tried something like:

    array<Process^>		^xProcesses;
    Process			^xProcess;
    ProcessModuleCollection	^xModuleCollection;
    ProcessModule		^xModule;
    int			i;
    
    xProcesses = Process::GetProcessesByName ("x");
    if (xProcesses->Length != 0)
    	{
    	xProcess = xProcesses[0];
    	xModuleCollection = xProcess->Modules;
    	for (i = 0; i < xModuleCollection->Count; i++)
    		{
    		xModule = xModuleCollection[i];
    		Debug::WriteLine(xModule->ModuleName);
    		Debug::WriteLine(xModule->FileName);
    		}
    	}
     

    Unfortunately, this throws an 'System.ComponentModel.Win32Exception' occurred in System.dll. I suspect it has something to do with incompatibility between 32 and 64 bit applications, but even that doesn't make sense since everything should be 64 bit in this situation.

    There may be other ways of getting the information I desire, perhaps registry query? Alternatives are solicited as well, I don't really care HOW I get it done, so long as I get it done.

    Thanks in advance!

    Monday, July 21, 2014 6:34 PM

Answers

  • Hi Odlumb,

    I found an article talking about how to give application elevated UAC, please see more information from http://stackoverflow.com/questions/6412896/giving-application-elevated-uac.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. &lt;br/&gt; Click &lt;a href=&quot;http://support.microsoft.com/common/survey.aspx?showpage=1&amp;scid=sw%3Ben%3B3559&amp;theme=tech&quot;&gt; HERE&lt;/a&gt; to participate the survey.

    Tuesday, July 22, 2014 9:39 AM
    Moderator

All replies

  • Replying to my own question. The question has changed a bit, because I've learned a bit since the original post. Here is the exact code (EXACTLY) I'm attempting to execute:

    private: enum ScopeStatus whichScopeIsRunning(System::Void)
    	{
    	array<Process^>		^scopeProcesses;
    	String			^fullPath;
    
    	scopeProcesses = Process::GetProcessesByName ("Scope");
    	if (scopeProcesses->Length != 0) //a least one instance is running
    		{
    		try
    			{
    			fullPath = scopeProcesses[0]->Modules[0]->FileName;
    			}
    
    		catch (Win32Exception^ w) 
    			{
    			Debug::WriteLine(w->Message);
    			Debug::WriteLine(w->ErrorCode);
    			Debug::WriteLine(w->NativeErrorCode);
    			Debug::WriteLine(w->StackTrace);
    			Debug::WriteLine(w->Source);
    			Exception^ e = w->GetBaseException();
    			Debug::WriteLine(e->Message);
    			}
    		}
    	}

    So here's the deal. If I substitute some friendly process name like "nvtray" instead of "Scope" in the first line, the code executes perfectly. Ditto for "iexplore" and many others. But for certain applications (Scope, AVP) the code throws an exception.

    Here seems to be the problem - my code doesn't have sufficient permission to access the ProcessModule handle of certain processes. Since the code works quite well, it doesn't need to be altered - what I need is to elevate the permission of my application to gain access to the ProcessModule(s) of those applications which are giving problems. So my question now becomes - How do I elevate the permissions, and exactly what permissions do I need?

    Thanks in advance!

    Tuesday, July 22, 2014 1:43 AM
  • Hi Odlumb,

    I found an article talking about how to give application elevated UAC, please see more information from http://stackoverflow.com/questions/6412896/giving-application-elevated-uac.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. &lt;br/&gt; Click &lt;a href=&quot;http://support.microsoft.com/common/survey.aspx?showpage=1&amp;scid=sw%3Ben%3B3559&amp;theme=tech&quot;&gt; HERE&lt;/a&gt; to participate the survey.

    Tuesday, July 22, 2014 9:39 AM
    Moderator