Hi @ all, b4 things get serious. :icon_smile:

Im using MFC with VCPP 6.
I have a simple dialoge based app. Im using a triggered event with which I want to update the content of a CEdit box.
The ASSERT of IsWindow fails because m_hWind = 0, I figure this is because the class of the trigger event is not base on CWnd or similar.

The setup is as follows:
1. Trigger event in MainControl.cpp (not CWnd based) creates a new instance of BERT_EVAL.cpp(CTabPage based, containg edit box).

2. Member funktion of BERT_EVAL.cpp called to update CEdit content.

3.
Fail at assert.

Snippets:
1. In trigger event (MainControl.cpp):

BERT_EVAL t_Eval = new BERT_EVAL;
t_Eval.OnReceivedFPGAIND();

2. In OnReceivedFPGAIND (BERT_EVAL.cpp):

CButton *SetTxt = new CButton;  
SetTxt = reinterpret_cast<CButton*>(GetDlgItem(IDC_EDT_SR1));
SetRndTxt->SetWindowText("OK!");

DoDataExchange:

DDX_Control(pDX, IDC_EDT_SR1, m_edt_sr1);

Note: IDC_EDT_SR1 is a CEdit Control.

As far as I understand the problem either at the time of assertion the window does not exist. The question is why since I create an instance of BERT_EVAL which is CTabPage based.

Thx in advance.
t.o.

Recommended Answers

All 9 Replies

I am rusty on this but as you say if hWind = 0
then you are using a method that requires a handle to a window if this is 0 it most likely means that you have not created a window successfully. So your new might not have created a window!

Another possibility is that you just need a method to get either ther current window or its parent without seeing context it is tricky to say for sure.

It would be helpful if you had included the code where m_hWind is called :)

Hi @ all, b4 things get The question is why since I create an instance of BERT_EVAL which is CTabPage based.

Thx in advance.
t.o.

Hi teron,
thx for the quick reply.

It would be helpful if you had included the code where m_hWind is called

m_hWind is called in the standard assert of

SetTxt = reinterpret_cast<CButton*>(GetDlgItem(IDC_EDT_SR1));

this would be:

CWnd* CWnd::GetDlgItem(int nID) const
{
	ASSERT(::IsWindow(m_hWnd));  <---

	if (m_pCtrlCont == NULL)
		return CWnd::FromHandle(::GetDlgItem(m_hWnd, nID));
	else
		return m_pCtrlCont->GetDlgItem(nID);
}

I have tried

CWnd* myWnd = GetActiveWindow();

which returns a non zero value, the question is now how do I

CButton *SetRndTxt = new CButton; 
SetRndTxt = reinterpret_cast<CButton*>(GetDlgItem(IDC_EDT_SR1));
SetRndTxt->SetWindowText("Good!!!");

for the window with that hWnd value?

I have become a little confused...

You want to add a button at runtime and then set its text?
At first glance what might be going wrong is the SetWindowText I think the button has to exist for this
to work and it doesn't until your create it.

to add a button as well as its constructor
you need to create it

try google or msdn:
http://msdn.microsoft.com/en-us/library/bw4e0cww.aspx

a tutorial that seems ok too is:

http://www.functionx.com/visualc/controls/button.htm

Your problem that you are showing there though is not dependent on the handle so I might not be answering your question very well.

Hi again,
sorry for the confusion but the entire GUI alredy exitst (create returned non-zero value) before the trigger event occurs because a button on that CTabPage needs to be pressed to start triggering. The problem is only that the the trigger event happens in a Class that is not derived from a window type.

I have had temporary good results using ff code:

CWnd* myWnd = GetActiveWindow();
CWnd* pControl = myWnd->GetDlgItem (IDC_EDT_XXX);
HWND hwndControl = pControl->m_hWnd;
pControl->SetWindowText("Good!!!");
pControl->InvalidateRect (NULL);
pControl->UpdateWindow ();

This worked once, after that the trigger event doesnt trigger any more and I get the Access Violation again.

The use of active window might not be what you want.

Where are you creating the window?

If you have visual studio there are ways to avoid having to do all this.

But with an object like a button it is an object that has to be attached to a window so you first need to have a window to add it to.

The active window may not give you this level of control to be able to add things.

If you try going through the second example I gave to get a window to show with a button I think you might see the point that I am making.

the reason for ASSERTing that the handle was not 0 is in case you had not initialised the window propperly. It is tricky to create a window but once you have done it once the code does not alter.

I have had temporary good results using ff code:

CWnd* myWnd = GetActiveWindow();
CWnd* pControl = myWnd->GetDlgItem (IDC_EDT_XXX);
HWND hwndControl = pControl->m_hWnd;
pControl->SetWindowText("Good!!!");
pControl->InvalidateRect (NULL);
pControl->UpdateWindow ();

This worked once, after that the trigger event doesnt trigger any more and I get the Access Violation again.

OK it seems like I'm a bad explainer so I'll give it another try :) :
The Window containing the Editbox ist created similar to your 2. link using the ClassWizzard. I created a buton and a CEditBox control. The BN_CLICK function of the button sends a request messages to a hardware to read a certain register. This hardware sends reply messages. The hardware sends many type of messages, so I use a "sorting class" (called MainControl.cpp) to switch case the message ids for the the desired id. (MainControl.cpp is not of type Wnd.) I then call a member funtion of the CTabPage Class (called BERT_EVAL.cpp) containing the CEditBox and the CButon with wich I want to disply the output of the recieved message.

The problem is that a non-Wnd derived class starts the trigger an there with no window is attached to the process.

If I place ff code directly in the BN_CLICK the text is displayed allright:

CWnd* pControl = GetDlgItem (IDC_EDT_SR1);
HWND hwndControl = pControl->m_hWnd;
pControl->SetWindowText("just good.");
pControl->InvalidateRect (NULL);
pControl->UpdateWindow ();

And with the lines

CWnd* myWnd = GetActiveWindow();
CWnd* pControl = myWnd->GetDlgItem (IDC_EDT_XXX);

the present windows DlgItem should be used, but pControl still ends up empty -.-

Active window can have changed.

http://msdn.microsoft.com/en-us/library/ms633497(VS.85).aspx

is an EnumWindows function that appears to let you step through all of the windows
if GetDlgItem is failing to find your resource it could well be the wrong window although I though t it returned a handle

If this doesn't help the question might be down to how you are handling the messages do you then post a message to the window? Why can't maincontrol just be an event listener in a window?

how does your tab page (? type of tab control work)

Sorry, I'm probably being a bad understander today:)

Solved it!!!

I declared

CButton *SetTxt = new CButton;

as global and initialized it as

SetTxt = reinterpret_cast<CButton*>(GetDlgItem(IDC_EDT_SR1));

in the BN_CLICK funktion. By the time the trigger snaps in its already declared and initialized so

SetRndTxt->SetWindowText("OK!");

works because it hast a valid hWnd.

Thank you very much for helping me out =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.