this vs. (Cwnd*)m_hWnd #2

Posted on 1998-03-19
Last Modified: 2013-11-19
Sorry, guys. The comment list was getting a little too long for my taste on that question, so let's start fresh. Here's what's going on:
Yes, I have tried changing that value. (to -1) Is that a valid thing to test out your idea, Shogi? In addition to that, thanks for the help guys, and here's that source:

#include "cdedit.h"

CDEdit :: CDEdit(int Mode)
      _validationMode = Mode;

void CDEdit :: OnKillFocus( CWnd* pNewWnd )
      char BoxText[255], NewText[255];
      double DummyD;

      GetWindowText(BoxText, 254);
      if(strlen(BoxText) == 0)
      if(_validator.ConvertString(_validationMode, BoxText, NewText, &DummyD))
            AfxMessageBox("Error with value!");


And the header:
#ifndef CDEDIT_H
#define CDEDIT_H

#include "stdafx.h"
#include "CDataVal.h"
class CDEdit : public CEdit
      CDEdit(int Mode);
      CDataValidation _validator;
      int _validationMode;
      void OnKillFocus( CWnd* pNewWnd );
// Defines for use with this class
// These specify the validation mode.
#define CD_AMOUNT_MODE       1//For cash amounts
#define CD_NUMBER_MODE       2//Just convert to value from string
#define CD_DISPLAY_DATE_MODE 3//In the form: mm/dd/yy
#define CD_PARAM_DATE_MODE   4//In the form: yyyymmdd from mm/dd/yy

#define CD_ERROR_DATE  5//General (no delimiters)
#define CD_ERROR_DAY   6
#define CD_ERROR_MONTH 7
#define CD_ERROR_YEAR  8
So what do you think?
Thanks again.
Question by:Beatachon
  • 10
  • 5

Author Comment

ID: 1317080
Keep in mind I'm programming a 16-bit dll here. Would that make any difference?


Expert Comment

ID: 1317081
(I would have suggested reducing the point value to 0 on the old question, marked it as answered and then opened up this new one.  Some people benefit from the *discussion*, not just the answer! :P)

   Anyway...  Back on topic..  I am running out of ideas on this one.  I cannot see, from the ST and source, what might be wrong.

   When the assertion fails, what is it failing on?  Is the Dialog (Create(...)) unable to resolve the passed CWnd into a valid window?  Try passing in a NULL CWnd (Dialogs do not HAVE to have a parent window).

   Also try this:  Override the constructor to accept a HWND instead of a CWnd*, and use CWnd::FromHandlePermanant() (or CWnd::FromHandle()) in the overridden Create(...) to get a CWnd*, and pass that to the Create() function.  Note that you might have the same problem as before:  CWnd::FromHandlePerm() might return NULL, which is basically what is happening now.

-=- James.

Author Comment

ID: 1317082
I'm not passing in a parent window to the dialog as it is. So by default Null is going in. I'll try your FromHandlePermanent suggestion to pass the pointer to the create function. I'm also going to try creating just a plain old CEdit object to see what happens. Let's narrow it down. I'll keep you posted.
(I know, REALLY bad pun.)

Author Comment

ID: 1317083
Well, no luck. I think it's pooping out at ASSERT_VALID(this); inside BOOL CMapWordToPtr::Lookup(WORD key, void*& rValue) const. But who's calling this? Is the Create function for the control calling it or is the dialog?
FromHandlePermanent(m_hWnd); gave me back the same thing the this pointer has.


Author Comment

ID: 1317084
It's actually the AfxWndProc. The line is:
pWnd = CWnd::FromHandlePermanent(hWnd);
Just like you said, it is the FromHandlePermanent.
But whose? The control or the dialog. I've now seen the FromHandlePermanent work for the dialog.

Expert Comment

ID: 1317085
  Let me see the stack trace again, please...

-=- James.

Author Comment

ID: 1317086
Of course, the this pointer at that point Does look like this:
-this = 0x23D7:0x8030
  +CObject = {...}
  +classCMapWordToPtr = {...}
  +m_pHashTable = 0x0000:0x0000 protected
   m_nHashTableSize = 0 protected
   m_nCount = 0 protected
  +m_pFreeList = 0x0000:0x0000 protected
  +m_pBlocks = 0x0000:0x0000 protected
   m_nBlockSize = 0 protected

All nulled. Hmm.
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Author Comment

ID: 1317087
Here you go:
This does all work when I build it in my win32 version, by the way. Is it something specific to 16-bit that I'm not doing properly?

Expert Comment

ID: 1317088
  The Assertion is in CDEdit::Create(...).  From here, it looks like it cannot find the HWND (window) associated with the CWnd* that is passed into Create(...).

   Does the Control (CDEdit) exist within the DLL or does the Dialog and Control exist in the DLL?  Maybe that has something to do with it.

   Remember my last suggestion, about overriding Create(...)?  Try it again, but this time, create a CWnd and wrap the HWND with it (use CWnd::Attach(...)).  Make the CWnd a member of the CDEdit class.

-=- James.

Author Comment

ID: 1317089
Does this look right to you?

in the header file under private I have:
CWnd _dummyCWnd;

and then implemented this:

BOOL CDEdit::Create(DWORD dwStyle, const RECT& rect, HWND HParent/*CWnd* pParentWnd*/, UINT nID)
      return CWnd::Create("EDIT", NULL, dwStyle, rect, &_dummyCWnd, nID);

The reason I ask is that the Attach function asserts also. Any ideas?

Author Comment

ID: 1317090
Asserts on this line:
ASSERT(FromHandlePermanent(hWndNew) == NULL);
                              // must not already be in permanent map

Expert Comment

ID: 1317091
  What about the DLL/Application question that I asked?

   FWIW, this is exactly what happens when you try to share CWnd (derived/using) objects between threads in Win32: Because each thread has its on handle-map, it cannot find mappings that were made in another thread.

   Attach() should not ASSERT unless (I believe) the HWND that was given to it is invalid.

   Try this...  What does IsWindow() (is that available under Win16?) return when you give it:

   o (In the dialog class, right before CDEdit::Create()) m_hWnd
   o (In CDEdit::Create()) HParent

   It should return a Non-Zero value indicating that the HWND that was given to it actually reperesents a valid (Existing) window.

-=- James.

Author Comment

ID: 1317092
IsWindow returns good values.
I'm giving up. I'm just going to try to lay the controls out in the dialog template and then subclass them. Think it'll work?
Am I right in thinking that there is no UnSubclassWindow() in 16-bit? All I have to do is call SubClassWindow()?
(Answer this question. I wanna give you the points for helping me out.)

Accepted Solution

jtwine100697 earned 250 total points
ID: 1317093
  I am not the only one that helped you out, but OK... :P

   AFAIK, all you have to do is call SubclassWindow() with your object to subclass an existing control.

-=- James.

Author Comment

ID: 1317094
Yeah, you're right, but you spent the most time. Thanks to you to, Shogi.
See you guys later.
(I mean that. I'll probably be back)
There was a reason we left 16-bit behind.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 89
sum13 challenge 24 90
matchUp  challenge 6 53
Problem to open Excel file 15 97
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now