15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Using MFC in a service

Dean Grimm -- dean@cortron.com
Monday, March 17, 1997

Environment: VC++ 4.2b, NT 4.0

I am preparing to write my first NT service, and in doing so,
have done a bit of research in MSDN, VC help, and the KB.
None of the examples I found used MFC, which makes me wonder...
Is it possible to use MFC in an NT service?

Any input along these lines will be greatly appreciated

Dean Grimm
Software Engineer / Cortron Corp.



Paul -- PGWestco@malvern.aussy.ingr.com
Thursday, March 20, 1997

[Mini-digest: 17 responses]

MFC in a service... Yes.

You can even use the framework, although I don't recommend it.

If you use the framework, you can put the call to
StartServiceCtrlDispatcher in the InitInstance() of your CWinApp derived
class. (But in the documentation for services,  it says to do this call
asap, and waiting for the framework to get there is not asap, but it
doesn't take -=that=- long, so it should be fine (well it does work on
NT 3.51 and 4).). InitInstance should then return FALSE, so the message
pump doesn't start.

After setting, from within the service,  the window station to "WinSta0"
and the desktop to "Default" you can even interact with an MFC created
window... But this is just getting silly =)

If you want any specific detail, as opposed to just knowing that it "can
be done" (which is often mighty useful!) then give me a yell (I am in
the process of joining this mailing list, but am waiting for "approval"
=) 

Paul.
>----------
>From: 	Kiely, Daryn
>Sent: 	Thursday, March 20, 1997 10:12 AM
>To: 	Westcott, Paul
>Subject: 	FW: Using MFC in a service
>
>
>
>----------
>From: 	Dean Grimm[SMTP:dean@cortron.com]
>Sent: 	Tuesday, March 18, 1997 1:17 AM
>To: 	'MFC list'
>Subject: 	Using MFC in a service
>
>Environment: VC++ 4.2b, NT 4.0
>
>I am preparing to write my first NT service, and in doing so,
>have done a bit of research in MSDN, VC help, and the KB.
>None of the examples I found used MFC, which makes me wonder...
>Is it possible to use MFC in an NT service?
>
>Any input along these lines will be greatly appreciated
>
>Dean Grimm
>Software Engineer / Cortron Corp.
>
>
-----From: "Dean Wiles" 

The non-GUI MFC classes can be used in an NT service - I've used CObject,
CString and the CTypedPtrArray objects.

You can start with the \MSDev\SDK\WinNT\Service example, or the ActiveX
Template Library (ATL) 1.1 has an AppWizard that can create a service with
MFC and COM support.

Note, however, that using MFC in a service does increase its size by 500K -
1M, even if you statically link it.  ATL/COM support also adds another 1M+
to the working set for the OLE DLL's.  Otherwise, it worked fine for me.

--------------------------------------------------------------------------
Dean Wiles (deanw@mail.isc-br.com)          Olivetti North America
Phone:  (509)927-7037                       22425 East Appleway Ave
Fax:    (509)927-2499                       Liberty Lake, WA 99019-9534
If the Son sets you free, you will be free indeed.  (John 8:36)

----------
> From: Dean Grimm 
> To: 'MFC list' 
> Subject: Using MFC in a service
> Date: Monday, March 17, 1997 7:17 AM
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?
> 
> Any input along these lines will be greatly appreciated
> 
> Dean Grimm
> Software Engineer / Cortron Corp.
-----From: "Dan Kirby" 

It really depends upon what you are trying to do.  MFC was not written or
tested for use within a service. As a general rule of thumb, if you have a
GUI portion of the application, make it a separate application which
communicates with the service rather than allowing a GUI service  to
interact with the desktop.  

Here are a couple of articles which you may want to check out:
 Q164166 
 Q156138

--dan

----------
> From: Dean Grimm 
> To: 'MFC list' 
> Subject: Using MFC in a service
> Date: Monday, March 17, 1997 7:17 AM
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?
> 
> Any input along these lines will be greatly appreciated
> 
> Dean Grimm
> Software Engineer / Cortron Corp.
-----From: "Dana M. Epp" 

Just recently we needed to do this. We ended up overriding the _tWinMain
and called StartServiceCtrlDispatcher() ourselves. This not only allowed us
to hook MFC apps to the service manager via a command line option, it also
allowed it to run as a normal app on other platforms without any worry.

