0

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?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Yoss
  • 9
  • 1
  • What specific exception type do you get? Have you looked at [this](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396)? – Camilo Terevinto Dec 26 '17 at 19:09
  • Yes, I do. Exception is Win32Exception but it does not make sense to me because I've checked it on Windows XP 32bit and Windows 7 32bit. – Yoss Dec 26 '17 at 21:05
  • I've checked other method of getting filename from Process and result is the same, error slighty different - Access denied. Even if service is running with admin credintentials. So it's pretty sure that being a service is a reason why there is no access to Process.MainModule. – Yoss Dec 26 '17 at 22:12

1 Answers1

0

Ever since Vista, services are barred from interacting with teh Desktop. Afaik a measure to make certain they would not circumvent the UAC.

And not having access to the Desktop (not an interactive session) can cause issues with just about anything, especially the old CoM Classes for Office File handling.

So that is the best thing I can give you, if you do not give us what is happening/not happening (in particular the error message). But the WinForms/Service difference makes this the likely cause.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I tried it on Windows 7 but also on Windows XP and results are the same, so it rather not because of UAC. But when I check in service properties that it may interact with desktop my service crashes at startup – Yoss Dec 26 '17 at 20:47
  • This seems to be a solution to a simialr case. Maybe it help you further? https://stackoverflow.com/a/9502058/3346583 – Christopher Dec 26 '17 at 21:01
  • Unfortunatelly not. My app is 32bit in 32bit OS, and it's working ok when I run it as a application, problem starts when I run it as a service. – Yoss Dec 26 '17 at 21:22
  • One of hte first hings this solution doe is explain how that is a error message from the Windows Itself (outside .NET Code) and how you need the precise information (Native Code Error), because otherwise it is guesswork for days. – Christopher Dec 26 '17 at 21:27