How to call declared function by AddressOf ?

Hello

I am using a Lib that is running well so far. One function is used like that:

[code]Soft Declare Function Read Lib D2XXLib Alias “FT_Read” (ftHandle As UInt64, pReadBuffer As Ptr, dwBytesToRead As UInt32, ByRef lpdwBytesReturned As UInt32) As UInt32

Status = Read(Terminal.FTHandle, pReadBuffer, BytesToRead, BytesReturned)[/code]

I would like now to call the declared Read function by AddressOf (instead of calling directly as code samples shows, so second line replaced by AddressOf call ). I have checked the documentation about AddressOf but now I’m more confused how to do that. Can someone kindly give me a short example how to do that?

There is no real advantage in using a pointer to call a function from an external dll: the steps required are more than using the declare way.
Why are you needing this?

I suppose you are doing this on Windows.
The AddressOf operator returns addresses of entities internal to the Xojo program and can’t be used for methods in external dlls.
A Delegate must be declared with the same signature of the external method: a delegate is a function pointer with a signature.
Load the external dll using LoadLibraryW .
Get the address of the external function using GetProcAddress and create the delegate with the address returned.
To call the function use the Invoke method of the delegate.

It’s a waste of time and resources: only in some very rare cases this can help.

Regards.

Thank you… I’m aware there is no real advantage of using a pointer calling the function but I have to. It is a bit complicated why I have to do that this way… I have to read data coming from the serial device. The problem: If there are no data the read function will not return and freeze the whole app (not even crash). There is a function called set_timeout that stops any read after seconds. But in my case the timeout function cannot/will not stop the read function even tough it should. After long research I found a forum entry of a guy having the same problem. Another one answered he has to call the read function by the pointer not directly (what I am doing in my sample above) but by a pointer. This way the read function is a child call and can be stopped by the timeout function. That’s why I need it. This is all assumption and I want to test that.

I know your posted links (except the MS ones) and I can’t find a proper sample how to use AddressOf or Delegate. Thats’s why I was asking if someone has a simple example for better understanding.
I’m developing on Mac even tough I am supporting Win, Mac and Linux with different Libs and the described behaviour (hang, freeze) is exactly the same on all 3 environments.

FT_GetQueueStatus is non-blocking, use that to get the number of bytes waiting in the receive queue, if >0 then call FT_Read with the value returned.

Sample code top half of Page 25 at https://www.ftdichip.com/Support/Documents/ProgramGuides/D2XX_Programmer’s_Guide(FT_000071).pdf

Tnx, I already have implemented this one. Write command can also lead to hang. Still looking for a solution executing the Read over AddressOf.

Why are you using an ftd lib while xojo has the serialconnection classes? Which should do exactly the same?

@Derk Jochems Good question… Some are the same like setBaud rate, etc. but how about writing to eprom, get modemstatus, etc. Therefore you have go over the programmer of JTAG and I don’t know whats “behind” those special FT-commands.

Using FT_Read via AddressOf isn’t a magic bullet that will stop it locking up, as soon as you invoke your function via the method Maurizio described above you’ll be in exactly the same situation as if you just used declares. The only reason you’d need a function pointer would be for a callback which I don’t think this library provides or for something like using different functions with the same code which doesn’t seem to be the case here.

If querying FT_GetQueueStatus before you read solves the read lockup but you now have a write lockup then you will probably need to rethink the write code as you might be sending too much data at a time and the transmit buffer might be full (that can be checked with FT_GetStatus). This is all speculation as I’ve never used this library.

Could you post the link to the article that mentions calling FT_Read by address and it fixes the problem?

It’s not related to “how you call the function”.
I don’t know anything about the device nor the library used to manage the device itself.
This “indirect call” that can do something different can’t be true: you are simply calling the same function so the behaviour is the same.

Back to the original question: the AddressOf.
In your project add a new Delegate: press the upper left blue “insert” cube and select “Delegate”.
Define the new Delegate exactly like the function you will call i.e. with the same parameters and return value.
Now you have created a new object as a pointer to a function with a signature i.e. with a unique list of parameters.

