Invalid Memory Access when executing callback via DLL

Windows specific forum
sepp
User
User
Posts: 10
Joined: Sat Jul 28, 2012 11:31 pm

Invalid Memory Access when executing callback via DLL

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Invalid Memory Access when executing callback via DLL

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
sepp
User
User
Posts: 10
Joined: Sat Jul 28, 2012 11:31 pm

Re: Invalid Memory Access when executing callback via DLL

Post 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
Post Reply