Download a file using C++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Download a file using C++?

  1. #1
    Join Date
    Nov 2007
    Posts
    87

    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 03:47 PM. Reason: Resolved... and "prettying" up the code

  2. #2
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Download a file using C++?

    Quote 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
    In theory, there is no difference between theory and paractice; 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

  3. #3
    Join Date
    Nov 2007
    Posts
    87

    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?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,642

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  5. #5
    Join Date
    Nov 2007
    Posts
    87

    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++)?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    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.

  7. #7
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  8. #8
    Join Date
    Nov 2007
    Posts
    87

    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?

  9. #9
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Download a file using C++?

    Quote 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
    In theory, there is no difference between theory and paractice; 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

  10. #10
    Join Date
    Nov 2007
    Posts
    87

    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?

  11. #11
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Download a file using C++?

    Quote 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
    In theory, there is no difference between theory and paractice; 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

  12. #12
    Join Date
    Nov 2007
    Posts
    87

    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?

  13. #13
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Download a file using C++?

    Quote 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
    In theory, there is no difference between theory and paractice; 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

  14. #14
    Join Date
    Nov 2007
    Posts
    87

    Re: Download a file using C++?

    I tried that, but I get this error:
    Code:
    "error C3861: '_T': identifier not found"

  15. #15
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Download a file using C++?

    Quote 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
    In theory, there is no difference between theory and paractice; 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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center