1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#define NOMINMAX
#include <Windows.h>
#include <WinInet.h>
#include <string>
#include "resource.h"
#pragma comment(lib, "wininet.lib")
HINSTANCE g_inst;
char* DownloadBytes(LPCWSTR szUrl) {
HINTERNET hOpen = NULL;
HINTERNET hFile = NULL;
HANDLE hOut = NULL;
char* data = NULL;
DWORD dataSize = 0;
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
hOpen = InternetOpenW(L"MyAgent", NULL, NULL, NULL, NULL);
if(!hOpen) return NULL;
hFile = InternetOpenUrlW(hOpen, szUrl, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, NULL);
if(!hFile) {
InternetCloseHandle(hOpen);
return NULL;
}
do {
char buffer[2000];
InternetReadFile(hFile, (LPVOID)buffer, _countof(buffer), &dwBytesRead);
char *tempData = new char[dataSize + dwBytesRead];
memcpy(tempData, data, dataSize);
memcpy(tempData + dataSize, buffer, dwBytesRead);
delete[] data;
data = tempData;
dataSize += dwBytesRead;
} while (dwBytesRead);
InternetCloseHandle(hFile);
InternetCloseHandle(hOpen);
return data;
}
#define SETRESULT(x) result = (LRESULT)(x); resultSet = true
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
bool handled = true;
LRESULT result = 0;
bool resultSet = false;
switch (msg)
{
case WM_CLOSE:
EndDialog(hDlg, 0);
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
{
HWND txt = GetDlgItem(hDlg, IDC_URL);
int size = GetWindowTextLengthW(txt);
LPWSTR url = new WCHAR[++size];
GetWindowTextW(txt, url, size);
char *data = DownloadBytes(url);
SetDlgItemTextA(hDlg, IDC_DATA, data);
delete[] data;
}
break;
case IDCANCEL:
SendMessage(hDlg, WM_CLOSE, 0, 0);
break;
}
}
default:
handled = false;
}
if (resultSet)
{
SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, result);
}
return handled;
}
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR szCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInst);
g_inst = hInst;
DialogBoxW(hInst, MAKEINTRESOURCE(IDD_DOWNLOAD), NULL, &DlgProc);
}
|