-
October 19th, 2008, 10:06 PM
#1
Download a file using C++? [RESOLVED]
Hello,
I have searched, but haven't found it, so I will ask.
How can I download a file from the internet using C++?
For example, it would be good if I could download a file like this:
Code:
int code = Download("codeguru.com/file.zip", "c:\\file.zip");
Thanks in advance.
RESOLVED:
You can use this code to download a file:
Code:
#include <tchar.h>
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
int main()
{
HRESULT hr = URLDownloadToFile ( NULL, _T("your web page"), _T("c:/web_page.html"), 0, NULL );
return 0;
}
Last edited by GamesSmash; October 21st, 2008 at 02:47 PM.
Reason: Resolved... and "prettying" up the code
-
October 19th, 2008, 10:15 PM
#2
Re: Download a file using C++?
Originally Posted by GamesSmash
Hello,
I have searched, but haven't found it, so I will ask.
How can I download a file from the internet using C++?
For example, it would be good if I could download a file like this:
Code:
int code = Download("codeguru.com/file.zip", "c:\\file.zip");
Thanks in advance.
Amazing Google displays samples right on the first page....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 19th, 2008, 11:06 PM
#3
Re: Download a file using C++?
I found some, but they require windows.h.
Windows.h doesn't work from my visual C++ 2005 express compiler.
Is there code that doesn't require windows.h available?
-
October 20th, 2008, 12:56 AM
#4
Re: Download a file using C++?
Another option is to goto MS and download the SDK. Make sure you follow every single step of the installation instructions.
-
October 20th, 2008, 03:31 PM
#5
Re: Download a file using C++?
Okay, I am following steps at http://forums.microsoft.com/MSDN/Sho...95837&SiteID=1 to make my compiler work with windows.h.
But seriously, why can't I just download a zipped file with windows.h libraries (even if it comes from Dev C++)?
-
October 20th, 2008, 03:32 PM
#6
Re: Download a file using C++?
Because C++ isn't *that* high-level a language. It'll give you access to the socket layer (TCP), not to the HTTP or FTP (application-layer) protocols typically used to download files. You can write some pretty quick code to handle that level yourself though.
The Boost libraries might have something to help with this.
-
October 21st, 2008, 03:32 AM
#7
Re: Download a file using C++?
Boost has asio headr file that does socket connection but i don't know there are suck API or SDK that help us to write downloader.
Thanks for your help.
-
October 21st, 2008, 01:44 PM
#8
Re: Download a file using C++?
I've gotten windows.h installed.
Well I don't want to download random libraries yet. I am just looking to simply download a file.
I searched some more and found this:
Code:
"HRESULT hr = URLDownloadToFile ( NULL, "http://www.codeguru.com/index.htm", "C:\\codguru.htm", 0, NULL );"
However, it doesn't work.
I get the errors
Code:
'URLDownloadToFileW' : cannot convert parameter 2 from 'char *' to 'LPCWSTR'
Anybody know why it isn't working?
-
October 21st, 2008, 01:46 PM
#9
Re: Download a file using C++?
Originally Posted by GamesSmash
I've gotten windows.h installed.
Well I don't want to download random libraries yet. I am just looking to simply download a file.
I searched some more and found this:
Code:
"HRESULT hr = URLDownloadToFile ( NULL, "http://www.codeguru.com/index.htm", "C:\\codguru.htm", 0, NULL );"
However, it doesn't work.
I get the errors
Code:
'URLDownloadToFileW' : cannot convert parameter 2 from 'char *' to 'LPCWSTR'
Anybody know why it isn't working?
Because you are using a "char *" where an LPCWSTR is required.
You are basically attempting to mix Unicode and 8 bit ASCII.....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 21st, 2008, 01:59 PM
#10
Re: Download a file using C++?
So.... I don't get it.
Does that mean I need to insert some random japanese character in the string to make it work?
I just tried it, it doesn't work.
What should I do?
-
October 21st, 2008, 02:01 PM
#11
Re: Download a file using C++?
Originally Posted by GamesSmash
So.... I don't get it.
Does that mean I need to insert some random japanese character in the string to make it work?
I just tried it, it doesn't work.
What should I do?
Did you do a search here on CodeGuru or on Google for "C++ Unicode Literal"???
Both reveal multiple solutions to your problem.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 21st, 2008, 02:12 PM
#12
Re: Download a file using C++?
I searched for LPCWSTR on Google but found some weird stuff like this "FAQ": http://www.codeguru.com/forum/showthread.php?t=231165
I tried its thing, but my code still doesn't work:
Code:
int main()
{
char *ansistr = "http://www.google.com/index.htm";
int a = lstrlenA(ansistr);
BSTR unicodestr = SysAllocStringLen(NULL, a);
::MultiByteToWideChar(CP_ACP, 0, ansistr, a, unicodestr, a);
char *ansistr2 = "C:\\file.html";
a = lstrlenA(ansistr2);
BSTR unicodestr2 = SysAllocStringLen(NULL, a);
::MultiByteToWideChar(CP_ACP, 0, ansistr2, a, unicodestr2, a);
HRESULT hr = URLDownloadToFile ( NULL, unicodestr, unicodestr2, 0, NULL );
//... when done, free the BSTR
::SysFreeString(unicodestr);
return 0;
}
Any ideas?
-
October 21st, 2008, 02:22 PM
#13
Re: Download a file using C++?
Originally Posted by GamesSmash
I searched for LPCWSTR on Google but found some weird stuff like this "FAQ": [<snip>
Any ideas?
I specifically suggested "C++ Unicode Literal" NOT LPCWSTR....
If you enter that in Google your first hit contains:
Handle literal strings properly.
The Visual C++ compiler interprets a literal string coded as:
L"this is a literal string"
to mean a string of Unicode characters. You can use the same prefix for literal characters. Use the _T macro to code literal strings generically, so they compile as Unicode strings under Unicode or as ANSI strings (including MBCS) without Unicode. For example, instead of:
pWnd->SetWindowText( "Hello" );
use:
pWnd->SetWindowText( _T("Hello") );
With _UNICODE defined, _T translates the literal string to the L-prefixed form; otherwise, _T translates the string without the L prefix.
The _T macro is identical to the _TEXT macro.
So simply "decorating" your literals (which should always be done for every string literal in ANY C++ program that is targeted towards VC++) with _T() will solve your problem.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
October 21st, 2008, 02:30 PM
#14
Re: Download a file using C++?
I tried that, but I get this error:
Code:
"error C3861: '_T': identifier not found"
-
October 21st, 2008, 02:34 PM
#15
Re: Download a file using C++?
Originally Posted by GamesSmash
I tried that, but I get this error:
Code:
"error C3861: '_T': identifier not found"
Did you even bother to read the link that I just gave you??????
Tchar.h supplies portable data types and the _T macro for translating literal strings and characters. For more information, see Generic-Text Mappings in Tchar.h.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|