Click here to Skip to main content
16,021,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have followed this article and trying to make simple windows service that open notepad while running.

What I have tried:

i have tried this in the ServiceWorkerThread, but is not working as it intended to work. i have checked and installed service successful still i don't know why this is not opening notepad.exe!!



DWORD WINAPI ServiceWorkerThread (LPVOID lpParam)
{
    // Perform the main task of the service here
    OutputDebugString(_T("Always Ready: ServiceWorkerThread: Start"));

        // Launch notepad
    ShellExecute(NULL, _T("open"), _T("notepad.exe"), NULL, NULL, SW_SHOWDEFAULT);
    while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
    {
        // Perform main service function here
        // ...
        Sleep(2000); // Simulate some work being done
    }

    OutputDebugString(_T("Always Ready: ServiceWorkerThread: Stop"));

    return ERROR_SUCCESS;
}
Posted
Updated 16-Mar-23 1:23am

1 solution

You can't.
Services cannot have any user interface because they can run when there is no logged in user - and in an isolated session.
The isolated session means there is nowhere for an app to run, so a Service can't start any app.

You would need an app which runs under your user which communicates with the service, and have the service ask the app to run Notepad. But obviously, that would only work as long as the user is logged in, and the app is running.
 
Share this answer
 
Comments
sahil ajmeri 2022 16-Mar-23 7:47am    
how can i apply your second suggestion ? can you guide me please !!
OriginalGriff 16-Mar-23 8:20am    
What do you need help with? Writing an app?
sahil ajmeri 2022 16-Mar-23 9:26am    
I want to create windows service that checks weather graphics card available or not in the system.
if graphics card available then open notepad.exe else open Microsoft edge browser.
OriginalGriff 16-Mar-23 9:53am    
Why are you using a service at all? Neither Notepad or Word can open unless a user is logged in so a service just adds significant complexity to what you are trying to do. Just use a normal app and run it at startup.
Dave Kreskowiak 16-Mar-23 12:54pm    
Uhhh... There's ALWAYS a "graphics card" of some kind available in the system. Your "requirement" doesn't make any sense.

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