PDA

Click to See Complete Forum and Search --> : Problems about winsock and wininet API


Apr 30th, 2000, 09:25 AM
Hi, everyone:

I have some problems about winsock and wininet API:

First problem is about winsock:
(1) I use winsock to download binary file (such as image, program-exe), but the downloaded file will become shorter ( or smaller). How
to avoid that thing?
(2)Once I download a file, the LIST command in the winsock can't
work normally, so I can't get the remote current Directory list
until I reconnect. why is this?

Here is the code

----------------------------------winsock code begin-------------------
Private WithEvents wscData As MSWinsockLib.Winsock
Private Sub wscData_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

Debug.Print "wscData_DataArrival - bytesTotal: " & bytesTotal
If bytesTotal = 0 Then Exit Sub

strData = String(bytesTotal + 10, vbNullChar)

wscData.GetData strData, vbByte, bytesTotal

If m_bTransferInProgress Then
If m_bFileIsOpened Then
'
'write data to local file
'
Put m_intLocalFileID, , strData
'
'raise DownloadProgress event
'
m_lDownloadedBytes = m_lDownloadedBytes + bytesTotal
RaiseEvent DownloadProgress(m_lDownloadedBytes)
End If
Else
m_strDataBuffer = m_strDataBuffer & strData
' Debug.Print strData
End If

m_objTimeOut.Reset

End Sub
-----------------------------------winsock code end---------------------

The Second problem is about WinInet API Functions:

I try to use the WinInet API function to download file,
but, After running InternetReadfile API function, I find that
the InternetFindFirst and FtpGetCurrentDirectory function can't
work normally.
The FtpFindFirst return error code like this:
ERROR_INTERNET_EXTENDED_ERROR
The FtpGetCurrentDirectory function return "" string.


Here's my code:

Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" _
(ByVal hFtpSession As Long, ByVal lpszDirectory As String, ByRef lpdwCurrentDirectory As Long) As Boolean

Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
(ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long

Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByRef sBuffer As Byte, _
ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer

Private Sub CmdGet_Click()

If hConnection = 0 Then
MsgBox "no connection"
Exit Sub
End If


On Error GoTo errline
Me.MousePointer = vbHourglass
Dim fullName As String 'Local filename
Dim ttt1 As String
Dim ttt
If OptionText.Value Then
dwType = FTP_TRANSFER_TYPE_ASCII
Else
dwType = FTP_TRANSFER_TYPE_BINARY
End If
ttt1 = ListViewRemote.SelectedItem.Text


If Right(File1.Path, 1) = "\" Then
fullName = File1.Path & ttt1
Else
fullName = File1.Path & "\" & ttt1
End If
If InStr(fullName, " ") <> 0 Then
MsgBox fullName & "Ftp-get don't support"
MousePointer = vbNormal
Exit Sub
End If
If FileExists(fullName) Then
ttt = MsgBox("file£º" & fullName & "already exists, overwrite?", vbYesNo)
If ttt = vbNo Then
Me.MousePointer = vbNormal
Exit Sub
Else
Kill fullName
End If
End If


Dim Size As Long
Size = Val(ListViewRemote.SelectedItem.SubItems(1))
If Size = 0 Then
MsgBox "File length is 0"
Me.MousePointer = vbNormal
Exit Sub
End If

ResetProgress ("Please wait...")


hFile = FtpOpenFile(hConnection, ttt1, GENERIC_READ, dwType, 0)
If hFile = 0 Then
MsgBox GetDllErrorInfor(Err.LastDllError) & ",OpenFile"
Me.MousePointer = vbNormal
Exit Sub
End If

Dim Sum As Long
Dim j As Long
Dim ii As Long
Dim dataBuffer() As Byte
ReDim dataBuffer(Size - 1) As Byte
Dim pData(TransUnitBytes + 100) As Byte
Dim readDone As Long

Timer1.Enabled = True

Sum = 0
For j = 1 To (Size \ TransUnitBytes)
For ii = 0 To TransUnitBytes
pData(ii) = 0
Next ii
If InternetReadFile(hFile, pData(0), TransUnitBytes, readDone) = 0 Then
MsgBox GetDllErrorInfor(Err.LastDllError) & ",ReadFile"
Call ResetTimer
Me.MousePointer = vbNormal
Exit Sub
End If
DoEvents
For ii = 0 To TransUnitBytes - 1
dataBuffer(Sum + ii) = pData(ii)
Next ii
Sum = Sum + TransUnitBytes
'Put #Fnum01, , DelRightEmpty(Data)
Call ShowTransferProgress("DownLoading...", ttt1, Size, Sum)
Next j


Dim Remainder1 As Long
Remainder1 = Size Mod TransUnitBytes
If Remainder1 > 0 Then
For ii = 0 To TransUnitBytes
pData(ii) = 0
Next ii
If (InternetReadFile(hFile, pData(0), Remainder1, readDone) = 0) Then
MsgBox GetDllErrorInfor(Err.LastDllError) & ",ReadFile"
Call ResetTimer
Me.MousePointer = vbNormal
Exit Sub
End If
For ii = 0 To Remainder1 - 1
dataBuffer(Sum + ii) = pData(ii)
Next ii
'Put #Fnum01, , Data
Call ShowTransferProgress("DownLoading...", ttt1, Size, Size)
End If

Dim Fnum01 As Integer
Fnum01 = FreeFile
Open fullName For Binary Access Write As #Fnum01
Put #Fnum01, , dataBuffer

Close #Fnum01
InternetCloseHandle (hFile)
Me.MousePointer = vbNormal

RefreshLocalList
Call ResetProgress("Completed!")
StatusBarLLp.Panels(4).Text = ""
StatusBarLLp.Panels(5).Text = ""
Call ResetTimer
Me.MousePointer = vbNormal
Exit Sub
errline:
MsgBox "Get_File," & Error() & Erl
Me.MousePointer = vbNormal


End Sub


chiliuli@yahoo.com

Wolfie
Jun 18th, 2000, 06:15 PM
I'm sorry, I don't have answers for you, but it seems if you can help me. My question is how in the world do you get the winsock control to work without putting it on a form? (The events).

I tried doing the same declarations in my program, but as soon as I try to connect,I get a message "Object variable or With block variable not set".

Please help?

Jun 21st, 2000, 06:49 PM
My Friend, WolFie:

Nice to see your reply, although it don't have my answers.

Winsock is an activeX control. so you can put it on a form,
or setup the vb References.
To do this, open Project-> References dialog, then click Browse button and select Mswinsck.ocx, ok.

If my answers can help you, or you have other questions,
or you have some experiences to share with me, Please mail to me. By the way, where do you come from?

chiliuli@yahoo.com

Wolfie
Jun 21st, 2000, 07:11 PM
llpfhq, thanks for the reply.
The thing is, I am writing an activex dll that does not have a form. I declared it withevents and is able to connect but still can't trap the events.

Please help my friend.

Hmpf
Jun 22nd, 2000, 02:32 AM
hi , really dont wanna ruin your message topic : help in some questions

:p , but i need to figure something out

i tried to add the mswinsck.ocx to my custom control
but it says i dont have the apropirate license for it

and it doesnt work , if someone can help me it would be great

10x :)

hope u will find answers :)