Modeless Dialog problem when passing values to it from socket

Hi,

I have a socket class that receives some data, upon receiving this data it does this:

      if (command == "ONLINEUSERLIST")
      {
            //AfxMessageBox(strRecvd);
            ((CListUsersDlg*)m_pWnd)->doProcessOnlineUsers(strRecvd);
      }



void CListUsersDlg::doProcessOnlineUsers(CString strRecvd)
{
      AfxMessageBox(strRecvd);

      char *received = strRecvd.GetBuffer(4096);
      char seps[] = " ";
      char *token;
      CString users;
                  
      token = strtok(received, seps);
      users = token; // what user here

      while (token != NULL)
      {
            m_ctlListUsers.AddString(users);

            token = strtok(NULL, seps);
            users = token; // client userName, to find the buddy file .ini to ammend/create
      }
}

but when m_ctlListUsers.AddString(users); is happens i get an assert error:

      { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }

Call Stack:
CListBox::AddString(const char * 0x00a92c5c) line 669 + 46 bytes
CListUsersDlg::doProcessOnlineUsers(CString {"ONLINEUSERLIST"}) line 82
...

Context:

AddString(users)
+      lpszItem      0x00a92c5c "ONLINEUSERLIST"
-      m_hWnd      0x00000000
      unused      CXX0030: Error: expression cannot be evaluated
-      this      0x0012faa8 {COptionsDlg hWnd=0x00000000}
+      [COptionsDlg]      {COptionsDlg hWnd=0x00000000}
-      CWnd      {CWnd hWnd=0x00000000}
+      CCmdTarget      {CCmdTarget}
+      classCWnd      {"CWnd"}
+      m_hWnd      0x00000000
+      wndTop      {CWnd hWnd=0x00000000}
+      wndBottom      {CWnd hWnd=0x00000001}
+      wndTopMost      {CWnd hWnd=0xffffffff}
+      wndNoTopMost      {CWnd hWnd=0xfffffffe}
+      m_hWndOwner      0x00000000
      m_nFlags      0
      m_pfnSuper      0x00000000
      m_nMsgDragList      49370
      m_nModalResult      0
+      m_pDropTarget      0x00000000 {COleDropTarget}
+      m_pCtrlCont      0x00000000 {COleControlContainer}
+      m_pCtrlSite      0x00000000 {COleControlSite}
+      _messageEntries      0x005bee28 struct AFX_MSGMAP_ENTRY const * const CWnd::_messageEntries
+      messageMap      {...}
-      classCListBox      {"CListBox"}
+      m_lpszClassName      0x005e0504 "CListBox"
      m_nObjectSize      60
      m_wSchema      65535
      m_pfnCreateObject      0x00000000
+      m_pBaseClass      0x005bee08 struct CRuntimeClass const CWnd::classCWnd
+      m_pNextClass      0x00000000 {???}


How do i fix this from happening?

Thanks

nikon70Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

nikon70Author Commented:
oh and here is how i created the dialog:

void CBuddyChatDlg::OnContactsListcontacts()
{
      // TODO: Add your command handler code here
      //m_dListUsersDlg.DoModal();
      CListUsersDlg* dlg = new CListUsersDlg;

      dlg->SetSockConnection(&m_sConnectSocket); //added to help classes interact

      dlg->Create(IDD_LIST_USERS_DIALOG);
      dlg->ShowWindow(SW_SHOW);      
      // adding pointer for dialog to reference later to get correct dlg to chat to
      
}
AndyAinscowFreelance programmer / ConsultantCommented:
Have you attatched a listbox to your control variable m_ctlListUsers?

ie used class wizard to create a variable or subclassed a control to the variable.

The error message relates to no window being associated with your control
nikon70Author Commented:
Hi,

Yes the m_ctlListUsers is attatched my CListBox.

CListBox      m_ctlListUsers;

=/
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

nikon70Author Commented:
if you want to see the code:

http://212.158.234.59/BuddyChat_v2.rar

cheers.
AndyAinscowFreelance programmer / ConsultantCommented:
Just to check, what happens if you add the line checking the contents of var users.  Is the expected value?

   users = token; // what user here
AfxMessageBox(users);
nikon70Author Commented:
i get a list of users, but then it tries to do:

m_ctlListUsers.AddString(users);

and it dies.
AndyAinscowFreelance programmer / ConsultantCommented:
I am puzzled.  Please give the following a go to make certain your dlg and list are windows.



void CListUsersDlg::doProcessOnlineUsers(CString strRecvd)
{
ASSERT(GetSafeHwnd() != NULL);
ASSERT( m_ctlListUsers.GetSafeHwnd() != NULL);


     AfxMessageBox(strRecvd);

     char *received = strRecvd.GetBuffer(4096);
     char seps[] = " ";
     char *token;
     CString users;
               
     token = strtok(received, seps);
     users = token; // what user here

     while (token != NULL)
     {
          m_ctlListUsers.AddString(users);

          token = strtok(NULL, seps);
          users = token; // client userName, to find the buddy file .ini to ammend/create
     }
}

nikon70Author Commented:
Problem here:

ASSERT(m_ctlListUsers.GetSafeHwnd() != NULL);