_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPTSTR lpCmdLine, int nCmdShow)
{
	if( strcmp( lpCmdLine, "-srv" )==0 ) {
		_shInstance = hInstance;
		_shPrevInstance = hPrevInstance;
		_sCmdLine = lpCmdLine;
		int _sCmdShow = nCmdShow;

		static SERVICE_TABLE_ENTRY   DispatchTable[] = 
		{  { TEXT("YOURSERVICE"), YOURSERVICEServiceStart }, 
			{ NULL,           NULL          } }; 

		if ( ! StartServiceCtrlDispatcher( DispatchTable ) ) {
			LPSTR lpMsgBuf;
 
			FormatMessage( 
				FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
				NULL, GetLastError(),
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				(LPTSTR) &lpMsgBuf,	0, NULL );
			// Free the buffer.
			LocalFree( lpMsgBuf );

			}
		return 0;
		}
	else {
		// call shared/exported WinMain
		return AfxWinMain( hInstance, hPrevInstance, lpCmdLine, nCmdShow );
		}
}


static DWORD WINAPI mainthread()
{
	return AfxWinMain( _shInstance, _shPrevInstance, _sCmdLine, _sCmdShow );
}

void WINAPI YOURSERVICEServiceStart( DWORD /*argc*/, LPSTR * /*argv*/ )
{
	_HService = RegisterServiceCtrlHandler( TEXT("YOURSERVICE"),
ServiceHandler );
	if( ! _HService ) {
		return;
	}
	_SStatus.dwServiceType =
SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS;
	_SStatus.dwCurrentState = SERVICE_RUNNING;
	_SStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
	_SStatus.dwWin32ExitCode = NO_ERROR;
	_SStatus.dwServiceSpecificExitCode = 0;
	_SStatus.dwCheckPoint = 0;
	_SStatus.dwWaitHint = 0;

	DWORD id;
	HANDLE hthread = CreateThread( NULL, NULL,
(LPTHREAD_START_ROUTINE)mainthread, 0, 0, &id );
	if ( hthread ){
		SetServiceStatus( _HService, & _SStatus );
	}
	else {
		_SStatus.dwCurrentState = SERVICE_STOPPED;
		SetServiceStatus( _HService, & _SStatus );
	}
}


At 07:17 AM 3/17/97 -0800, you wrote:
>Environment: VC++ 4.2b, NT 4.0
>
>I am preparing to write my first NT service, and in doing so,
>have done a bit of research in MSDN, VC help, and the KB.
>None of the examples I found used MFC, which makes me wonder...
>Is it possible to use MFC in an NT service?
>
>Any input along these lines will be greatly appreciated
>
>Dean Grimm
>Software Engineer / Cortron Corp.
>


PC'ing you,                     
Dana M. Epp          
eppdm@uniserve.com
http://bedrock.cyberhq.com/dana

     "How can one work with the technology of today, using yesterdays
      software and methods, and still be on the leading edge tomorrow?
      Why settle for less... I won't! "
-----From: Steve Dunn 


Yes it is.

I've used MFC in a service, although only string handling and message boxes, although I can't see any problems with using the rest of it.

Steve Dunn
Backroom Software

-----Original Message-----
From:	Dean Grimm [SMTP:dean@cortron.com]
Sent:	17 March 1997 15:17
To:	'MFC list'
Subject:	Using MFC in a service

Environment: VC++ 4.2b, NT 4.0

I am preparing to write my first NT service, and in doing so,
have done a bit of research in MSDN, VC help, and the KB.
None of the examples I found used MFC, which makes me wonder...
Is it possible to use MFC in an NT service?

Any input along these lines will be greatly appreciated

Dean Grimm
Software Engineer / Cortron Corp.


-----From: Mike Blaszczak 

At 07:17 3/17/97 -0800, Dean Grimm wrote:
>Environment: VC++ 4.2b, NT 4.0

>Is it possible to use MFC in an NT service?

Yes, it is.


.B ekiM
http://www.nwlink.com/~mikeblas/
These words are my own. I do not speak on behalf of Microsoft.
       One is too many and a million is not enough.
-----From: "Matthias Bohlen" 

On 17 Mar 97 at 7:17, Dean Grimm wrote about Using MFC in a service:

Environment: VC++ 4.2b, NT 4.0

> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?

Hello Dean,

yes, it is possible! I have just written an NT service that uses
some of the MFC collection classes and CString. I would not use the
MFC user interface classes (documents, views, dialogues, etc.)
because a Windows NT service normally does not have a user
interface!

You may use the user interface classes inside control panel applets 
or other applications that control your service remotely.

I have based my NT service on the MSDN article sample "Creating a
Simple Windows NT Service in C++" by Nigel Thompson of November 1995. 

Do a search in MSDN library for the keyword NTSERVICE and you'll find 
this sample. Based on it, converting a console application to a 
service was just a matter of overriding a virtual function "Run()" 
inside a class "CNTService" that Nigel had written.

Hint: I have linked the service with the static version of MFC 
because I do not know which MFC DLL's are installed on the NT server.

Good luck in writing it!
Matthias

**************************************
Come on, love what you do or leave it!
**************************************

