-
May 4th, 2010, 12:22 AM
#1
Convert HRESULT hex error code to string
Hi all,
Is there a way to convert an HRESULT hex error code to a string? To better illustrate this, here is a sample code:
HRESULT hr = CoInitialize(NULL);
if(FAILED(hr))
{
//This is what I want to store into a string.
//"Failed to Initialize COM. Error code = 0x" << hex << hr << endl;
string hexerrorcode = ?;
CoUninitialize();
return 0;
}
I am not sure how to put it into string.
Can anyone help me with this?
Thank you!
-
May 4th, 2010, 08:09 AM
#2
Re: Convert HRESULT hex error code to string
Something like this (works for most windows error codes):
Code:
void ErrorMessage(DWORD dwError)
{
LPVOID pText = 0;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPWSTR)&pText,0,NULL);
MessageBox(NULL,(LPCWSTR)pText,0,MB_OK);
LocalFree(pText);
}
-
May 4th, 2010, 08:24 AM
#3
Re: Convert HRESULT hex error code to string
Code:
#include <string>
#include <sstream>
std::ostringstream oss;
oss << "Failed to Initialize COM. Error code = 0x" << std::hex << hr << std::endl;
std::string hexerrorcode = oss.str();
-
May 4th, 2010, 08:29 AM
#4
Re: Convert HRESULT hex error code to string
Sorry, I misread the question.
-
May 5th, 2010, 10:18 AM
#5
Re: Convert HRESULT hex error code to string
With printf-family of functions you can simply use %x or %X format specifier!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|