Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

MFC in console application?


What parts of MFC (if any) can be used in an application
without a CWinApp-derived class (no message pump)?

This is a list of things I've been using:
* CFile, CSocketFile
* CArchive
* the Internet services (CInternetSession)
* simple value types (CString, CTime, ...)
* Windows sockets (CAsyncSocket/CSocket)
* different synchronisation objects (CEvent, CMutex, ...)

Am I right to assume that these will work correctly
in, for example, a simple console application provided I
pull in the correct header files and link with MFC?

If not, why? For 200 points I expect more than a Yes/No
answer. :-)  (A nice URL or a chapter in any of the more
famous MFC books is also accepted.)

If yes, what other MFC classes works correctly in an
environment without CWinApp/message pump?
0
Daniel_E
Asked:
Daniel_E
  • 2
1 Solution
 
zingoCommented:
Yes. This is how I do it:

1. Create a console application
2. Change run-time library to Multi-threaded DLL (in Project -> Settings)
3. Add the defines _AFXDLL and _MBCS (in Project -> Settings)
4. Copy the files StdAfx.cpp and stdafx.h to your project from a MFC project
5. Include stdafx.h in your source files and StdAfx.cpp in your project
6. Go ahead and use MFC (non-gui classes)

These are the classes I have used in this way:

CString, CObArray, CAsyncSocket, and some more.

I expect that all non-gui classes would work.

Good luck!
0
 
Daniel_EAuthor Commented:
I'm sorry zingo, but you didn't actually answer the question. I'm
therefore reopening it to other experts.

I wasn't looking for confirmation that it _might_ work. I'm
already using MFC in console apps and regular DLLs (I'm sorry if
this wasn't clear). I'm looking for hard proof that it _will_
work, and possibly a list of exactly which classes can be used
without CWinApp/message pump. I'll rephrase the question to make
this clear.

You're more than welcome to re-answer it if you feel you can
provide me with the information I'm looking for.

Thanks anyway!

0
 
tma050898Commented:
Daniel,

I don't know of any source that goes through every one of the hundreds of MFC classes specifying which ones can be used in console apps. However, here's the list that you presented. Not that sockets can be used from a console application if you manually create the message pump yourself. I had to do this once a couple of years ago when I needed to use sockets from an NT service.

* CFile, CSocketFile = can be used
* CArchive = can be used
* the Internet services (CInternetSession) = can be used
* simple value types (CString, CTime, ...) = can be used
* Windows sockets (CAsyncSocket/CSocket) = can NOT be used
* different synchronisation objects (CEvent, CMutex, ...) = can be used

Tom

0
 
tma050898Commented:
Excuse the typo in my answer. The second sentence should start "Note that sockets..."
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now