Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dears

I developed a process manager liked windows TaskManager. and now I want to get the process execution path.
I used the Process.MainMadule.FileName but the program throw an exception.
"Only part of a ReadProcessMemory or WriteProcessMemory request was completed"

My program platform target is AnyCPU and I used the .Net 2.0. Also I used the GetModuleFileNameEx API to get the process location, but some times it's returned null or Access is Denied Exception (Noted that my program run as admin).

My code is here:

C#
var findCurrentProcessById = new Hashtable();
foreach (Process proc in Process.GetProcesses())
{
    if (proc.Id == 0 || proc.Id == 4 || proc.ProcessName.ToLower() == "audiodg".ToLower())
        continue;

    string pid;
    try
    {
        pid = proc.Id + "$" + proc.StartTime.Ticks;
        findCurrentProcessById.Add(pid, proc);
    }
    catch (Exception) { continue; }

    if (findProcessById.ContainsKey(pid))
        continue;

    // Process Started!
    var plt = new ProcessLifeTime()
            {
                PID = proc.Id,
                Process = proc,
                ProcessName = proc.ProcessName,
                StartTime = proc.StartTime,
            };
    try
    {
/* In this line some times throw an exception. or when I use the WMI some times the path is empty or some times throw Access is Denied exception 
*/
        var path = ProcessExtension.GetModuleFileNameEx(proc.Handle, proc.Id);
        plt.ProcessLocation = path;// proc.MainModule.FileName;
    }
    catch (Exception) { }
    try
    {
        plt.ProcessFileDescription = proc.MainModule.FileVersionInfo.FileDescription;
    }
    catch (Exception) { }

    findProcessById.Add(pid, plt);
    OnProcessChanged(plt, ProcessStatus.Added);
}



I use this code in a background worker. Its monitor the processes.

Also I used the WMI Win32_Processes and I can't get any results from it.
My system platform is 64bit and my processes maybe are 32bit or 64bit.

How can I do this, independent of programs platform(32b or 64b) that are running?

Thanks in advanced.
Posted
Updated 21-Dec-14 0:58am
v2
Comments
OriginalGriff 21-Dec-14 5:08am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Perhaps the relevant code fragment would help us understand what you are doing, and why it is causing a problem?
Use the "Improve question" widget to edit your question and provide better information.
Afzaal Ahmad Zeeshan 21-Dec-14 6:34am    
I don't think even if he Improved the question, there won't be any good resource for us to work on. This is a broad topic, and I don't believe it can be covered in a short answer here. Also, the problem is not even included here, just a simple string that the return is a null is shown.
Kornfeld Eliyahu Peter 21-Dec-14 7:08am    
On which process you got that error?
[no name] 21-Dec-14 8:39am    
It's random. But when I open the TaskManager some times I see this error.
Also when I running other programs liked Cmd or cleanMgr I see this error.
Ravi Bhavnani 21-Dec-14 13:42pm    
> How can I do this, independent of programs platform(32b or 64b) that are running?

See the 2nd answer to this SO question:
http://stackoverflow.com/questions/5497064/c-how-to-get-the-full-path-of-running-process

/ravi

1 solution

From your code, I never saw such a basic thing as using System.Diagnostics.MainModule and the module property System.Diagnostics.ProcessModule.FileName. This is exactly what you are looking for and this is what you should have started with:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule%28v=vs.110%29.aspx[^].

As to 32-bit or 64-bit processes, it seems to be totally irrelevant.

—SA
 
Share this answer
 
Comments
Maciej Los 21-Dec-14 14:51pm    
+5!
Sergey Alexandrovich Kryukov 21-Dec-14 14:53pm    
Thank you, Maciej.
—SA
Kornfeld Eliyahu Peter 22-Dec-14 4:20am    
You right about the use of that methods, however in some mystical way even that will fail for certain processes with the specified error...It is really unrelated to the 32/64 bit issue and seems to be connected to the process itself.
I run a check on all of our computers at the office and found that the processes of the drivers of sound and video cards are fail on each and every one of them - a very interesting thing I would say...
Sergey Alexandrovich Kryukov 22-Dec-14 10:22am    
Thank you, Peter.

It sounds funny, but driver processes are special things, and some drivers are kernel-mode, some are not; no wonder, kernel-mode drivers don't allow to show much. I don't know the detail... If by "fail" you mean exceptions on reading of some properties, this is one of the rare cases when local exception catching should be used; so OP could sandwich the loop body in try-catch and catch such exceptions and, say, call "continue;" from the exception handler...

—SA
[no name] 22-Dec-14 11:55am    
Special thanks for Peter.
In GetModuleFileNameEx method I just use this API and get the file location.
The platform target of my App is Any CPU. And I don't now that why this exception occurred. Any way, finally I use the WMI solution. It's better because at least WMI doesn't throw any exception and just returned a string.Empty value.
Thank you all

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