Click here to Skip to main content
15,903,856 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CheckMenuItem() problem Pin
Dan Nicolici30-Aug-04 1:36
Dan Nicolici30-Aug-04 1:36 
GeneralHelp required Pin
Sreekanth Muralidharan26-Aug-04 22:22
Sreekanth Muralidharan26-Aug-04 22:22 
GeneralRe: Help required Pin
KaЯl27-Aug-04 1:09
KaЯl27-Aug-04 1:09 
GeneralSend a file to my server using MFC Pin
anderslundsgard26-Aug-04 22:19
anderslundsgard26-Aug-04 22:19 
GeneralRe: Send a file to my server using MFC Pin
Rory Solley26-Aug-04 23:12
Rory Solley26-Aug-04 23:12 
GeneralRe: Send a file to my server using MFC Pin
darkbyte27-Aug-04 13:44
darkbyte27-Aug-04 13:44 
GeneralDock Bar like ObjectDock Pin
puresilence_200226-Aug-04 21:33
puresilence_200226-Aug-04 21:33 
GeneralPassing single service in command line Pin
Its due when26-Aug-04 21:29
Its due when26-Aug-04 21:29 
Hi,

Im pretty new at the whole c++ thing and have attempted to modify an app that requires the controlling of services. The start, stop, pause, continue etc work fine but the thing is that the person who first wrote the app has enabled multiple services to be able to be controlled from the one string of arguments. Eg. start service name service name service name which will call on the start method to start the 3 specified services. I guess my question is how do i limit this to only allowing 1 service to be passed. The code is below for clarification. The first method is the parse command line method followed by the int main();

boolean ParseCommandLine(int argc, TCHAR* argv[], DWORD &dwAction, DWORD &dwTimeout, Services &services)
{
if (lstrcmpi( argv[1], TEXT("/?") ) == 0)
{
printf("\nSCM controls Windows services, formerly know as NT services.\n");
printf("\nSCM start/stop/pause/resume/keepalive/status 'Service Name' [/k] [Timeout in");
printf("\nseconds]\n");
printf("\n start Starts the desired service if service is in a stopped state.");
printf("\n stop Stops the desired service.");
printf("\n pause Pauses the desired service if the service is running.");
printf("\n resume Resumes the service to a running state from a paused state.");
printf("\n keepalive Keeps the service running regardless of the state it is in.");
printf("\n status Returns the current state of the service as an error level.");
printf("\n ServiceName The name of the service to be controlled. This name has to be");
printf(" the services' name and not the display name.");
printf("\n/k (optional) Passing the /k command performs a kill function on the");
printf("\n service if the desired state or function called is not ");
printf("\n reached within a specified time, determined by the timeout");
printf(" specified.");
printf("\nTimeout in seconds Time in which to perform the specified action. The timeout");
printf("\n<optional> range is specified between 1 - 3600 seconds. Any timeout");
printf("\n outside of this range will result in the command not being");
printf(" executed.\n\n");
}

else if (argc < 3)
{ printf("\nNot enough arguments.\nArgument sequence is:\n\nSCM start/stop/pause/resume/status 'service name' [/k] [timeout in seconds].\n\n");
return false;
}


else if (lstrcmpi( argv[1], TEXT("start") ) == 0 )
dwAction = SERVICE_START;
else if ( argc > 1 && lstrcmpi( argv[1], TEXT("stop") ) == 0 )
dwAction = SERVICE_CONTROL_STOP;
else if ( argc > 1 && lstrcmpi( argv[1], TEXT("pause") ) == 0 )
dwAction = SERVICE_CONTROL_PAUSE;
else if ( argc > 1 && lstrcmpi( argv[1], TEXT("resume") ) == 0 )
dwAction = SERVICE_CONTROL_CONTINUE;
else if ( argc > 1 && lstrcmpi( argv[1], TEXT("keepalive") ) == 0 )
{
dwAction = 80;
}
else if ( argc > 1 && lstrcmpi( argv[1], TEXT("status") ) == 0 )
{
dwAction = 99;
QuerySvc(hSCM, argv[2]);
}
else
{
printf("\nBad Command.\nArgument sequence is:\nSCM start/stop/pause/continue/keepalive/status 'service name' [/k] [timeout in seconds]\n");
return false;
}


for (int i = 2; i < argc; i++)
{

string sz = argv[i];

if (sz.length() <= 0) continue;

if (sz[0] == _T('/')) break;

services.push_back(sz);
}

if (i < argc)
{

string sz = argv[i];

if (sz == _T("/k"))
{
if (i+1 >= argc)
{
printf("\nNot enough arguments.\n Argument sequence is:\nSCM start/stop/pause/resume 'service name' [/k] [timeout in seconds].\n");
return false;
}
// Check that argument is only digits
for (int c = 0; c < _tcslen(argv[i+1]); c++)
{
if (!iswdigit(*argv[i+1]))
{
printf("\nTimeout specified is not a number\n");
printf("\nArgument sequence is:\nSCM start/stop/pause/resume/keepalive/status 'service name' [/k] [timeout in seconds].\n");
return false;
}
}

// Check that timeout is within specified range
dwTimeout = _ttol(argv[i+1]);

if (dwTimeout < 1 || dwTimeout > 3600)
{
printf("\nTimeout not within specified range of 1 - 3600.\n");
return false;
}

dwTimeout *= 1000;

}
else
{
printf("\nBad Command.\nSequence of command is:\nSCM start/stop/pause/resume/keepalive/status 'Service Name' [/k] [time in seconds between 1 - 3600]\n");
return false;
}
}

return true;
}

