Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SUB & FUNCTION when declaring dynamic library
#9
I think there's something worth clarifying here in regards to `Declare Library` that is non-obvious. In C++ there's a big difference between the two lines in the `mycode` function here:

Code: (Select All)
int externalfunc(int);

int mycode() {
    externalfunc(2); // The return value of externalfunc(2) is ignored

    ((void(*)(int))externalfunc) (2); // externalfunc is treated as a function with no return value, and then called.
}

The first line of `mycode()` is perfectly valid C++ where the return value of a function is ignored. The second line is invalid (but still compiles and runs) where the external function is treated as having no return value at all. C++ makes no guarantees that the second one works, and indeed there are cases where the function will be called incorrectly.

For `Declare Library`, this becomes relevant. If you use regular `Declare Library` to call a function that's already accessible, then the call turns into a line like the first line in `mycode()`. Declaring it as a `SUB` does not matter because the declaration is still correct and the return value simply gets ignored.

If you use `Declare Dynamic Library` or `Declare CustomType Library` then your code is actually turned into something similar to the second line, where the `DECLARE` line is used to generate the function signature. This is where you can get into trouble if you introduce a mismatch.
Reply


Messages In This Thread
RE: SUB & FUNCTION when declaring dynamic library - by DSMan195276 - 06-09-2024, 09:24 PM



Users browsing this thread: 3 Guest(s)