Quick question about DXGetErrorString/Description and Win32.

Started by
7 comments, last by Evil Steve 15 years, 3 months ago
Hello, I am proficient with DirectX but am somewhat new to Win32. I know that the functions DXGetErrorString and DXGetErrorDescription work with Win32 but I would like to have separate Win32 error handling support not reliant on DirectX. Question) Are there Win32 versions of DirectX functions DXGetErrorString and DXGetErrorDescription? If not, how do I handle get error information in Win32 when not using DirectX? Thank you for your help. Jeremy
Advertisement
Do some research on GetLastError mate :), I recommend you search something like "Win32 Error Handling".
FormatMessage. From my code:
std::wstring Util::Win32ErrorToString(DWORD dwErrorCode){	wchar_t* szMsgBuffer;	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |		FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, 0, (LPTSTR)&szMsgBuffer, 0, NULL);	std::wstring strRet(szMsgBuffer);	LocalFree(szMsgBuffer);	return strRet;}
Thank you both for your help.

Evil Steve -
I got the sample function to work but only if I changed it from wstring to string. Otherwise it was just outputing garbage. I have never used a wstring before so I'll do some research into it. If you have any additional information I would love to hear it.
Just an idea, but you might want to pass the output error string by reference so you can dodge the copy constructor call on return.

So ... I guess the sample function works as far as a Win32 version of DXGetErrorDescription, but is there a function that converts the error code to its equivalent text value i.e. "ERROR_RESOURCE_LANG_NOT_FOUND" like DXGetErrorString does?

Thank you both for your help.
Jeremy
std::wstring is like std::string, except it uses wchar_t (16-bit) instead of char (8-bit). The Windows API will use strings of wchar_t to represent Unicode strings, and in general you'll want to use Unicode since these days there's almost no reason not to.

If you want to read up a bit on how Unicode works in the Windows API, you can read this blog entry.
Quote:Original post by MJP
std::wstring is like std::string, except it uses wchar_t (16-bit) instead of char (8-bit).

To be pedantic, wchar_t isn't always 16 bits. It is on Windows, but on most other platforms, wchar_t is a 32 bit type.
Thank you both -

MJP -
That was a fantastic explanation, I have been needing that information for quite a while, so thank you.

All -
If anyone knows of/has a way to implement a Win32 version of DXGetErrorString I would greatly appreciate it. The DXGetErrorDescription seems to be covered in the sample code above, but DXGetErrorString is not.

Thank you for your help.
Jeremy
Sorry, I don't of any way to get the actual name of a Win32 error code.
I'm also not aware of any way of getting the error code. However, is it really that useful? If it's to log the error for reporting, just log the error number.

Personally, I don't think I've ever used DXGetErrorString, I always use DXGetErrorDescription.

This topic is closed to new replies.

Advertisement