I have a ULONG value that contains the address.
The address is basically of string(array of wchar_t terminated by NUL开发者_运维百科L character)
I want to retrieve that string.
what is the best way to do that?
@KennyTM's answer is right on the money if by "basically of a string" you mean it's a pointer to an instance of the std::string
class. If you mean it's a pointer to a C string, which I suspect may be more likely, you need:
char *s = reinterpret_cast<char *>(your_ulong);
Or, in your case:
whcar_t *s = reinterpret_cast<wchar_t *>(your_ulong);
Note also that you can't safely store pointers in any old integral type. I can make a compiler with a 32-bit long
type and a 64-bit pointer type. If your compiler supports it, a proper way to store pointers in integers is to use the stdint.h
types intptr_t
and uintptr_t
, which are guaranteed (by the C99 standard) to be big enough to store pointer types.
However, C99 isn't part of C++, and many C++ compilers (read: Microsoft) may not provide this kind of functionality (because who needs to write portable code?). Fortunately, stdint.h
is useful enough that workarounds exist, and portable (and free) implementations of stdint.h
for compatability with older compilers can be found easily on the internet.
string& s = *reinterpret_cast<string*>(your_ulong);
精彩评论