Solved

How to create an MFC based Windows NT Service

Posted on 2000-04-26
11
303 Views
Last Modified: 2013-11-25
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--<
0
Comment
Question by:LizardKing
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 2751872
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2753668
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
 

Author Comment

by:LizardKing
ID: 2754470
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754714
Can you get the samples from codeguru etc working as is ??

Have you considered an ATL NT Service app ??

0
 

Expert Comment

by:raschalk
ID: 2770633
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:yesnathan22
ID: 2773065
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
 

Expert Comment

by:yesnathan22
ID: 2773117
If you want i will send you entire code for installing  and service program
0
 

Author Comment

by:LizardKing
ID: 2773858
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
 

Author Comment

by:LizardKing
ID: 2773862
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
 

Author Comment

by:LizardKing
ID: 2773864
oh yeah Email alanfogarty@ireland.com
0
 

Accepted Solution

by:
yesnathan22 earned 51 total points
ID: 2793483
Hello Lizard King
         do you accept the answer???Then give me the points ya
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 112
Changing the installation path of this MSI 5 94
Using MS Code on my Mac 6 66
Not needed 13 95
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now