Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a windows service in Visual C++ 2008, and I am able to start/stop the service. The problem I am facing is in the CreateProcess which I have written in OnStart method.

My OnStart method is:
virtual void OnStart(array<String^>^ args) override
{
   STARTUPINFO siStartupInfo;
   PROCESS_INFORMATION piProcessInfo;
   memset(&siStartupInfo, 0, sizeof(siStartupInfo));
   memset(&piProcessInfo, 0, sizeof(piProcessInfo));
   siStartupInfo.cb = sizeof(siStartupInfo);
   std::ofstream o("Hello.txt");

   if(CreateProcess(L"C:\\windows\\notepad.exe",
                    L"C:\\windows\\test.txt",
                    0,
                    0,
                    TRUE,
                    CREATE_NO_WINDOW,
                    0,
                    0,
                    &siStartupInfo,
                    &piProcessInfo) == FALSE)
    {
        o << "FALSE\n" << std::endl;
    }
    else
    {
        o << "TRUE \n" << std::endl;
    }
}

When I install and start the service, it starts successfully and the file Hello.txt is created and result of CreateProcess is written to file as "TRUE", but the notepad is not opened. Can you please help me in getting the notepad open when I start the service?

Thanks.
Posted
Updated 5-Jan-11 18:20pm
v2
Comments
JF2015 6-Jan-11 0:20am    
Edited to add code formatting.
Rajesh R Subramanian 6-Jan-11 4:05am    
Don't post your follow-up queries as "answers". Post a comment instead by clicking "Add comment" instead.

Try enabling 'Allo interact with desktop' option in the service property dialog. This might help.
 
Share this answer
 
Comments
Member 2430817 6-Jan-11 0:54am    
Thanks Rajeesh for your answer, i tried that option but still it does not work
Try this:
WIN32_FIND_DATA fd;
HANDLE          hf = FindFirstFile(L"C:\\windows\\notepad.exe",&fd);
ASSERT(INVALID_HANDLE_VALUE!=hf); FindClose(hf);
if(0==CreateProcess(L"C:\\windows\\notepad.exe",
                    L" C:\\windows\\test.txt",
// -------------------^ this is a bug
                    0,
                    0,
                    TRUE,
                    CREATE_NO_WINDOW,
                    0,
                    0,
                    &siStartupInfo,
                    &piProcessInfo))
 {
     o << "FALSE\n" << std::endl;
 }
 else
 {
     o << "TRUE \n" << std::endl;
 }

good luck.
 
Share this answer
 
v2
Comments
Member 2430817 6-Jan-11 22:55pm    
Tried this as well, but no luck
If you try to catch last error code what do you get ?

Also based on issue I also got to make it running, Since Vista and UAC it become incredibly more complex, like crazy, (consider switching to Linux, no joke!) to use CreateProcess.
There is no longer any kind of default that can help.
Specially if the process itself that you start need special access.
It may happens that running Notepad.exe is more complex than your targeted process. So no longer the obvious choice for a "quick try"

Also you need to use CREATE_BREAKAWAY_FROM_JOB flag during creation (just to help debugging it)
and create a manifest for UAC management

see http://stackoverflow.com/questions/89588/assignprocesstojobobject-fails-with-access-denied-error-when-running-under-the[^]

No idea about the "service" case.
Good luck.
 
Share this answer
 
Don't use the first argument of CreateProcess. Put all the command line in the second argument:
if(CreateProcess(
    NULL,
    L"C:\\windows\\notepad.exe C:\\windows\\test.txt",
    0,
    0,
    TRUE,
    CREATE_NO_WINDOW,
    0, 
    0,
    &siStartupInfo,
    &piProcessInfo) == FALSE)
{
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900