Solved

calling "postmessage()" in C++ mfc

Posted on 2008-10-31
5
1,019 Views
Last Modified: 2013-11-20
I'm use "Visual Studio 2005".  I'm  use C++  mfc (microsoft  foundation class).  I made a pointer  bottom of the  file, you can see it here.

};==========================>end of test11Dlg.h file  
extern Ctest11Dlg *g_MainDlg;

I use  pointer "g_MainDlg" to call "postmessage()" , you can see it below.

I want to know the  solution and  reason to the cause.

I mentioned the output.
 

#ifndef PORTSIP_CALLBACKEVENT_hxx

#define PORTSIP_CALLBACKEVENT_hxx

 

 

#include "stdafx.h"

#include "test11Dlg.h"

//#include <iostream.h>

 

 

 

// for SIP

 

struct SIPEvent : public SIPCallbackEvent 

{

	

 

	virtual void onCommand(SIPCallbackCommand * command)

	{

		if (!command)

		{

			return;

		}

 

 

 

		if (!g_MainDlg->PostMessage(WM_SIPEVENT, (WPARAM)command, 0))

		{

			

			delete command;

			command = NULL;

			

		}

	

 

 

 

 

 

	}

	

 

};

 

 

#endif

Open in new window

0
Comment
Question by:shaf81
5 Comments
 
LVL 14

Expert Comment

by:wayside
ID: 22850741
> extern Ctest11Dlg *g_MainDlg;

You've declared an external reference to a global variable. Somewhere in your code you have to allocate storage for the global.

So in one of your source files you need to have a line that looks like

Ctest11Dlg *g_MainDlg;

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 22856145
You better would make the g_MainDlg a static member fo your dialog class:

// test11Dlg.h
...
class Ctest11Dlg : public CDialog
{
public:
       static Ctest11Dlg *g_MainDlg;
       ....
};


// test11Dlg.cpp

....
// somewhere (outside of functions)
Ctest11Dlg* Ctest11Dlg::g_MainDlg = NULL;   // always make an initialization


// in constructor

Ctest11Dlg::Ctest11Dlg(CWnd* pParent, IDD, ....)
: CDialog(pParent)
{
      ....
      Ctest11Dlg::g_MainDlg = this;          // here you set the pointer
}


Then your code changes to

          if (Ctest11Dlg::g_MainDlg != NULL && 
             !Ctest11Dlg::g_MainDlg->PostMessage(WM_SIPEVENT, (WPARAM)command, 0))
          {

0
 
LVL 1

Expert Comment

by:suryaxchange
ID: 22865077
Hi,

extern variable do not allocate memory for the specified. They only link to the existing storage.
In your case

extern Ctest11Dlg *g_MainDlg;

g_MainDlg pointer is not allocated but tries to link to the global pointer to Ctest11Dlg which is not created.

if the storage is already created with the pointer of the same name somewhere in the code externally then the extern keyword can link to this pointer, for this reason the linking got failed in your case.

e.g., create a global integer pointer as follows in any .cpp file

int g_nIntVal = 9;

In any other file you refernece the same global variable in this way
extern int g_nIntVal;

here the name and datatype of the variable need to be same. Here the second declaration will not create a storage but  try to link to the existing storage.

-Surya





0
 
LVL 3

Author Closing Comment

by:shaf81
ID: 31512063
Thank you very much for your successful effort. Your  answer is perfectly correct.
0
 
LVL 3

Author Comment

by:shaf81
ID: 22865321
Hello Surya,

Thank you very much.
0

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
sumDigits challenge 9 122
abstract class with all non abstract mentods 6 76
How to gracefully close the c++ 11 thread? 3 83
Which Linux flavors will this run on? 6 76
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

21 Experts available now in Live!

Get 1:1 Help Now