Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Web API
Hello guys,
I use function URLDownloadToFile to download a HTML page.
(see urlmon.h)
 
Some pages download fine, but some is not download.
For example, http://www.microsoft.com, http://www.google.com etc...
The function return error MK_E_SYNTAX _HRESULT_TYPEDEF_(0x800401E4L) O_O. What is it?
Any idea?
Thank you for attention.
Posted 12-Aug-12 1:43am
Style-71.2K
Comments
enhzflep at 12-Aug-12 9:04am
   
Well, the error message indicates to me that there's an error in the syntax of the arguments passed to the function.
 
I had thought initially that the problem may lie with a missing '/' on the end of the url, however - I compiled a minimal program and had no problem with it. This code works fine for me:
 
#include <windows.h>
#include <urlmon.h>
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HRESULT hRes;
hRes = URLDownloadToFile(NULL, "http://www.google.com", "google.com.file", 0, NULL);
 
return 0;
}
Style-7 at 12-Aug-12 10:57am
   
Thank you a lot,
But I have same error in your code and have not idea how to fix it.
I have Windows XP and VS 2005.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It seems that Win7 does some of the work for us - that is to say, I just tried the code in a virtual machine with XP and VS2005 - I got the same problem, i.e error (from the watch window) - "hRes 0x800401e4 Invalid syntax HRESULT"
 
Earlier in the evening I had read that you need to make sure COM is initialized for the thread. I disregarded this and plowed on - this was fine when I used Win7 and Vs2010.
 
Anyway, the fix was to use CoInitialize(NULL); before the call to URLDownloadToFile - after this change, hRes holds S_OK afterwards. Smile | :)
 
Listing as tested with VS2005 in XP
#include "stdafx.h"
#include <windows.h>
#include <urlmon.h>

int main()
{
	CoInitialize(NULL);
	HRESULT hRes;
	hRes = URLDownloadToFile(NULL, L"http://www.google.com/", L"google.com.file", 0, NULL);
 
	return 0;
}
  Permalink  
Comments
Style-7 at 12-Aug-12 15:39pm
   
Oh thanks again. Great! But I have next problem...
Note. Only "wide" version of function woks (URLDownloadToFileW), but this is not problem. I use thread and it is not works, but work in main program (or main thread). But(!!) if you call function
URLDownloadToFile(NULL, L"http://www.google.com/", L"google.com.file", 0, NULL);
in main function before then it is works... lol Hmm. I need download "somthing" for start correctly work. lol
I know one way: use timer and global variable for run it in main thread. But it is not good. Any ideas?
enhzflep at 12-Aug-12 21:23pm
   
You're welcome. Cool.
When you used a thread, did you call CoInitialize *from within that thread*?
CoInitialize needs to be called for each thread that makes a call that requires it's been called first. I.e you can't just call it in the main thread then spin off a bunch of child threads and expect functions to work correctly in them.
 
If that doesn't work, you may wish to use the code found in this thread. I'm told it misses some urls, though haven't seen them yet. I have definitely used this code in threads, often running 15 threads at the same time all downloading a seperate file. (You just need to call WSAstartup/WSACleanup in the main thread)
Reading a website in c++
Style-7 at 13-Aug-12 1:33am
   
Cool ! This is works. I call CoInitialize in my thread. Thank you again. I am author of the site http://www.catalogofsoftware.com , so if you have programs welcome to submit and call me for free promotions.
enhzflep at 13-Aug-12 1:41am
   
:thumbs-up:
Thought that may be the problem. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 893
1 OriginalGriff 400
2 CPallini 275
3 George Jonsson 191
4 Richard Deeming 145
0 OriginalGriff 5,450
1 Sergey Alexandrovich Kryukov 4,532
2 CPallini 4,500
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 12 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100