Solved

Passing Pointer to Pointer in SendMessage

Posted on 2007-11-27
10
915 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
no14 challenge 14 57
wait notify demo infinite loop 3 81
C++ question 3 46
computer science syllabus 3 52
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…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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