int _tmain( int argc, TCHAR* argv[] )
{
DWORD dwTimeout = -1;
DWORD dwAction;
Services services;

if (!ParseCommandLine(argc, argv, dwAction, dwTimeout, services))
{

return -1;
}

for (int i = 0; i < services.size(); i++)
{
if ( dwAction < 0)
{
ReportStatus(services[i].c_str());
}
else
{
switch (dwAction)
{
case SERVICE_START:
StartService(services[i].c_str(), dwTimeout);
break;
case SERVICE_CONTROL_STOP:
StopService(services[i].c_str(), dwTimeout);
break;
case SERVICE_CONTROL_PAUSE:
PauseService(services[i].c_str(), dwTimeout);
break;
case SERVICE_CONTROL_CONTINUE:
ContinueService(services[i].c_str(), dwTimeout);
break;
case 80:
KeepAlive(services[i].c_str());
break;
case 99:
//get service handle
hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
hService = OpenService(hSCM, services[i].c_str(), SERVICE_QUERY_STATUS);

if (hService == NULL)
{
PrintError(GetLastError());

return -2;
}
SERVICE_STATUS status;

//query status
if(!QueryServiceStatus(hService,&status))
{
::CloseServiceHandle(hSCM);
PrintError(GetLastError());
}
return status.dwCurrentState;
break;
}
}
}


return 0;
}

Any help would be greatly appreciated. Thanks

nicolai

"Yesterday it worked. Today it didnt. Windows is like that.
GeneralRe: Passing single service in command line Pin
rotu27-Aug-04 0:51
rotu27-Aug-04 0:51 
GeneralRe: Passing single service in command line Pin
Its due when29-Aug-04 14:21
Its due when29-Aug-04 14:21 
GeneralRe: Passing single service in command line Pin
David Crow27-Aug-04 2:21
David Crow27-Aug-04 2:21 
GeneralRe: Passing single service in command line Pin
Its due when29-Aug-04 14:26
Its due when29-Aug-04 14:26 
GeneralRe: Passing single service in command line Pin
David Crow30-Aug-04 2:34
David Crow30-Aug-04 2:34 
QuestionHow to Share Data between DLL to DLL Pin
Savita Kashyap26-Aug-04 20:13
Savita Kashyap26-Aug-04 20:13 
AnswerRe: How to Share Data between DLL to DLL Pin
jmkhael27-Aug-04 2:38
jmkhael27-Aug-04 2:38 
GeneralMFC thread affinity and COM Pin
xcavin26-Aug-04 19:57
xcavin26-Aug-04 19:57 
GeneralRe: MFC thread affinity and COM Pin
palbano26-Aug-04 20:09
palbano26-Aug-04 20:09 
GeneralRe: MFC thread affinity and COM Pin
xcavin26-Aug-04 20:42
xcavin26-Aug-04 20:42 
GeneralRe: MFC thread affinity and COM Pin
22491726-Aug-04 20:56
22491726-Aug-04 20:56 
GeneralRe: MFC thread affinity and COM Pin
palbano26-Aug-04 20:58
palbano26-Aug-04 20:58 
Questionhow to register windows service?? Pin
Capriono26-Aug-04 19:42
Capriono26-Aug-04 19:42 
AnswerRe: how to register windows service?? Pin
Anonymous26-Aug-04 20:03
Anonymous26-Aug-04 20:03 
GeneralRe: how to register windows service?? Pin
Capriono26-Aug-04 21:02
Capriono26-Aug-04 21:02 
GeneralRe: how to register windows service?? Pin
Anonymous26-Aug-04 21:25
Anonymous26-Aug-04 21:25 
GeneralRe: how to register windows service?? Pin
Prasadsm27-Aug-04 19:32
Prasadsm27-Aug-04 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.