Thread: URLDownloadToFile dont work in worker thread

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    URLDownloadToFile dont work in worker thread

    URLDownloadToFile dont work in worker thread

    how do i make URLDownloadToFile work in a worker thread. because this is a blocking function, i got to use a worker thread when i call it. but unforturnately when i tried to use it in a thread it doesnt even work at all. its LRESULT return value becomes like -29581853 or something like that.

    also, the file never shows up in c:\abc.txt

    here is the code i used
    Code:
    	download_result=URLDownloadToFile(NULL, yahoo_url, "C:\\abc.txt", 0, NULL);
    how can i make it work in my worker thread?
    ref http://msdn2.microsoft.com/en-us/library/ms918866.aspx

    using vc++6.0
    winxp
    MFC STYLE
    Last edited by hanhao; 07-01-2007 at 09:43 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    something like that
    Is not good enouth. Have you checked the error code with the Error Lookup utility? Does it find the message?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    thanks for all your help
    after some playing around with URLDownloadToFile function, i discovered something about it.

    if you use URLDownloadToFile in the main user interface thread, it will definately work.

    if you use URLDownloadToFile in a worker thread, it may or may not work depending on your OS and your internet explorer. I am not sure which version is usable, but if your code is version dependable, its best to use another style.


    here's the code by someone else that i would recommend.
    Code:
    	CString yahoo_url= "yahoo.com"
    CString Data;
    	DeleteUrlCacheEntry(yahoo_url);// delete the old stupid cache
    
    HINTERNET IntOpen = ::InternetOpen("Sample", LOCAL_INTERNET_ACCESS, NULL, 0, 0);
    HINTERNET handle = ::InternetOpenUrl(IntOpen, yahoo_url, NULL, NULL, NULL, NULL);
    //HANDLE hFile	= ::CreateFile("c:\\index.txt", GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    
    char Buffer[1024];
    DWORD dwRead =0;
    while(::InternetReadFile(handle, Buffer, sizeof(Buffer), &dwRead) == TRUE)
    {
    	if ( dwRead == 0) 
    	 break;
    	DWORD dwWrite = 0;
    //	::WriteFile(hFile, Buffer, dwRead, &dwWrite, NULL);
    	Data+=Buffer;
    }
    
     //::CloseHandle(hFile);
    ::InternetCloseHandle(handle);
    in order to delete cache you need the following at the top
    Code:
    #include "wininet.h" // for clearing URL cache DeleteUrlCacheEntry
    #pragma comment(lib, "wininet.lib") // for clearing URL cache DeleteUrlCacheEntry
    the reason why you should delete cache is because you would want the newest version of the page and IE would not download the newest version if it has it cached

    please give me a rating if this has helped you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  2. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  5. Multithreading
    By Cela in forum Windows Programming
    Replies: 13
    Last Post: 01-15-2003, 03:02 PM