-
August 22nd, 2018, 11:16 PM
#1
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
-
August 23rd, 2018, 06:30 AM
#2
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.
-
August 23rd, 2018, 03:19 PM
#3
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.
-
August 23rd, 2018, 11:38 PM
#4
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by TubularX
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
-
August 31st, 2018, 04:29 PM
#5
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by saraswathisrinath
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
-
September 3rd, 2018, 12:00 AM
#6
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by Igor Vartanov
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
Originally Posted by Igor Vartanov
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
-
September 3rd, 2018, 01:05 AM
#7
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by saraswathisrinath
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?
-
September 3rd, 2018, 01:57 AM
#8
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)
-
September 4th, 2018, 04:51 AM
#9
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by Arjay
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
-
September 4th, 2018, 05:03 AM
#10
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by 2kaud
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
-
September 4th, 2018, 05:58 AM
#11
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)
-
September 4th, 2018, 09:05 AM
#12
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by saraswathisrinath
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
-
September 6th, 2018, 01:58 AM
#13
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by 2kaud
[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?
Originally Posted by Arjay
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
-
September 6th, 2018, 02:05 AM
#14
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
-
September 6th, 2018, 02:37 AM
#15
Re: how to start a MFC.exe using a windows service Application?
Originally Posted by saraswathisrinath
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|