Matthias Bohlen             |  Logotec Software GmbH
Phone: +49 228 64 80 520    |  Chateauneufstr. 10 
FAX:   +49 228 64 80 525    |  D-53347 Alfter, Germany
                            |  http://www.logotec.com/
E-mail: mattes@logotec.com  |  CAD systems development
-----From: Phil Daley 

At 07:17 AM 3/17/97 -0800, Dean Grimm wrote:
 
>Environment: VC++ 4.2b, NT 4.0
>
>I am preparing to write my first NT service, and in doing so,
>have done a bit of research in MSDN, VC help, and the KB.
>None of the examples I found used MFC, which makes me wonder...
>Is it possible to use MFC in an NT service?
>
>Any input along these lines will be greatly appreciated

No problems.  (well, except the normal multi-threading problems)


Phil Daley     Relay Technology
http://www.conknet.com/~p_daley

-----From: Chris Shafer 

Dean,

There's a good article on this in the Nov 1995 MSDN called:

"Creating a Simple Windows NT Service in C++"

I used the code examples to create a service in C++ that uses the Windows Sockets
classes.  It works slick!

Hope this helps...

Chris Shafer

-----From: John Ferguson 

Yes. There are no "Service" wizards. You have no need for any of the
doc-view architecture.
We use alot of the non-gui mfc classes (collection classes, CString,
etc.)

>-----Original Message-----
>From:	Dean Grimm [SMTP:dean@cortron.com]
>Sent:	Monday, March 17, 1997 9:17 AM
>To:	'MFC list'
>Subject:	Using MFC in a service
>
>Environment: VC++ 4.2b, NT 4.0
>
>I am preparing to write my first NT service, and in doing so,
>have done a bit of research in MSDN, VC help, and the KB.
>None of the examples I found used MFC, which makes me wonder...
>Is it possible to use MFC in an NT service?
>
>Any input along these lines will be greatly appreciated
>
>Dean Grimm
>Software Engineer / Cortron Corp.
-----From: "Chris McKillop" 

> 
> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?
> 
> Any input along these lines will be greatly appreciated
> 
> Dean Grimm
> Software Engineer / Cortron Corp.
> 


	At work we are starting to use NT and have
been moving our UNIX daemon type process over to
NT Serivce type process in the last few months.  I will
try to help you out based on what we have learned.  

	As I am sure you know, an NT service consists of
two threads, one to talk to the serivce manager and the
other to do the work.  This worker thread can ONLY interact
with the desktop if you set it up so it can when you install
the service. If you include the flag SERVICE_INTERACTIVE_PROCESS
when you create the service, it will be allowed to open dialogs
and consoles.  Since it can do this, you could probably
write an MFC app that could on the worker thread, the question
is, do you want to?

	What we are doing here at focus is to write an MFC
front end to the service that talks to the service to display
and change information.  That way the service can run without
cluttering up the users' desktop when they log on, and we can 
have a runtime configurable service with a nice front end
written in MFC.  We are using our own IPC (which I just
finished writting, 2x faster than named pipes!) but using
named pipes is really simple.  Feel free to mail me
persoanlly if you like and pick my brain (the bill
will arrive in a month or two!  ).

	Chris

------------------------------------------------
cdmckill@focus-systems.on.ca

It's the end of the world as we know it, and I feel fine.
             - REM, "Document"


-----From: hou@tfn.com (Bing Hou)


Yes, MFC shipped with VC++4.2 and later is safe to use in NT services. 
However since you cannot have a main window in an NT service, it's probably 
why those samples didn't use MFC.

If you want to use MFC ODBC, that is another story. 

Bing Hou
hou@tfn.com
------------------------------------------------------------------------
  "Always take a job that is too big for you."




______________________________ Reply Separator _________________________________
Subject: Using MFC in a service
Author:  Dean Grimm  at Internet
Date:    3/17/97 7:17 AM


Environment: VC++ 4.2b, NT 4.0
     
I am preparing to write my first NT service, and in doing so, 
have done a bit of research in MSDN, VC help, and the KB.
None of the examples I found used MFC, which makes me wonder... 
Is it possible to use MFC in an NT service?
     
Any input along these lines will be greatly appreciated
     
Dean Grimm
Software Engineer / Cortron Corp.
-----From: wayne.dengel@octel.com

     YES, you can, I have a service WIZARD that uses MFC extensively.
     It is basicly an APP, that 
        1: Starts a thread for the SERVICE LOOP
        2: Opens a Hidden window for your MFC app.
     
     It's pretty easy and works great.
     
     E_mail me if you want help or the WIZARD
     
     Wayne G. Dengel II
     Sr. Software Engineer
     OCTEL


______________________________ Reply Separator _________________________________
Subject: Using MFC in a service
Author:  mfc-l@netcom.com at P_Internet_Mail
Date:    3/17/97 7:17 AM


Environment: VC++ 4.2b, NT 4.0
     
