Window Service Question
-
hi I wrote a window service in which i am using the SOCKET API who listens to the clinet. This service runs fine on win2000 but if i run this on win2003 Server, it stop with some access violation error c0000005. I don't know whats is the problem. If i run my code without service means i make the console application and run this it does not show any error on win2003. I have this code like: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; if(FillParams()) _chdir(DIR_PATH); else return 0; SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = "TibcoTCPServer"; ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; ServiceTable[1].lpServiceName = NULL; ServiceTable[1].lpServiceProc = NULL; // Start the control dispatcher thread for our service StartServiceCtrlDispatcher(ServiceTable); return nRetCode; } void ServiceMain(int argc, char** argv) { int error; ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; // SERVICE_WIN32_SHARE_PROCESS;// SERVICE_WIN32; ServiceStatus.dwCurrentState = SERVICE_START_PENDING; ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwServiceSpecificExitCode = 0; ServiceStatus.dwCheckPoint = 0; ServiceStatus.dwWaitHint = 0; hStatus = RegisterServiceCtrlHandler( "TibcoTCPServer", (LPHANDLER_FUNCTION)ControlHandler); if (hStatus == (SERVICE_STATUS_HANDLE)0) { // Registering Control Handler failed WriteToLog("Registring Control Handler failed"); return; } // Initialize Service error = InitService(); if (error) { // Initialization failed ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwWin32ExitCode = -1; SetServiceStatus(hStatus, &ServiceStatus); return; } // WriteToLog("Service Started _2."); // We report the running status to SCM. ServiceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus (hStatus, &ServiceStatus); AfxBeginThread(MTServerThread,0); return; } UINT MTServerThread(LPVOID pParam) { WSADATA wsaData; sockaddr_in local; int wsaret=WSAStartup(0x101,&wsaData); if(wsaret!=0) { return 0; } local.sin_family=AF_INET; local.sin_addr.s_addr=INADDR_ANY; int nPort = atoi(PORT); local.sin_port=htons((u_short)nPort); server=s
-
hi I wrote a window service in which i am using the SOCKET API who listens to the clinet. This service runs fine on win2000 but if i run this on win2003 Server, it stop with some access violation error c0000005. I don't know whats is the problem. If i run my code without service means i make the console application and run this it does not show any error on win2003. I have this code like: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; if(FillParams()) _chdir(DIR_PATH); else return 0; SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = "TibcoTCPServer"; ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; ServiceTable[1].lpServiceName = NULL; ServiceTable[1].lpServiceProc = NULL; // Start the control dispatcher thread for our service StartServiceCtrlDispatcher(ServiceTable); return nRetCode; } void ServiceMain(int argc, char** argv) { int error; ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; // SERVICE_WIN32_SHARE_PROCESS;// SERVICE_WIN32; ServiceStatus.dwCurrentState = SERVICE_START_PENDING; ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwServiceSpecificExitCode = 0; ServiceStatus.dwCheckPoint = 0; ServiceStatus.dwWaitHint = 0; hStatus = RegisterServiceCtrlHandler( "TibcoTCPServer", (LPHANDLER_FUNCTION)ControlHandler); if (hStatus == (SERVICE_STATUS_HANDLE)0) { // Registering Control Handler failed WriteToLog("Registring Control Handler failed"); return; } // Initialize Service error = InitService(); if (error) { // Initialization failed ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwWin32ExitCode = -1; SetServiceStatus(hStatus, &ServiceStatus); return; } // WriteToLog("Service Started _2."); // We report the running status to SCM. ServiceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus (hStatus, &ServiceStatus); AfxBeginThread(MTServerThread,0); return; } UINT MTServerThread(LPVOID pParam) { WSADATA wsaData; sockaddr_in local; int wsaret=WSAStartup(0x101,&wsaData); if(wsaret!=0) { return 0; } local.sin_family=AF_INET; local.sin_addr.s_addr=INADDR_ANY; int nPort = atoi(PORT); local.sin_port=htons((u_short)nPort); server=s
hi Shailesh, If ur code works well as a console app and not as a service, do one thing, goto SCM, take the properties of ur service, goto log on tab and click on "Run As" option button - enter ur username and password. Because, it seems in win2003 the local system account doens't have the same set of rights as it has in 2000. But I have noted some other issues in the code. First of don't use
AfxBeginthread
in a console app, instead of this, usecreatethread
. Use of AfxBeginthread will cause serious memory leak. chk msdn, very gud explanation is there.. Second thing, in ServiceMain in the last line there is noWaitForSingleObject
call. So After u call AfxBeginthread serviceMain will terminate. i don't understand how it worked on win2000. chk thease two things first and try again in 2003. rgds...mil10 -
hi Shailesh, If ur code works well as a console app and not as a service, do one thing, goto SCM, take the properties of ur service, goto log on tab and click on "Run As" option button - enter ur username and password. Because, it seems in win2003 the local system account doens't have the same set of rights as it has in 2000. But I have noted some other issues in the code. First of don't use
AfxBeginthread
in a console app, instead of this, usecreatethread
. Use of AfxBeginthread will cause serious memory leak. chk msdn, very gud explanation is there.. Second thing, in ServiceMain in the last line there is noWaitForSingleObject
call. So After u call AfxBeginthread serviceMain will terminate. i don't understand how it worked on win2000. chk thease two things first and try again in 2003. rgds...mil10 -
hi Shailesh, If ur code works well as a console app and not as a service, do one thing, goto SCM, take the properties of ur service, goto log on tab and click on "Run As" option button - enter ur username and password. Because, it seems in win2003 the local system account doens't have the same set of rights as it has in 2000. But I have noted some other issues in the code. First of don't use
AfxBeginthread
in a console app, instead of this, usecreatethread
. Use of AfxBeginthread will cause serious memory leak. chk msdn, very gud explanation is there.. Second thing, in ServiceMain in the last line there is noWaitForSingleObject
call. So After u call AfxBeginthread serviceMain will terminate. i don't understand how it worked on win2000. chk thease two things first and try again in 2003. rgds...mil10Hello Mil Service is started now on win2003, Now i am using the createthread and waitforsingleobject. There is no memory error but now i got one more problem it is giving me the clinet Socket error from the function below. I don't know what is the problem now i did not change any part of code for client request. can u suggest me something. UINT ClientThread(LPVOID pParam) { char buff[MAX_BUFFER_LENGTH]; CString cmd; CString params; int n; BOOL auth=false; SOCKET client=(SOCKET)pParam; // send(client,buff,strlen(buff),0); int nCounter = 0; CString strData; CString strFileName; SYSTEMTIME sysTime; while(true) { n=recv(client,buff,MAX_BUFFER_LENGTH,0); if(n==SOCKET_ERROR ) { WriteToLog("Client Socket Error"); break; } if(n == 0) { //continue; //if no data recieve exit the loop and close the connection and Client Thread break; } buff[n]=0; params = (char*)buff; if(nCounter == 0) { nCounter += n; WriteToLog((LPSTR)(LPCTSTR)params); GetLocalTime(&sysTime); strFileName.Format("%s\\User%d%d%d%d%d%d%d.xml",DataFolder,sysTime.wMonth,sysTime.wDay ,sysTime.wYear ,sysTime.wHour ,sysTime.wMinute ,sysTime.wSecond ,sysTime.wMilliseconds ); if(!xmlFile.Open(strFileName,CFile::modeWrite | CFile::modeCreate,NULL)) { errorMsg.Format("Unable to create %s file",strFileName); WriteToLog((LPSTR)(LPCTSTR)errorMsg); } int nRetVal = params.Find(";",0); if(nRetVal != -1) { cmd = params.Left(nRetVal); if(n > nRetVal) { strData = params.Mid(nRetVal + 1); { LPSTR lpstr = (LPSTR)(LPCTSTR)(strData); xmlFile.Write((PVOID)lpstr,strlen(lpstr)); } } }//end nRetVal else { // USES_CONVERSION; LPSTR lpstr = (LPSTR)(LPCTSTR)(params); xmlFile.Write((PVOID)lpstr,strlen(lpstr)); } if(params.Find(ENDXMLTAG) != -1) { xmlFile.Close (); nCounter = 0; ExecuteDosent(cmd,strFileName); cmd.MakeUpper(); if(cmd.Compare("DRUSER")) ExecuteInvokeBat(strFileName); //break; } }//End nCounter else { nCounter += n; LPSTR lpstr = (LPSTR)(LPCTSTR)(params); xmlFile.Write((PVOID)lpstr,strlen(lpstr)); if(params.Find(ENDXMLTAG) != -1) { xmlFile.Close (); nCounter = 0; WriteToLog((LPSTR)(LPCTSTR)params); ExecuteDosent(cmd,strFileName); cmd.MakeUpper(); if(cmd.Com