Fix displaying Win32 errors (the Reg* APIs don't set the last error code!)

svn path=/trunk/; revision=24375
This commit is contained in:
Thomas Bluemel 2006-10-03 17:02:36 +00:00
parent 7b5fcdd9fa
commit fd990e9ada
4 changed files with 40 additions and 18 deletions

View file

@ -116,7 +116,7 @@ SetNTPServer(HWND hwnd)
&hKey); &hKey);
if (Ret != ERROR_SUCCESS) if (Ret != ERROR_SUCCESS)
{ {
GetError(); DisplayWin32Error(Ret);
return; return;
} }
@ -127,7 +127,7 @@ SetNTPServer(HWND hwnd)
(LPBYTE)szSel, (LPBYTE)szSel,
sizeof(szSel)); sizeof(szSel));
if (Ret != ERROR_SUCCESS) if (Ret != ERROR_SUCCESS)
GetError(); DisplayWin32Error(Ret);
RegCloseKey(hKey); RegCloseKey(hKey);
@ -180,7 +180,10 @@ GetNTPServerAddress(LPSTR* lpAddress)
0, 0,
dwSize); dwSize);
if (buf == NULL) if (buf == NULL)
{
Ret = ERROR_NOT_ENOUGH_MEMORY;
goto fail; goto fail;
}
Ret = RegQueryValueExW(hKey, Ret = RegQueryValueExW(hKey,
szSel, szSel,
@ -212,6 +215,7 @@ GetNTPServerAddress(LPSTR* lpAddress)
NULL, NULL,
NULL)) NULL))
{ {
Ret = GetLastError();
goto fail; goto fail;
} }
@ -223,7 +227,7 @@ GetNTPServerAddress(LPSTR* lpAddress)
return TRUE; return TRUE;
fail: fail:
GetError(); DisplayWin32Error(Ret);
if (hKey) RegCloseKey(hKey); if (hKey) RegCloseKey(hKey);
HeapFree(GetProcessHeap(), 0, buf); HeapFree(GetProcessHeap(), 0, buf);
HeapFree(GetProcessHeap(), 0, *lpAddress); HeapFree(GetProcessHeap(), 0, *lpAddress);
@ -242,7 +246,7 @@ GetTimeFromServer(VOID)
if (! GetNTPServerAddress(&lpAddress)) if (! GetNTPServerAddress(&lpAddress))
return 0; return 0;
if (InitialiseConnection(lpAddress)) if (InitializeConnection(lpAddress))
{ {
if (SendData()) if (SendData())
{ {
@ -283,7 +287,7 @@ UpdateSystemTime(ULONG ulTime)
/* convert to a file time */ /* convert to a file time */
if (! SystemTimeToFileTime(&stNew, &ftNew)) if (! SystemTimeToFileTime(&stNew, &ftNew))
{ {
GetError(); DisplayWin32Error(GetLastError());
return; return;
} }
@ -295,7 +299,7 @@ UpdateSystemTime(ULONG ulTime)
/* convert back to a system time */ /* convert back to a system time */
if (! FileTimeToSystemTime(&ftNew, &stNew)) if (! FileTimeToSystemTime(&ftNew, &stNew))
{ {
GetError(); DisplayWin32Error(GetLastError());
return; return;
} }
@ -304,7 +308,7 @@ UpdateSystemTime(ULONG ulTime)
I thought SetSystemTime already dealt I thought SetSystemTime already dealt
with this */ with this */
if (! SetSystemTime(&stNew)) if (! SetSystemTime(&stNew))
GetError(); DisplayWin32Error(GetLastError());
} }
@ -342,7 +346,7 @@ GetSyncSetting(HWND hwnd)
&hKey); &hKey);
if (Ret != ERROR_SUCCESS) if (Ret != ERROR_SUCCESS)
{ {
GetError(); DisplayWin32Error(Ret);
return; return;
} }
@ -355,7 +359,7 @@ GetSyncSetting(HWND hwnd)
&Size); &Size);
if (Ret != ERROR_SUCCESS) if (Ret != ERROR_SUCCESS)
{ {
GetError(); DisplayWin32Error(Ret);
} }
if (lstrcmp(Data, L"NTP") == 0) if (lstrcmp(Data, L"NTP") == 0)
@ -369,7 +373,7 @@ GetSyncSetting(HWND hwnd)
static VOID static VOID
InitialiseDialog(HWND hwnd) InitializeDialog(HWND hwnd)
{ {
GetSyncSetting(hwnd); GetSyncSetting(hwnd);
@ -390,7 +394,7 @@ InetTimePageProc(HWND hwndDlg,
{ {
case WM_INITDIALOG: case WM_INITDIALOG:
{ {
InitialiseDialog(hwndDlg); InitializeDialog(hwndDlg);
} }
break; break;

View file

@ -15,7 +15,7 @@ SOCKET Sock;
SOCKADDR_IN myAddr, ntpAddr; SOCKADDR_IN myAddr, ntpAddr;
BOOL BOOL
InitialiseConnection(LPSTR lpAddress) InitializeConnection(LPSTR lpAddress)
{ {
WSADATA wsaData; WSADATA wsaData;
HOSTENT *he; HOSTENT *he;

View file

@ -22,22 +22,35 @@ APPLET Applets[NUM_APPLETS] =
{IDC_CPLICON, IDS_CPLNAME, IDS_CPLDESCRIPTION, Applet} {IDC_CPLICON, IDS_CPLNAME, IDS_CPLDESCRIPTION, Applet}
}; };
#if DBG
VOID GetError(VOID) VOID DisplayWin32ErrorDbg(DWORD dwErrorCode, const char *file, int line)
#else
VOID DisplayWin32Error(DWORD dwErrorCode)
#endif
{ {
LPVOID lpMsgBuf; LPVOID lpMsgBuf;
#if DBG
TCHAR szMsg[255];
#endif
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, NULL,
GetLastError(), dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, (LPTSTR) &lpMsgBuf,
0, 0,
NULL ); NULL );
MessageBox(NULL, lpMsgBuf, _T("Error!"), MB_OK | MB_ICONERROR); #if DBG
if (_stprintf(szMsg, _T("%hs:%d: %s"), file, line, lpMsgBuf))
{
MessageBox(NULL, szMsg, NULL, MB_OK | MB_ICONERROR);
}
#else
MessageBox(NULL, lpMsgBuf, NULL, MB_OK | MB_ICONERROR);
#endif
LocalFree(lpMsgBuf); LocalFree(lpMsgBuf);
} }

View file

@ -42,7 +42,12 @@ INT_PTR CALLBACK InetTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM
/* timedate.c */ /* timedate.c */
VOID GetError(VOID); #if DBG
VOID DisplayWin32ErrorDbg(DWORD dwErrorCode, const char *file, int line);
#define DisplayWin32Error(e) DisplayWin32ErrorDbg(e, __FILE__, __LINE__);
#else
VOID DisplayWin32Error(DWORD dwErrorCode);
#endif
/* clock.c */ /* clock.c */
@ -51,7 +56,7 @@ VOID UnregisterClockControl(VOID);
/* ntpclient.c */ /* ntpclient.c */
BOOL InitialiseConnection(CHAR *szIpAddr); BOOL InitializeConnection(CHAR *szIpAddr);
VOID DestroyConnection(VOID); VOID DestroyConnection(VOID);
BOOL SendData(VOID); BOOL SendData(VOID);
ULONG RecieveData(VOID); ULONG RecieveData(VOID);