?
Solved

Passing Pointer to Pointer in SendMessage

Posted on 2007-11-27
10
Medium Priority
?
939 Views
Last Modified: 2013-11-20
Hello all,

I'm attempting to pass the address of a pointer as the LPARAM argument in SendMessage.
I need to have this argument return the (this) pointer of the object receiving the message.
I have the following (doesn't work)

class CClassA
{
     CCommon *pCommon;
}

CClassA::GetObjectPointer()
{
     SendMessage( m_hDlgToSendTo, UM_XXX, xxx, (LPARAM) &pCommon);
}

In the dialog DLL I have:

LRESULT CClassBDlg::OnWorkspaceModelMessage(WPARAM wParam, LPARAM lParam)
{
     . . .
     CCommon * pCommon = *((CCommon **) lParam);
     . . .
     pInterface = (CCommon *) this;
     lParam = (LPARAM) pInterface;
      . . .
}

What is the correct way to do this?

Thank you!
trishm
0
Comment
Question by:trishm
  • 6
  • 3
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1500 total points
ID: 20359467
You are passing the address of the pointer. Just use

class CClassA
{
     CCommon *pCommon;
}

CClassA::GetObjectPointer()
{
     SendMessage( m_hDlgToSendTo, UM_XXX, xxx, (LPARAM) pCommon);
}

LRESULT CClassBDlg::OnWorkspaceModelMessage(WPARAM wParam, LPARAM lParam)
{
     . . .
     CCommon * pCommon = ((CCommon **) lParam;
     . . .
     pInterface = (CCommon *) this;
     lParam = (LPARAM) pInterface;
      . . .
}

which should work fine (been there, done that).
0
 

Author Comment

by:trishm
ID: 20359618
jkr:

Sorry, that didn't work. After the call to SendMessage
     pInterface = 0xcdcdcdcd

When I pass:
     pCommon
instead of
     &pCommon
to SendMessage

the receiving function gets a bad lParam value:
     lParam = -89830280

0
 
LVL 86

Expert Comment

by:jkr
ID: 20359664
Sorry, that should be

   CCommon * pCommon = (CCommon *) lParam;

Is 'pCommon' initialized correctly when you call 'SendMessage()'?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:peetm
ID: 20359792
The thing you're sending this to is in the same process right?
0
 

Author Comment

by:trishm
ID: 20359869
jkr:

I initially caught the typo and changed it to your corrected version before I responded.
I did, however, add initialization of pCommon to NULL before SendMessage.

It didn't help. The only different is that the receiving function now receives
     lParam = 0 instead of lParam = -838473984
So, now
     CCommon * pCommon = (CCommon *) lParam;
assigns 0 to pCommon

After SendMessage
     pCommon = 0
I need a valid pointer to call a function in pCommon
     pCommon->DoSomething()





0
 

Author Comment

by:trishm
ID: 20359895
peetm:

No, they are in different DLLs.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20359900
Different DLLs, but same process?
0
 

Author Comment

by:trishm
ID: 20359985
Yes, same process.

In a previous attempt, I  successfully retrieved the pointer I need with:
     pCommon = (CCommon *) SendMessage(m_hDlgToSendTo, xxxx, xxxx, NULL);
and returning the object pointer
     return (LRESULT) this

This gave me what I needed but I was instructed to pass a pointer to the pointer instead:
     SendMessage( ... ... ..., (LPARAM) &pCommon)

0
 

Author Comment

by:trishm
ID: 20360116
The objective is to get the pointer to "this" via an argument to SendMessage()
0
 

Author Comment

by:trishm
ID: 20367091
The following code gave me what I needed:

SendMessage(m_hDlgToSendTo, xxx, xxx, (LPARAM) &m_pCommon);

In the receiving dialog:

CCommon ** ppCommon = (CCommon **) lParam;

*ppCommon = this;
lParam = (LPARAM) ppCommon;

This returns a pointer to "this" in the LPARAM argument.

Thank you for your comments.
trishm


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

864 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