0

Hello..

How do I get smart pointer from pointer address?
For instance:

class someclass {
public:
    char test;
};

typedef boost::shared_ptr<someclass> someclass_p;    //smart pointer

int main() {
    someclass_p testp = someclass_p( new someclass() );
    ULONG_PTR uptr = (ULONG_PTR)testp.get();
    //so I have pointer address here
    //How do I get testp from uptr?
    
    return 0;
}

The reason I want to do that is because I call windows API with pointer address, and it will return it for action.

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
1

How do I get smart pointer from pointer address?

You can't. Smart pointers are smart, but regular pointers are very dumb. They don't know which objects they belong to, or even if they belong to an object at all. The only way to figure it out is by coming up with some way to index the two together.

The reason I want to do that is because I call windows API with pointer address, and it will return it for action.

shared_ptr::get() gives you a dumb pointer that points to the memory paired with the smart pointer. The memory is the same, so you can pass that dumb pointer to the API function and everything will just work. At least, it should be if I'm not wrong. :D It's hard to say without an example of the API call from your code.

Votes + Comments
Nice ~~ SpS
0

None of the win32 api functions I'v used take smart pointers as a parameter or pass them back. Why? because they can all be called from other languages which know nothing at all about c++ smart pointers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.