Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing references between Dialogs

Status
Not open for further replies.

Valius

Programmer
Oct 20, 2000
174
US
I'm trying to figgure out how to pass variable references between Dialogs. I think I figgured out a way, but it looks kinda messy and I want to clean it up a bit. Any info on this topic would be greatly appreciated. Thanks in advance!

Niky Williams
NTS Marketing
 
Try to use Windows messages for passing references between dialogs.

BlueJacket
 
Niky,

First, pass a reference only when you are certain the origin variable will not go out of scope.

Second, I prefer to use an 'Interface' to obtain data in dialogs, windows and threads. If you use an interface you can always move the resource to another code location and not have to change all the previous code that used the interface to obtain the data.

struct SessionStatus{
CString user;
CString ID;
COleDateTime loggedIn;
};

class IStatusProvider{
SessionStatus& getStatus();
};

class MyDoc : public CDocument, public IStatusProvider{
protected:
SessionStatus _status;
public:
SessionStatus& getStatus(){ return _status; }
};

class MyDlg : public CDialog{
protected:
IStatusProvider* _pStatusProvider;
public:
void setStatusProvider( IStatusProvider* pProvider){
_pStatusProvider = pProvider;
}
}

void MyDoc::eek:nSomeEvent(...){
MyDlg dlg();
dlg.setStatusProvider(this);
dlg.DoModal();
}

LRESULT MyDlg::OnInitDialog(..){
SessionStatus& sStat = _pStatusProvider->getStatus();
}

"But, that's just my opinion... I could be wrong".
-pete
 
Wow, a lot of info to digest. I never thought of that. Gonna take me a while to glean the information. Thank you so much for your input!

Niky Williams
NTS Marketing
 
How do you use windows messages? I've looked them up and tried them, but I haven't had any luck. I'm about ready to pull my hair out. If you point me to some resources on it, I would appreciate it very much! I searched MSDN, but I didn't get anything that made sense...this is something new to me. Thanks in advance.

Niky Williams
NTS Marketing
 
Niky,

> I searched MSDN, but I didn't get anything that made sense...this is something new to me.

Well, I don't know where you can find more information than on MSDN.

The drawback to sending data using the message API's is that you lose the 'type' safety of C++ since you must cast all data to void*. This technique is not considered desirable in the year 2000.

There are however other uses for messaging. The oldest of the messaging API's are:

SendMessage() // blocking
PostMessage() // non-blocking

They both send a 'messageID: unsigned int' to the target HWND along with two other parameters WPARAM, LPARAM. SendMessage waits (blocks) until the target windows handler function has processed the message event before returning control to the previous code stack. PostMessage places the message into the target window's queue and returns control to the calling code stack.

In both cases the target window must watch for the message by it's 'messageID' and then perform the desired processing upon reciept.

There are more recent additions to the API like SendNotifyMessage(). There are many more messaging API's that handle reading the queue, crossing thread boundaries, etc.

Hope this helps
-pete

 
I found a book the other day in the mall and was reading up on some stuff and found out that Windows messages was only for passing data between two SEPERATE applications? Is this true? If so, then I'm lookin in the wrong spot. I guess what I need is a variable that I can access using the global namespace. My two dialogs are in the same program...one is a parent the other is the child. Maybe if I could define a variable in one of my classes, I could access it another class? Could I do that by defining my variable as public, then could I access that variable through the other class? You guys have been really helpful and I GREATLY appreciate the time you take out of your day to help folks like us! Many thanks!

Niky Williams
NTS Marketing
 
> Windows messages was only for passing data between two SEPERATE applications? Is this true?

NO, No, no... absolutly NOT. The message queue has been and continues to be the UI event passing mechanism within a single application.

> I guess what I need is a variable that I can access using the global namespace.

YIKES!!! Global data!?

The bottom line is that any of those methods will work to get your application running. So the real question is, what is your priority?

If it is as in most cases, getting the application running, then you should use the technique that you are most comfortable with.

If however your priority is to learn and implement modern programming techniques then you may want to attempt a more advanced Object Oriented technique until you can understand it.

As you learn and understand more advanced techniques later you can implement them when and where you wish.

Good luck
-pete
 
Well, the problem I was having with using messages was that when I tried to pass a message from the parent to the child, the program would freeze/endless loop, the reason was because DoModal() had not been called so in essence there was not a dialog to pass that message to. Then I tried passing a message from the child back to the parent after I had called DoModal(). This failed because I got an assertion error when it tried to access the parent window because ::IsWindow() failed (I think that was the function). So then I tried to make the parent window the active window BEFORE I sent it a message, but it still failed. ACK this is driving me crazy! Back to MSDN I guess. Thanks again for the input. I'll look somemore into window messages now that I know it's not just for seperate apps. I'm attempting to learn the 'better' way to program...but, like you said, you can only do with what you know at the time and improve it as you go. Thanks again for the info guys. Any and all help is so much appreciated.

Niky Williams
NTS Marketing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top