添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. I have some visual basic code that downloads a file. Could anyone help me translate this code to C++?
Code:
Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal lpszAgent As String, ByVal dwAccessType As Long, _
    ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, _
    ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias _
    "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, _
    ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, _
    ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As _
    Long) As Integer
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As _
    Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer
' Download a file from Internet and save it to a local file
' it works with HTTP and FTP, but you must explicitly include
' the protocol name in the URL, as in
'    CopyURLToFile "http://www.vb2themax.com/default.asp", "C:\vb2themax.htm"
Sub CopyURLToFile(ByVal URL As String, ByVal FileName As String)
    Dim hInternetSession As Long
    Dim hUrl As Long
    Dim FileNum As Integer
    Dim ok As Boolean
    Dim NumberOfBytesRead As Long
    Dim Buffer As String
    Dim fileIsOpen As Boolean
    On Error GoTo ErrorHandler
    ' check obvious syntax errors
    If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5
    ' open an Internet session, and retrieve its handle
    hInternetSession = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_PRECONFIG, _
        vbNullString, vbNullString, 0)
    If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , _
        "An error occurred calling InternetOpen function"
    ' open the file and retrieve its handle
    hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0, _
        INTERNET_FLAG_EXISTING_CONNECT, 0)
    If hUrl = 0 Then Err.Raise vbObjectError + 1000, , _
        "An error occurred calling InternetOpenUrl function"
    ' ensure that there is no local file
    On Error Resume Next
    Kill FileName
    On Error GoTo ErrorHandler
    ' open the local file
    FileNum = FreeFile
    Open FileName For Binary As FileNum
    fileIsOpen = True
    ' prepare the receiving buffer
    Buffer = Space(4096)
        ' read a chunk of the file - returns True if no error
        ok = InternetReadFile(hUrl, Buffer, Len(Buffer), NumberOfBytesRead)
        ' exit if error or no more data
        If NumberOfBytesRead = 0 Or Not ok Then Exit Do
        ' save the data to the local file
        Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
    ' flow into the error handler
ErrorHandler:
    ' close the local file, if necessary
    If fileIsOpen Then Close #FileNum
    ' close internet handles, if necessary
    If hUrl Then InternetCloseHandle hUrl
    If hInternetSession Then InternetCloseHandle hInternetSession
    ' report the error to the client, if there is one
    If Err Then Err.Raise Err.Number, , Err.Description
End Sub
Rather than converting a code sample from one language to another, you should find a sample in the target language instead - especially for something 'standard' like this.
Not only does it take less effort, but it also means that you are likely to get better results - as some of the methods used are likely to be specific to the source language, whereas they can be done more efficiently (or perhaps need more work) in the target language.
There is also less chance of having bugs in the code, as the sample is likely to be written by somebody who understands the target language well - rather than somebody who knows the source language well.
C++ Code:
  1. #include <CkHttp.h>
  2.  
  3. void ChilkatSample(void)
  4.     {
  5.     CkHttp http;
  6.  
  7.     bool success;
  8.  
  9.     //  Any string unlocks the component for the 1st 30-days.
  10.     success = http.UnlockComponent("Anything for 30-day trial");
  11.     if (success != true) {
  12.         printf("%s\n",http.lastErrorText());
  13.         return;
  14.     }
  15.  
  16.     //  Download the Python language install.
  17.     //  Note: This URL may have changed since this example was created.
  18.     success = http.Download("http://www.python.org/ftp/python/2.5/python-2.5.msi","python-2.5.msi");
  19.     if (success != true) {
  20.         printf("%s\n",http.lastErrorText());
  21.     }
  22.     else {
  23.         printf("Python Download Complete!\n");
  24.     }
  25.  
  26.     }
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.