Q: How to get the reason for a failure of a SDK function?
A: First of all, take a look in MSDN and read the documentation of the function. See the meaning of the returned value.
Most Windows SDK functions return a value showing if the function succeeded or failed (in our example 0 if failed and nonzero if succeeded).
Usually, next is written in MSDN: To get extended error information, call 'GetLastError()'.
Also, we can get the error text using 'FormatMessage()':Code:BOOL bRet = CreateDirectory(_T("X:\\MyApp\\Bin\\Shared"), NULL); if(!bRet) { DWORD dwError = GetLastError(); // put a breakpoint here and type the value of dwError in Error Lookup tool }
The following example shows how to use it:Code:void ReportLastError() { LPCTSTR pszCaption = _T("Windows SDK Error Report"); DWORD dwError = GetLastError(); if(NOERROR == dwError) { MessageBox(NULL, _T("No error"), pszCaption, MB_OK); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, dwError, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox(NULL, (LPCSTR)pTextBuffer, pszCaption, MB_OK|MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox(NULL, _T("Unknown error"), pszCaption, MB_OK|MB_ICONERROR); } } }
Code:BOOL bRet = CreateDirectory(_T("X:\\MyApp\\Bin\\Shared"), NULL); if(!bRet) { ReportLastError(); }




Reply With Quote
