LizardKing
asked on
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--<
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--<
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 ????
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 ????
ASKER
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-----<
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-----<
Can you get the samples from codeguru etc working as is ??
Have you considered an ATL NT Service app ??
Have you considered an ATL NT Service app ??
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
--Rudy
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.lpServ iceName = lpParamServiceName;
stServiceTableEntry.lpServ iceProc = ( LPSERVICE_MAIN_FUNCTION)Se rviceMain;
int iErr ;
if(!StartServiceCtrlDispat cher(&stSe rviceTable Entry))
{
// Error in starting dispatcher
iErr = GetLastError();
wsprintf(szTempDisp,"error in starting dispatcher %d",iErr);
MessageBox(NULL,szTempDisp ,"SOCKSERV ICE",MB_OK );
}
This is the service main function
void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
unsigned long iThread1;
MyServiceStatusHandle= RegisterServiceCtrlHandler ( "SA", (LPHANDLER_FUNCTION)fnHand ler);
SERVICE_STATUS stServiceStatus;
stServiceStatus.dwServiceT ype =SERVICE_WIN32_OWN_PROCESS ;
stServiceStatus.dwCurrentS tate = SERVICE_RUNNING;
stServiceStatus.dwControls Accepted= SERVICE_ACCEPT_STOP;
stServiceStatus.dwWin32Exi tCode =NO_ERROR;
stServiceStatus.dwCheckPoi nt= 0;
stServiceStatus.dwWaitHint = 0;
//Set Service Status
SetServiceStatus(MyService StatusHand le,&stServ iceStatus) ;
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,"Insid e handler with %d",dwControl);
MessageBox(NULL,szTempDisp ,"SERVICE" ,MB_OK);
switch (dwControl)
{
case SERVICE_STOP:
MessageBox(NULL,"STOPPNG", "SERVICE-S TOP",MB_OK );
exit(0);
break;
case SERVICE_CONTROL_STOP:
MessageBox(NULL,"CONTROLST OPPING","S ERVICE-CON TROL-STOP" ,MB_OK);
SERVICE_STATUS stServiceStatus;
stServiceStatus.dwServiceT ype =SERVICE_WIN32_OWN_PROCESS ;
stServiceStatus.dwCurrentS tate = SERVICE_STOPPED;
stServiceStatus.dwControls Accepted= SERVICE_ACCEPT_STOP;
stServiceStatus.dwWin32Exi tCode =NO_ERROR;
stServiceStatus.dwCheckPoi nt= 0;
stServiceStatus.dwWaitHint = 0;
SetServiceStatus(MyService StatusHand le,&stServ iceStatus) ;
break;
case SERVICE_START:
MessageBox(NULL,"Sstarting ","SERVICE -START",MB _OK);
break;
}//end of switch
}
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.lpServ
stServiceTableEntry.lpServ
int iErr ;
if(!StartServiceCtrlDispat
{
// Error in starting dispatcher
iErr = GetLastError();
wsprintf(szTempDisp,"error
MessageBox(NULL,szTempDisp
}
This is the service main function
void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
unsigned long iThread1;
MyServiceStatusHandle= RegisterServiceCtrlHandler
SERVICE_STATUS stServiceStatus;
stServiceStatus.dwServiceT
stServiceStatus.dwCurrentS
stServiceStatus.dwControls
stServiceStatus.dwWin32Exi
stServiceStatus.dwCheckPoi
stServiceStatus.dwWaitHint
//Set Service Status
SetServiceStatus(MyService
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,"Insid
MessageBox(NULL,szTempDisp
switch (dwControl)
{
case SERVICE_STOP:
MessageBox(NULL,"STOPPNG",
exit(0);
break;
case SERVICE_CONTROL_STOP:
MessageBox(NULL,"CONTROLST
SERVICE_STATUS stServiceStatus;
stServiceStatus.dwServiceT
stServiceStatus.dwCurrentS
stServiceStatus.dwControls
stServiceStatus.dwWin32Exi
stServiceStatus.dwCheckPoi
stServiceStatus.dwWaitHint
SetServiceStatus(MyService
break;
case SERVICE_START:
MessageBox(NULL,"Sstarting
break;
}//end of switch
}
If you want i will send you entire code for installing and service program
ASKER
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--<<<
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--<<<
ASKER
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--<<<
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--<<<
ASKER
oh yeah Email alanfogarty@ireland.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, what kind of errors you are running into with those samples.