Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can I create a process in Windows service in MFC?

Can I create a process in Windows service in MFC?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++designquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laksh2204
    wrote on last edited by
    #1

    I am trying to do something like this: > STARTUPINFO si; > PROCESS_INFORMATION pi; > BOOL bRes = false; > DWORD dwCode = 0; > ZeroMemory ( &si, sizeof ( STARTUPINFO)); > si.cb = sizeof ( STARTUPINFO); > si.dwFlags = STARTF_USESHOWWINDOW; > si.wShowWindow = SW_SHOWNORMAL; > > bRes = CreateProcess( NULL, > (char *)it->second.c_str(), // some exe > NULL, > NULL, > TRUE, // also tried FALSE > NORMAL_PRIORITY_CLASS, //also tried 0 > NULL, > NULL, > &si, > &pi > ); > DWORD error = GetLastError(); > XERROR1( "ExecuteFailoverCommand :after execution Error=%d", error); > if( bRes == false ) > { > XERROR3( "ExecuteFailoverCommand :Error=%d, Could not invoke process %s > for service %s", error, it->second.c_str(), ServiceName.c_str()); > } > else > { > XTRACE2("ExecuteFailoverCommand : executed command %s on failure of %s > returned %d", it->second.c_str(), ServiceName.c_str(),bRes); > return true; > } This is working fine when I am calling CreateProcess from an exe but same is not working when it is being called from a running service. This may be due to reason that service puts the UI on a different desktop. So, is there any other solution to this problem. thanks in Adv

    L M 2 Replies Last reply
    0
    • L laksh2204

      I am trying to do something like this: > STARTUPINFO si; > PROCESS_INFORMATION pi; > BOOL bRes = false; > DWORD dwCode = 0; > ZeroMemory ( &si, sizeof ( STARTUPINFO)); > si.cb = sizeof ( STARTUPINFO); > si.dwFlags = STARTF_USESHOWWINDOW; > si.wShowWindow = SW_SHOWNORMAL; > > bRes = CreateProcess( NULL, > (char *)it->second.c_str(), // some exe > NULL, > NULL, > TRUE, // also tried FALSE > NORMAL_PRIORITY_CLASS, //also tried 0 > NULL, > NULL, > &si, > &pi > ); > DWORD error = GetLastError(); > XERROR1( "ExecuteFailoverCommand :after execution Error=%d", error); > if( bRes == false ) > { > XERROR3( "ExecuteFailoverCommand :Error=%d, Could not invoke process %s > for service %s", error, it->second.c_str(), ServiceName.c_str()); > } > else > { > XTRACE2("ExecuteFailoverCommand : executed command %s on failure of %s > returned %d", it->second.c_str(), ServiceName.c_str(),bRes); > return true; > } This is working fine when I am calling CreateProcess from an exe but same is not working when it is being called from a running service. This may be due to reason that service puts the UI on a different desktop. So, is there any other solution to this problem. thanks in Adv

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      laksh2204 wrote:

      This may be due to reason that service puts the UI on a different desktop.

      Possibly.

      laksh2204 wrote:

      So, is there any other solution to this problem.

      Maybe, depends on your definition of the "this problem". Most services don't have a user interface because they don't need one due to the nature of the problem they are solving. What problem are you trying to solve?

      led mike

      1 Reply Last reply
      0
      • L laksh2204

        I am trying to do something like this: > STARTUPINFO si; > PROCESS_INFORMATION pi; > BOOL bRes = false; > DWORD dwCode = 0; > ZeroMemory ( &si, sizeof ( STARTUPINFO)); > si.cb = sizeof ( STARTUPINFO); > si.dwFlags = STARTF_USESHOWWINDOW; > si.wShowWindow = SW_SHOWNORMAL; > > bRes = CreateProcess( NULL, > (char *)it->second.c_str(), // some exe > NULL, > NULL, > TRUE, // also tried FALSE > NORMAL_PRIORITY_CLASS, //also tried 0 > NULL, > NULL, > &si, > &pi > ); > DWORD error = GetLastError(); > XERROR1( "ExecuteFailoverCommand :after execution Error=%d", error); > if( bRes == false ) > { > XERROR3( "ExecuteFailoverCommand :Error=%d, Could not invoke process %s > for service %s", error, it->second.c_str(), ServiceName.c_str()); > } > else > { > XTRACE2("ExecuteFailoverCommand : executed command %s on failure of %s > returned %d", it->second.c_str(), ServiceName.c_str(),bRes); > return true; > } This is working fine when I am calling CreateProcess from an exe but same is not working when it is being called from a running service. This may be due to reason that service puts the UI on a different desktop. So, is there any other solution to this problem. thanks in Adv

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        laksh2204 wrote:

        This may be due to reason that service puts the UI on a different desktop

        It would be helpful to know if that's actually the case. "not working" is pretty vague don't you think? Have you tried using the STARTUPINFO.lpDesktop field? You may also want to consider using CreateProcessAsUser() instead of creating the process as the user the service is running as. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        L 1 Reply Last reply
        0
        • M Mark Salsbery

          laksh2204 wrote:

          This may be due to reason that service puts the UI on a different desktop

          It would be helpful to know if that's actually the case. "not working" is pretty vague don't you think? Have you tried using the STARTUPINFO.lpDesktop field? You may also want to consider using CreateProcessAsUser() instead of creating the process as the user the service is running as. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Well you were a little late, but welcome to the fishing derby! :beer:

          led mike

          M 1 Reply Last reply
          0
          • L led mike

            Well you were a little late, but welcome to the fishing derby! :beer:

            led mike

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            led mike wrote:

            you were a little late

            Yeah, but nothing else going on here - so what the heck :)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            L 1 Reply Last reply
            0
            • M Mark Salsbery

              led mike wrote:

              you were a little late

              Yeah, but nothing else going on here - so what the heck :)

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              L Offline
              L Offline
              laksh2204
              wrote on last edited by
              #6

              Actually, I have a service running on Windows Server2003 sp2 called locator. This service keeps a control over another 4 services in the system. If any one of these four is down, I am supposed to run a process say Handler which is yet to be developed. So I was trying with an existing like cmd,calc etc... Now As Mark is saying use CreateProcessAsUser() and lpDesktop, I have a problem that I am new to windows domain. I don't know what should be the value for lpDesktop or for hToken i.e. first parameter of CreateProcessAsUser(). I am getting error 6 INVALID_HANDLE. I think, The discussion stopped due to timezone difference. --Arun

              A M 2 Replies Last reply
              0
              • L laksh2204

                Actually, I have a service running on Windows Server2003 sp2 called locator. This service keeps a control over another 4 services in the system. If any one of these four is down, I am supposed to run a process say Handler which is yet to be developed. So I was trying with an existing like cmd,calc etc... Now As Mark is saying use CreateProcessAsUser() and lpDesktop, I have a problem that I am new to windows domain. I don't know what should be the value for lpDesktop or for hToken i.e. first parameter of CreateProcessAsUser(). I am getting error 6 INVALID_HANDLE. I think, The discussion stopped due to timezone difference. --Arun

                A Offline
                A Offline
                Ahmed Charfeddine
                wrote on last edited by
                #7

                hello, you said that you already have the "locator" service that keep control of the other 4 ones. I think,if possible, that instead of developing another service, the "handler" that would execute a certain code under the scrash event of one of the other 4 services,then just put that code in the "locator", why complicate it ? Of course I suppose here that you have hands on the "locator" source code, something you didn't said. Regarding the use of createProcess,etc..I wonder why you develop an MFC app for such thing as to respond to the crashing of an NT service, then may be restart it from using that MFC app. It is right, your UI wo'nt showup when exectued in context of a service.It's like there is a fictiouness desktop with which services interact I remember myself even to have problem trying to debug a service by trying to catch the TRACEs and log them into an UI application Window. I am also know that it is said that it is not advised even to try make a service interact with session-wise objects : for example try to communicate something in the desktop of a logged user by the service. Do not even suppose that while your service is running or that when it has crashed that there is a user who is logged that time in order to notify him. I have something like that : a remote service (developed by means of CUTService : a very good class that helps you write services : it is included in Ultimate TCP/IP you find here in CodeProject) which I control by a remote application which collects performances and key indicators but also with another local service (ie in the same server machine as the first service) which can open local socket channel and do efficient control of the service state..but no interaction with the desktop of the remote server machine) Anyway..try do something and come here back if there is something to talk about.

                Our Philosophy, Mohammed Baqir Al Sadr

                1 Reply Last reply
                0
                • L laksh2204

                  Actually, I have a service running on Windows Server2003 sp2 called locator. This service keeps a control over another 4 services in the system. If any one of these four is down, I am supposed to run a process say Handler which is yet to be developed. So I was trying with an existing like cmd,calc etc... Now As Mark is saying use CreateProcessAsUser() and lpDesktop, I have a problem that I am new to windows domain. I don't know what should be the value for lpDesktop or for hToken i.e. first parameter of CreateProcessAsUser(). I am getting error 6 INVALID_HANDLE. I think, The discussion stopped due to timezone difference. --Arun

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  laksh2204 wrote:

                  I don't know what should be the value for lpDesktop

                  You could try "Winsta0\default". Which desktop you use and which account the spawned process runs as are two separate issues. If your service runs with privileges above what the user should have (as is often the case), then you need to use CreateProcessAsUser(). You should really study the docs carefully - I haven't gone into why a service that interacts with the desktop and/or current user is a bad idea. It's not guaranteed to work in future operating systems. Services[^] Window Stations and Desktops[^] Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  L 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    laksh2204 wrote:

                    I don't know what should be the value for lpDesktop

                    You could try "Winsta0\default". Which desktop you use and which account the spawned process runs as are two separate issues. If your service runs with privileges above what the user should have (as is often the case), then you need to use CreateProcessAsUser(). You should really study the docs carefully - I haven't gone into why a service that interacts with the desktop and/or current user is a bad idea. It's not guaranteed to work in future operating systems. Services[^] Window Stations and Desktops[^] Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    L Offline
                    L Offline
                    laksh2204
                    wrote on last edited by
                    #9

                    I think its not a bad idea in general scenario. But might not be good idea in my scenario. As according to my problem when a particular service is down, the user which is logged in must have a process running which can handle the situation. In this way, that user is required to be logged in permanently. Which for me doesn't seems to be a good idea. In other words, What if no user is logged in and one of those services goes down. No one will be there to handle the situation. However, "Winsta0\default" looks quite different to me. May be like someone who is always there. In this case it should work. I'll try it out. Thanks everyone for your valuable time. This forum is really one of the bests I've ever seen. :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups