Wininet (InternetConnect)

T

Thomas

Hi newsgroup !

I'm trying to use functions from the Wininet library but I've some problems.
During the execution, both on PPC2003 and on a CE.NET 4.2 device, the
InternetOpen function returns me a valid handle but the InternetConnect
fuction always raises a native exception.
According to samples I found on Internet, I can't see my errors. Maybe you
can help me ?

Here is my code :
'---------------------------------------------------------------------------

'Déclaration des fonctions de la bibliothèque DLL Wininet.dll

'---------------------------------------------------------------------------

'Initialisation d'une connexion

<DllImport("wininet.dll", EntryPoint:="InternetOpenW")> _

Public Function InternetOpen(ByVal sAgent As String, ByVal nAccessType As
Int32, ByVal sProxy As String, _

ByVal sProxyBypass As String, ByVal nFlags As Int32) As Int32

End Function

'Ouverture d'une connexion à interun serveur FTP

<DllImport("wininet.dll", EntryPoint:="InternetConnectW",
CharSet:=CharSet.Unicode)> _

Public Function InternetConnect(ByVal hInternet As Int32, ByVal ServerName
As String, ByVal ServerPort As Int16, _

ByVal sUserName As String, ByVal sPassword As String, ByVal nService As
Int32, ByVal nFlags As Int32, _

ByVal nContext As Int32) As Int32

End Function

'---------------------------------------------------------------------------

'---------------------------------------------------------------------------

'Définition des constantes utilisées par les fonctions de la DLL Wininet.dll

'---------------------------------------------------------------------------

Const INTERNET_INVALID_PORT_NUMBER = 0

Const INTERNET_DEFAULT_FTP_PORT = 21

Const INTERNET_DEFAULT_GOPHER_PORT = 70

Const INTERNET_DEFAULT_HTTP_PORT = 80

Const INTERNET_DEFAULT_HTTPS_PORT = 443

Const INTERNET_DEFAULT_SOCKS_PORT = 1080

Const INTERNET_FLAG_PASSIVE = &H8000000

Const INTERNET_FLAG_TRANSFER_ASCII = &H1

Const INTERNET_FLAG_TRANSFER_BINARY = &H2

Const INTERNET_OPEN_TYPE_PRECONFIG = 0

Const INTERNET_OPEN_TYPE_DIRECT = 1

Const INTERNET_OPEN_TYPE_PROXY = 3

Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4

Const INTERNET_SERVICE_FTP = 1

Const INTERNET_SERVICE_GOPHER = 2

Const INTERNET_SERVICE_HTTP = 3

'---------------------------------------------------------------------------

'---------------------------------------------------------------------------

'Définition des membres de la classe

'---------------------------------------------------------------------------

Dim HandleConnexion As Int32

Dim HandleFTP As Int32

'---------------------------------------------------------------------------

Public Function Connexion() As Boolean

Try

'Initialisation de la connexion

HandleConnexion = InternetOpen("Test", INTERNET_OPEN_TYPE_PRECONFIG,
vbNullString, vbNullString, 0)

MsgBox(HandleConnexion)

'Ouverture de la connexion

HandleFTP = InternetConnect(HandleConnexion, "192.168.1.7",
INTERNET_DEFAULT_FTP_PORT, "pageup", "pageup", INTERNET_SERVICE_FTP, 0, 0)

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Function
 
M

Maloney

Thomas I am having the same problem. I have not been able find any answers
to this problem. If anybody knows what is happening, your help would be
greatly Appreciated.
 
M

Michael Neumann

Thomas I am having the same problem. I have not been able find any answers
to this problem. If anybody knows what is happening, your help would be
greatly Appreciated.



Hi i've written a Class witch may solve your problems. It works perfectly:


Dim oFTP As New cFtp

If Not oFTP.Open(sFTPServer, sFTPUser, sFTPPass) Then
MessageBox.Show("No Connection!")
Return True
End If

oFTP.SetCurrentDirectory("test")

If oFTP.GetFile("test.txt","test.txt",dDate) Then
MessageBox.Show("OK")
Else
MessageBox.Show("Error")
End If

oFTP.Close()


---------------------------------------------- FTP.vb --------------------------------------------
Imports System.Text
Imports System.Runtime.InteropServices

Public Class cFtp
Private Const INTERNET_OPEN_TYPE_PRECONFIG As Integer = 0
Private Const INTERNET_OPEN_TYPE_DIRECT As Integer = 1
Private Const INTERNET_OPEN_TYPE_PROXY As Integer = 2
Private Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY As Integer = 4

Private Const INTERNET_DEFAULT_FTP_PORT As Integer = 21
Private Const INTERNET_FLAG_TRANSFER_BINARY As Integer = &H2

Private Const GENERIC_WRITE As Integer = &H40000000
Private Const FILE_SHARE_WRITE As Integer = &H2
Private Const OPEN_EXISTING As Integer = 3

Private Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
Private Const FILE_ATTRIBUTE_NORMAL As Integer = &H80

Private Const INTERNET_SERVICE_FTP As Integer = 1
Private Const INTERNET_FLAG_PASSIVE As Integer = &H8000000
Private Const INTERNET_FLAG_DONT_CACHE As Integer = &H4000000

Private Const MAX_PATH As Integer = 260

Private Structure FILETIME
Dim dwLowDateTime As UInt32
Dim dwHighDateTime As UInt32
End Structure

Private Structure lpFindFileData
Dim dwFileAttributes As Integer
Dim ftCreationTime As FILETIME
Dim ftLastAccessTime As FILETIME
Dim ftLastWriteTime As FILETIME
Dim nFileSizeHigh As Integer
Dim nFileSizeLow As Integer
Dim dwOID As Integer
'Public cFileName(260) As string
End Structure

Private Declare Function InternetOpen Lib "wininet.dll" (ByVal lpszAgent As String, _
ByVal dwAccessType As Integer, ByVal lpszProxy As String, ByVal lpszProxyBypass As String, _
ByVal dwFlags As Integer) As Integer

Private Declare Function InternetConnect Lib "wininet.dll" (ByVal hInternet As Integer, _
ByVal lpszServerName As String, ByVal nServerPort As Integer, ByVal lpszUserName As String, _
ByVal lpszPassword As String, ByVal dwService As Int32, ByVal dwFlags As Integer, _
ByVal dwContext As Integer) As Integer

Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInternet As Integer) As Boolean

Private Declare Function FtpGetFile Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _
ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean

Private Declare Function FtpPutFile Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Integer, _
ByVal dwContext As Integer) As Boolean

Private Declare Function FtpDeleteFile Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszFileName As String) As Boolean

Private Declare Function FtpRenameFile Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean

Private Declare Function FtpFindFirstFile Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszSearchFile As String, ByVal lpFindFileData As IntPtr, ByVal dwFlags As Integer, _
ByVal dwContext As Integer) As Integer

Private Declare Function InternetFindNextFile Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpvFindData As IntPtr) As Boolean

Private Declare Function FtpCreateDirectory Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszDirectory As String) As Boolean

Private Declare Function FtpRemoveDirectory Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszDirectory As String) As Boolean

Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszDirectory As String) As Boolean

Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" (ByVal hConnect As Integer, _
ByVal lpszCurrentDirectory As StringBuilder, ByRef lpszCurrentDirectory As Integer) As Boolean

Private Declare Function SetFileTime Lib "Coredll" (ByVal hFile As Integer, _
ByVal lpCreationTime As Integer, ByVal lpLastAccessTime As Integer, _
ByVal lpLastWriteTime As IntPtr) As Boolean

Private Declare Function CreateFile Lib "Coredll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDispostion As Integer, _
ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer

Private Declare Function CloseHandle Lib "Coredll" (ByVal hObject As Integer) As Boolean

Private hInternet As Integer
Private hFtp As Integer
Private nID As Integer

Public Function Open(ByVal stUrl As String, ByVal stUser As String, ByVal stPass As String) As Boolean
hInternet = InternetOpen("PosPDA", INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
If hInternet = 0 Then Return False

hFtp = InternetConnect(hInternet, stUrl, INTERNET_DEFAULT_FTP_PORT, _
stUser, stPass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, nID)
If hFtp = 0 Then
InternetCloseHandle(hInternet)
Return False
End If

Return True
End Function

Public Sub Close()
If hFtp > 0 Then InternetCloseHandle(hFtp)
hFtp = 0
If hInternet > 0 Then InternetCloseHandle(hInternet)
hInternet = 0
End Sub

Public Function GetFile(ByVal stSrc As String, ByVal stDst As String, ByVal oDate As DateTime) As Boolean
Dim hFile As Integer
Dim ft As FILETIME
Dim structPtr As IntPtr
Dim size As Integer = Marshal.SizeOf(ft)

If hFtp = 0 Then Return False
If Not FtpGetFile(hFtp, stSrc, stDst, False, FILE_ATTRIBUTE_NORMAL, INTERNET_FLAG_TRANSFER_BINARY, nID) Then
Return False
End If

'If ftTime.dwHighDateTime = 0 Or ftTimeHi = 0 Then Return True
hFile = CreateFile(stDst, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If hFile = 0 Then Return True

ft = SetTime(oDate)

structPtr = Memory.AllocHLocal(size)
Marshal.StructureToPtr(ft, structPtr, False)
SetFileTime(hFile, 0, 0, structPtr)
Memory.FreeHLocal(structPtr)

CloseHandle(hFile)
Return True
End Function

Public Function PutFile(ByVal stSrc As String, ByVal stDst As String) As Boolean
If hFtp = 0 Then Return False
Return FtpPutFile(hFtp, stSrc, stDst, INTERNET_FLAG_TRANSFER_BINARY, nID)
End Function

Public Function GetCurrentDirectory(ByRef stDir As String) As Boolean
Dim stTemp As New StringBuilder
Dim nLen As Integer

If hFtp = 0 Then Return False
stTemp.Capacity = MAX_PATH
nLen = MAX_PATH
If FtpGetCurrentDirectory(hFtp, stTemp, nLen) Then
stDir = stTemp.ToString()
Return True
End If
Return False
End Function

Public Function SetCurrentDirectory(ByVal stDir As String) As Boolean
If hFtp = 0 Then Return False
Return FtpSetCurrentDirectory(hFtp, stDir)
End Function

Public Function CreateDirectory(ByVal stDir As String) As Boolean
If hFtp = 0 Then Return False
Return FtpCreateDirectory(hFtp, stDir)
End Function

Public Function EnumEntries(ByRef oDirEntriesP() As cDirEntry, ByVal sFilter As String, ByVal bDir As Boolean) As
Boolean
Dim hFind As Integer
Dim lpFile As lpFindFileData
Dim naByte(MAX_PATH) As Byte
Dim oDirExtries As New ArrayList
Dim oDirExtry As cDirEntry
Dim structPtr, OffsetPtr As IntPtr
Dim size As Integer = Marshal.SizeOf(lpFile) + (MAX_PATH * Marshal.SystemDefaultCharSize)

If hFtp = 0 Then Return False

' Allocate the memory
structPtr = Memory.AllocHLocal(size)

hFind = FtpFindFirstFile(hFtp, sFilter, structPtr, INTERNET_FLAG_DONT_CACHE, nID)
If hFind = 0 Then Return False

lpFile = CType(Marshal.PtrToStructure(structPtr, GetType(lpFindFileData)), lpFindFileData)
If (((lpFile.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0) And (Not bDir)) Or _
(((lpFile.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0) And (bDir)) Then
OffsetPtr = New IntPtr(structPtr.ToInt32() + Marshal.SizeOf(lpFile))
Marshal.Copy(OffsetPtr, naByte, 0, MAX_PATH)
oDirExtry = New cDirEntry
oDirExtry.stName = ConvByteToStr(naByte)
oDirExtry.dDate = GetTime(lpFile.ftLastWriteTime)
oDirExtries.Add(oDirExtry)
End If
While InternetFindNextFile(hFind, structPtr)
lpFile = CType(Marshal.PtrToStructure(structPtr, GetType(lpFindFileData)), lpFindFileData)
If (((lpFile.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0) And (Not bDir)) Or _
(((lpFile.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0) And (bDir)) Then
OffsetPtr = New IntPtr(structPtr.ToInt32() + Marshal.SizeOf(lpFile))
Marshal.Copy(OffsetPtr, naByte, 0, MAX_PATH)
oDirExtry = New cDirEntry
oDirExtry.stName = ConvByteToStr(naByte)
oDirExtry.dDate = GetTime(lpFile.ftLastWriteTime)
oDirExtries.Add(oDirExtry)
End If
End While
Memory.FreeHLocal(structPtr)
InternetCloseHandle(hFind)
oDirEntriesP = oDirExtries.ToArray(GetType(cDirEntry))
Return True
End Function

Private Function GetTime(ByVal lpTime As FILETIME) As DateTime
Dim naDate(7), naLByte(3), naHByte(3) As Byte

naLByte = BitConverter.GetBytes(lpTime.dwLowDateTime)
naHByte = BitConverter.GetBytes(lpTime.dwHighDateTime)
naLByte.CopyTo(naDate, 0)
naHByte.CopyTo(naDate, 4)
GetTime = New DateTime(BitConverter.ToInt64(naDate, 0))
GetTime = GetTime.AddYears(1600)
End Function

Private Function SetTime(ByVal oTime As DateTime) As FILETIME
Dim naDate(7), naLByte(3), naHByte(3) As Byte

oTime = oTime.UtcNow.AddYears(-1600)
naDate = BitConverter.GetBytes(oTime.Ticks)

naDate.Copy(naDate, 0, naLByte, 0, 4)
naDate.Copy(naDate, 4, naHByte, 0, 4)

SetTime.dwHighDateTime = BitConverter.ToUInt32(naHByte, 0)
SetTime.dwLowDateTime = BitConverter.ToUInt32(naLByte, 0)
End Function

Private Function ConvByteToStr(ByVal naByte() As Byte) As String
Dim n As Integer = 0

ConvByteToStr = ""
While naByte(n) > 0
ConvByteToStr = ConvByteToStr & Chr(naByte(n))
n += 2
End While
End Function

Public Function IsFTPConnected() As Boolean
Return (hFtp > 0)
End Function

Public Sub New()
hInternet = 0
hFtp = 0
nID = 1
End Sub
End Class

Public Class cDirEntry
Public stName As String
Public dDate As DateTime
End Class
---------------------------------------------- FTP.vb --------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top