Created
November 14, 2013 03:45
-
-
Save Aaronontheweb/7461004 to your computer and use it in GitHub Desktop.
How to format the output of Win32's GetLastError() method into a string using FormatString
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DWORD dLastError = GetLastError(); | |
LPCTSTR strErrorMessage = NULL; | |
FormatMessage( | |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER, | |
NULL, | |
dLastError, | |
0, | |
(LPWSTR) &strErrorMessage, | |
0, | |
NULL); | |
//Prints debug output to the console | |
OutputDebugString(strErrorMessage); |
You will have a memory leak
@aianau at which part?
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessage
FORMAT_MESSAGE_ALLOCATE_BUFFER
The function allocates a buffer large enough to hold the formatted message, and places a pointer to the allocated buffer at the address specified by lpBuffer.
[...]
The caller should use the LocalFree function to free the buffer when it is no longer needed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for consistency, change casting from
LPWSTR
toLPTSTR
at line 9