Date: 02-16-2022
Return to Index
created by gbSnippets
'When reading a small file, particularly one that will read the entire file in a single
'read block, the code can be simplified somewhat over downloading a file that requires
'reading of multiple blocks of data.
'Primary Code:
'Credit: Semen Matusovski
'The InternetOpen, InternetOpenURL, InternetReadURL and InternetCloseHandle
'WinInet API are needed to perform the task.
Local sBuffer$
DownloadInfo.URLPath = "http://www.garybeene.com/files/gbsnippets.msg"
DownloadInfo.hWndParent = CB.Hndl : DownloadInfo.wMsg = %WM_User + 1001
DownloadInfo.lpBuf = VarPTR(sBuffer) : DownloadInfo.BytesRead = 0
DownloadInfo.Result = 0 : DownloadInfo.@lpBuf = Space$(4096)
hInternetSession = InternetOpen("gbSnippets", %INTERNET_OPEN_TYPE_PRECONFIG, ByVal 0, ByVal 0, 0) 'open session
hFile = InternetOpenUrl(hInternetSession, DownloadInfo.URLPath, _ 'open URL
ByVal 0, ByVal 0, %INTERNET_FLAG_PRAGMA_NOCACHE Or _
%INTERNET_FLAG_NO_CACHE_WRITE Or %INTERNET_FLAG_RELOAD, 0)
InternetReadFile (hFile, ByVal StrPTR(DownloadInfo.@lpBuf), 4096, ByRef BytesRead) 'read the file
InternetCloseHandle hFile : InternetCloseHandle hInternetSession 'close branch and session
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "WinInet.Inc"
Type tagDownloadInfo
hWndParent As Dword
wMsg As Dword
URLPath As Asciiz * 128 ' Enough
Result As Long
BytesRead As Dword
lpBuf As String Ptr
End Type
Global hDlg as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local DownloadInfo As tagDownloadInfo, sBuffer As String, x As Long
Local hInternetSession As Dword, hFile As Long, BytesRead As Dword
Select Case CB.Msg
Case %WM_Command
If CB.Ctl = 100 Then
DownloadInfo.URLPath = "http://www.garybeene.com/files/gbsnippets.msg"
DownloadInfo.hWndParent = CB.Hndl : DownloadInfo.wMsg = %WM_User + 1001
DownloadInfo.lpBuf = VarPTR(sBuffer) : DownloadInfo.BytesRead = 0
DownloadInfo.Result = 0 : DownloadInfo.@lpBuf = Space$(4096)
hInternetSession = InternetOpen("gbSnippets", %INTERNET_OPEN_TYPE_PRECONFIG, ByVal 0, ByVal 0, 0) 'open session
hFile = InternetOpenUrl(hInternetSession, DownloadInfo.URLPath, _ 'open URL
ByVal 0, ByVal 0, %INTERNET_FLAG_PRAGMA_NOCACHE Or _
%INTERNET_FLAG_NO_CACHE_WRITE Or %INTERNET_FLAG_RELOAD, 0)
InternetReadFile (hFile, ByVal StrPTR(DownloadInfo.@lpBuf), 4096, ByRef BytesRead) 'read the file
InternetCloseHandle hFile : InternetCloseHandle hInternetSession 'close branch and session
MsgBox DownloadInfo.@lpBuf
End If
End Select
End Function
'gbs_00363
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm