How to create an MFC based Windows NT Service

Like the title says I need to create a pretty basic Win NT 4 Service program which can run on an NT machine. I have gone through some samples from the Codeguru and Codeproject Site but came up with ERRORS in both..

I need help fast. If anyone has any sample code , pointers , hints , tips , tutorials , websites , projects which do all / some or any thing to do with NT services can they please reply

ThanX in Advance

LK--<
LizardKingAsked:
Who is Participating?
 
yesnathan22Connect With a Mentor Commented:
Hello Lizard King
         do you accept the answer???Then give me the points ya
0
 
naveenkohliCommented:
Since you already have got samples from Codeguru and Codeproject, may be with a little bit of more work you shoul dbe able to trace the errors and make them work.

BTW, what kind of errors you are running into with those samples.
0
 
RONSLOWCommented:
Have you considered using ATL instead of MFC ??  Or even use WTL (ATL extenstion - get this from latest Platform SDK) ??

MFC is fairly 'heavy' for use as a service.

But there is a good starting point in MSDN ("Creating a Simple Windows NT Service in C++") which has some smaple code.

But, if you cannot get the smaples at codeguru and codeproject working, it could well be that you are doing something fundamentally wrong.

BTW: What version of Visual C++ do you have?  I seem to remember there being problems with the linker and/or settings when building servies with some (later) VC versions.  I have an NT service here .. but its been so long since I've (re)build it, I've forgotten the issues involved - think it was with VC5 ????


0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
LizardKingAuthor Commented:
naveenkohli
I have worked through the samples given and have problems migrating the sample code into my project. The errors I get involve The service crashing and not being able to stop it from the service manager...


RONSLOW
Yeah I probably am doing something FUNDAMENTALLY wrong. The samples I went through had issues wrong with them that were documented. Such as problems registering service and such. I just asked if anyone has seen / done any other types of services. I will have a look at MSDN ("Creating a Simple Windows NT Service in C++")


THANx

LK-----<
0
 
RONSLOWCommented:
Can you get the samples from codeguru etc working as is ??

Have you considered an ATL NT Service app ??

0
 
raschalkCommented:
MSDN has some good examples. There is also a simple way to create a service from visual basic using an OLE control. This is also described in MSDN.

--Rudy
0
 
yesnathan22Commented:
hi Lizerdking
   
      already i wrote one NT service program in SDK and it works well.(i hope you know the basics of NT service)This the code for Service program.(you should write seprate program from installing service)

From main()

                         SERVICE_TABLE_ENTRY   stServiceTableEntry;
            char *lpParamServiceName = "SA";
            char  szTempDisp[100];
      
            stServiceTableEntry.lpServiceName =  lpParamServiceName;
            stServiceTableEntry.lpServiceProc  = ( LPSERVICE_MAIN_FUNCTION)ServiceMain;
            int iErr ;
      
            if(!StartServiceCtrlDispatcher(&stServiceTableEntry))
            {
                  // Error in starting dispatcher
                  iErr = GetLastError();
                     wsprintf(szTempDisp,"error in starting dispatcher %d",iErr);
                  MessageBox(NULL,szTempDisp,"SOCKSERVICE",MB_OK);
            }



This is the service main function


void  WINAPI  ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
      unsigned long iThread1;
      MyServiceStatusHandle= RegisterServiceCtrlHandler( "SA", (LPHANDLER_FUNCTION)fnHandler);
      
      SERVICE_STATUS stServiceStatus;
      stServiceStatus.dwServiceType =SERVICE_WIN32_OWN_PROCESS;
    stServiceStatus.dwCurrentState = SERVICE_RUNNING;
    stServiceStatus.dwControlsAccepted= SERVICE_ACCEPT_STOP;
      stServiceStatus.dwWin32ExitCode =NO_ERROR;
      stServiceStatus.dwCheckPoint= 0;
    stServiceStatus.dwWaitHint = 0;
      //Set Service Status
      SetServiceStatus(MyServiceStatusHandle,&stServiceStatus);
      
      MessageBox(NULL,"inside servicemain ","SAMPLESERVICE",MB_OK);
      //Creation of primary Thread
// you should call your function here
}

Handler function for service


void WINAPI  fnHandler(DWORD  dwControl)
{
          char szTempDisp[100];
            
            wsprintf(szTempDisp,"Inside handler with %d",dwControl);
            MessageBox(NULL,szTempDisp,"SERVICE",MB_OK);
          switch (dwControl)
            {
            case SERVICE_STOP:
                  MessageBox(NULL,"STOPPNG","SERVICE-STOP",MB_OK);
                  exit(0);
                  break;
            case SERVICE_CONTROL_STOP:
                  MessageBox(NULL,"CONTROLSTOPPING","SERVICE-CONTROL-STOP",MB_OK);
                  SERVICE_STATUS stServiceStatus;
                  stServiceStatus.dwServiceType =SERVICE_WIN32_OWN_PROCESS;
                  stServiceStatus.dwCurrentState = SERVICE_STOPPED;
                  stServiceStatus.dwControlsAccepted= SERVICE_ACCEPT_STOP;
                  stServiceStatus.dwWin32ExitCode =NO_ERROR;
                  stServiceStatus.dwCheckPoint= 0;
                  stServiceStatus.dwWaitHint = 0;
                  SetServiceStatus(MyServiceStatusHandle,&stServiceStatus);
                  break;
            case  SERVICE_START:
                  MessageBox(NULL,"Sstarting","SERVICE-START",MB_OK);
                  break;
            }//end of switch
}






0
 
yesnathan22Commented:
If you want i will send you entire code for installing  and service program
0
 
LizardKingAuthor Commented:
Looks good yesnathanyes22

yes


If you could send me the code it would be a good help. There is a few things in the code you posted that Im not sure of so the full code could shine some light on to the situation for me

ThanX loadz

IF IT HELPS YA GOT THE POINTS

LK--<<<
0
 
LizardKingAuthor Commented:
Looks good yesnathanyes22

yes


If you could send me the code it would be a good help. There is a few things in the code you posted that Im not sure of so the full code could shine some light on to the situation for me

ThanX loadz

IF IT HELPS YA GOT THE POINTS

LK--<<<
0
 
LizardKingAuthor Commented:
oh yeah Email alanfogarty@ireland.com
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.