-      m_ctlListUsers      {CListBox hWnd=0x00000000}
+      CWnd      {CWnd hWnd=0x00000000}
+      classCListBox      {"CListBox"}
-      this      0x0012fa4c {CBuddyChatDlg hWnd=???}
+      CDialog      {CDialog hWnd=0x002f07ec}
+      m_ctlListUsers      {CListBox hWnd=0x00000000}
+      _messageEntries      0x005bda18 struct AFX_MSGMAP_ENTRY const * const CListUsersDlg::_messageEntries
+      messageMap      {...}
+      m_Socket      0x00000082 {CConnSocket}
AndyAinscowFreelance programmer / ConsultantCommented:
Another thing to try

    while (token != NULL)
     {
          m_ctlListUsers.AddString(users);   //ADD BREAKPOINT HERE

use a breakpoint (F9 key) and single step to check when it fails and what value 'users' has.
AndyAinscowFreelance programmer / ConsultantCommented:
Ignore last comment.
I've just done a quickie dialog

in .h file

      //{{AFX_DATA(CAboutDlg)
      enum { IDD = IDD_ABOUTBOX };
      CListBox      m_lstUsers;
      //}}AFX_DATA

in .cpp file
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CAboutDlg)
      DDX_Control(pDX, IDC_LIST1, m_lstUsers);
      //}}AFX_DATA_MAP
}

Do you have code like that to link your list control to a control that exists?
nikon70Author Commented:
right let me start from the beginning:

i click search and a modeless dialog opens, from my CBuddyChatDlg, and opens a CListUsersDlg,

i then click a button on my CListUsersDlg box, view online users, which sends some data over a socket and then

data comes in from a socket which is passed to from my CConnSocket class to ((CListUsersDlg*)m_pWnd)->doProcessOnlineUsers(strRecvd);

which is where it does this: m_ctlListUsers.AddString(users);

ok, now yes in my CListUsersDlg.h i have:

public:
      void doProcessOnlineUsers(CString);
      void SetSockConnection(CConnSocket *);
      CListUsersDlg(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
      //{{AFX_DATA(CListUsersDlg)
      enum { IDD = IDD_LIST_USERS_DIALOG };
      CListBox      m_ctlListUsers;
      //}}AFX_DATA

and in my CListUsersDlg.cpp i have:

void CListUsersDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CListUsersDlg)
      DDX_Control(pDX, IDC_LISTUSERS, m_ctlListUsers);
      //}}AFX_DATA_MAP
}

does that all seem to be correct the way i am passing data and that i have the correct code in the correct class(s) ??

AndyAinscowFreelance programmer / ConsultantCommented:
You posted earlier
void CBuddyChatDlg::OnContactsListcontacts()
{
     // TODO: Add your command handler code here
     //m_dListUsersDlg.DoModal();
     CListUsersDlg* dlg = new CListUsersDlg;

     dlg->SetSockConnection(&m_sConnectSocket); //added to help classes interact

     dlg->Create(IDD_LIST_USERS_DIALOG);
     dlg->ShowWindow(SW_SHOW);    
     // adding pointer for dialog to reference later to get correct dlg to chat to
     
}

and from your latest post
data comes in from a socket which is passed to from my CConnSocket class to ((CListUsersDlg*)m_pWnd)->doProcessOnlineUsers(strRecvd);


This m_pWnd is a pointer to the modeless CListUsersDlg dialog you create.  Please check your code to confirm.
Otherwise I can't see anything wrong with what you post.  (I assume IDC_LISTUSERS is the resource ID of a listbox).
In the OnInitDialog of your CListUsersDlg after it calls the base class OnInitDialog you could also check the list control has been correctly hooked.

CDialog::OnInitDialog();
ASSERT(m_ctlListUsers.GetSafeHwnd() != NULL);
nikon70Author Commented:
m_pWnd is set in my CConnSocket class and the code is:

void CConnSocket::SetParent(CDialog *pWnd)
{
      //set pointer
      m_pWnd = pWnd;
}

i also use this point for:

      if (command == "MSG")
      {
            //CString msg = "connsocket on receive if command == msg .. ";
            //AfxMessageBox(msg + strRecvd);

            ((CBuddyChatDlg*)m_pWnd) -> doProcessMsg(strRecvd); // receive a message

      }

and:

      if (command == "ONLINEUSERLIST")
      {
            //AfxMessageBox(strRecvd);
            ((CListUsersDlg*)m_pWnd)->doProcessOnlineUsers(strRecvd);
      }

is this right?

and yes  IDC_LISTUSERS is my list box resource, control name m_ctlListUsers
AndyAinscowFreelance programmer / ConsultantCommented:
    if (command == "MSG")
     {
          //CString msg = "connsocket on receive if command == msg .. ";
          //AfxMessageBox(msg + strRecvd);

          ((CBuddyChatDlg*)m_pWnd) -> doProcessMsg(strRecvd); // receive a message

     }

and:

     if (command == "ONLINEUSERLIST")
     {
          //AfxMessageBox(strRecvd);
          ((CListUsersDlg*)m_pWnd)->doProcessOnlineUsers(strRecvd);
     }

The first if statement casts m_pWnd to CBuddyChatDlg*, the second casts it to CListUsersDlg*,
One (or both) of these must be wrong.
I think your CListUsersDlg is a child of the buddy dialog so keep a pointer to it there and get it through the buddy.  (I assume the pointer to the CConnSocket is a pointer to your buddy so the second if should be something like

   if (command == "ONLINEUSERLIST")
     {
          //AfxMessageBox(strRecvd);
          ((CListUsersDlg*)((CBuddyChatDlg*)m_pWnd)->m_pWndListUserDlg)->doProcessOnlineUsers(strRecvd);
     }
nikon70Author Commented:
that is still causing the same error.
AndyAinscowFreelance programmer / ConsultantCommented:
Can you e-mail it to me.  I'll have a look monday
{{ email addr removed -- DR / EE PE }}
AndyAinscowFreelance programmer / ConsultantCommented:
I think you need to get at the dialog with the list in like this
((CBuddyChatDlg*)m_pWnd)->m_dListUsersDlg.m_ctlListUsers

You will need the list dlg as a public member and you ought to check it is initialised before passing the message onto it.

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
AndyAinscowFreelance programmer / ConsultantCommented:
My final comment should have solved it.
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.