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

View file

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

View file

@ -22,22 +22,35 @@ APPLET Applets[NUM_APPLETS] =
{IDC_CPLICON, IDS_CPLNAME, IDS_CPLDESCRIPTION, Applet}
};
VOID GetError(VOID)
#if DBG
VOID DisplayWin32ErrorDbg(DWORD dwErrorCode, const char *file, int line)
#else
VOID DisplayWin32Error(DWORD dwErrorCode)
#endif
{
LPVOID lpMsgBuf;
#if DBG
TCHAR szMsg[255];
#endif
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
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);
}

View file

@ -42,7 +42,12 @@ INT_PTR CALLBACK InetTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM
/* 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 */
@ -51,7 +56,7 @@ VOID UnregisterClockControl(VOID);
/* ntpclient.c */
BOOL InitialiseConnection(CHAR *szIpAddr);
BOOL InitializeConnection(CHAR *szIpAddr);
VOID DestroyConnection(VOID);
BOOL SendData(VOID);
ULONG RecieveData(VOID);