/*-------------------------------------------------------
NETTIME.C -- Sets System Clock from Internet Services
(c) Charles Petzold, 1998
-------------------------------------------------------*/
#include <windows.h>
#include "resource.h"
#define WM_SOCKET_NOTIFY (WM_USER + 1)
#define ID_TIMER 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK MainDlg (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK ServerDlg (HWND, UINT, WPARAM, LPARAM) ;
void ChangeSystemTime (HWND hwndEdit, ULONG ulTime) ;
void FormatUpdatedTime (HWND hwndEdit, SYSTEMTIME * pstOld,
SYSTEMTIME * pstNew) ;
void EditPrintf (HWND hwndEdit, TCHAR * szFormat, ...) ;
HINSTANCE hInst ;
HWND hwndModeless ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("NetTime") ;
HWND hwnd ;
MSG msg ;
RECT rect ;
WNDCLASS wndclass ;
hInst = hInstance ;
wndclass.style = 0 ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = NULL ;
wndclass.hbrBackground = NULL ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Set System Clock from Internet"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_BORDER | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
// Create the modeless dialog box to go on top of the window
hwndModeless = CreateDialog (hInstance, szAppName, hwnd, MainDlg) ;
// Size the main parent window to the size of the dialog box.
// Show both windows.
GetWindowRect (hwndModeless, &rect) ;
AdjustWindowRect (&rect, WS_CAPTION | WS_BORDER, FALSE) ;
SetWindowPos (hwnd, NULL, 0, 0, rect.right - rect.left,
rect.bottom - rect.top, SWP_NOMOVE) ;
ShowWindow (hwndModeless, SW_SHOW) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
// Normal message loop when a modeless dialog box is used.
while (GetMessage (&msg, NULL, 0, 0))
{
if (hwndModeless == 0 || !IsDialogMessage (hwndModeless, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SETFOCUS:
SetFocus (hwndModeless) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
BOOL CALLBACK MainDlg (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static char szIPAddr[32] = { "132.163.135.130" } ;
static HWND hwndButton, hwndEdit ;
static SOCKET sock ;
static struct sockaddr_in sa ;
static TCHAR szOKLabel[32] ;
int iError, iSize ;
unsigned long ulTime ;
WORD wEvent, wError ;
WSADATA WSAData ;
switch (message)
{
case WM_INITDIALOG:
hwndButton = GetDlgItem (hwnd, IDOK) ;
hwndEdit = GetDlgItem (hwnd, IDC_TEXTOUT) ;
return TRUE ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDC_SERVER:
DialogBoxParam (hInst, TEXT ("Servers"), hwnd, ServerDlg,
(LPARAM) szIPAddr) ;
return TRUE ;
case IDOK:
// Call "WSAStartup" and display description text
if (iError = WSAStartup (MAKEWORD(2,0), &WSAData))
{
EditPrintf (hwndEdit, TEXT ("Startup error #%i.\r\n"),
iError) ;
return TRUE ;
}
EditPrintf (hwndEdit, TEXT ("Started up %hs\r\n"),
WSAData.szDescription);
// Call "socket"
sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
if (sock == INVALID_SOCKET)
{
EditPrintf (hwndEdit,
TEXT ("Socket creation error #%i.\r\n"),
WSAGetLastError ()) ;
WSACleanup () ;
return TRUE ;
}
EditPrintf (hwndEdit, TEXT ("Socket %i created.\r\n"), sock) ;
// Call "WSAAsyncSelect"
if (SOCKET_ERROR == WSAAsyncSelect (sock, hwnd, WM_SOCKET_NOTIFY,
FD_CONNECT | FD_READ))
{
EditPrintf (hwndEdit,
TEXT ("WSAAsyncSelect error #%i.\r\n"),
WSAGetLastError ()) ;
closesocket (sock) ;
WSACleanup () ;
return TRUE ;
}
// Call "connect" with IP address and time-server port
sa.sin_family = AF_INET ;
sa.sin_port = htons (IPPORT_TIMESERVER) ;
sa.sin_addr.S_un.S_addr = inet_addr (szIPAddr) ;
connect(sock, (SOCKADDR *) &sa, sizeof (sa)) ;
// "connect" will return SOCKET_ERROR because even if it
// succeeds, it will require blocking. The following only
// reports unexpected errors.
if (WSAEWOULDBLOCK != (iError = WSAGetLastError ()))
{
EditPrintf (hwndEdit, TEXT ("Connect error #%i.\r\n"),
iError) ;
closesocket (sock) ;
WSACleanup () ;
return TRUE ;
}
EditPrintf (hwndEdit, TEXT ("Connecting to %hs..."), szIPAddr) ;
// The result of the "connect" call will be reported
// through the WM_SOCKET_NOTIFY message.
// Set timer and change the button to "Cancel"
SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
GetWindowText (hwndButton, szOKLabel, sizeof (szOKLabel) /
sizeof (TCHAR)) ;
SetWindowText (hwndButton, TEXT ("Cancel")) ;
SetWindowLong (hwndButton, GWL_ID, IDCANCEL) ;
return TRUE ;
case IDCANCEL:
closesocket (sock) ;
sock = 0 ;
WSACleanup () ;
SetWindowText (hwndButton, szOKLabel) ;
SetWindowLong (hwndButton, GWL_ID, IDOK) ;
KillTimer (hwnd, ID_TIMER) ;
EditPrintf (hwndEdit, TEXT ("\r\nSocket closed.\r\n")) ;
return TRUE ;
case IDC_CLOSE:
if (sock)
SendMessage (hwnd, WM_COMMAND, IDCANCEL, 0) ;
DestroyWindow (GetParent (hwnd)) ;
return TRUE ;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Windows程序设计 NetTime(源代码).rar
共9个文件
exe:2个
rc:1个
mak:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 68 浏览量
2023-02-17
13:51:54
上传
评论
收藏 60KB RAR 举报
温馨提示
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,本人不对所涉及的版权问题或内容负法律责任。如有侵权,请举报或通知本人删除。
资源推荐
资源详情
资源评论
收起资源包目录
Windows程序设计 NetTime(源代码).rar (9个子文件)
NetTime
NetTime.dsw 537B
NetTime.dsp 4KB
NetTime.c 14KB
resource.h 1008B
NetTime.mak 4KB
Release
NetTime.exe 32KB
NetTime.rc 4KB
Debug
NetTime.exe 160KB
NetTime.dep 83B
共 9 条
- 1
资源评论
金枝玉叶9
- 粉丝: 138
- 资源: 7640
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 优化BP神经网络(GWO-BP)灰狼算法
- 基于vue+nodejs毕业设计-在线购物商城系统(源码+数据库+文档说明)
- python+django+博客+学习
- (灰狼算法)优化BP神经网络(GWO-BP)
- 000001~000030股票数据(上市至2017年7月5日)
- 人种黄种人白人黑人分类数据集970张4类别.7z
- 基于SpringBoot+Vue+MySql的网上图书商城(包括源码、数据库)高分毕设
- 车载显示引领座舱智能化,行业提质扩容在即
- 基于springboot+vue的网上书店系统源码+数据库+文档说明(高分毕设)
- 数据仓库与数据挖掘实验报告(python实现,包括关联规则、决策树、聚类分析、贝叶斯网络、神经网络和遗传算法的应用)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功