CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    how to start a MFC.exe using a windows service Application?

    Hi,

    I want to execute a exe file from windows service application. The below code runs in Console Application and ran successfully.
    But Not working through Windows service application.

    Code:
    ShellExecute(NULL, "Open", "C:\\Users\\sara\\Desktop\\MFCScreen.exe", NULL, NULL ,SW_SHOWDEFAULT);
    In Service app ShellExecute not working.

    Code:
    TCHAR* path = "C:\\Users\\sara\\Desktop\\MFCScreen.exe";
    
    STARTUPINFO info;
    PROCESS_INFORMATION processInfo;
    
    ZeroMemory( &info, sizeof(info) );
    info.cb = sizeof(info);
    ZeroMemory( &processInfo, sizeof(processInfo) );
    
    
    if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
    {
        ::WaitForSingleObject(processInfo.hProcess, INFINITE);
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);
    }
    In service application if condition failed.

    Pls clear me.
    Regards,

    SaraswathiSrinath

  2. #2
    Join Date
    Aug 2006
    Posts
    231

    Re: how to start a MFC.exe using a windows service Application?

    You can send the command as second parameter.

    See example:

    https://docs.microsoft.com/en-us/win...ting-processes

    If still not working, call GetLastError() and let us know what it returns.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how to start a MFC.exe using a windows service Application?

    Unless the service uses the "Local System account" and has the "Allow service to interact with desktop" checkbox checked, the service will be running in a different desktop than a logged on user.

    Allowing the service to interact with the desktop has security implications and isn't recommended by MS. In addition, if there are multiple logged on users, the service wouldn't know which interactive desktop to use.

    To find out more, search for "launching an interactive process from a windows service".
    Last edited by Arjay; August 23rd, 2018 at 03:22 PM.

  4. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by TubularX View Post
    You can send the command as second parameter.
    Couldn't start my service application, Getting below Error

    Windows Could not start the services on local computer
    error 1067 the process terminated unexpectedly
    Code:
    // Internal name of the service
    #define SERVICE_NAME             L"AServiceApp"
    
    // Displayed name of the service
    #define SERVICE_DISPLAY_NAME     L"AServiceApp"
    
    // Service start options.
    #define SERVICE_START_TYPE       SERVICE_DEMAND_START
    
    // List of service dependencies - "dep1\0dep2\0\0"
    #define SERVICE_DEPENDENCIES     L""
    
    // The name of the account under which the service should run
    #define SERVICE_ACCOUNT          L"NT AUTHORITY\\LocalService"
    
    // The password to the service account name
    #define SERVICE_PASSWORD         NULL
    Regards,

    SaraswathiSrinath

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by saraswathisrinath View Post
    Couldn't start my service application
    You definitely have an error somewhere in your code.

    Now getting serious. What is your plan here? You show us no code, neither MFC nor service app. So how do you expect us to help? By recommending a good reading on Windows programming in C/C++? Jeffrey Richter is the name to start with.
    Last edited by Igor Vartanov; August 31st, 2018 at 04:33 PM.
    Best regards,
    Igor

  6. #6
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by Igor Vartanov View Post
    You definitely have an error somewhere in your code.
    True. I',m using database connection with my service application. Some time Database couldn't connect, tats why getting Error:1067


    Quote Originally Posted by Igor Vartanov View Post
    Now getting serious. What is your plan here? You show us no code, neither MFC nor service app. So how do you expect us to help? By recommending a good reading on Windows programming in C/C++? Jeffrey Richter is the name to start with.
    I was download the and use the code from internet. so only didn't showed my code

    A basic Windows service in C++
    using the above link, my service application couldn't open another application file.

    So i was go with the below link. Now i can use the windows service application with the combination off database and execute exe from service application.
    Launch your application in Vista under the local system account without the UAC popup


    But now getting one more issues,
    every 10 minutes, i was updating my database from windows service application. If time forward (one month/year) then suddenly my windows service application stopped. What is the problem with this?
    Please clear me. If need tell me i will attach my code
    Regards,

    SaraswathiSrinath

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by saraswathisrinath View Post
    True. I',m using database connection with my service application. Some time Database couldn't connect, tats why getting Error:1067



    I was download the and use the code from internet. so only didn't showed my code

    A basic Windows service in C++
    using the above link, my service application couldn't open another application file.

    So i was go with the below link. Now i can use the windows service application with the combination off database and execute exe from service application.
    Launch your application in Vista under the local system account without the UAC popup


    But now getting one more issues,
    every 10 minutes, i was updating my database from windows service application. If time forward (one month/year) then suddenly my windows service application stopped. What is the problem with this?
    Please clear me. If need tell me i will attach my code
    You have an error in your code causing the service to crash. Have you looked in the event viewer to see if the error was logged?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to start a MFC.exe using a windows service Application?

    When you call an API (or any function/method), are you checking for failure/error every time? if not, then I suggest you do. If you are then I suggest you log the error codes to a file so you see what is happening.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by Arjay View Post
    You have an error in your code causing the service to crash. Have you looked in the event viewer to see if the error was logged?
    In event viewer showed as SERVICE_CONTROL_STOP.
    Regards,

    SaraswathiSrinath

  10. #10
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by 2kaud View Post
    When you call an API (or any function/method), are you checking for failure/error every time? if not, then I suggest you do. If you are then I suggest you log the error codes to a file so you see what is happening.
    How to check the failure/error every time? is ServiceCtrlHandler do that work?
    Code:
    void WINAPI ServiceCtrlHandler(DWORD Opcode)
    {	
      WriteEventLogEntry(  "ServiceCtrlHandler called " , EVENTLOG_INFORMATION_TYPE);
      switch(Opcode)
      {
    ////////////////////////////////////////////////////////////////////////////////////
    //Added By Sara, to recieve a custom message from a user app
    	
    	case SERVICE_CONTROL_CUSTOM_MESSAGE:
    		//LaunchAppIntoDifferentSession(); 
    		WriteEventLogEntry(  "SERVICE_CONTROL_CUSTOM_MESSAGE" , EVENTLOG_INFORMATION_TYPE);
    	break; 
    	case SERVICE_CONTROL_PAUSE: 
          m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
    		WriteEventLogEntry(  "SERVICE_CONTROL_PAUSE" , EVENTLOG_INFORMATION_TYPE);
          break;
        case SERVICE_CONTROL_CONTINUE:
          m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    		WriteEventLogEntry(  "SERVICE_CONTROL_CONTINUE" , EVENTLOG_INFORMATION_TYPE);
          break;
        case SERVICE_CONTROL_STOP:
          m_ServiceStatus.dwWin32ExitCode = 0;
          m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
          m_ServiceStatus.dwCheckPoint = 0;
          m_ServiceStatus.dwWaitHint = 0;
    	  SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
    
    		WriteEventLogEntry(  "SERVICE_CONTROL_STOP" , EVENTLOG_INFORMATION_TYPE);
    		QueryStr = "INSERT INTO `LOG` (`TIME_STAMP`,`MESSAGE`) VALUES (CURRENT_TIMESTAMP, 'Service Stoped' )";
    		DB.ExecuteSQL(QueryStr);
    		DB.ExecuteSQL(FlushStr);
    
          bRunning=false;
          break;
        case SERVICE_CONTROL_INTERROGATE:
    		WriteEventLogEntry(  "SERVICE_CONTROL_INTERROGATE" , EVENTLOG_INFORMATION_TYPE);
          break; 
      }
      return;
    }
    Regards,

    SaraswathiSrinath

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how to start a MFC.exe using a windows service Application?

    Code:
    if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
    {
    ...}
    in service application if condition failed.
    yes, but with what error? What does GetLastError() return if CreateProcess() fails? This is what I meant by checking for API failures and logging error codes. For every call to an API, there should be a check for failure and for an unexpected failure the error code (see individual description of the API to see how this is provided) should be logged/reported.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by saraswathisrinath View Post
    In event viewer showed as SERVICE_CONTROL_STOP.
    You need to capture the return code from CreateProcess and log it if it is ==0 with the WriteEventLogEntry method.
    Last edited by Arjay; September 4th, 2018 at 12:25 PM. Reason: Cp returns 0 for failure

  13. #13
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by 2kaud View Post
    [code]
    For every call to an API, there should be a check for failure and for an unexpected failure the error code (see individual description of the API to see how this is provided) should be logged/reported.
    As you told Added Like below,
    Code:
     bResult = CreateProcessAsUser(
          hUserTokenDup,            // client's access token
          _T("C:\\System32\\Test.exe"),              // file to execute
          NULL,     // command line
          NULL,              // pointer to process SECURITY_ATTRIBUTES
          NULL,              // pointer to thread SECURITY_ATTRIBUTES
          FALSE,             // handles are not inheritable
          dwCreationFlags,  // creation flags
          pEnv,              // pointer to new environment block 
          NULL,              // name of current directory 
          &si,               // pointer to STARTUPINFO structure
          &pi                // receives information about new process
       );
    // End impersonation of client.
    
    //GetLastError Shud be 0
    
       int iResultOfCreateProcessAsUser = GetLastError();
    
       sTemp.Format("iResultOfCreateProcessAsUser = %d ",iResultOfCreateProcessAsUser);
       WriteEventLogEntry( sTemp , EVENTLOG_INFORMATION_TYPE);
    Now i can call exe file from my service application. Only Test.exe was opened when calling from out side of System32 location and returned error code iResultOfCreateProcessAsUser = 0 .
    I like to open my Test.exe file from any location of my computer. Is it possible?


    Quote Originally Posted by Arjay View Post
    You need to capture the return code from CreateProcess and log it if it is ==0 with the WriteEventLogEntry method.
    Test.exe tried to open from System32 location, exe was not opened and returned the error code iResultOfCreateProcessAsUser = 3.
    Pls clear me.
    Regards,

    SaraswathiSrinath

  14. #14
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: how to start a MFC.exe using a windows service Application?

    every 10 minutes, i was updating my database from windows service application. If time forward / Reversed then suddenly my windows service application stopped.
    Code:
    BOOL updateDB()
    {
            QueryStr = "UPDATE TESTTABLE SET FLAG = '" + oCSV.sflag + "' WHERE CNT='1'";		
    	WriteEventLogEntry(  QueryStr , EVENTLOG_INFORMATION_TYPE);		
    	TRY
    	{
    		DB.ExecuteSQL(QueryStr);		
    		DB.ExecuteSQL(FlushStr);		
    	}
    	CATCH(CDBException, e)
    	{
    		WriteEventLogEntry(  e->m_strError , EVENTLOG_INFORMATION_TYPE);
    		return FALSE;
    	}
    	END_CATCH;
    }
    Output from Event log:
    UPDATE TESTTABLE SET FLAG = '1' WHERE CNT='1'
    MySQL server has gone away

    Then My Service application stopped. But Status Showing like Started in services.msc. (no service stop option)
    Regards,

    SaraswathiSrinath

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how to start a MFC.exe using a windows service Application?

    Quote Originally Posted by saraswathisrinath View Post
    Test.exe tried to open from System32 location, exe was not opened and returned the error code iResultOfCreateProcessAsUser = 3.
    Pls clear me.
    Just read the MSDN:
    ERROR_PATH_NOT_FOUND

    3 (0x3)

    The system cannot find the path specified.
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured