#include <windows.h>
#include <richedit.h>
#include <wininet.h>
BOOL CloseInternetHandle(HINTERNET hInternet);
void DeleteBuffer(char **ppBuffer, unsigned long dwBufferSize);
HINTERNET InitializeInternetFunctions(void);
HINTERNET OpenURL(HINTERNET hInternet, char *szURL);
BOOL ReadURL(HINTERNET hURL, char **ppData, unsigned long *pdwDataSize);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
//Måske nok lidt overkill at lave tre separate funktioner til at kalde
//InternetCloseHandle, InternetOpen og InternetOpenUrl,
//men på denne måde kan fejlhåndteringen (MessageBox ved fejl) fjernes fra den centrale kode
BOOL CloseInternetHandle(HINTERNET hInternet){
BOOL bResult = InternetCloseHandle(hInternet);
if(!bResult)
MessageBox(NULL, \"Error closing Internet handle\", \"Error: CloseInternetHandle\", MB_ICONERROR);
return bResult;
}
HINTERNET InitializeInternetFunctions(void){
HINTERNET hResult = InternetOpen(\"URL fun\", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hResult)
MessageBox(NULL, \"Error initializing Internet functions\", \"Error: InitializeInternetFunctions\", MB_ICONERROR);
return hResult;
}
HINTERNET OpenURL(HINTERNET hInternet, char *szURL){
HINTERNET hResult = InternetOpenUrl(hInternet, szURL, NULL, 0, 0, 0);
if(!hResult)
MessageBox(NULL, \"Error opening URL\", \"Error: OpenURL\", MB_ICONERROR);
return hResult;
}
//Dealloker en buffer, og sæt pointeren til NULL
void DeleteBuffer(char **ppBuffer, unsigned long dwBufferSize){
if(*ppBuffer){
if(dwBufferSize > 1)
delete [] *ppBuffer;
else
delete *ppBuffer;
*ppBuffer = NULL;
}
}
BOOL ReadURL(HINTERNET hURL, char **ppData, unsigned long *pdwDataSize){
BOOL bResult;
unsigned long dwBytesAvailable;
unsigned long dwBytesRead;
unsigned long dwResultSize = 0;
char *pResult = NULL;
char *pTemp;
while((bResult = InternetQueryDataAvailable(hURL, &dwBytesAvailable, 0, 0)) && dwBytesAvailable){
pTemp = pResult;
if(pResult = new char[dwResultSize + dwBytesAvailable]){
memcpy(pResult, pTemp, dwResultSize);
InternetReadFile(hURL, pResult + dwResultSize, dwBytesAvailable, &dwBytesRead);
}
DeleteBuffer(&pTemp, dwResultSize);
dwResultSize += dwBytesAvailable;
}
if(bResult){ //Stoppede while-løkken, fordi alle data er hentet???
*ppData = pResult;
*pdwDataSize = dwResultSize;
}else{ // Eller fordi der er sket en fejl???
MessageBox(NULL, \"Error reading URL\", \"Error: ReadURL\", MB_ICONERROR);
DeleteBuffer(&pResult, dwResultSize);
}
return bResult;
}
//1) Alloker ny buffer med plads til et afsluttende \'\\0\'
//2) Vis teksten i hWnd
//3) Slet den nye buffer
void ShowData(HWND hWnd, char *pData, unsigned long dwDataSize){
char *pTemp;
if(hWnd && pData && dwDataSize && (pTemp = new char[dwDataSize+1])){
memcpy(pTemp, pData, dwDataSize);
pTemp[dwDataSize] = \'\\0\';
SetWindowText(hWnd, pTemp);
DeleteBuffer(&pTemp, dwDataSize + 1);
}
else
MessageBox(NULL, \"Error showing the data\", \"Error: ShowData\", MB_ICONERROR);
}
//Ikke andet end en standard WinMain der opretter et vindue,
//og starter message loopet
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
HWND hWnd;
MSG msg;
TCHAR szClassName[] = \"URLfun\";
WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WindowProc;
WndClass.lpszClassName = szClassName;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&WndClass)){
MessageBox(NULL, \"Error registering the window class\", \"Error: RegisterClass\", MB_ICONERROR);
return 0;
}
hWnd = CreateWindow(szClassName, \"URL fun\", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
static unsigned long dwBufferSize;
static HWND hFetchButton;
static HINSTANCE hInstance;
static HINTERNET hInternet;
static HWND hMainWnd;
static HINSTANCE hRichEditLib;
static HWND hRichEditWnd;
static HINTERNET hURL;
static HWND hURLWnd;
static int iURLLength;
static char *pBuffer;
static RECT rect;
static char *szURL;
switch(uMsg){
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED){
if((iURLLength = GetWindowTextLength(hURLWnd)) && (szURL = new char[iURLLength+1])){
if((GetWindowText(hURLWnd, szURL, iURLLength+1) == iURLLength) && (hURL = OpenURL(hInternet, szURL))){
ReadURL(hURL, &pBuffer, &dwBufferSize);
CloseInternetHandle(hURL);
}
delete [] szURL;
szURL = NULL;
}
if(pBuffer){
ShowData(hRichEditWnd, pBuffer, dwBufferSize);
DeleteBuffer(&pBuffer, dwBufferSize);
}
else
SetWindowText(hRichEditWnd, \"\");
}
return 0;
case WM_CREATE:
dwBufferSize = 0;
hInternet = hURL = pBuffer = NULL;
hMainWnd = hWnd;
GetClientRect(hMainWnd, &rect);
hRichEditLib = LoadLibrary(\"riched20.dll\");
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
hURLWnd = CreateWindow(\"EDIT\", \"Enter a valid URL (
http://www.eksperten.dk) and click the Fetch button\",
WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_LEFT, 0, 0,
rect.right - 60, 20, hMainWnd, NULL, hInstance, NULL);
hFetchButton = CreateWindow(\"BUTTON\", \"Fetch\", WS_CHILD | WS_VISIBLE, rect.right - 60, 0, 60, 20,
hMainWnd, NULL, hInstance, NULL);
hRichEditWnd = CreateWindow(RICHEDIT_CLASS, NULL,
WS_CHILD | WS_HSCROLL | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE,
0, 20, rect.right, rect.bottom - 20, hMainWnd, NULL, hInstance, NULL);
hInternet = InitializeInternetFunctions();
break;
case WM_DESTROY:
if(hInternet) CloseInternetHandle(hInternet);
if(hRichEditWnd) DestroyWindow(hRichEditWnd);
if(hFetchButton) DestroyWindow(hFetchButton);
if(hURLWnd) DestroyWindow(hURLWnd);
FreeLibrary(hRichEditLib);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}