Page 1 of 1

Invalid Memory Access when executing callback via DLL

Posted: Wed Aug 15, 2012 1:40 pm
by sepp
Hello all,

I am trying to execute a callback-routine via DLL, where I am passing the address of the callback-routine as parameter. The parameter is arriving at the DLL correctly, but when calling it (via Inline-Assembler-function CALL) I always get "Invalid Memory Access".

Does anyone have an idea what's wrong here, isn't CALL the correct function?

Thanks,
sepp

mydll.pb

Code: Select all

Declare ErrorHandler()

ProcedureDLL test(*address)
  
  ; Acticate Error Handler
  OnErrorCall(@ErrorHandler())
  
  ; Debugging-Messages
  MessageRequester("DLL", ProgramFilename() + #CRLF$ + FormatDate("%yyyy/%mm/%dd, %hh:%ii:%ss", #PB_Compiler_Date ), 0)
  MessageRequester("DLL", "Executing Address: 0x" + Hex(*address), 0)
  
  ; Execute Callback-Function
  EnableASM
  CALL *address
  DisableASM
  
  ; Debugging-Message
  MessageRequester("DLL", "Execution finished!", 0)
  
EndProcedure

Procedure ErrorHandler()
 MessageRequester("DLL-Error", ErrorMessage())
EndProcedure

; IDE Options = PureBasic 4.61 (Windows - x86)
; ExecutableFormat = Shared Dll
; CursorPosition = 22
; Folding = -
; EnableOnError
; Executable = mydll.dll
; CPU = 1
; DisableDebugger
dllcaller.pb

Code: Select all

Declare mainfunction()

; Debugging-Message
MessageRequester("Main", ProgramFilename() + #CRLF$ + FormatDate("%yyyy/%mm/%dd, %hh:%ii:%ss", #PB_Compiler_Date ), 0)

; Call DLL-Function
If OpenLibrary(0, "mydll.dll")
  MessageRequester("Main", "Callback-Address: 0x" + Hex(@mainfunction), 0)
  CallFunction(0, "test", @mainfunction)
  CloseLibrary(0)
EndIf

; Callback-Function which should be called from DLL
Procedure mainfunction()
  MessageRequester("Main", "It worked!", 0)
EndProcedure

; IDE Options = PureBasic 4.61 (Windows - x86)
; CursorPosition = 7
; Folding = -
; EnableUnicode
; Executable = dllcaller.exe
; CPU = 1
; DisableDebugger

Re: Invalid Memory Access when executing callback via DLL

Posted: Wed Aug 15, 2012 2:16 pm
by STARGÅTE
Use a prototype:

Code: Select all

Prototype Function()

ProcedureDLL test(Function.Function)
  
  ; Acticate Error Handler
  OnErrorCall(@ErrorHandler())
  
  ; Debugging-Messages
  MessageRequester("DLL", ProgramFilename() + #CRLF$ + FormatDate("%yyyy/%mm/%dd, %hh:%ii:%ss", #PB_Compiler_Date ), 0)
  MessageRequester("DLL", "Executing Address: 0x" + Hex(Function), 0)
  
  ; Execute Callback-Function
  Function()
  
  ; Debugging-Message
  MessageRequester("DLL", "Execution finished!", 0)
  
EndProcedure
Edit:
and use ():

Code: Select all

Declare mainfunction()

; Debugging-Message
MessageRequester("Main", ProgramFilename() + #CRLF$ + FormatDate("%yyyy/%mm/%dd, %hh:%ii:%ss", #PB_Compiler_Date ), 0)

; Call DLL-Function
If OpenLibrary(0, "mydll.dll")
  MessageRequester("Main", "Callback-Address: 0x" + Hex(@mainfunction()), 0)
  CallFunction(0, "test", @mainfunction())
  CloseLibrary(0)
EndIf

; Callback-Function which should be called from DLL
Procedure mainfunction()
  MessageRequester("Main", "It worked!", 0)
EndProcedure

; IDE Options = PureBasic 4.61 (Windows - x86)
; CursorPosition = 7
; Folding = -
; EnableUnicode
; Executable = dllcaller.exe
; CPU = 1
; DisableDebugger

Re: Invalid Memory Access when executing callback via DLL

Posted: Wed Aug 15, 2012 3:17 pm
by sepp
Thanks STARGÅTE, you are right, @mainfunction is wrong it must be @mainfunction()!
Then it also works with CALL, but with Prototype it is much nicer :D