[code]Soft Declare Function LoadLibraryW Lib “kernel32” (LibName As WString) As Unsigned
Soft Declare Function GetProcAddress Lib “kernel32” (LibHandle As Unsigned, SymbolName As CString) As Ptr

dim hinst As Uinsigned = LoadLibraryW( “put here the name of your dll”)
if hinst = 0 Then
MsgBox(“Unable to load the library”)
break
end if

dim pt As Ptr
pt = GetProcAddress(hinst, “put here the name of the function you need to call”)
if pt = Nil Then
MsgBox(“Unable to find the function”)
break
End if

dim MyDelegate As New TheNameYouAssignedToTheDelegateYouCreatedOnTheFirstStep(pt)

pt.Invoke(insert here the parameter list for the function you are calling)
[/code]

Are you sure that this sentence isn’t extracted from a broader context so something is missing in your post?

These are commands over the serial line, not sure what you are trying to do with the library itself. JTAG is something i won’t do using xojo serial but the other stuff is just much simpler to do with the xojo classes.

Thank you all, highly appreciated help and good input! As you @Maurizio Rossi answered the initial question I will set your last answer as the one.

The D2XXX library for FTDI offers functions that I (probably) can’t handle by self programming (as I don’t know what they do/send in background). The FTDI chip is on a FPGA port and as soon as I attach it to the PC it shows up TWO connection possibilities (one for the Programmer (using the D2XXX functions) and the other is the hypervisor terminal (acting as “normal” serial terminal, works very well with Xojo’s SerialConnect as well as with D2XXX drivers). I’m still learning on this, all new and trying out how in detail it works.
And yes, Julian, you are right: the ft_write is special as you can send 64 Bytes (or a multiple of if up to 4096 Bytes) for each cycle.

By saying all this it could be that the post I have found in this context and bringing me to the initial question may not work as I haven’t seen the whole picture (I’m neither a Linux- nor a C-guy). See the second last entry here (@JulianS this also the link you were asking for).

I will try it now by calling the function over a pointer, not sure if it works but I’m willing to try. If it won’t I have to find a solution around what you @JulianS mentioned.

[quote=487257:@Maurizio Rossi]Back to the original question: the AddressOf.
In your project add a new Delegate: press the upper left blue “insert” cube and select “Delegate”.
Define the new Delegate exactly like the function you will call i.e. with the same parameters and return value.
Now you have created a new object as a pointer to a function with a signature i.e. with a unique list of parameters.

Soft Declare Function LoadLibraryW Lib “kernel32” (LibName As WString) As Unsigned
Soft Declare Function GetProcAddress Lib “kernel32” (LibHandle As Unsigned, SymbolName As CString) As Ptr

dim hinst As Uinsigned = LoadLibraryW( “put here the name of your dll”)
if hinst = 0 Then
MsgBox(“Unable to load the library”)
break
end if

dim pt As Ptr
pt = GetProcAddress(hinst, “put here the name of the function you need to call”)
if pt = Nil Then
MsgBox(“Unable to find the function”)
break
End if

dim MyDelegate As New TheNameYouAssignedToTheDelegateYouCreatedOnTheFirstStep(pt)

pt.Invoke(insert here the parameter list for the function you are calling)[/quote]

Xojo should hire you to write documentation!

Looking briefly at the post you linked, I see many different details from what we are talking about here.
The code is for Linux and the first instruction in the main function

int pid = fork();

create a child process i.e. an exact copy of the same program running in parallel with the first one.
The instruction

if (pid > 0) { //replace with: if (pid == 0) {

is for selecting where to execute the code i.e. in the program or in the child process

The “hang or not to hang” problem and the “calling function by pointer” are something totally unrelated to Xojo programs running or not under Windows.

What is asked and discussed on stackoverflow is something related to the use of the dll in the process or in the child process and the possible problem, they don’t know, caused by dll internal data that can’t be shared between the process and it’s child process.
In this context “calling by pointer” is related to the use of the dll in a way similar to what we are doing here: using the Soft Declare way or the Delegate way.

Please be careful, what is posted on stackoverflow has nothing to do with the device nor the dll library used to manage the device and the “calling using AddressOf” question is from a total misconception about what was asked by the poster on stackoverflow

Please, follow directions posted by Julian.

Best regards.