|
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.
|
|
|
|
|
For a very quick fix try commenting out this line of code:
for (int i = 2; i < argc; i++)
{
string sz = argv[i];
if (sz.length() <= 0) continue;
if (sz[0] == _T('/')) break;
//comment out the line below
//services.push_back(sz);
}
you should still optimize some part though.
hope this helps for the meantime.
|
|
|
|
|
It does help but unfortunately commenting out that line will cause none of the command line functions to execute - it just terminates. So not good for the app if it cant actually control anything.
|
|
|
|
|
Its due when wrote:
...how do i limit this to only allowing 1 service to be passed.
Look at the argc variable passed to ParseCommandLine() .
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
What is it in particular that im looking at. The variable itslef has to exist as a count for the number of arguments passed to the command line, right?
|
|
|
|
|
Its due when wrote:
What is it in particular that im looking at.
The argc parameter. I'm assuming it's the same argc that was passed to the main() function.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Hello,
I want to share the data between Dll to DLL. That is I am having two Dll and one Application program. I know how to share data between a Dll and Application program as given below.
#pragma data_seg(".myseg")
int share=0;
#pragma data_seg()
by defining in the "DEF" like
SECTIONS
.myseg Read Write Shared
but by this method I am not able to share a Data between DLL to DLL
Please suggest me the solution.
Regards
Savita
|
|
|
|
|
A memory mapped file?
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
avoid using MFC in COM srevers as it has thread affinity. wot does this means exactly . !!!
Thanks in advance.
|
|
|
|
|
|
can you tell me wot thread affinity means, in a single sentence also fine.
|
|
|
|
|
most of the MFC objects internally uses local thread storage extensively.
so they are bound to their owner thread, and not supposed to be shared with other threads.
this is why MFC has thread affinity.
It's not a bug, it's an undocumented feature. suhredayan@gmail.com
|
|
|
|
|
To have thread affinity means to be "bound to the thread". Therefore the object can only be used safely from that thread.
In the case of MFC, some objects are bound to threads through MFCs use of Thread Local Storage. Attempting to use the object in a thread other than the thread it is bound to will result in errors since the thread local storage data is not present.
Hope that helps?
-- signature under construction -- -pete
|
|
|
|
|
I have created a windows service using visual C++.
Kindly tell me how do i register its .exe file as windows service.
What command will be used and what steps ll be needed.
reply soon,
Thanks
|
|
|
|
|
CreateService is used to create a service object as well as add the service to the SCM database.
SC_HANDLE CreateService(
SC_HANDLE hSCManager,
LPCTSTR lpServiceName,
LPCTSTR lpDisplayName,
DWORD dwDesiredAccess,
DWORD dwServiceType,
DWORD dwStartType,
DWORD dwErrorControl,
LPCTSTR lpBinaryPathName,
LPCTSTR lpLoadOrderGroup,
LPDWORD lpdwTagId,
LPCTSTR lpDependencies,
LPCTSTR lpServiceStartName,
LPCTSTR lpPassword
);
|
|
|
|
|
no i have .exe file , which i want to register with windows as windows service.
What should i type on command prompt to register it as a service.
thanks.
|
|
|
|
|
|
use command "servicefullpath" -service ,from commandline
prasad
|
|
|
|
|
I want to use IActiveDesktop and have some problems.a dialog based project, codes as below:
stdafx.h:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <wininet.h> //???
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
MyTestDlg.cpp:
#include <shlobj.h> // IActiveDesktop
but the comiler says:IActiveDesktop is not defined,why?
|
|
|
|
|
IActiveDesktop is defined in shlobj.h. This file is part of the Platform SDK. Are you #including this file?
Regards,
John
|
|
|
|
|
my codes as below:
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <wininet.h> //!!!
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
MyTestDlg.cpp:
#include <shlobj.h> // IActiveDesktop
BOOL CMyTestDlg::EnableActiveDesktop(BOOL bEnable)
{
HRESULT hr;
IActiveDesktop* pIAD;//Error
.....
}
|
|
|
|
|
Hope this helps.
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <wininet.h>
#include <shlobj.h>
int main (int argc, char* argv[])
{
CoInitialize(NULL);
HRESULT hr;
IActiveDesktop* pAD;
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, (void**)&pAD);
CoUninitialize();
return 0;
}
Regards,
John
|
|
|
|
|
|
stdafx.h:
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <wininet.h> //!!!
#include <shlobj.h> // IActiveDesktop
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes!!!!
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
and I have also added the:#define _WIN32_WINNT 0x0500
it still has those error
|
|
|
|
|
Please fix the < and > symbols so that we can see what you are including, or at least preview your post beforehand.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|