Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Comments
enhzflep 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 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

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. :)

Listing as tested with VS2005 in XP
C++
#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;
}
 
Share this answer
 
Comments
Style-7 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 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900