473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to create a service and run in service control panel

Hi,

I used the following code to create a new service in visual c++6.0. while i am running it (using ctrl+F5) it doesnot shows any output.


Expand|Select|Wrap|Line Numbers
  1. // sample.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Windows.h"
  6. #include "Winsvc.h"
  7. #include "time.h"
  8. #include "stdio.h"
  9.  
  10. SERVICE_STATUS m_ServiceStatus;
  11. SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
  12. BOOL bRunning=true;
  13. void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
  14. void WINAPI ServiceCtrlHandler(DWORD Opcode);
  15. BOOL InstallService();
  16. BOOL DeleteService();
  17. int main(int argc, char* argv[])
  18. {
  19.   if(argc>1)
  20.   {
  21.     if(strcmp(argv[1],"-i")==0)
  22.     {
  23.       if(InstallService())
  24.         printf("\n\nService Installed Sucessfully\n");
  25.       else
  26.         printf("\n\nError Installing Service\n");
  27.     }
  28.     if(strcmp(argv[1],"-d")==0)
  29.     {
  30.       if(DeleteService())
  31.         printf("\n\nService UnInstalled Sucessfully\n");
  32.       else
  33.         printf("\n\nError UnInstalling Service\n");
  34.     }
  35.     else
  36.     {
  37.       printf("\n\nUnknown Switch Usage\n\nFor Install use Srv1 -i\n\nFor UnInstall use Srv1 -d\n");
  38.     }
  39.   }
  40.   else
  41.   {
  42.     SERVICE_TABLE_ENTRY DispatchTable[]=
  43.                 {{"Service1",ServiceMain},{NULL,NULL}};
  44.     StartServiceCtrlDispatcher(DispatchTable);
  45.   }
  46.   return 0;
  47. }
  48.  
  49. void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
  50. {
  51.   DWORD status;
  52.   DWORD specificError;
  53.   m_ServiceStatus.dwServiceType = SERVICE_WIN32;
  54.   m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  55.   m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  56.   m_ServiceStatus.dwWin32ExitCode = 0;
  57.   m_ServiceStatus.dwServiceSpecificExitCode = 0;
  58.   m_ServiceStatus.dwCheckPoint = 0;
  59.   m_ServiceStatus.dwWaitHint = 0;
  60.  
  61.   m_ServiceStatusHandle = RegisterServiceCtrlHandler("svc", ServiceCtrlHandler); 
  62.   if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
  63.   {
  64.     return;
  65.   }
  66.   m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  67.   m_ServiceStatus.dwCheckPoint = 0;
  68.   m_ServiceStatus.dwWaitHint = 0;
  69.   if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
  70.   {
  71.   }
  72.  
  73.   bRunning=true;
  74.   while(bRunning)
  75.   {
  76.     Sleep(3000);
  77.     //Place Your Code for processing here....
  78.   }
  79.   return;
  80. }
  81.  
  82. void WINAPI ServiceCtrlHandler(DWORD Opcode)
  83. {
  84.   switch(Opcode)
  85.   {
  86.     case SERVICE_CONTROL_PAUSE: 
  87.       m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
  88.       break;
  89.     case SERVICE_CONTROL_CONTINUE:
  90.       m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  91.       break;
  92.     case SERVICE_CONTROL_STOP:
  93.       m_ServiceStatus.dwWin32ExitCode = 0;
  94.       m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  95.       m_ServiceStatus.dwCheckPoint = 0;
  96.       m_ServiceStatus.dwWaitHint = 0;
  97.  
  98.       SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
  99.       bRunning=false;
  100.       break;
  101.     case SERVICE_CONTROL_INTERROGATE:
  102.       break; 
  103.   }
  104.   return;
  105. }
  106.  
  107. BOOL InstallService()
  108. {
  109.   char strDir[1024];
  110.   HANDLE schSCManager,schService;
  111.   GetCurrentDirectory(1024,strDir);
  112.   strcat(strDir,"\\Srv1.exe"); 
  113.   schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  114.  
  115.   if (schSCManager == NULL) 
  116.     return false;
  117.   LPCTSTR lpszBinaryPathName=strDir;
  118.  
  119.   schService = CreateService(schSCManager,"Service1", 
  120.         "The Display Name Needed", // service name to display
  121.      SERVICE_ALL_ACCESS, // desired access 
  122.      SERVICE_WIN32_OWN_PROCESS, // service type 
  123.      SERVICE_DEMAND_START, // start type 
  124.      SERVICE_ERROR_NORMAL, // error control type 
  125.      lpszBinaryPathName, // service's binary 
  126.      NULL, // no load ordering group 
  127.      NULL, // no tag identifier 
  128.      NULL, // no dependencies
  129.      NULL, // LocalSystem account
  130.      NULL); // no password
  131.  
  132.   if (schService == NULL)
  133.     return false; 
  134.  
  135.   CloseServiceHandle(schService);
  136.   return true;
  137. }
  138.  
  139. BOOL DeleteService()
  140. {
  141.   HANDLE schSCManager;
  142.   SC_HANDLE hService;
  143.   schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  144.  
  145.   if (schSCManager == NULL)
  146.     return false;
  147.   hService=OpenService(schSCManager,"Service1",SERVICE_ALL_ACCESS);
  148.   if (hService == NULL)
  149.     return false;
  150.   if(DeleteService(hService)==0)
  151.     return false;
  152.   if(CloseServiceHandle(hService)==0)
  153.     return false;
  154.  
  155. return true;
  156. }

what i missed here. i couldn't see the service1 in service control panel. how to resolve it.

thanks in advance
Jul 31 '13 #1
0 1111

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: R6_2003 | last post by:
Hello all, i dunno if that's the right newsgroup to ask, but i'll try, please ignore me if u feel its not 0:) i've been messing with a control panel app for so long.. previously i was using...
2
by: AKR | last post by:
I have to create control panel applet using C# . I have read in the net that it is not possible using C# . How can i create control panel applet using C# .
0
by: GregO | last post by:
Windows 2000 Server SP3 ..NET Framework 1.1 I have a problem opening the control panel on a number of servers. Only when we kill the asp_wp.exe does the control panel kick into life. Is it...
3
by: pealy2 | last post by:
Sorry if this is in the wrong group, I've searched long & hard without finding anything even slightly useful. (recommendations for a more relevant group gratefuly received) I need to change the...
1
by: Glenn | last post by:
Hello Is there any documentation around which shows you ( if it is possible) how to create a new category for the windows xp control panel.( in category view) I would like to create a new...
7
by: Jay | last post by:
Hey There, I've been trying to see if there is a way to programmatically block, or hide, the Control Panel. Since it is a "Virtual Folder", just blocking an .exe from running doesn't work. Even...
1
by: Frank Burleigh | last post by:
A while ago I installed Net 2.0 to our Server 2003 web machine, but the Control Panel folder icon for configuring Net 2.0 never made it to the Control Panel folder. Thinking this might indicate a...
0
by: strontiumpaul | last post by:
Hi, First, let me state, I am a total newbie!!! C# Application. I want to create a floating control panel similar to the PhotoShop's floating designer panel (with images as buttons). It must...
3
by: Grok | last post by:
My C# application is a Windows Service. I have two problems that I would like to solve in the correct way using C#.NET. 1) There are a number of user settings which I'd like to store in an XML...
2
by: xtremebass | last post by:
Hi Bytes, In my system, cant show the tool Add/Remove Programs in Control Panel. can you please suggest how can i recover it? Os :Windows XP,Service Pack 3, Help..
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.