Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bug in "Dim ... As Function()" ?
#1
Hallo all!

I still have some trouble using "Dim ... As Function", I tried it several times with different functions.

Look at the example below, it's working (downloading a file from Internet), but only if I'm using the assembler-routine.

Change the line "If 1=1 ..." to "If 1=2 ..." Then it's crashing because then it's using "Dim Download As Function(...)".

Code:
Option Explicit
Option Private

'$include once:'win\kernel32.bi'
'$include once:'win\user32.bi'
'$include once:'win\gdi32.bi'

Declare Sub DownloadFile(sUrl As String, sFile As String)

Declare Function WinMain(ByVal hInstance As Long, _
        ByVal hPrevInstance As Long, _
        szCmdLine As String, ByVal iCmdShow As Integer ) As Integer
End WinMain( GetModuleHandle( NULL ), NULL, Command$, SW_NORMAL )
'
Function WinMain ( ByVal hInstance As Long, _
               ByVal hPrevInstance As Long, _
               szCmdLine As String, ByVal iCmdShow As Integer ) As Integer
    
  DownloadFile("http://www.daxa-chart.de", "C:\test.htm")

  WinMain = 0 'wMsg.wParam

End Function
'
Sub DownloadFile(sUrl As String, sFile As String)
  Dim addr As Long, hUrlmonDll As Long, back As Long
  Dim UrlPtr, FilePtr
    Dim Download As Function(ByVal pCaller As Long, _
      ByRef szURL As String, ByRef szFileName As String, _
      ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

  hUrlmonDll = LoadLibraryEx("URLMON.DLL", 0, 0)
  addr = GetProcAddress(hUrlmonDll,"URLDownloadToFileA")
  If addr<>0 Then
    If 1=1 Then     ' here is version No. 1 (working)
      UrlPtr=StrPtr(sUrl)
      FilePtr=StrPtr(sFile)
      ASM
        Push 0
        Push 0
        Push [FilePtr]
        Push [UrlPtr]
        Push 0
        Call [addr]
        Mov DWord Ptr [back], eax
      End Asm
    Else   ' here is version No. 2 using "Dim ... As Function" (crashing)
      back = Download(0, sUrl, sFile, 0, 0)
    End If
    If back<>0 Then
      MessageBox(0, "Download Error Number "+Str$(back), "Error", 0)
    End If
  Else
    MessageBox(0, "Urlmon.dll-Initialisation-Error", "Error", 0)
  End If
  FreeLibrary(hUrlmonDll)

End Sub

I think it's crashing here (from the ASM-File that FBC was creating):

Code:
push 0
push 0
mov eax, dword ptr [ebp+12]
push eax
mov eax, dword ptr [ebp+8]
push eax
push 0
call dword ptr [ebp-16]         ' <- crashing here?
mov dword ptr [ebp-12], eax     ' <- crashing here?

Kind regards from Germany

Peter
erman freeBASIC-mailinglist: de.groups.yahoo.com/group/free-basic.
For subscribing send an empty e-mail to free-basic-subscribe@yahoogroups.de.
Reply
#2
String arguments passed to C API's have to be declared as BYVAL, BYREF will pass pointer to the string descriptor, BYVAL will pass the pointer to the string data.
Reply
#3
You only declared a function called download but which function should be used?
There is something missing like:
Code:
function realfunc(ByVal pCaller As Long, _
      Byval szURL As long, Byval szFileName As long, _
      ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

' do something
end function

download = @realfunc

Or am I wrong...
Reply
#4
Hallo Victor!

Quote:String arguments passed to C API's have to be declared as BYVAL, BYREF will pass pointer to the string descriptor, BYVAL will pass the pointer to the string data.

I tried both already, it's still not working, even I use ByVal.

Bye, Peter
erman freeBASIC-mailinglist: de.groups.yahoo.com/group/free-basic.
For subscribing send an empty e-mail to free-basic-subscribe@yahoogroups.de.
Reply
#5
Hallo fsw!

Quote:You only declared a function called download but which function should be used?
There is something missing like:
Code:
function realfunc(ByVal pCaller As Long, _
      Byval szURL As long, Byval szFileName As long, _
      ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

' do something
end function

download = @realfunc

Or am I wrong...

It should be used the function in the DLL, I'll call the DLL-Function directly without a lib*.dll.a-file, but until now it's only working by ASM.

There is no compiling-error, but DIM ... AS FUNCTION is crashing when I click on "run".

Bye, Peter
erman freeBASIC-mailinglist: de.groups.yahoo.com/group/free-basic.
For subscribing send an empty e-mail to free-basic-subscribe@yahoogroups.de.
Reply
#6
Quote:It should be used the function in the DLL,
but there is no link/connection/relation:

download -> "URLDownloadToFileA"

Quote:I'll call the DLL-Function directly
how, I can't see it.
you don't even have:
Code:
Download = addr
or something...

How should the code know which function to use?
Reply
#7
You have to assign the pointer as you are doing with "addr", this worked fine here:

Code:
sub downloadfile(surl as string, sfile as string)
  dim hurlmondll as long, back as long
  dim download as function(byval pcaller as long, _
      byval szurl as string, byval szfilename as string, _
      byval dwreserved as long, byval lpfncb as long) as long

  hurlmondll = loadlibraryex("urlmon.dll", 0, 0)
  download = getprocaddress(hurlmondll,"URLDownloadToFileA")
  back = download(0, surl, sfile, 0, 0)
  
    if back<>0 then
      messagebox(0, "download error number "+str$(back), "error", 0)
    end if
  freelibrary(hurlmondll)

end sub

The html file was downloaded okay, etc..
Reply
#8
Quote:You have to assign the pointer as you are doing with "addr", this worked fine here:

Code:
download = getprocaddress(hurlmondll,"URLDownloadToFileA")

The html file was downloaded okay, etc..

exactly what I did: download = addr Big Grin
Reply
#9
Hallo Victor and fsw,

thank you very much for you help! Yes, it was really my fault, sorry that I'm making trouble here, your compiler is working great!

Have a nice day!


Peter
erman freeBASIC-mailinglist: de.groups.yahoo.com/group/free-basic.
For subscribing send an empty e-mail to free-basic-subscribe@yahoogroups.de.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)