WinInet class

From Axaptapedia

Jump to: navigation, search

[edit] What it can do for you

This class provides a way to interact with web-pages on the internet.


To save an image from a web-link, you need to add the following method to the WinInet class:

#winapi
int internetSaveFile(int hdl, str _fileName)
{
    Binary _buffer = new Binary(2048);
    Binary _bytesRead = new Binary(4);
    int cnt = 0;
    boolean ret;
    DLLFunction     fileCreate;
    DLLFunction     writeFile;
    DLL             dll = new Dll(@"KERNEL32.DLL");
    int             hFile;
    ;
    if (_fileName)
    {
        fileCreate     = new DLLFunction(dll, @"CreateFileA");
        fileCreate.returns(ExtTypes::DWord);
        fileCreate.arg(ExtTypes::String,
                        ExtTypes::DWord,
                        ExtTypes::DWord,
                        ExtTypes::DWord,
                        ExtTypes::DWord,
                        ExtTypes::DWord,
                        ExtTypes::DWord);
 
        writeFile = new DllFunction(dll, @"WriteFile");
        writeFile.arg(ExtTypes::DWord, ExtTypes::Pointer, ExtTypes::DWord, ExtTypes::Pointer, ExtTypes::DWord);
        writeFile.returns(ExtTypes::DWord);
 
        hFile = fileCreate.call(_fileName, #GENERIC_WRITE, 0, 0, #CREATE_ALWAYS, 0, 0);
        while (hFile != #INVALID_HANDLE_VALUE)
        {
            ret = _internetReadFile.call(hdl,_buffer,2000,_bytesRead);
            if (ret && _bytesRead.dWord(0))
            {
                cnt += _bytesRead.dWord(0);
                writeFile.call(hFile, _buffer, _bytesRead.dWord(0), _bytesRead, 0);
            }
            else
                break;
        }
        if (hFile != #INVALID_HANDLE_VALUE)
            winapi::closeHandle(hFile);
    }
    return cnt;
}

Here is a test job showing how it works:

static void internetSaveFile(Args _args)
{
    WinInet     winInet;
    int         hdl;
    ;
    winInet = new WinInet();
    hdl = winInet.internetOpenUrl("http://www.axforum.info/forums/customavatars/avatar4154_1.gif");
    info (strfmt("%1 bytes saved", winInet.internetSaveFile(hdl, "c:\\temp\\avatar4154_1.gif")));
}
Personal tools