I'm writing a service in which I need to get a filename of process curently being active.
I have code similar to that below (I have stripped out sanity checks)
Process currentProcess = GetActiveProcess();
ProcessModule currentModule = currentProcess.MainModule;
string FileName = currentModule.FileName.ToLower();
And it works like a charm when I compile it and run as a winforms application.
But when I run the exact same code as a service (no matter if I run service as a LocalSystem user or admin user same that runs code as application) it throws exception at second line:
Unable to enumerate the process modules
I've already tried to:
add:
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")] [HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true, Synchronization = true, ExternalProcessMgmt = true, SelfAffectingProcessMgmt = true)] [PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")] class WindowsService : ServiceBase
set service to start as an admin user
add to app.manifest:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
None of this worked. What do I have to do to get MainModule?