在 Windows 服务中使用 MFC?

问题描述:

我开始开发 Windows 服务.我想使用我自己的一些类,它们对某些 MFC 类(如 CString、CSocket、CArchive、CMemFile 和 CObject)几乎没有依赖性.MSDN 说你需要非常小心哪些您在 Windows 服务中使用的 MFC 片段,但不要指定它,也不要描述可能发生的问题.

I'm starting to develop a Windows service. I want to use some classes from my own, that has little dependencies to some MFC classes like CString, CSocket, CArchive, CMemFile and CObject. MSDN says you need to be very careful about which pieces of MFC you use in the Windows service, but don't specifies it and don't describes the problems that can occur.

我的问题是:

  • 可以使用哪些 MFC?
  • 使用 MFC 会出现什么问题?
  • Windows 服务的哪些部分对 MFC 的使用至关重要?
  • 对于 Windows 服务是否建议使用 ATL 而不是 MFC?

我不确定 MSDN 文章中的意思.只要您不使用任何 GUI 功能就可以了 - 但这是开发服务时的一般设计问题.

I'm not sure what they mean in teh MSDN article. As long as you don't use any of the GUI functionality you'll be fine - but that's a general design issue when developing services.

话虽如此,ATL 具有专为构建服务 IIRC 设计的功能,因此您最好使用它.

That being said, ATL has functionality specifically designed for building services IIRC so you may be better off using that.

回答您的问题(尽我所知):

To answer your questions (to the best of my knowledge):

1) 您指定的那些没有问题.

1) the ones you specify are no problem.

2) 我猜他们的意思是 UI 组件的同步问题.只要您不使用任何 CWnd 派生类,您就会没事.

2) I guess they mean synchronization issues with UI components. As long as you don't use any CWnd-derived classes you'll be fine.

3) 不明白这个问题.

3) don't understand the question.

4) 之前见,加上 ATL 更轻量级,因此您必须分发更少,并提供内置功能,从而减少开发服务的痛苦.见例如CAtlServiceModuleT.您仍然可以主要使用您自己的类,因为现在 CString 在 MFC 和 ATL 之间共享,并且 ATL 具有用于套接字编程和内存文件映射本身的类.它没有 CArchive 的等效项,而且我不确定您在 CObject 中使用了什么功能,所以我不能说 ATL 中是否有等效项.所以总而言之,我对这个问题说是".

4) See before, plus ATL is more lightweight so you'll have to distribute less, and provides build-in functionality that'll make it less of a pain to develop the service. See e.g. CAtlServiceModuleT. You'll still be able to mostly use your own classes, as CString is shared between MFC and ATL nowadays and ATL has classes for socket programming and memory file mapping itself. It doesn't have an equivalent for CArchive, and I'm not sure what functionality you use in CObject so I can't say whether there's an equivalent in ATL. So to conclude, I'd say 'yes' to this question.