| | |
How to create windows service for EXE??
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I thought long and hard about how to solve this problem.
But after a while a decided to change Salem's searchstring ( "
http://clusty.com/search?input-form=clusty-simple&v%3Asources=webplus&query=how+to+create+a+windows+service+in+c%2B%2B
A lot of interesting search results.
Damn. That was hard
But after a while a decided to change Salem's searchstring ( "
how to create a windows service ") to "how to create a windows service in c++" I got this:http://clusty.com/search?input-form=clusty-simple&v%3Asources=webplus&query=how+to+create+a+windows+service+in+c%2B%2B
A lot of interesting search results.
Damn. That was hard
Last edited by niek_e; Jun 6th, 2008 at 4:36 am.
•
•
Join Date: Apr 2008
Posts: 293
Reputation:
Solved Threads: 11
In my code it gives error like
It gives error start service failed 1053
C++ Syntax (Toggle Plain Text)
if (!StartService( schService, 0, NULL) ) { printf("Hello"); printf("\nStartService failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; }
It gives error start service failed 1053
•
•
Join Date: Apr 2008
Posts: 293
Reputation:
Solved Threads: 11
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <stdio.h> #include<conio.h> #define szSvcName TEXT("abc") #define SVCNAME TEXT("abc") SC_HANDLE schSCManager; SC_HANDLE schService; VOID DoStartSvc(); VOID SvcInstall() { SC_HANDLE schSCManager; SC_HANDLE schService; TCHAR szPath[MAX_PATH]; if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ) { printf("Cannot install service (%d)\n", GetLastError()); return; } schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == schSCManager) { printf("OpenSCManager failed (%d)\n", GetLastError()); return; } schService = CreateService( schSCManager, SVCNAME, SVCNAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START , SERVICE_ERROR_NORMAL, szPath, NULL, NULL, NULL, NULL, NULL); if (schService == NULL) { printf("CreateService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } else printf("Service installed successfully\n"); DoStartSvc(); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } VOID DoStartSvc() { printf("\nIn DoStart"); SERVICE_STATUS_PROCESS ssStatus; DWORD dwOldCheckPoint; DWORD dwStartTickCount; DWORD dwWaitTime; DWORD dwBytesNeeded; schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == schSCManager) { printf("OpenSCManager failed (%d)\n", GetLastError()); return; } printf("\nIn open service"); schService = OpenService( schSCManager, szSvcName, SERVICE_ALL_ACCESS); if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); printf("\nIn service Null"); CloseServiceHandle(schSCManager); return; } if (!QueryServiceStatusEx( schService, SC_STATUS_PROCESS_INFO, (LPBYTE) &ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) ) { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING) { printf("Cannot start the service because it is already running\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) { dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 ) dwWaitTime = 1000; else if ( dwWaitTime > 10000 ) dwWaitTime = 10000; Sleep( dwWaitTime ); if (!QueryServiceStatusEx( schService, SC_STATUS_PROCESS_INFO, (LPBYTE) &ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) ) { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) { dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; } else { if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) { printf("Timeout waiting for service to stop\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } } } if (!StartService( schService, 0, NULL) ) { printf("Hello"); printf("\nStartService failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } else printf("Service start pending...\n"); if (!QueryServiceStatusEx( schService, SC_STATUS_PROCESS_INFO, (LPBYTE) &ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) ) { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; while (ssStatus.dwCurrentState == SERVICE_START_PENDING) { dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 ) dwWaitTime = 1000; else if ( dwWaitTime > 10000 ) dwWaitTime = 10000; Sleep( dwWaitTime ); if (!QueryServiceStatusEx( schService, SC_STATUS_PROCESS_INFO, (LPBYTE) &ssStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) ) { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); break; } if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) { dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; } else { if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) { break; } } } if (ssStatus.dwCurrentState == SERVICE_RUNNING) { printf("Service started successfully.\n"); } else { printf("Service not started. \n"); printf(" Current State: %d\n", ssStatus.dwCurrentState); printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode); printf(" Check Point: %d\n", ssStatus.dwCheckPoint); printf(" Wait Hint: %d\n", ssStatus.dwWaitHint); } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); } int main() { SvcInstall(); getch(); }
No way! I thought you made it yourself 
Anyway:
"abc" isn't really a service is it? So it can't be started.
These kind programs are quite difficult for a beginner. (your getch() gave you away)
I suggest you start of with some simpler programs...

Anyway:
#define szSvcName TEXT("abc") "abc" isn't really a service is it? So it can't be started.
These kind programs are quite difficult for a beginner. (your getch() gave you away)
I suggest you start of with some simpler programs...
Last edited by niek_e; Jun 6th, 2008 at 7:56 am.
![]() |
Similar Threads
- How to create a windows service? (Visual Basic 4 / 5 / 6)
- Problem with Windows Service (C#)
- VX2? Nail.exe? Aurora popups? H E L P ! (Viruses, Spyware and other Nasties)
- For some reason program installations cannot create new start menu shortcuts... (Windows NT / 2000 / XP)
- winlgon.exe? (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Socket connecting to telnet server
- Next Thread: issues with static_cast< double >
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






