Converting Hanlde to string  
Author Message
Dato0011





PostPosted: Visual C++ General, Converting Hanlde to string Top

hello, i'm doing some key hooks and I need to convert the HWND or Handle types to string in C. is this possible as I know this types are simple integers, I'm converting it without any problem with delphi, but I'm too week in C :(

any help would be appreciated :)




Visual C++8  
 
 
Simple Samples





PostPosted: Visual C++ General, Converting Hanlde to string Top

Is the following the type of thing you are looking for Note that you don't need to use a function such as in the following, but at least this should be complete. Also, the following will convert to decimal, but for a handle, you might want to get hexadecimal. Look at the documentation for how to do that.

errno_t DWordToString(const unsigned long FromValue, std::string &ToString) {
	char Buffer[12];
	errno_t e;
e = _ultoa_s(FromValue, Buffer, sizeof Buffer, 10);
if (e)
	ToString.erase();
else
	ToString = Buffer;
return e;
}


 
 
einaros





PostPosted: Visual C++ General, Converting Hanlde to string Top

Would that be C or C++ In the former case, use snprintf, such as

char buf[9] = {0};
_snprintf(buf, sizeof(buf), "%p", someHandle);

Print the buffer to screen by use of printf.

For C++, you can use the stream classes, such as

std::ostringstream str;
str << std::uppercase << std::setw(8) << std::setfill('0') << std::hex << (unsigned long)someHandle;

Print the buffer to screen by use of std::cout << str.str().