0

I've got a HANDLE declared like this:

HANDLE handle = new string("test");

How can I get the value from handle?

Something like this:

string myval = (string)handle; //Cast doesn't work
ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • 1
    What type is `HANDLE`? – Petr Jul 27 '15 at 16:00
  • `string myval = *handle;` ??? – R Sahu Jul 27 '15 at 16:00
  • Do you mean [this kind of `HANDLE`](http://stackoverflow.com/a/902987/1858225)? – Kyle Strand Jul 27 '15 at 16:01
  • `HANDLE` could be absolutely everything, so your question is not answerable without wild guessing. – Christian Hackl Jul 27 '15 at 16:11
  • First off, what are you *really* trying to do? What you have posted almost certainly won't do it, but if you let us in on your intent we might be able to suggest an alternative. The immediate problem: Assuming HANDLE is an anonymous pointer that doesn't care what you point it at, (void *), `string myval = (string)handle;` didn't work because you have a pointer to string and that the cast will attempt to brute force the pointer into a string. You would need to cast handle to a pointer to string, and then get the value at the pointer, and that brings you to @Bathsheba's answer. – user4581301 Jul 27 '15 at 16:46
  • All this comes because the use of `SetProp(HWND hwnd, LPCSTR prop, HANDLE val);` I want to set a string value as a property of the `HWND` and then I need to retrieve that value with `GetProp`. – ProtectedVoid Jul 28 '15 at 07:22

1 Answers1

2

If HANDLE is either a void* or a string* then you can use

string myval = *(string*)handle;

or the clearer

string myval = *reinterpret_cast<string*>(handle);

If HANDLE is any other type then it's likely that the behaviour of your program is undefined.

Note that a value copy of your string will be taken.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483