Passing Pointer of a Function

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by sawa.

Hi everyone,

I would like to call an external function from a PureBasic DLL.

I passed a pointer of the external function to the DLL-function but I don't know how to call this function via Pointer. The compiler doesn't know that I want to call a function and returns an error.

Here is an example:

ProcedureDLL MyFunction(*p_func)
*p_func()
EndProcedure


Does anybody know how I can realize this?


Thanks

Sawa
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

This is only for 'normal' Call but should also be easy in a DLL

Code: Select all

Global ADR1Procedure Test()
 MessageRequester("INFO","TEST",0)
EndProcedure

Procedure MyFunction(p_func.l)
ADR1.l=p_func
!CALL [v_ADR1] ;InlineAssembler :)
EndProcedure

Result=MyFunction(@Test())
Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by WolfgangS.

Hi !
Welcome in the wonderful world of PureBasic :wink:
Normaly you have to call function via Name ...

If you like to call a function of a .dll try this:
Result = CallFunction(#Library, FunctionName$ [,Parameter1 [, Parameter2...]])

and compare with this:
viewtopic.php?t=2488

MFG
WolfgangS
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by sawa.

To avoid misunderstandings,

I want to call different functions of the calling exe from a (PureBasic)DLL.

I tried the Assembler stuff from Rings and it crashes at the end. But anyway it is not very comfortably if I have functions with a variety count of parameters. When I can't call a function without pushing the parameters I also can put the functionality into PureBasic. I don't want to go that way, because the dll is used to change the output of a program while touching only the part of the dll and nothing more.


Sawa
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by WolfgangS.

Hi !
>I want to call different functions of the calling exe from a (PureBasic)DLL.
?
I would try to give the adress of a exe-function(?!) to the dll via Parameter .. if your dll is selfmade ...

callfunction(#ID_Mydll, "MyFunction",?ProcName())

procedure ProcName()
...
endprocedure

Compare with the pointer infos in the PB-Info.


MFG
WolfgangS

Don´t hurt me if i´m wrong ...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

i think you have to post more sources/examples so that we can see what your really problem is.Your description is not clear enough(for me).
If you wanna have more paramters for a Procedure, use Inlineasm : PUSH

but i think your problem can be solved without that tricky way.
First i have to understand what is your Destination.



Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by sawa.

Here is a sample code:
---------------------------------------------
Exe-file Source code (Written in MS-Basic)
---------------------------------------------

Declare Sub MyFunction Lib "c:\purebasic.dll" Alias "_MyFunction" (ByRef p_func As String)

Public Sub main()
Call MyFunction(msg)
End Sub

'This function should be called from the dll
Public Function msg(s As String)
MsgBox (s)
End Function

------------------------------------------
PureBasic DLL
------------------------------------------
ProcedureDLL MyFunction(*p_func)
call (*p_func("Message")) ;Wrong syntax but I hope it explanes what
;I mean.
EndProcedure



I hope this explanes the problem exactly.

Sawa
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

I did not know exactly what is your purpose, but here is a Working solution/example how a Purebasic-DLL calls a VB-Function.

First the Code for the PureBasic-DLL

Code: Select all

Global ADR1Global Back

ProcedureDLL MyFunction(p_func.l)
ADR1.l=p_func
Back.l=1234
MessageRequester("inside Purebasic-DLL","VB-Adress(SUB MSG(Byval value as long)) is:"+Hex(p_Func),0)
;InlineAssembler :)
;First save Registers
!PUSH   eax
!PUSH   ebx
!PUSH   ecx
!PUSH   ebp
!PUSH   esi
!PUSH   edi

;Now push arguments
!PUSH dword[v_Back];First Argument

!CALL [v_ADR1] ;Call now!

;POP back Registers
!Pop   eax
!Pop   ebx
!Pop   ecx
!Pop   ebp
!Pop   esi
!Pop   edi 
MessageRequester("inside Purebasic-DLL","Back from VB's MSG Function:",0)
EndProcedure
Now the Code for VB (Module only)

Code: Select all

Declare Sub MyFunction Lib "c:\purebasic.dll" (ByVal p_func As Long)
Dim Pointer As Long
'This function should be called from the dll
Public Sub msg(ByVal MyLong As Long)
MsgBox "Holla, Inside VB's msg-SUB   Value is:" + Str(MyLong)
End Sub

Sub Main()
Pointer = Getpointer(AddressOf msg)
MsgBox "Adress to call is :" + Hex(Pointer)
Call MyFunction(Pointer)
End Sub

Public Function Getpointer(MyLong)
 Getpointer = MyLong
End Function
This is a example for one argument.
i did not test with any others, strings or functions, coz that is not my Part . :) but i think thats a good startingPoint .......

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Just use CallFunctionFast(p_func, .....) which is much more readable :)

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

okay so that is enough in PureBasic:

Code: Select all

ProcedureDLL MyFunction(p_func.l)Back.l=1234
MessageRequester("inside Purebasic-DLL","VB-Adress(SUB MSG(Byval value as long)) is:"+Hex(p_Func),0)
CallFunctionFast(p_func,Back) 
MessageRequester("inside Purebasic-DLL","Back from VB's MSG Function:",0)
EndProcedure

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by sawa.

Yeah,

That's what I looked for.

Thanks a lot.


Sawa


From now on PureBasic got a new fan
Post Reply