Article View: borland.public.cppbuilder.nativeapi
Article #1730Custom Error Function Problems
From: poojo hackma
Date: Mon, 25 Jun 2007 15:45
Date: Mon, 25 Jun 2007 15:45
49 lines
2678 bytes
2678 bytes
I've got a Custom Error handling function that I've used for a long time:
In the Header:
void ShowLastError(LPTSTR lpszMsgTitle);
The Code:
const DWORD dwSLEFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM;
const WORD wSLEBuffer = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
void ShowLastError(LPTSTR lpszMsgTitle)
{
LPVOID lpErrorMessage;
FormatMessage(dwSLEFlags, NULL, GetLastError(), wSLEBuffer,
(LPTSTR)&lpErrorMessage, 0, NULL);
MessageBox(NULL, (LPTSTR)lpErrorMessage, lpszMsgTitle, MB_OK |
MB_ICONERROR);
LocalFree(lpErrorMessage);
}
I'd like to change this a little. Sometimes the error is ERROR_SUCCESS or
something else that I don't really want to show the error for, so I'd like
to have the option of passing in the dwError value.
I tried changing my custom code to the following, but now I get ambiguous
call to overloaded function errors:
In the new Header:
void ShowLastError(LPTSTR lpszMsgTitle, DWORD dwErr = GetLastError());
The new Code:
const DWORD dwSLEFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM;
const WORD wSLEBuffer = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
void ShowLastError(LPTSTR lpszMsgTitle, DWORD dwErr)
{
LPVOID lpErrorMessage;
FormatMessage(dwSLEFlags, NULL, dwErr, wSLEBuffer,
(LPTSTR)&lpErrorMessage, 0, NULL);
MessageBox(NULL, (LPTSTR)lpErrorMessage, lpszMsgTitle, MB_OK |
MB_ICONERROR);
LocalFree(lpErrorMessage);
}
If I do not supply a value for dwErr in the new versions, shouldn't the code
attempt to fill this with the GetLastError default?
Why am I getting this error? What is the best way to correct my code?
Technical Details
Message-ID:
<467fe307$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bfb6513b9cc0c13f,start X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "poojo hackma" <poojo.com/mail> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Custom Error Function Problems Date: Mon, 25 Jun 2007 10:45:16 -0500 Lines: 52 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138 X-RFC2646: Format=Flowed; Original X-Antivirus: avast! (VPS 000751-3, 06/22/2007), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 12.191.89.68 Message-ID: <467fe307$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1182786311 12.191.89.68 (25 Jun 2007 08:45:11 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail X-Original-Bytes: 2528