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.

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.

commented: Nice ~~ SpS +3

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.