Often an error message occurs:

“The process cannot access the file because it is being used by another process.”

Typically it occurs when you try to copy or rebuild the file.
How do you determine which process is using it? One way to release the file may be to reboot. What a pain!
Here’s an easier way to find which process is using it. The .NET framework has some very simple classes that can enumerate the processes running on your machine. We can access these from VFP by COM interop.

Here’s some fox code to determine which processes have, for example, “kernel32.dll” loaded.

x=NEWOBJECT("loadedmodule.loadedmodule")

arr=x.LoadedModule("kernel32.dll")

FOR EACH oProc IN arr

      IF !ISNULL(oproc)

            ?oProc.id,oProc.ProcessName

      ENDIF

ENDFOR

To create the .NET project:

Start Visual Studio 2003
Choose File->New->Project
Choose Visual Basic Projects, ClassLibrary project, Call it “LoadedModule”
It creates a project with a Class1.vb file. Delete that file from the Solution explorer.
Choose Project->Add User Control->COM Class. Name it LoadedModule.vb
Add this line at the top:

imports System.Diagnostics.Debug

Paste this code just before the “End Class”

public function LoadedModule(ModuleName as String) as LMproc()

        Dim oProc As Diagnostics.Process

        dim aResult(0) as LMproc

        dim nCnt as Integer=0

        For Each oProc In Process.GetProcesses()    ' Each running process

            Try

                For Each omod As Diagnostics.ProcessModule In oProc.Modules()   ' Each DLL/EXE loaded in the process

                    If omod.ModuleName.ToString().ToLower() = ModuleName.ToLower Then

                        dim oLMProc as LMproc= new LMproc   ' create a new instance

                        oLMproc.ID =oProc.Id

                        oLMproc.ProcessName=oProc.ProcessName

                        oLMproc.WorkingSet = oProc.WorkingSet

                        redim preserve aResult(nCnt)

                        aResult(nCnt) = oLMProc ' add it to the array

                        nCnt=nCnt+1

                    End If

                Next

           Catch ex As Exception

                ' Idle, System processes throw exception "Unable to enumerate the process modules."

                debug.WriteLine("Exception " + ex.Message+" "+oProc.ProcessName)  

          End Try

        Next

        return aResult

End Function

 

public class LMproc

    public ProcessName as String

    public ID as Integer

    public WorkingSet as Integer

End Class


Then choose Build->Build Solution and run the VFP code above.

The GetProcesses() static method of the Process class.returns an array of all the currently running processes on the machine. The Modules property returns the modules loaded for that particular process. If the module.ModuleName matches the passed in parameter, then a new array element is created for that Process class The LoadedModule method returns that array.

The Process class has lots of useful members. You can add things like memory usage values and processor time to the LoadedModuleObject class as properties and to the array elements and then access them from VFP. You can close a process or even kill it.

You can add a method to return information about a particular process using the GetProcessById method There are lots of other goodies available from the Process class: check it out!

I’ve added WorkingSet (physical memory currently used by process) for this example to see which process is using the most memory

x=NEWOBJECT("loadedmodule.loadedmodule")

arr=x.LoadedModule("kernel32.dll")

CREATE CURSOR results (id i, WorkingSet i,Name c(20))

FOR EACH oproc IN arr

      IF !ISNULL(oproc)

            ?oProc.id,oProc.WorkingSet,oProc.ProcessName

            INSERT INTO results VALUES (oProc.id,oProc.WorkingSet,oProc.ProcessName)

      ENDIF

ENDFOR

SELECT * FROM results ORDER BY WorkingSet descending,name

Please let me know how you expand on this example.

64528