Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

derek6433

Windows XP Service program -- Please help!!

This topic is 5475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, so I need to make a service for windows XP. I already have some code(that doesn''t work) for it. It compiles alright but when I try to launch it, the Service Control thing says "program did not respond or start in a timely fashion" All I want my service to do right now is bring up a MessageBox, so that I know it started. Here is the code... I would greatly appreciate it if someone could tell me what is wrong with it. SERVICE_STATUS MyServiceStatus; SERVICE_STATUS_HANDLE MyServiceStatusHandle; VOID _stdcall MyServiceStart (DWORD argc, LPTSTR *argv); VOID _stdcall MyServiceCtrlHandler (DWORD opcode); DWORD MyServiceInitialization (DWORD argc, LPTSTR *argv, DWORD *specificError); VOID SvcDebugOut(LPSTR String, DWORD Status) { CHAR Buffer[1024]; if (strlen(String) < 1000) { sprintf(Buffer, String, Status); OutputDebugStringA(Buffer); } } VOID _CRTAPI1 main( ) { SERVICE_TABLE_ENTRY DispatchTable[] = { { TEXT("MyService"), &MyServiceStart }, { NULL, NULL } }; if (!StartServiceCtrlDispatcher( DispatchTable)) { SvcDebugOut(" [MY_SERVICE] StartServiceCtrlDispatcher error = %d\n", GetLastError()); } } void _stdcall MyServiceStart (DWORD argc, LPTSTR *argv) { DWORD status; DWORD specificError; MyServiceStatus.dwServiceType = SERVICE_WIN32; MyServiceStatus.dwCurrentState = SERVICE_START_PENDING; MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; MyServiceStatus.dwWin32ExitCode = 0; MyServiceStatus.dwServiceSpecificExitCode = 0; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; MyServiceStatusHandle = RegisterServiceCtrlHandler(TEXT("MyService"), &MyServiceCtrlHandler); if (MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) { SvcDebugOut(" [MY_SERVICE] RegisterServiceCtrlHandler failed %d\n", GetLastError()); return; } // Initialization code goes here. status = MyServiceInitialization(argc,argv, &specificError); // Handle error condition if (status != NO_ERROR) { MyServiceStatus.dwCurrentState = SERVICE_STOPPED; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; MyServiceStatus.dwWin32ExitCode = status; MyServiceStatus.dwServiceSpecificExitCode = specificError; SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus); return; } // Initialization complete - report running status. MyServiceStatus.dwCurrentState = SERVICE_RUNNING; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) { status = GetLastError(); SvcDebugOut(" [MY_SERVICE] SetServiceStatus error%ld\n",status); } // This is where the service does its work. SvcDebugOut(" [MY_SERVICE] Returning the Main Thread \n",0); return; } // Stub initialization function. DWORD MyServiceInitialization(DWORD argc, LPTSTR *argv, DWORD *specificError) { argv; argc; specificError; return(0); } VOID _stdcall MyServiceCtrlHandler (DWORD Opcode) { DWORD status; switch(Opcode) { case SERVICE_CONTROL_PAUSE: // Do whatever it takes to pause here. MyServiceStatus.dwCurrentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: // Do whatever it takes to continue here. MyServiceStatus.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_STOP: // Do whatever it takes to stop here. MyServiceStatus.dwWin32ExitCode = 0; MyServiceStatus.dwCurrentState = SERVICE_STOPPED; MyServiceStatus.dwCheckPoint = 0; MyServiceStatus.dwWaitHint = 0; if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) { status = GetLastError(); SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",status); } SvcDebugOut(" [MY_SERVICE] Leaving MyService \n",0); return; case SERVICE_CONTROL_INTERROGATE: // Fall through to send current status. break; default: SvcDebugOut(" [MY_SERVICE] Unrecognized opcode %ld\n", Opcode); } // Send current status. if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) { status = GetLastError(); SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",status); } return; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox(0,"!","!",0); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Services should not have UIs, and therefore Windows won''t allow you to use MessageBox and similar functions. It''s really a bad thing to do, and you should find other means for whatever you''re trying to do. A workaround would be to allow the service to interact with desktop, but I stronlgy advise not to.

Share this post


Link to post
Share on other sites
Just write to the sytem logs:


void consolemsg( const char *in_strMessage )
{
HANDLE l_hEventSrc;
char * l_pszStrings[ 2 ];

l_hEventSrc = ::RegisterEventSource( NULL, TEXT("MyService") );
if ( l_hEventSrc != NULL )
{
l_pszStrings[0] = "MyService:";
l_pszStrings[1] = in_strMessage;

::ReportEvent(l_hEventSrc,EVENTLOG_ERROR_TYPE,0,0,NULL,2,0, (const char **)l_pszStrings,NULL);
::DeregisterEventSource( l_hEventSrc );
}
}


-cb

[edited by - cbenoi1 on August 19, 2003 1:45:38 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!