Code: Select all
Procedure FuncA(param1.l, param2.l)
;......
EndProcedure
Define *p = @FuncA()
Code: Select all
(*p)(param1, param2);
Code: Select all
Procedure FuncA(param1.l, param2.l)
;......
EndProcedure
Define *p = @FuncA()
Code: Select all
(*p)(param1, param2);
Code: Select all
Prototype Proto(param1.l, param2.l=3)
Procedure FuncA(param1.l, param2.l)
ProcedureReturn param1 + param2
EndProcedure
Define P.proto
P = @FuncA()
Debug P(2, 43)
Debug P(4)
Code: Select all
Procedure.d FuncByVal(p1.s, p2.d)
Protected.d x
x = ValD(p1) + p2
ProcedureReturn x
EndProcedure
Prototype.d ProtoByVal(p1.s, p2.d)
Define PByVal.ProtoByVal = @FuncByVal()
Procedure.d FuncByRef(*p1, *p2.Double)
Protected.d x
x = ValD(PeekS(*p1)) + *p2\d
PokeS(*p1, "Changed")
*p2\d = 999
ProcedureReturn x
EndProcedure
;Prototype.d ProtoByRef(*p1.s, *p2.d) ; <--- Which is correct?
Prototype.d ProtoByRef(*p1, *p2.Double) ; <--- Which is correct?
Define PByRef.ProtoByRef = @FuncByRef()
Global s.s, d.d
s = "1"
d = 10
Debug "...ByVal..."
Debug PByVal(s, d)
Debug s
Debug d
Debug " "
Debug "...ByRef..."
Debug PByRef(@s, @d)
Debug s
Debug d
Code: Select all
PokeS(*p1, "Changed")
skywalk wrote:I have a question on ByRef use in Prototypes?
Code: Select all
Prototype.d ProtoByRef(*p1.s, *p2.d) ; <--- This is definately not correct!
Prototype.d ProtoByRef(*p1, *p2.Double) ; <--- Which is correct?
Prototype.d ProtoByRef(*p1.String, *p2.Double ; <--- This one requires the address of a String structure as the first parameter
Code: Select all
Prototype.d ProtoByRef(*p1, *p2.Double)
;Prototype.d ProtoByRef(*p1, *p2.Byte) ;This will work because the procedure actually contains the code for structuring the pointer parameter
Code: Select all
Procedure Add_d(*p1.Double,*p2.Double)
Debug StrD(*p1\d + *p2\d)
EndProcedure
Procedure Add_b(*p1.Byte,*p2.Byte)
Debug Str(*p1\b + *p2\b)
EndProcedure
Procedure Add_string(*p1.String,*p2.String)
Debug Str(Val(*p1\s) + Val(*p2\s))
EndProcedure
Prototype addFunc_prt(*n1,*n2)
Define nd.d = 1.0, nb.b = 2, nString.String\s = "3"
Define genAddFunc.addFunc_prt
genAddFunc = @Add_d() : genAddFunc(@nd, @nd)
genAddFunc = @Add_b() : genAddFunc(@nb, @nb)
genAddFunc = @Add_string() : genAddFunc(nString, nString)
Code: Select all
Procedure Add_d(*p1.Double,*p2.Double)
Debug StrD(*p1\d + *p2\d)
EndProcedure
Procedure Add_b(*p1.Byte,*p2.Byte)
Debug Str(*p1\b + *p2\b)
EndProcedure
Procedure Add_string(*p1,*p2)
Debug Str(Val(PeekS(*p1)) + Val(PeekS(*p2)))
EndProcedure
Prototype addFunc_ptr(*n1,*n2)
Define nd.d = 1.0, nb.b = 2, nString.S = "3"
Define genAddFunc.addFunc_ptr
genAddFunc = @Add_d() : genAddFunc(@nd, @nd)
genAddFunc = @Add_b() : genAddFunc(@nb, @nb)
genAddFunc = @Add_string() : genAddFunc(@nString, @nString)
i know, that you will use prototypes for external calls. but i think, it's easier to make correct procedures with byval and byref first, and then you can add the prototypes functionality.skywalk wrote:My intended use of prototype is to call external DLL functions that modify some of the passed parameters.
The procedure FuncByRef was my attempt to mimic calling an external DLL.
you write in the variable s the value "1". so this stringvariable has a length of one character. you can't write "Changed" with poke at this pointer. there is no reserved place in memory.skywalk wrote:Also, if the Global variable 's' was assigned, what more memory is required to allocate?
Code: Select all
; Simple PureBasic example for the LabJack UE9.
; support@labjack.com
; Jul 31, 2006
OpenConsole()
ConsoleTitle ("LabJackUD PureBasic Example:")
EnableGraphicalConsole(0)
lngErrorcode.l
lngHandle.l
dblValue.d
Prototype.d ProtoGetDriverVersion()
Prototype.l ProtoOpenLabJack(DeviceType.l, ConnectionType.l, Address.s, FirstFound.l, *lngHandle.l)
Prototype.l ProtoeGetS(pHandle.l, IOType.s, Channel.l, *dblValue.d, x1.l) ;<--- Is this correct?
Prototype.l ProtoeGetSS(pHandle.l, IOType.s, Channel.s, *dblValue.d, x1.l) ;<--- *dblValue.d or *dblValue.Double
;The former is used with most basic IOTypes that operate on a particular
;channel, while the latter is generally used with the put_config/get_config
;types operating on the device as a whole.
If OpenLibrary(0,"labjackud.dll")
fGetDriverVersion.ProtoGetDriverVersion = GetFunction(0, "GetDriverVersion")
fOpenLabJack.ProtoOpenLabJack = GetFunction(0, "OpenLabJack")
feGetS.ProtoeGetS = GetFunction(0, "eGetS")
feGetSS.ProtoeGetSS = GetFunction(0, "eGetSS")
;Get the UD driver version.
driverVersion.d = fGetDriverVersion()
PrintN("GetDriverVersion = "+StrD(driverVersion,2))
;Open the first found USB LabJack UE9.
;DeviceType: U3=3 or UE9=9
;ConnectionType: USB=1 or Ethernet=2
lngErrorcode = fOpenLabJack( 9, 1, "1", 1, @lngHandle)
PrintN("OpenLabJack errorcode = "+Str(lngErrorcode))
;Read the serial number of the LabJack.
lngErrorcode = feGetSS( lngHandle, "LJ_ioGET_CONFIG", "LJ_chSERIAL_NUMBER", @dblValue, 0)
PrintN("")
PrintN("errorcode = "+Str(lngErrorcode))
PrintN("Serial Number = "+StrD(dblValue,0))
;Get a reading from analog input 0 (AIN0).
lngErrorcode = feGetS( lngHandle, "LJ_ioGET_AIN", 0, @dblValue, 0)
PrintN("")
PrintN("errorcode = "+Str(lngErrorcode))
PrintN("AIN0 = "+StrD(dblValue,6))
;Get a reading from digital input 0 (FIO0).
lngErrorcode = feGetS( lngHandle, "LJ_ioGET_DIGITAL_BIT", 0, @dblValue, 0)
PrintN("")
PrintN("errorcode = "+Str(lngErrorcode))
PrintN("FIO0 = "+StrD(dblValue,0))
;Set analog output 0 (DAC0) to 2.5 volts.
dblValue = 2.5
lngErrorcode = feGetS( lngHandle, "LJ_ioPUT_DAC", 0, @dblValue, 0)
PrintN("")
PrintN("errorcode = "+Str(lngErrorcode))
PrintN("DAC0 set to 2.5 volts")
;Set digital line 1 (FIO1) to output-low .
dblValue = 0
lngErrorcode = feGetS( lngHandle, "LJ_ioPUT_DIGITAL_BIT", 1, @dblValue, 0)
PrintN("")
PrintN("errorcode = "+Str(lngErrorcode))
PrintN("FIO1 set to output-low")
Else
PrintN("Open Library Failed")
EndIf
PrintN("")
Print ("Press <Enter> to exit: ")
name$=Input()
CloseLibrary(0)
CloseConsole()
End
Code: Select all
;Read the serial number of the LabJack.
lngErrorcode = feGetSS( lngHandle, "LJ_ioGET_CONFIG", "LJ_chSERIAL_NUMBER", @dblValue, 0)
PrintN("")
PrintN("errorcode = "+Str(lngErrorcode))
PrintN("Serial Number = "+StrD(dblValue,0))
it works because a pointer is a pointer.skywalk wrote:
I found this on the forum and want to understand why it works for the typed double pointer.