GetDlgItem(IDC_BTN_STARTSERVICE)->EnableWindow(TRUE);
GetDlgItem(IDC_BTN_STOPSERVICE)->EnableWindow(FALSE);
stopService();
}
void CUpdateIpDlg::OnTimer(UINT_PTR nIDEvent ){
startUpdateInstance();
}
void CUpdateIpDlg::OnDestroy(){
Shell_NotifyIcon(NIM_DELETE, &NotifyIcon);
return CDialog::OnDestroy();
}
BOOL CUpdateIpDlg::OnEraseBkgnd(CDC* pDC){
static bool bFirst=true;
if(bFirst)
{
bFirst=false;
ShowWindow(SW_HIDE);
return TRUE;
}
return CDialog::OnEraseBkgnd(pDC);
}
int CUpdateIpDlg::OnCreate(LPCREATESTRUCT lpCreateStruct){
addNotification();
return CDialog::OnCreate(lpCreateStruct);
}
void CUpdateIpDlg::startService(int interval){
m_nTimer = SetTimer(TIMER_UPDATE,interval * 1000,0);
m_bIsRunning = TRUE;
CString strResult;
GetDlgItemText(IDC_EDIT_RESULT,strResult);
strResult += _T("startService:\r\n");
SetDlgItemText(IDC_EDIT_RESULT,strResult);
}
void CUpdateIpDlg::stopService(){
KillTimer(m_nTimer);
stopUpdateInstance();
m_bIsRunning = FALSE;
CString strResult;
GetDlgItemText(IDC_EDIT_RESULT,strResult);
strResult += _T("stopService:\r\n");
SetDlgItemText(IDC_EDIT_RESULT,strResult);
}
int count=0;
void OnBegin( const happyhttp::Response* r, void* userdata )
{
printf( "BEGIN (%d %s)\n", r->getstatus(), r->getreason() );
count = 0;
}
void OnData( const happyhttp::Response* r, void* userdata, const unsigned char* data, int n )
{
fwrite( data,1,n, stdout );
count += n;
}
void OnComplete( const happyhttp::Response* r, void* userdata )
{
printf( "COMPLETE (%d bytes)\n", count );
}
void CUpdateIpDlg::startUpdateInstance(){
//启动新的线程去发起Http请求
stopUpdateInstance();
CString strResult;
GetDlgItemText(IDC_EDIT_RESULT,strResult);
strResult += _T("启动更新请求:\r\n");
SetDlgItemText(IDC_EDIT_RESULT,strResult);
try
{
char uid[] = "0000";
char szTemp[1024] = {0};
_snprintf_s(szTemp,sizeof(szTemp),sizeof(szTemp),"/updateip.php m=update&uid=%s",uid);
happyhttp::Connection conn(c_szRequestHost, 80 );
conn.setcallbacks( OnBegin, OnData, OnComplete, 0 );
conn.request( "GET", szTemp, 0, 0,0 );
while( conn.outstanding() )
conn.pump();
/*
TCHAR uid[] = _T("0000");
TCHAR szTemp[1024] = {0};
_sntprintf_s(szTemp,sizeof(szTemp),sizeof(szTemp),_T("/updateip.php m=update&uid=%s"),uid);
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"A WinHTTP Example Program/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect( hSession, c_szRequestHost,
INTERNET_DEFAULT_HTTP_PORT, 0);
// Create an HTTP Request handle.
if (hConnect)
hRequest = WinHttpOpenRequest( hConnect, L"PUT", szTemp,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
// Send a Request.
if (hRequest)
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0);
// Place additional code here.
// Report errors.
if (!bResults{
CString strResult;
GetDlgItemText(IDC_EDIT_RESULT,strResult);
strResult += _T("请求发送失败,URL = ");
strResult += c_szRequestHost;
strR