Creating a window from a console app supporting MFC

The subject contains the whole question.
Note that I'd like to create a window, not a messagebox.

Some lines of VC code would be greatly appreciated.

(right now, I'm using the following:
      d->CreateWindowEx(WS_EX_CONTROLPARENT,"#32770",windowname,WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 0, 0, X,Y, NULL, NULL);

->It works from a dialog app, but crashes from a console app.
(it seems that it is looking for a parent window)
LVL 1
gaggioAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
0
gaggioAuthor Commented:
Thanks again, chensu :-)

This example is great but I can't accept this answer because
I'm using visual C++ (the example is raw C code) and I'm
trying to develop a console application that supports MFC
(see the second dialog box when you create a new Win32
application project)

0
chensuCommented:
Can't you learn how to do it from the sample?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Nicolay_ChCommented:
Create new thread and from it new window...
0
gaggioAuthor Commented:
Thanks Nicolay
Thanks Chensu

There is something I don't get quite well: you both seems to
be very skilled in MFC and C++, but none of you is giving me
a few lines of code to do the trick.

If it is that easy to create a new thread, why don't you give me
the line of code that does it?

Chensu: no, I can't, because my application supports MFC
and I'd like to use MFC if possible.
0
mjswartCommented:
#include <afx.h>
to use MFC classes, and you'll probably have to change the project settings to link with MFC (ALT+F7, General Tab, use MFC in a shared DLL)

Hope this helps
0
gaggioAuthor Commented:
mjswart, did you read the question?

I don't need to include MFC since I HAVE A ALREADY A CONSOLE
APPLICATION COMPILED WITH MFC SUPPORT!

please do not propose an answer if you haven't read the question!
0
mikeblasCommented:
You're not giving us enough information to go on.  What kind of window do you want to create from your console application? It looks like you're trying to create a dialog box, but doing so in a pretty screwy way.

What is the crash you suffer? Nothing in the single line of code you shown tells me that MFC should be looking for a parent window--so your diagnosis makes no sense to me.  What's "d" pointing to, by the way?

Me, I'm also very experienced in MFC and C++. But I'm not going to write any code for you because 1) you haven't been specific enough about what kind of code you would like to see written, and 2) you haven't offered enough points.

..B ekiM
0
Nicolay_ChCommented:
Sorry gaggio.

I try to create Window by MFC by using threads. I create it. But function Run of thread working wrong...
I look to it but nothing find..

If you want i show how I create it...

May be in MFC not possible to create window in console APP?

By windows API i not check... but it is not too dificult...
0
mikeblasCommented:
> May be in MFC not possible to create window in console APP?

No. It's very possible, and pretty easy to do.

..B ekiM
0
Nicolay_ChCommented:
mikeblast:

Why CWinThread not exitting from run function?
If i not call this function, then MFC not calling InitInstance function...

Or I need to call it by my hands?

When I analyze my code and trace MFC code, i found, that MFC think that my application are DLL... I don't know why...
0
mikeblasCommented:
> Why CWinThread not exitting from run function?

I don't know. There's at least a couple of conditions which make CWinThread::Run() exit; I guess none of them are being met in your case.

Please ask more specific questions.

..B ekiM
0
Nicolay_ChCommented:
in function _tmain:

_____________________________________
CMyWinThread* winThread;
AfxBeginThread(
     RUNTIME_CLASS(CMyThread)
)
winThread->Run();
^^^^^^^^^^^^^^^^^
 \
  \-Not terminating...
_____________________________________

My class CMyWinThread in InitInstance function create new window like in normal Windows based Application

I see, that CMyWinThread::InitInstance run only when I call CWinThread::Run
May be this happens because console application not work properly with windows messages?
0
mikeblasCommented:
What are you doing that makes you think CWinThread::Run() should terminate?  It will terminate if you (or your user) destroys the m_pMainWnd of the thread, or if you post a quit message to a window owned by that thread.

Are you doing those things? Are you sure you're doing them right? What's the state of that subordinate thread when you break into it in the debugger? Is m_pMainWnd still valid? Can you show that the thread is receiving WM_QUIT?

I still need a more specific question to provide any more help.  Maybe you should post your project so I can debug it for you--I don't think teaching you how to debug is going to work.

Or do you want me to write a working sample for you? What do you want the sample to do, specifically?

..B ekiM
0
Nicolay_ChCommented:
YEEEEES... It's working... ;-)
This is a code in function _tmain:
----------------------------------------
CMyWinThread*      mythread;

mythread = (CMyWinThread*)AfxBeginThread(RUNTIME_CLASS(CMyWinThread));
do {
} while (mythread->m_ThreadTerminated == false);
----------------------------------------
in header of CMyWinThread class defining boolean variable:
bool m_ThreadTerminated;
----------------------------------------
m_ThreadTerminated boolean variable setting to false in constructor of CMyThreadConstructor and to true in ExitInstance
----------------------------------------
InitInstance of your thread like in normal application (create your own window)
----------------------------------------
Window like normal, but in PostNCDestroy need to call AfxPostQuitMessage(0) function. If we not calling this function our thread not calling ExitInstance procedure...
----------------------------------------

thats all...
Not needed to call Run function of your thread...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikeblasCommented:

 > mythread = (CMyWinThread*)AfxBeginThread(RUNTIME_CLASS(CMyWinThread));
 > do {
 > } while (mythread->m_ThreadTerminated == false);

That code is absolutely miserable. You're spinning that tight loop to poll the flag, and that wastes an immense amount of processor time.

You should, instead, code this:

   mythread = (CMyWinThread*)AfxBeginThread(RUNTIME_CLASS(CMyWinThread));

   DWORD dwResult = WaitForSingleObject(mythread->m_hThread, 10000);
   if (dwResult == WAIT_TIMEOUT)
   {
      // the thread didn't finish within 10000 milliseconds
      // do something--maybe wait some more!
   }
   else
   {
      // the thread is finished and we can continue
   }

WaitForSingleObject() will block until the thread is done. There's no need to sit and waste CPU time.

..B ekiM
0
mikeblasCommented:
Wait a minute: I'm here answering questions for the guy who ended up submitting an answer to the original question for points?

That's sick and wrong.

..B ekiM
0
Nicolay_ChCommented:
mikeblas: Ok...Ok...
You are right with WaitForSingleObject...

But sorry...
I not understant you second comment...
(English not my native)
0
Nicolay_ChCommented:
Ughm...

Sorry if I take your points...
If you want you may take it...

But why?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.