wininet源代码获取例子 update2

来自http://tunps.com/wininet-get-web-source-example-3

#include <windows.h>
#include <wininet.h>
#include <process.h>
#include "resource.h"
#pragma comment(lib, "wininet")
#define ID_URLEDITBOX    1
#define ID_GETBUTTON    2
#define ID_SHOWEDITBOX    3
#define WEBSOURCE_SIZE (1024*1024*sizeof(char))
 
static HWND hwndUrlEdt,hwndGetBtn,hwndShowEdt;
 
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("BtnLook") ;
    HWND         hwnd ;
    MSG          msg ;
    WNDCLASS     wndclass ;
    wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, MAKEINTRESOURCE(IDI_MYICON)) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    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 ("WinINet源代码获取"),
        WS_OVERLAPPED | WS_SYSMENU | WS_BORDER | WS_MINIMIZEBOX,GetSystemMetrics(SM_CXSCREEN)/4,GetSystemMetrics(SM_CYSCREEN)/4,650,350,
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hwnd, iCmdShow) ;
    UpdateWindow (hwnd) ;
 
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    return msg.wParam ;
}
void Thread1 (PVOID pvoid)
{
    HINTERNET hConnect,hSession;
    char *szBuffer = malloc(WEBSOURCE_SIZE);
    wchar_t *wszBuffer = (wchar_t *)malloc(WEBSOURCE_SIZE);
    TCHAR szStr[100];
    DWORD dwSize=0;
    DWORD dwDownloaded;
    int len;
    if(szBuffer == NULL)
    {
        MessageBox(hwndGetBtn,TEXT("error"),TEXT("Error"),MB_ICONHAND);
    }
    GetWindowText(hwndUrlEdt,szStr,sizeof(szStr)/sizeof(TCHAR));
    hSession = InternetOpen(TEXT("testWinINet"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
    hConnect = InternetOpenUrl(hSession,szStr,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
    ZeroMemory(szBuffer,WEBSOURCE_SIZE);
    while (InternetReadFile(hConnect,szBuffer,WEBSOURCE_SIZE,&dwDownloaded))
    {
        if (0==dwDownloaded) break;
        szBuffer[dwDownloaded]=0;
    }
    len=MultiByteToWideChar(CP_UTF8,0,szBuffer,-1,NULL,0);
    MultiByteToWideChar(CP_UTF8,0,szBuffer,-1,wszBuffer,sizeof(wszBuffer));
 
    SetWindowText(hwndShowEdt,szBuffer);
 
    //释放内存
    free(szBuffer);
    szBuffer=NULL;
    //按钮状态
    SetWindowText(hwndGetBtn,TEXT("GO"));
    EnableWindow (hwndGetBtn, TRUE) ;
 
    _endthread () ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HICON hIcon;
    HINSTANCE hInstance;
    HFONT font;
    static int cxClient,cyClient;
    font = CreateFont(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "microsoft yahei");
    switch (message)
    {
    case WM_CREATE:
        //loadicon
        //hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
        //hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (NULL)) ;
        //child window
        hwndUrlEdt = CreateWindow (TEXT ("EDIT"), NULL,
            WS_CHILD | WS_VISIBLE |  WS_BORDER | ES_LEFT | ES_AUTOHSCROLL    ,
            20,20,500,25, hwnd, (HMENU)ID_URLEDITBOX,
            ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
 
        hwndGetBtn = CreateWindow (TEXT ("BUTTON"), TEXT("GO"),
            WS_CHILD | WS_VISIBLE |  BS_DEFPUSHBUTTON   ,
            530,20,100,25, hwnd, (HMENU)ID_GETBUTTON,
            ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
 
        hwndShowEdt = CreateWindow (TEXT ("EDIT"), TEXT(""),
            WS_CHILD | WS_VISIBLE | WS_BORDER  | WS_HSCROLL | WS_VSCROLL |  ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE  ,
            20,60,600,250, hwnd, (HMENU)ID_SHOWEDITBOX,
            ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
 
        SendMessage(hwndUrlEdt,WM_SETFONT,(WPARAM)font,TRUE);
        SendMessage(hwndGetBtn,WM_SETFONT,(WPARAM)font,TRUE);
        SendMessage(hwndShowEdt,WM_SETFONT,(WPARAM)font,TRUE);
 
        return 0;
    case WM_SIZE:
        cyClient = LOWORD(lParam);
        cxClient = LOWORD(lParam);
        return 0;
    case WM_COMMAND:
        if(LOWORD(wParam) == ID_GETBUTTON)
        {
            //按钮状态
            SetWindowText(hwndGetBtn,TEXT("下载中..."));
            EnableWindow (hwndGetBtn, FALSE) ;
            _beginthread (Thread1, 0, NULL) ;
        }
        return 0;
    case WM_DESTROY:
        DeleteObject(font);
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc (hwnd, message, wParam, lParam) ;
}

存储源代码的变量使用malloc申请内存空间就可以摆脱1MB栈大小的限制(char *szBuffer = malloc(WEBSOURCE_SIZE);),点击按钮后可以先EnableWindow (hwndGetBtn, FALSE) ;把按钮变灰,避免重复点击。

字体通过
font = CreateFont(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "microsoft yahei");
SendMessage(hwndUrlEdt,WM_SETFONT,(WPARAM)font,TRUE);
修改。

最后的字符集问题,核心函数是MultiByteToWideChar,搞了1天,utf-8网页还是乱码。下载了一个数年前使用的明小子用delphi写的注入工具domain web旁注工具,版本已经更新到了3.6,结果提交表单到一个utf-8网页也是乱码,我现在终于知道底层处理编码是多么蛋疼的一件事情。

About tunpishuang

just 4 fun·····
This entry was posted in 未分类 and tagged , , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>