none
Memory Mapped Files and windows service RRS feed

  • Question

  • Hi All

    I have a service that scans and processes images every 15 minutes. I wanted to run a GUI that would show progress values as it runs. Previously this was one windows forms app but I know want it to run whether the machine is logged on or not so I can transfer it to a server.

    I've started down the path of IPC and rigged up a Memory Mapped File library which works fine between say a console app and a form. Sadly I get no communication if I have a windows service and windows form. I've tried running the service as Local System and Local Service but I'm suspecting there maybe some memory or process isolation for security reasons that is preventing it communicating? The Memory Mapped files are non persistent. Have not tried file based yet, but will do to see if that helps. Machine is Windows 10.

    Cheers

    Friday, May 11, 2018 1:47 PM

All replies

  • Please check this documentation.  It should be a workable solution.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 11, 2018 2:28 PM
    Moderator
  • There appears to be a relevant comment on this sample project:

    "To communicate with a service, you must use the "Global\" memory "area". The accessrights are restricted. You can´t access to this memory area between different users. I grant the access for my MMFile to "Everyone". Then it work´s fine."


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 11, 2018 2:35 PM
    Moderator
  • This is correct. Starting with Windows Vista, Services run in session isolation. The below link will explain it a bit more and indicate what you need to do:

    http://blogs.networkingfutures.co.uk/post/2015/12/28/Windows-Services-Implementing-Non-Persisted-Memory-Mapped-Files-Exposing-IPC-Style-Communications.aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, May 11, 2018 8:46 PM
  • Thank you guys for the info. Obviously didn't "Google" enough.

    it makes sense so I think I can use this info to modify things to work.

    Cheers

    Friday, May 11, 2018 9:50 PM
  • So it looks like it isn't as easy as first thought. I presumed using "Global\" on the map name would do the trick but adding that causes a hang on the MemoryMappedFIle.CreateOrOpen. Adding the Everyone right doesn't make a whole lot of difference as I have no trouble with the function if both run under my session. Here is the code I use in New.

    Public Sub New(ByVal MapName As String, ByVal Capacity As Long)
    	'MapName = "Global\" + MapName
    	MMFSecurity = New MemoryMappedFileSecurity
    	MMFSecurity.AddAccessRule(New AccessRule(Of MemoryMappedFileRights)("Everyone", MemoryMappedFileRights.FullControl, AccessControlType.Allow))
    	MappedFile = MemoryMappedFile.CreateOrOpen(MapName, Capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, MMFSecurity, HandleInheritability.Inheritable)
    	MMView = MappedFile.CreateViewAccessor
    
    	ReadPosition = -1
    	WritePosition = -1
    	DataToSend = New List(Of Byte())()
    	MyCallBack = New SendOrPostCallback(AddressOf OnDataAvailableInternal)
    	MyOperation = AsyncOperationManager.CreateOperation(Nothing)
    End Sub

    I thought it might have been a quick answer to what I needed but might start down the named pipes method instead.

    Sunday, May 13, 2018 11:52 PM
  • Hi All

    I have a service that scans and processes images every 15 minutes. I wanted to run a GUI that would show progress values as it runs. Previously this was one windows forms app but I know want it to run whether the machine is logged on or not so I can transfer it to a server.

    I've started down the path of IPC and rigged up a Memory Mapped File library which works fine between say a console app and a form. Sadly I get no communication if I have a windows service and windows form. I've tried running the service as Local System and Local Service but I'm suspecting there maybe some memory or process isolation for security reasons that is preventing it communicating? The Memory Mapped files are non persistent. Have not tried file based yet, but will do to see if that helps. Machine is Windows 10.

    Cheers

    Hi Palace,

    About communicating with a windows service from a form application, I find one thread that you can take a look:

    https://social.msdn.microsoft.com/forums/vstudio/en-US/27dd76cf-a68b-41b8-beab-04f23187ea6a/best-way-to-communicate-with-a-windows-service-from-a-form-application

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 14, 2018 7:20 AM
    Moderator
  • Hi Cherry

    Yes I've been playing around with a few options. I liked the memory mapped option as it "seemed" the easiest as I wanted to capture a collection of status data from the running service. I've now looked at WCF and it too has some extra things to do to get it to work as detailed here

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-host-a-wcf-service-in-a-managed-windows-service

    So I'm going to try sockets next. It's a little old school, but as the Form and service are on the same server it should be OK. It has been a learning exercise and all the options work great until you want to run one as a service.

    Cheers

    Monday, May 14, 2018 1:08 PM
  • FWIW I usually use sockets with services, mostly because it leaves open the option of remote management/monitoring clients if you later decide it would be useful to talk to the service from other machines on the network.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, May 14, 2018 1:48 PM
    Moderator