Click here to Skip to main content
13,287,116 members (50,357 online)
Rate this:
Please Sign up or sign in to 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,, 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 2:43am
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)
hRes = URLDownloadToFile(NULL, "", "", 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

Rate this: bad
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. :)

Listing as tested with VS2005 in XP
#include "stdafx.h"
#include <windows.h>
#include <urlmon.h>
int main()
	hRes = URLDownloadToFile(NULL, L"", L"", 0, NULL);
	return 0;
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"", L"", 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 , so if you have programs welcome to submit and call me for free promotions.
enhzflep 13-Aug-12 1:41am
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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 12 Aug 2012
Copyright © CodeProject, 1999-2017
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