I am preparing to write my first NT service, and in doing so, 
have done a bit of research in MSDN, VC help, and the KB.
None of the examples I found used MFC, which makes me wonder... 
Is it possible to use MFC in an NT service?
     
Any input along these lines will be greatly appreciated
     
Dean Grimm
Software Engineer / Cortron Corp.
-----From: "David Cunningham" 

Dean,

The short answer is yes, it is possible.  You will need to modify the 
MFC source code however, as you need to add the service functionality 
at the WinMain() level.  

I seem to remember there being a KB article recommending against 
using MFC based apps as services, you should check the KB for this 
informaiton.

If you have a very basic need, the Windows NT reskit has a utility 
called SRVANY.EXE, which will allow you to run a plain EXE as a 
service.  It's not great, corrupts any open files upon termination, 
etc. etc. but it does work.

If you are producing a professional product, or just plain want a 
better level of quality, you should check out our VB Service package, 
it had a complete Visual Configuration Manager, integrates with 
packages like InstallShield, etc.  If you attended DevDays or VCDC 
you may already have a demo of the product from our Demo CD or the 
DevDays Components CD.  If not drop me a line and I'll send you one.

HTH,

Dave
Dundas



> Environment: VC++ 4.2b, NT 4.0
> 
> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?
> 
> Any input along these lines will be greatly appreciated
> 
> Dean Grimm
> Software Engineer / Cortron Corp.
> 
===================================================
                DUNDAS SOFTWARE LTD.  
===================================================
Internet: sales@dundas.com   Compuserve: 76060,101
Sales:  (800) 463-1492   or   (416) 239-7472 
Fax:    (416) 239-2183
WWW:      www.dundas.com      FTP:   ftp.dundas.com    
===================================================
-----From: "David Carballo" 

There is a sample on MDSN  that implements a NT Service in a C++ class
(Author Nigel Thompson I think). You can generate a AppWizard SDI
application, then remove mainframe, document, view, and any other GUI
classes (about box, ...). You can place the code in the main function of
the sample in your InitInstance.

Hope this helps.

David Carballo Escudero
fibeto@redestb.es
http://www.redestb.es/Personal/Fibeto

----------
> De: Dean Grimm 
> A: 'MFC list' 
> Asunto: Using MFC in a service
> Fecha: lunes 17 de marzo de 1997 16:17
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?
> 
> Any input along these lines will be greatly appreciated
> 
> Dean Grimm
> Software Engineer / Cortron Corp.
-----From: Rajitha Wijayaratne 

I havent actually written a service myself but I read in a magazine
somewhere that you could debug 
a service that is running already by running Visual C++ as :  "MSDEV -p
PID", where PID is the process ID which you can obtain by using PVIEW or
PSTAT. 

Rajitha.

>-----Original Message-----
>From:	Dean Grimm [SMTP:dean@cortron.com]
>Sent:	Tuesday, March 18, 1997 1:17 AM
>To:	'MFC list'
>Subject:	Using MFC in a service
>
>Environment: VC++ 4.2b, NT 4.0
>
>I am preparing to write my first NT service, and in doing so,
>have done a bit of research in MSDN, VC help, and the KB.
>None of the examples I found used MFC, which makes me wonder...
>Is it possible to use MFC in an NT service?
>
>Any input along these lines will be greatly appreciated
>
>Dean Grimm
>Software Engineer / Cortron Corp.
-----From: "Daniel W. Levi" 

Dean,

Yes, you can use MFC to write a service, but you may not want to.

Services usually have no user interface, and thats half of what MFC is for.
(Most services don't need to override OnSize() )
Also, using MFC may greatly expand the resources consumed.

A Service user interface would actually be for managment/confguration, and
for monitoring.

The right approach is probably 
1. Write the service as a non-MFC app
2. Write a service management/configuration tool using MFC.
3. Write a service monitoring tool using MFC.
4. Give the service a child thread with simple 
    message pump, that allow the user interface tools to 
    control the service.
5. The service can send messages to the monitoring app 
    to update its display

For example, dial-up networking comes in three parts, 
   Remote access = the service
   Remote Access Admin = management/configuration
   Dialup Networking monitor = monitoring tool

good luck,


Dan Levi

----------
> From: Dean Grimm 
> To: 'MFC list' 
> Subject: Using MFC in a service
> Date: Monday, March 17, 1997 10:17 AM
> 
> Environment: VC++ 4.2b, NT 4.0
> 
> I am preparing to write my first NT service, and in doing so,
> have done a bit of research in MSDN, VC help, and the KB.
> None of the examples I found used MFC, which makes me wonder...
> Is it possible to use MFC in an NT service?
> 
> Any input along these lines will be greatly appreciated
> 
> Dean Grimm
> Software Engineer / Cortron Corp.




Become an MFC-L member | Вернуться в корень Архива |