Solved

Passing Pointer to Pointer in SendMessage

Posted on 2007-11-27
10
923 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 500 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

822 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