Code:
#include <tchar.h>
#include <gwc.h> // Holds cp(int);
void tGetInternetFile(char buffer[], LPCTSTR szURL, size_t cbMaxSize, size_t* lpcbActualSize)
{
HINTERNET hNet = NULL;
HINTERNET hUrlFile = NULL;
DWORD cbBytesRead = 0;
SIZE_T cbBytesTotal = 0;
BOOL bResult = FALSE;
const DWORD cbReadSize = 0x4000;
cp(0);
if (!(hNet = InternetOpen(TEXT("Downloader"), PRE_CONFIG_INTERNET_ACCESS, NULL, NULL, 0)))
{
cp(-1);
goto cleanup;
}
else
cp(1);
if (!(hUrlFile = InternetOpenUrl(hNet, szURL, NULL, 0, INTERNET_FLAG_RESYNCHRONIZE, 0)))
{
cp(-2);
goto cleanup;
}
else
cp(2);
do
{
if (!(buffer = (char*) ReallocOrFree(buffer, cbBytesTotal + cbReadSize)))
{
cp(-3);
goto cleanup;
}
else
cp(3);
if (!InternetReadFile(hUrlFile, buffer + cbBytesTotal, cbReadSize, &cbBytesRead))
{
cp(-4);
goto cleanup;
}
else
cp(4);
cbBytesTotal += cbBytesRead;
/* Max size check and size_t overflow check */
if (cbBytesTotal > cbMaxSize || ((((size_t) -1) - cbReadSize) - 1) < cbBytesTotal)
{
cp(-5);
goto cleanup;
}
else
cp(5);
} while (cbBytesRead > 0);
if (!(buffer = (char*) ReallocOrFree(buffer, cbBytesTotal + 1)))
{
cp(-6);
goto cleanup;
}
else
cp(6);
buffer[cbBytesTotal] = '\0';
bResult = TRUE;
cleanup:
if (hUrlFile) InternetCloseHandle(hUrlFile);
if (hNet) InternetCloseHandle(hNet);
if (!bResult) free(buffer);
if (lpcbActualSize) *lpcbActualSize = (bResult ? cbBytesTotal : 0);
return;
}
void DoIt()
{
size_t sz;
char my_temp_buff[10];
char my_buffer[200];
tGetInternetFile(my_temp_buff,"http://cboard.cprogramming.com/login.php?vb_login_username=Cool-August&cookieuser=1&vb_login_password=sulten&do=login",10,&sz);
tGetInternetFile(my_buffer,"http://cboard.cprogramming.com/usercp.php?",sizeof(my_buffer),&sz);
tGetInternetFile(my_temp_buff,"http://cboard.cprogramming.com/login.php?do=logout&u=14571",10,&sz);
QuickWrite("my_file.txt",my_buffer);
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
/* Make the paint variable available globally */
HDC hdc;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
int winX = 544;
int winY = 375;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows Application", /* Title Text */
WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
GetSystemMetrics(SM_CXSCREEN)/2-winX/2, /* Windows decides the position */
GetSystemMetrics(SM_CYSCREEN)/2-winY/2, /* where the window ends up on the screen */
winX, /* The programs width */
winY, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Get the window's DC */
hdc = GetDC(hwnd);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
DoIt();
}
break;
case WM_DESTROY:
{
PostQuitMessage (0);
}
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}