NT/2000 Service with some of MFC support 
Author Message
 NT/2000 Service with some of MFC support

I am going by ATL to create an NT/2000 Service project. Is
there any way that is able to be included MFC features (I
believe it is but I don't know what head files should
be "#include"d), especially CString, since I like to embed
code with MFC from a existing project into this NT Service
project.

Thanks in advance.



Sun, 30 Jan 2005 05:56:24 GMT  
 NT/2000 Service with some of MFC support
I'll strongly advise against adding MFC support into an NT service.
MFC is officially unsupported in NT services. Try using WTL if it
is only for CString... Or use basic_string<TCHAR>.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:

> I am going by ATL to create an NT/2000 Service project. Is
> there any way that is able to be included MFC features (I
> believe it is but I don't know what head files should
> be "#include"d), especially CString, since I like to embed
> code with MFC from a existing project into this NT Service
> project.

> Thanks in advance.



Mon, 31 Jan 2005 01:43:45 GMT  
 NT/2000 Service with some of MFC support
it's ok to use MFC in service apps, it won't have any gui code anyways, but
if you're just looking for CString and stuff.. ATL7 has shared classes with
MFC. so you can use CAtlString and some other shared utility classes

Quote:

> I am going by ATL to create an NT/2000 Service project. Is
> there any way that is able to be included MFC features (I
> believe it is but I don't know what head files should
> be "#include"d), especially CString, since I like to embed
> code with MFC from a existing project into this NT Service
> project.

> Thanks in advance.



Fri, 04 Feb 2005 11:23:34 GMT  
 NT/2000 Service with some of MFC support
Hi Booster,

Actually Alexander's recommendation is very sound. While you may be able to
successfully link in some of the MFC codebase into an NT Service, it is
still recommended that you avoid this scenario.

MFC make very heavy use of process and thread state information, and it's
very easy to get into a situation where it becomes very difficult to debug
problems due to this state getting corrupted due to MFC not being
initialized properly on the ServiceMain thread. The core issue is that an
NT Service has 2 threads. The startup thread which implements the initial
entrypoint (typically WinMain), and a service thread (ServiceMain) that is
launched by the OS when the WinMain calls StartServiceCtrlDispatcher.

The problem is that MFC is initialized on that first thread, and not the
second. Most people want to use MFC in the ServiceMain thread, and thread
state information is not propery initialized for that thread.
StartServiceCtrlDispatcher simple calls CreateThread, and doesn't use
AfxBeginThread. So you can easily end up with thread state and handle map
problems, when you do anything outside of using trivial classes like
CString.

Over here on the VC Developer Support team, we've had to recommend
customers backtrack and reimplement portions of their service after a lot
of debugging and analysis confirmed that the problem was due to how they
were attempting to use MFC in the ServiceMain thread. It's a pretty ugly
process, and one that we hope people avoid up front. Hence Alexander's
recommendation.

As an alternative, I usually recommend folks put the MFC specific stuff in
a regular MFC DLL, and call into the DLL via a number of exported
functions. This helps to avoid thread state and handle map issues
altogether. If VC++ .Net is an option, then I highly recommend the new
CString classes, as they are now templatized and no longer tied to the MFC
libraries.

Sincerely,
Ed Dore [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.



Tue, 08 Feb 2005 03:15:28 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Windows NT/2000 Service with MFC and Sockets

2. Using MFC DB support in NT Service

3. ActiveX exe with MFC support as NT service

4. ActiveX EXE as NT service with MFC support

5. Using MFC DB support in NT Service

6. supporting XP, 2000, NT, and 98

7. Writing a Service for Windows 2000/NT

8. NT/2000 Service(s)

9. Windows 2000 & MFC-exe server + ATL-support

10. Windows 2000 & MFC-exe server + ATL-support

11. MFC Database/Office 2000 Support

12. NT Service Using MFC (Samples\sdk\winnt\service) PLEASE HELP

 

 
Powered by phpBB® Forum Software